-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFileUtils.cs
More file actions
192 lines (165 loc) · 7.01 KB
/
FileUtils.cs
File metadata and controls
192 lines (165 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace ServiceControlInstaller.Engine.FileSystem
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.Threading;
static class FileUtils
{
public static string SanitizeFolderName(string folderName)
{
return Path.GetInvalidPathChars().Aggregate(folderName, (current, c) => current.Replace(c, '-'));
}
public static void DeleteDirectory(string path, bool recursive, bool contentsOnly, params string[] excludes)
{
var di = new DirectoryInfo(path);
if (!di.Exists)
{
return;
}
var originalPath = path;
// ADD randomness to path, not replacing folder name so that it can still be identified
string pathToDelete = path + Path.GetRandomFileName();
try
{
// Move folder to ensure no files are in use so that it is less likely that we corrupt the folder
// when still in use
RunWithRetries(() => di.MoveTo(pathToDelete));
path = pathToDelete;
DeleteDirectoryInternal(path, recursive, contentsOnly, excludes);
if (contentsOnly)
{
di.MoveTo(originalPath);
}
}
catch (Exception ex)
{
throw new($"Failure during folder {(contentsOnly ? "cleanup" : "removal")}. Remaining folder content is available at: {path}", ex);
}
// Intentionally not moving back in a finally as folder is likely corrupted
}
static void DeleteDirectoryInternal(string path, bool recursive, bool contentsOnly, params string[] excludes)
{
var di = new DirectoryInfo(path);
if (!di.Exists)
{
return;
}
if (recursive)
{
var subfolders = Directory.EnumerateDirectories(path);
foreach (var s in subfolders)
{
if (excludes.Any(p => string.Equals(p, Path.GetDirectoryName(s), StringComparison.OrdinalIgnoreCase)))
{
continue;
}
DeleteDirectoryInternal(s, true, false, excludes);
}
}
var files = Directory.EnumerateFiles(path);
foreach (var f in files)
{
if (excludes.Any(p => string.Equals(p, Path.GetFileName(f), StringComparison.OrdinalIgnoreCase)))
{
continue;
}
var fi = new FileInfo(f);
RunWithRetries(() =>
{
fi.Attributes &= ~(FileAttributes.ReadOnly | FileAttributes.System);
fi.Delete();
});
}
if (contentsOnly)
{
return;
}
RunWithRetries(() =>
{
di.Attributes &= ~(FileAttributes.ReadOnly | FileAttributes.System);
di.Delete();
});
}
public static void CloneDirectory(string srcDir, string destDir, params string[] includes)
{
Directory.CreateDirectory(destDir);
var files = Directory.EnumerateFiles(srcDir, "*", SearchOption.AllDirectories);
foreach (var srcFile in files)
{
var filename = Path.GetFileName(srcFile);
var isMatch = includes.Any(p => string.Equals(p, filename, StringComparison.OrdinalIgnoreCase));
if (isMatch)
{
var relativePath = srcFile.Substring(srcDir.Length + 1);
var destFile = Path.Combine(destDir, relativePath);
var destFileDir = Path.GetDirectoryName(destFile);
Directory.CreateDirectory(destFileDir);
File.Copy(srcFile, destFile);
}
}
}
public static void CreateDirectoryAndSetAcl(string path, FileSystemAccessRule accessRule)
{
var destination = new DirectoryInfo(path);
if (!destination.Exists)
{
destination.Create();
}
var accessRules = destination.GetAccessControl(AccessControlSections.Access);
accessRules.ResetAccessRule(accessRule);
destination.SetAccessControl(accessRules);
}
public static void UnzipToSubdirectory(string zipResourceName, string targetPath)
{
var assembly = Assembly.GetExecutingAssembly();
using var zipStream = assembly.GetManifestResourceStream(zipResourceName);
UnzipToSubdirectory(zipStream, targetPath);
}
internal static void UnzipToSubdirectory(Stream zipStream, string targetPath)
{
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read, leaveOpen: true, entryNameEncoding: null);
archive.ExtractToDirectory(targetPath, overwriteFiles: true);
// Validate 3rd-party security software didn't delete any of the files, but first, a small delay
// so that any tool out there has a chance to remove the file before prematurely declaring victory
Thread.Sleep(1000);
foreach (var entry in archive.Entries)
{
var pathParts = entry.FullName.Split('/', '\\');
var allParts = new string[pathParts.Length + 1];
allParts[0] = targetPath;
Array.Copy(pathParts, 0, allParts, 1, pathParts.Length);
var destinationPath = Path.Combine(allParts);
var fileInfo = new FileInfo(destinationPath);
if (!fileInfo.Exists || fileInfo.Length != entry.Length)
{
throw new Exception($"The following file was removed after install, perhaps due to a false positive in a 3rd-party security tool. Add an exception for the path in the tool's configuration and try again: " + destinationPath);
}
}
}
static void RunWithRetries(Action action)
{
var attempts = 10;
while (true)
{
try
{
action();
break;
}
catch (IOException ex) when (--attempts > 0)
{
Debug.WriteLine($"ServiceControlInstaller.Engine.FileSystem.FileUtils::RunWithRetries Action failed, {attempts} attempts remaining. Reason: {ex.Message} ({ex.GetType().FullName})");
// Yes, Task.Delay would be better but would require all calls to be async
// and in 99.9% this sleep will not hit
Thread.Sleep(100);
}
}
}
}
}