Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy original file permissions to updated file. (modified) #90

Merged
merged 5 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}