-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
PathHelpers.cs
292 lines (254 loc) · 9.79 KB
/
PathHelpers.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using R2RTest;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// A set of helper to manipulate paths into a canonicalized form to ensure user-provided paths
/// match those in the ETW log.
/// </summary>
static class PathExtensions
{
/// <summary>
/// Millisecond timeout for file / directory deletion.
/// </summary>
const int DeletionTimeoutMilliseconds = 10000;
/// <summary>
/// Back-off for repeated checks for directory deletion. According to my local experience [trylek],
/// when the directory is opened in the file explorer, the propagation typically takes 2 seconds.
/// </summary>
const int DirectoryDeletionBackoffMilliseconds = 500;
internal static string AppendOSExeSuffix(this string path) => (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? path + ".exe" : path);
internal static string AppendOSDllSuffix(this string path) => path +
(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so");
internal static string ToAbsolutePath(this string argValue) => Path.GetFullPath(argValue);
internal static string ToAbsoluteDirectoryPath(this string argValue) => argValue.ToAbsolutePath().StripTrailingDirectorySeparators();
internal static string StripTrailingDirectorySeparators(this string str)
{
if (String.IsNullOrWhiteSpace(str))
{
return str;
}
while (str.Length > 0 && str[str.Length - 1] == Path.DirectorySeparatorChar)
{
str = str.Remove(str.Length - 1);
}
return str;
}
internal static string ConcatenatePaths(this IEnumerable<string> paths)
{
return string.Join(Path.PathSeparator, paths);
}
// TODO: this assumes we're running tests from the root
internal static string DotNetAppPath => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet" : "Tools/dotnetcli/dotnet";
internal static void RecreateDirectory(this string path)
{
if (Directory.Exists(path))
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Task<bool> deleteSubtreeTask = path.DeleteSubtree();
deleteSubtreeTask.Wait();
if (deleteSubtreeTask.Result)
{
Console.WriteLine("Deleted {0} in {1} msecs", path, stopwatch.ElapsedMilliseconds);
}
else
{
throw new Exception($"Error: Could not delete output folder {path}");
}
}
Directory.CreateDirectory(path);
}
internal static bool IsParentOf(this DirectoryInfo outputPath, DirectoryInfo inputPath)
{
DirectoryInfo parentInfo = inputPath.Parent;
while (parentInfo != null)
{
if (parentInfo == outputPath)
return true;
parentInfo = parentInfo.Parent;
}
return false;
}
public static string FindFile(this string fileName, IEnumerable<string> paths)
{
foreach (string path in paths)
{
string fileOnPath = Path.Combine(path, fileName);
if (File.Exists(fileOnPath))
{
return fileOnPath;
}
}
return null;
}
/// <summary>
/// Parallel deletion of multiple disjunct subtrees.
/// </summary>
/// <param name="path">List of directories to delete</param>
/// <returns>Task returning true on success, false on failure</returns>
public static bool DeleteSubtrees(this string[] paths)
{
return DeleteSubtreesAsync(paths).Result;
}
private static async Task<bool> DeleteSubtreesAsync(this string[] paths)
{
bool succeeded = true;
var tasks = new List<Task<bool>>();
foreach (string path in paths)
{
try
{
if (!Directory.Exists(path))
{
// Non-existent folders are harmless w.r.t. deletion
Console.WriteLine("Skipping non-existent folder: '{0}'", path);
}
else
{
Console.WriteLine("Deleting '{0}'", path);
tasks.Add(path.DeleteSubtree());
}
}
catch (Exception ex)
{
Console.Error.WriteLine("Error deleting '{0}': {1}", path, ex.Message);
succeeded = false;
}
}
await Task<bool>.WhenAll(tasks);
foreach (var task in tasks)
{
if (!task.Result)
{
succeeded = false;
break;
}
}
return succeeded;
}
private static async Task<bool> DeleteSubtree(this string folder)
{
Task<bool>[] subtasks = new []
{
DeleteSubtreesAsync(Directory.GetDirectories(folder)),
DeleteFiles(Directory.GetFiles(folder))
};
await Task<bool>.WhenAll(subtasks);
bool succeeded = subtasks.All(subtask => subtask.Result);
if (succeeded)
{
Stopwatch folderDeletion = new Stopwatch();
folderDeletion.Start();
while (Directory.Exists(folder))
{
try
{
Directory.Delete(folder, recursive: false);
}
catch (DirectoryNotFoundException)
{
// Directory not found is OK (the directory might have been deleted during the back-off delay).
}
catch (Exception)
{
Console.WriteLine("Folder deletion failure, maybe transient ({0} msecs): '{1}'", folderDeletion.ElapsedMilliseconds, folder);
}
if (!Directory.Exists(folder))
{
break;
}
if (folderDeletion.ElapsedMilliseconds > DeletionTimeoutMilliseconds)
{
Console.Error.WriteLine("Timed out trying to delete directory '{0}'", folder);
succeeded = false;
break;
}
Thread.Sleep(DirectoryDeletionBackoffMilliseconds);
}
}
return succeeded;
}
private static async Task<bool> DeleteFiles(string[] files)
{
Task<bool>[] tasks = new Task<bool>[files.Length];
for (int i = 0; i < files.Length; i++)
{
int temp = i;
tasks[i] = Task<bool>.Run(() => files[temp].DeleteFile());
}
await Task<bool>.WhenAll(tasks);
return tasks.All(task => task.Result);
}
private static bool DeleteFile(this string file)
{
try
{
File.Delete(file);
return true;
}
catch (Exception ex)
{
Console.Error.WriteLine($"{file}: {ex.Message}");
return false;
}
}
public static string[] LocateOutputFolders(string folder, string coreRootFolder, IEnumerable<CompilerRunner> runners, bool recursive)
{
ConcurrentBag<string> directories = new ConcurrentBag<string>();
LocateOutputFoldersAsync(folder, coreRootFolder, runners, recursive, directories).Wait();
return directories.ToArray();
}
private static async Task LocateOutputFoldersAsync(string folder, string coreRootFolder, IEnumerable<CompilerRunner> runners, bool recursive, ConcurrentBag<string> directories)
{
if (coreRootFolder == null || !StringComparer.OrdinalIgnoreCase.Equals(folder, coreRootFolder))
{
List<Task> subfolderTasks = new List<Task>();
foreach (string dir in Directory.EnumerateDirectories(folder))
{
if (Path.GetExtension(dir).Equals(".out", StringComparison.OrdinalIgnoreCase))
{
foreach (CompilerRunner runner in runners)
{
if (runner.GetOutputPath(folder) == dir)
{
directories.Add(dir);
}
}
}
else if (recursive)
{
subfolderTasks.Add(Task.Run(() => LocateOutputFoldersAsync(dir, coreRootFolder, runners, recursive, directories)));
}
}
await Task.WhenAll(subfolderTasks);
}
}
public static bool DeleteOutputFolders(string folder, string coreRootFolder, IEnumerable<CompilerRunner> runners, bool recursive)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine("Locating output {0} {1}", (recursive ? "subtree" : "folder"), folder);
string[] outputFolders = LocateOutputFolders(folder, coreRootFolder, runners, recursive);
Console.WriteLine("Deleting {0} output folders", outputFolders.Length);
if (DeleteSubtrees(outputFolders))
{
Console.WriteLine("Successfully deleted {0} output folders in {1} msecs", outputFolders.Length, stopwatch.ElapsedMilliseconds);
return true;
}
else
{
Console.Error.WriteLine("Failed deleting {0} output folders in {1} msecs", outputFolders.Length, stopwatch.ElapsedMilliseconds);
return false;
}
}
public static StringComparer OSPathCaseComparer => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
}