Move ResetAcls to Core.Native from Core
Also simplify copying/moving files directory creation.
Rob Mensching committed
Mar 16, 2021 at 14:43 UTC
d056d00e1e1111b6ef68c57f1f03ef6843204723
2 files changed
+27
-9
src/WixToolset.Core.Native/FileSystem.cs
+26
-9
@@ -3,8 +3,10 @@
3
namespace WixToolset.Core.Native
4
{
5
using System;
6
+ using System.Collections.Generic;
7
using System.IO;
8
using System.Runtime.InteropServices;
9
+ using System.Security.AccessControl;
10
11
/// <summary>
12
/// File system helpers.
@@ -19,10 +21,7 @@ namespace WixToolset.Core.Native
21
/// <param name="allowHardlink">Allow hardlinks.</param>
22
public static void CopyFile(string source, string destination, bool allowHardlink)
23
{
22
- if (File.Exists(destination))
23
- {
24
- File.Delete(destination);
25
- }
24
+ EnsureDirectoryWithoutFile(destination);
25
26
if (!allowHardlink || !CreateHardLink(destination, source, IntPtr.Zero))
27
{
@@ -41,18 +40,36 @@ namespace WixToolset.Core.Native
40
/// <param name="destination">The destination file.</param>
41
public static void MoveFile(string source, string destination)
42
{
44
- if (File.Exists(destination))
43
+ EnsureDirectoryWithoutFile(destination);
44
+
45
+ File.Move(source, destination);
46
+ }
47
+
48
+ /// <summary>
49
+ /// Reset the ACLs on a set of files.
50
+ /// </summary>
51
+ /// <param name="files">The list of file paths to set ACLs.</param>
52
+ public static void ResetAcls(IEnumerable<string> files)
53
+ {
54
+ var aclReset = new FileSecurity();
55
+ aclReset.SetAccessRuleProtection(false, false);
56
+
57
+ foreach (var file in files)
58
{
46
- File.Delete(destination);
59
+ new FileInfo(file).SetAccessControl(aclReset);
60
}
61
+ }
62
+
63
+ private static void EnsureDirectoryWithoutFile(string path)
64
+ {
65
+ File.Delete(path);
66
+
67
+ var directory = Path.GetDirectoryName(path);
68
49
- var directory = Path.GetDirectoryName(destination);
69
if (!String.IsNullOrEmpty(directory))
70
{
71
Directory.CreateDirectory(directory);
72
}
54
-
55
- File.Move(source, destination);
73
}
74
75
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
src/WixToolset.Core.Native/WixToolset.Core.Native.csproj
+1
@@ -35,6 +35,7 @@
35
36
<ItemGroup>
37
<PackageReference Include="WixToolset.Data" Version="4.0.*" />
38
+ <PackageReference Include="System.IO.FileSystem.AccessControl" Version="4.6.0" />
39
</ItemGroup>
40
41
<ItemGroup>