Skip to content

Commit

Permalink
Possibly resolving the permission issue while updating
Browse files Browse the repository at this point in the history
Still needs a thorough review and testing, as it appears to always request elevation, which is not good
  • Loading branch information
synhershko committed Aug 10, 2012
1 parent 73e2d89 commit c571e51
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/NAppUpdate.Framework/Tasks/FileUpdateTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Threading;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Utils;

namespace NAppUpdate.Framework.Tasks
{
Expand Down Expand Up @@ -90,6 +92,25 @@ public override TaskExecutionStatus Execute(bool coldRun)
// Only allow execution if the apply attribute was set to hot-swap, or if this is a cold run
if (CanHotSwap || coldRun)
{
if (File.Exists(_destinationFile))
{
//if (FileSystem.IsExeRunning(_destinationFile))
//{
// UpdateManager.Instance.Logger.Log(Logger.SeverityLevel.Warning, "Process {0} is still running", _destinationFile);
// Thread.Sleep(1000); // TODO: retry a few times and throw after a while
//}

if (!PermissionsCheck.HaveWritePermissionsForFileOrFolder(_destinationFile))
{
if (coldRun)
{
UpdateManager.Instance.Logger.Log(Logger.SeverityLevel.Warning, "Don't have permissions to touch {0}", _destinationFile);
File.Delete(_destinationFile); // get the original exception from the system
}
CanHotSwap = false;
}
}

try
{
if (File.Exists(_destinationFile))
Expand Down
16 changes: 15 additions & 1 deletion src/NAppUpdate.Framework/Utils/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace NAppUpdate.Framework.Utils
Expand Down Expand Up @@ -62,5 +64,17 @@ public static IEnumerable<string> GetFiles(string path, string searchPattern, Se
files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
return files;
}

public static bool IsExeRunning(string path)
{
var processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.MainModule.FileName.StartsWith(path, StringComparison.InvariantCultureIgnoreCase))
return true;
}
return false;
}

}
}

0 comments on commit c571e51

Please sign in to comment.