From d056d00e1e1111b6ef68c57f1f03ef6843204723 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 16 Mar 2021 14:43:36 -0700 Subject: Move ResetAcls to Core.Native from Core Also simplify copying/moving files directory creation. --- src/WixToolset.Core.Native/FileSystem.cs | 35 ++++++++++++++++------ .../WixToolset.Core.Native.csproj | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/WixToolset.Core.Native/FileSystem.cs b/src/WixToolset.Core.Native/FileSystem.cs index b9691d44..d843a9e8 100644 --- a/src/WixToolset.Core.Native/FileSystem.cs +++ b/src/WixToolset.Core.Native/FileSystem.cs @@ -3,8 +3,10 @@ namespace WixToolset.Core.Native { using System; + using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; + using System.Security.AccessControl; /// /// File system helpers. @@ -19,10 +21,7 @@ namespace WixToolset.Core.Native /// Allow hardlinks. public static void CopyFile(string source, string destination, bool allowHardlink) { - if (File.Exists(destination)) - { - File.Delete(destination); - } + EnsureDirectoryWithoutFile(destination); if (!allowHardlink || !CreateHardLink(destination, source, IntPtr.Zero)) { @@ -41,18 +40,36 @@ namespace WixToolset.Core.Native /// The destination file. public static void MoveFile(string source, string destination) { - if (File.Exists(destination)) + EnsureDirectoryWithoutFile(destination); + + File.Move(source, destination); + } + + /// + /// Reset the ACLs on a set of files. + /// + /// The list of file paths to set ACLs. + public static void ResetAcls(IEnumerable files) + { + var aclReset = new FileSecurity(); + aclReset.SetAccessRuleProtection(false, false); + + foreach (var file in files) { - File.Delete(destination); + new FileInfo(file).SetAccessControl(aclReset); } + } + + private static void EnsureDirectoryWithoutFile(string path) + { + File.Delete(path); + + var directory = Path.GetDirectoryName(path); - var directory = Path.GetDirectoryName(destination); if (!String.IsNullOrEmpty(directory)) { Directory.CreateDirectory(directory); } - - File.Move(source, destination); } [DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] diff --git a/src/WixToolset.Core.Native/WixToolset.Core.Native.csproj b/src/WixToolset.Core.Native/WixToolset.Core.Native.csproj index 4069b6b4..94ab812a 100644 --- a/src/WixToolset.Core.Native/WixToolset.Core.Native.csproj +++ b/src/WixToolset.Core.Native/WixToolset.Core.Native.csproj @@ -35,6 +35,7 @@ + -- cgit v1.2.3-55-g6feb