Repository navigation
Expand file tree
/
Copy pathUtility.cs
More file actions
401 lines (327 loc) · 13.4 KB
/
Copy pathUtility.cs
File metadata and controls
401 lines (327 loc) · 13.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Threading;
using Splat;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Diagnostics;
namespace Squirrel
{
static class Utility
{
public static IEnumerable<FileInfo> GetAllFilesRecursively(this DirectoryInfo rootPath)
{
Contract.Requires(rootPath != null);
return rootPath.EnumerateFiles("*", SearchOption.AllDirectories);
}
public static IEnumerable<string> GetAllFilePathsRecursively(string rootPath)
{
Contract.Requires(rootPath != null);
return Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories);
}
public static string CalculateFileSHA1(string filePath)
{
Contract.Requires(filePath != null);
using (var stream = File.OpenRead(filePath)) {
return CalculateStreamSHA1(stream);
}
}
public static string CalculateStreamSHA1(Stream file)
{
Contract.Requires(file != null && file.CanRead);
using (var sha1 = SHA1.Create()) {
return BitConverter.ToString(sha1.ComputeHash(file)).Replace("-", String.Empty);
}
}
public static async Task CopyToAsync(string from, string to)
{
Contract.Requires(!String.IsNullOrEmpty(from) && File.Exists(from));
Contract.Requires(!String.IsNullOrEmpty(to));
if (!File.Exists(from)) {
Log().Warn("The file {0} does not exist", from);
// TODO: should we fail this operation?
return;
}
// XXX: SafeCopy
await Task.Run(() => File.Copy(from, to, true));
}
public static void Retry(this Action block, int retries = 2)
{
Contract.Requires(retries > 0);
Func<object> thunk = () => {
block();
return null;
};
thunk.Retry(retries);
}
public static T Retry<T>(this Func<T> block, int retries = 2)
{
Contract.Requires(retries > 0);
while (true) {
try {
T ret = block();
return ret;
} catch (Exception) {
if (retries == 0) {
throw;
}
retries--;
Thread.Sleep(250);
}
}
}
public static Task<int> InvokeProcessAsync(string fileName, string arguments)
{
var psi = new ProcessStartInfo(fileName, arguments);
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.ErrorDialog = false;
return InvokeProcessAsync(psi);
}
public static async Task<int> InvokeProcessAsync(ProcessStartInfo psi)
{
var pi = Process.Start(psi);
await Task.Run(() => pi.WaitForExit());
return pi.ExitCode;
}
public static Task ForEachAsync<T>(this IEnumerable<T> source, Action<T> body, int degreeOfParallelism = 4)
{
return ForEachAsync(source, x => Task.Run(() => body(x)), degreeOfParallelism);
}
public static Task ForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> body, int degreeOfParallelism = 4)
{
return Task.WhenAll(
from partition in Partitioner.Create(source).GetPartitions(degreeOfParallelism)
select Task.Run(async () => {
using (partition)
while (partition.MoveNext())
await body(partition.Current);
}));
}
static string directoryChars;
public static IDisposable WithTempDirectory(out string path)
{
var di = new DirectoryInfo(Environment.GetEnvironmentVariable("SQUIRREL_TEMP") ?? Environment.GetEnvironmentVariable("TEMP") ?? "");
if (!di.Exists) {
throw new Exception("%TEMP% isn't defined, go set it");
}
var tempDir = default(DirectoryInfo);
directoryChars = directoryChars ?? (
"abcdefghijklmnopqrstuvwxyz" +
Enumerable.Range(0x4E00, 0x9FCC - 0x4E00) // CJK UNIFIED IDEOGRAPHS
.Aggregate(new StringBuilder(), (acc, x) => { acc.Append(Char.ConvertFromUtf32(x)); return acc; })
.ToString());
foreach (var c in directoryChars) {
var target = Path.Combine(di.FullName, c.ToString());
if (!File.Exists(target) && !Directory.Exists(target)) {
Directory.CreateDirectory(target);
tempDir = new DirectoryInfo(target);
break;
}
}
path = tempDir.FullName;
return Disposable.Create(() =>
DeleteDirectory(tempDir.FullName).Wait());
}
public static async Task DeleteDirectory(string directoryPath)
{
Contract.Requires(!String.IsNullOrEmpty(directoryPath));
Log().Info("Starting to delete folder: {0}", directoryPath);
if (!Directory.Exists(directoryPath)) {
Log().Warn("DeleteDirectory: does not exist - {0}", directoryPath);
return;
}
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
var files = new string[0];
try {
files = Directory.GetFiles(directoryPath);
} catch (UnauthorizedAccessException ex) {
var message = String.Format("The files inside {0} could not be read", directoryPath);
Log().Warn(message, ex);
}
var dirs = new string[0];
try {
dirs = Directory.GetDirectories(directoryPath);
} catch (UnauthorizedAccessException ex) {
var message = String.Format("The directories inside {0} could not be read", directoryPath);
Log().Warn(message, ex);
}
var fileOperations = files.ForEachAsync(file => {
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
});
var directoryOperations =
dirs.ForEachAsync(async dir => await DeleteDirectory(dir));
await Task.WhenAll(fileOperations, directoryOperations);
Log().Debug("Now deleting folder: {0}", directoryPath);
File.SetAttributes(directoryPath, FileAttributes.Normal);
try {
Directory.Delete(directoryPath, false);
} catch (Exception ex) {
var message = String.Format("DeleteDirectory: could not delete - {0}", directoryPath);
Log().ErrorException(message, ex);
}
}
public static Tuple<string, Stream> CreateTempFile()
{
var path = Path.GetTempFileName();
return Tuple.Create(path, (Stream) File.OpenWrite(path));
}
public static string PackageDirectoryForAppDir(string rootAppDirectory)
{
return Path.Combine(rootAppDirectory, "packages");
}
public static string LocalReleaseFileForAppDir(string rootAppDirectory)
{
return Path.Combine(PackageDirectoryForAppDir(rootAppDirectory), "RELEASES");
}
static TAcc scan<T, TAcc>(this IEnumerable<T> This, TAcc initialValue, Func<TAcc, T, TAcc> accFunc)
{
TAcc acc = initialValue;
foreach (var x in This)
{
acc = accFunc(acc, x);
}
return acc;
}
public static bool IsHttpUrl(string urlOrPath)
{
try {
var url = new Uri(urlOrPath);
return new[] {"https", "http"}.Contains(url.Scheme.ToLowerInvariant());
} catch (Exception) {
return false;
}
}
public static async Task DeleteDirectoryWithFallbackToNextReboot(string dir)
{
try {
await Utility.DeleteDirectory(dir);
} catch (UnauthorizedAccessException ex) {
var message = String.Format("Uninstall failed to delete dir '{0}', punting to next reboot", dir);
LogHost.Default.WarnException(message, ex);
Utility.DeleteDirectoryAtNextReboot(dir);
}
}
public static void DeleteDirectoryAtNextReboot(string directoryPath)
{
var di = new DirectoryInfo(directoryPath);
if (!di.Exists) {
Log().Warn("DeleteDirectoryAtNextReboot: does not exist - {0}", directoryPath);
return;
}
// NB: MoveFileEx blows up if you're a non-admin, so you always need a backup plan
di.GetFiles().ForEach(x => safeDeleteFileAtNextReboot(x.FullName));
di.GetDirectories().ForEach(x => DeleteDirectoryAtNextReboot(x.FullName));
safeDeleteFileAtNextReboot(directoryPath);
}
static void safeDeleteFileAtNextReboot(string name)
{
if (MoveFileEx(name, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT)) return;
// thank you, http://www.pinvoke.net/default.aspx/coredll.getlasterror
var lastError = Marshal.GetLastWin32Error();
Log().Error("safeDeleteFileAtNextReboot: failed - {0} - {1}", name, lastError);
}
static IFullLogger logger;
static IFullLogger Log()
{
return logger ??
(logger = Locator.CurrentMutable.GetService<ILogManager>().GetLogger(typeof(Utility)));
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);
[Flags]
enum MoveFileFlags
{
MOVEFILE_REPLACE_EXISTING = 0x00000001,
MOVEFILE_COPY_ALLOWED = 0x00000002,
MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
MOVEFILE_WRITE_THROUGH = 0x00000008,
MOVEFILE_CREATE_HARDLINK = 0x00000010,
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
}
}
/*
public sealed class SingleGlobalInstance : IDisposable
{
readonly static object gate = 42;
bool HasHandle = false;
Mutex mutex;
EventLoopScheduler lockScheduler = new EventLoopScheduler();
public SingleGlobalInstance(string key, int timeOut)
{
if (RxApp.InUnitTestRunner()) {
HasHandle = Observable.Start(() => Monitor.TryEnter(gate, timeOut), lockScheduler).First();
if (HasHandle == false)
throw new TimeoutException("Timeout waiting for exclusive access on SingleInstance");
return;
}
initMutex(key);
try
{
if (timeOut <= 0)
HasHandle = Observable.Start(() => mutex.WaitOne(Timeout.Infinite, false), lockScheduler).First();
else
HasHandle = Observable.Start(() => mutex.WaitOne(timeOut, false), lockScheduler).First();
if (HasHandle == false)
throw new TimeoutException("Timeout waiting for exclusive access on SingleInstance");
}
catch (AbandonedMutexException)
{
HasHandle = true;
}
}
private void initMutex(string key)
{
string mutexId = string.Format("Global\\{{{0}}}", key);
mutex = new Mutex(false, mutexId);
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);
}
public void Dispose()
{
if (HasHandle && RxApp.InUnitTestRunner()) {
Observable.Start(() => Monitor.Exit(gate), lockScheduler).First();
HasHandle = false;
}
if (HasHandle && mutex != null) {
Observable.Start(() => mutex.ReleaseMutex(), lockScheduler).First();
HasHandle = false;
}
lockScheduler.Dispose();
}
}
*/
public static class Disposable
{
public static IDisposable Create(Action action)
{
return new AnonDisposable(action);
}
class AnonDisposable : IDisposable
{
static readonly Action dummyBlock = (() => { });
Action block;
public AnonDisposable(Action b)
{
block = b;
}
public void Dispose()
{
Interlocked.Exchange(ref block, dummyBlock)();
}
}
}
}