Skip to content

Commit

Permalink
Merge pull request #90 from synhershko/ianwall-fix-file-permissions
Browse files Browse the repository at this point in the history
Copy original file permissions to updated file. (modified)
  • Loading branch information
robinwassen authored Nov 13, 2016
2 parents 5765df5 + bae5c6f commit ba99ee2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/NAppUpdate.Framework/Tasks/FileUpdateTask.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
using NAppUpdate.Framework.Common;
using NAppUpdate.Framework.Utils;
Expand Down Expand Up @@ -116,7 +118,9 @@ public override TaskExecutionStatus Execute(bool coldRun)
try
{
if (File.Exists(_destinationFile))
{
{
FileSystem.CopyAccessControl(new FileInfo(_destinationFile), new FileInfo(_tempFile));

File.Delete(_destinationFile);
}

Expand Down Expand Up @@ -146,7 +150,8 @@ public override TaskExecutionStatus Execute(bool coldRun)
return TaskExecutionStatus.RequiresPrivilegedAppRestart;
}
return TaskExecutionStatus.RequiresAppRestart;
}
}


public override bool Rollback()
{
Expand Down
21 changes: 20 additions & 1 deletion src/NAppUpdate.Framework/Utils/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

using System.Security.AccessControl;
using System.Security.Principal;

namespace NAppUpdate.Framework.Utils
{
public static class FileSystem
Expand Down Expand Up @@ -95,6 +97,23 @@ public static bool IsFileLocked(FileInfo file)

//file is not locked
return false;
}

public static void CopyAccessControl(FileInfo src, FileInfo dst)
{
FileSecurity fs = src.GetAccessControl();

bool hasInheritanceRules = fs.GetAccessRules(false, true, typeof(SecurityIdentifier)).Count > 0;
if (hasInheritanceRules)
{
fs.SetAccessRuleProtection(false, false);
}
else
{
fs.SetAccessRuleProtection(true, true);
}

dst.SetAccessControl(fs);
}
}
}

0 comments on commit ba99ee2

Please sign in to comment.