-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
EngineFileUtilities.cs
571 lines (514 loc) · 30.5 KB
/
EngineFileUtilities.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Build.BackEnd.Components.Logging;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
#nullable disable
namespace Microsoft.Build.Internal
{
internal static class EngineFileUtilities
{
private const string DriveEnumeratingWildcardMessageResourceName = "WildcardResultsInDriveEnumeration";
// Regexes for wildcard filespecs that should not get expanded
// By default all wildcards are expanded.
private static List<Regex> s_lazyWildCardExpansionRegexes;
static EngineFileUtilities()
{
if (Traits.Instance.UseLazyWildCardEvaluation)
{
CaptureLazyWildcardRegexes();
}
}
// used by test to reset regexes
internal static void CaptureLazyWildcardRegexes()
{
s_lazyWildCardExpansionRegexes = PopulateRegexFromEnvironment();
}
/// <summary>
/// Used for the purposes of evaluating an item specification. Given a filespec that may include wildcard characters * and
/// ?, we translate it into an actual list of files. If the input filespec doesn't contain any wildcard characters, and it
/// doesn't appear to point to an actual file on disk, then we just give back the input string as an array of length one,
/// assuming that it wasn't really intended to be a filename (as items are not required to necessarily represent files).
/// Any wildcards passed in that are unescaped will be treated as real wildcards.
/// The "include" of items passed back from the filesystem will be returned canonically escaped.
/// The ordering of the list returned is deterministic (it is sorted).
/// Will never throw IO exceptions. If path is invalid, just returns filespec verbatim.
/// </summary>
/// <param name="directoryEscaped">The directory to evaluate, escaped.</param>
/// <param name="filespecEscaped">The filespec to evaluate, escaped.</param>
/// <param name="loggingMechanism">Accepted loggers for drive enumeration: TargetLoggingContext, ILoggingService,
/// and EvaluationLoggingContext.</param>
/// <param name="excludeLocation">Location of Exclude element in file, used after drive enumeration detection.</param>
/// <returns>Array of file paths, unescaped.</returns>
internal static string[] GetFileListUnescaped(
string directoryEscaped,
string filespecEscaped,
object loggingMechanism = null,
IElementLocation excludeLocation = null)
{
return GetFileList(
directoryEscaped,
filespecEscaped,
returnEscaped: false,
forceEvaluateWildCards: false,
excludeSpecsEscaped: null,
fileMatcher: FileMatcher.Default,
loggingMechanism: loggingMechanism,
excludeLocation: excludeLocation);
}
/// <summary>
/// Used for the purposes of evaluating an item specification. Given a filespec that may include wildcard characters * and
/// ?, we translate it into an actual list of files. If the input filespec doesn't contain any wildcard characters, and it
/// doesn't appear to point to an actual file on disk, then we just give back the input string as an array of length one,
/// assuming that it wasn't really intended to be a filename (as items are not required to necessarily represent files).
/// Any wildcards passed in that are unescaped will be treated as real wildcards.
/// The "include" of items passed back from the filesystem will be returned canonically escaped.
/// The ordering of the list returned is deterministic (it is sorted).
/// Will never throw IO exceptions. If path is invalid, just returns filespec verbatim.
/// </summary>
/// <param name="directoryEscaped">The directory to evaluate, escaped.</param>
/// <param name="filespecEscaped">The filespec to evaluate, escaped.</param>
/// <param name="excludeSpecsEscaped">Filespecs to exclude, escaped.</param>
/// <param name="forceEvaluate">Whether to force file glob expansion when eager expansion is turned off.</param>
/// <param name="fileMatcher">Class that contains functions for matching filenames with patterns.</param>
/// <param name="loggingMechanism">Accepted loggers for drive enumeration: TargetLoggingContext, ILoggingService,
/// and EvaluationLoggingContext.</param>
/// <param name="includeLocation">Location of Include element in file, used after drive enumeration detection.</param>
/// <param name="excludeLocation">Location of Exclude element in file, used after drive enumeration detection.</param>
/// <param name="importLocation">Location of Import element in file, used after drive enumeration detection.</param>
/// <param name="buildEventContext">Context to log a warning, used after drive enumeration detection.</param>
/// <param name="buildEventFileInfoFullPath">Full path to project file to create BuildEventFileInfo,
/// used after drive enumeration detection.</param>
/// <param name="disableExcludeDriveEnumerationWarning">Flag used to detect when to properly log a warning
/// for the Exclude attribute after detecting a drive enumerating wildcard.</param>
/// <returns>Array of file paths, escaped.</returns>
internal static string[] GetFileListEscaped(
string directoryEscaped,
string filespecEscaped,
IEnumerable<string> excludeSpecsEscaped = null,
bool forceEvaluate = false,
FileMatcher fileMatcher = null,
object loggingMechanism = null,
IElementLocation includeLocation = null,
IElementLocation excludeLocation = null,
IElementLocation importLocation = null,
BuildEventContext buildEventContext = null,
string buildEventFileInfoFullPath = null,
bool disableExcludeDriveEnumerationWarning = false)
{
return GetFileList(
directoryEscaped,
filespecEscaped,
returnEscaped: true,
forceEvaluate,
excludeSpecsEscaped,
fileMatcher ?? FileMatcher.Default,
loggingMechanism: loggingMechanism,
includeLocation: includeLocation,
excludeLocation: excludeLocation,
importLocation: importLocation,
buildEventFileInfoFullPath: buildEventFileInfoFullPath,
buildEventContext: buildEventContext,
disableExcludeDriveEnumerationWarning: disableExcludeDriveEnumerationWarning);
}
internal static bool FilespecHasWildcards(string filespecEscaped)
{
if (!FileMatcher.HasWildcards(filespecEscaped))
{
return false;
}
// If the item's Include has both escaped wildcards and real wildcards, then it's
// not clear what they are asking us to do. Go to the file system and find
// files that literally have '*' in their filename? Well, that's not going to
// happen because '*' is an illegal character to have in a filename.
return !EscapingUtilities.ContainsEscapedWildcards(filespecEscaped);
}
/// <summary>
/// Used for the purposes of evaluating an item specification. Given a filespec that may include wildcard characters * and
/// ?, we translate it into an actual list of files. If the input filespec doesn't contain any wildcard characters, and it
/// doesn't appear to point to an actual file on disk, then we just give back the input string as an array of length one,
/// assuming that it wasn't really intended to be a filename (as items are not required to necessarily represent files).
/// Any wildcards passed in that are unescaped will be treated as real wildcards.
/// The "include" of items passed back from the filesystem will be returned canonically escaped.
/// The ordering of the list returned is deterministic (it is sorted).
/// Will never throw IO exceptions: if there is no match, returns the input verbatim.
/// </summary>
/// <param name="directoryEscaped">The directory to evaluate, escaped.</param>
/// <param name="filespecEscaped">The filespec to evaluate, escaped.</param>
/// <param name="returnEscaped"><code>true</code> to return escaped specs.</param>
/// <param name="forceEvaluateWildCards">Whether to force file glob expansion when eager expansion is turned off.</param>
/// <param name="excludeSpecsEscaped">The exclude specification, escaped.</param>
/// <param name="fileMatcher">Class that contains functions for matching filenames with patterns.</param>
/// <param name="loggingMechanism">Accepted loggers for drive enumeration: TargetLoggingContext, ILoggingService,
/// and EvaluationLoggingContext.</param>
/// <param name="includeLocation">Location of Include element in file, used after drive enumeration detection.</param>
/// <param name="excludeLocation">Location of Exclude element in file, used after drive enumeration detection.</param>
/// <param name="importLocation">Location of Import element in file, used after drive enumeration detection.</param>
/// <param name="buildEventContext">Context to log a warning, used after drive enumeration detection.</param>
/// <param name="buildEventFileInfoFullPath">Full path to project file to create BuildEventFileInfo,
/// used after drive enumeration detection.</param>
/// <param name="disableExcludeDriveEnumerationWarning">Flag used to detect when to properly log a warning
/// for the Exclude attribute after detecting a drive enumerating wildcard.</param>
/// <returns>Array of file paths.</returns>
private static string[] GetFileList(
string directoryEscaped,
string filespecEscaped,
bool returnEscaped,
bool forceEvaluateWildCards,
IEnumerable<string> excludeSpecsEscaped,
FileMatcher fileMatcher,
object loggingMechanism = null,
IElementLocation includeLocation = null,
IElementLocation excludeLocation = null,
IElementLocation importLocation = null,
BuildEventContext buildEventContext = null,
string buildEventFileInfoFullPath = null,
bool disableExcludeDriveEnumerationWarning = false)
{
ErrorUtilities.VerifyThrowInternalLength(filespecEscaped, nameof(filespecEscaped));
string[] fileList;
// Used to properly detect and log drive enumerating wildcards when applicable.
FileMatcher.SearchAction action = FileMatcher.SearchAction.None;
string excludeFileSpec = string.Empty;
var noWildcards = !FilespecHasWildcards(filespecEscaped) || FilespecMatchesLazyWildcard(filespecEscaped, forceEvaluateWildCards);
// It is possible to return original string if no wildcard matches and no entries in Exclude set.
if (noWildcards && excludeSpecsEscaped?.Any() != true)
{
// Just return the original string.
fileList = new string[] { returnEscaped ? filespecEscaped : EscapingUtilities.UnescapeAll(filespecEscaped) };
}
else
{
if (Traits.Instance.LogExpandedWildcards)
{
ErrorUtilities.DebugTraceMessage("Expanding wildcard for file spec {0}", filespecEscaped);
}
// Unescape before handing it to the filesystem.
var directoryUnescaped = EscapingUtilities.UnescapeAll(directoryEscaped);
var filespecUnescaped = EscapingUtilities.UnescapeAll(filespecEscaped);
var excludeSpecsUnescaped = excludeSpecsEscaped?.Where(IsValidExclude).Select(i => EscapingUtilities.UnescapeAll(i)).ToList();
// Get the list of actual files which match the filespec. Put
// the list into a string array. If the filespec started out
// as a relative path, we will get back a bunch of relative paths.
// If the filespec started out as an absolute path, we will get
// back a bunch of absolute paths. Also retrieves the search action
// and relevant Exclude filespec for drive enumerating wildcard detection.
(fileList, action, excludeFileSpec) = fileMatcher.GetFiles(directoryUnescaped, filespecUnescaped, excludeSpecsUnescaped);
// Determines whether Exclude filespec or passed in file spec should be
// used in drive enumeration warning or exception.
bool excludeFileSpecIsEmpty = string.IsNullOrWhiteSpace(excludeFileSpec);
string fileSpec = excludeFileSpecIsEmpty ? filespecUnescaped : excludeFileSpec;
switch (action)
{
case (FileMatcher.SearchAction.LogDriveEnumeratingWildcard):
switch (loggingMechanism)
{
// Logging mechanism received from ItemGroupIntrinsicTask.
case TargetLoggingContext targetLoggingContext:
LogDriveEnumerationWarningWithTargetLoggingContext(
targetLoggingContext,
includeLocation,
excludeLocation,
excludeFileSpecIsEmpty,
disableExcludeDriveEnumerationWarning,
fileSpec);
break;
// Logging mechanism received from Evaluator.
case ILoggingService loggingService:
LogDriveEnumerationWarningWithLoggingService(
loggingService,
includeLocation,
buildEventContext,
buildEventFileInfoFullPath,
filespecUnescaped);
break;
// Logging mechanism received from Evaluator and LazyItemEvaluator.IncludeOperation.
case EvaluationLoggingContext evaluationLoggingContext:
LogDriveEnumerationWarningWithEvaluationLoggingContext(
evaluationLoggingContext,
importLocation,
includeLocation,
excludeLocation,
excludeFileSpecIsEmpty,
filespecUnescaped,
fileSpec);
break;
default:
throw new InternalErrorException($"Logging type {loggingMechanism.GetType()} is not understood by {nameof(GetFileList)}.");
}
break;
case (FileMatcher.SearchAction.FailOnDriveEnumeratingWildcard):
switch (loggingMechanism)
{
// Logging mechanism received from ItemGroupIntrinsicTask.
case TargetLoggingContext targetLoggingContext:
ThrowDriveEnumerationExceptionWithTargetLoggingContext(
includeLocation,
excludeLocation,
excludeFileSpecIsEmpty,
filespecUnescaped,
fileSpec);
break;
// Logging mechanism received from Evaluator.
case ILoggingService loggingService:
ThrowDriveEnumerationExceptionWithLoggingService(includeLocation, filespecUnescaped);
break;
// Logging mechanism received from Evaluator and LazyItemEvaluator.IncludeOperation.
case EvaluationLoggingContext evaluationLoggingContext:
ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(
importLocation,
includeLocation,
excludeLocation,
filespecUnescaped,
fileSpec,
excludeFileSpecIsEmpty);
break;
default:
throw new InternalErrorException($"Logging type {loggingMechanism.GetType()} is not understood by {nameof(GetFileList)}.");
}
break;
default: break;
}
ErrorUtilities.VerifyThrow(fileList != null, "We must have a list of files here, even if it's empty.");
// Before actually returning the file list, we sort them alphabetically. This
// provides a certain amount of extra determinism and reproducability. That is,
// we're sure that the build will behave in exactly the same way every time,
// and on every machine.
Array.Sort(fileList, StringComparer.OrdinalIgnoreCase);
if (returnEscaped)
{
// We must now go back and make sure all special characters are escaped because we always
// store data in the engine in escaped form so it doesn't interfere with our parsing.
// Note that this means that characters that were not escaped in the original filespec
// may now be escaped, but that's not easy to avoid.
for (int i = 0; i < fileList.Length; i++)
{
fileList[i] = EscapingUtilities.Escape(fileList[i]);
}
}
}
return fileList;
}
private static void LogDriveEnumerationWarningWithTargetLoggingContext(TargetLoggingContext targetLoggingContext, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, bool disableExcludeDriveEnumerationWarning, string fileSpec)
{
// Both condition lines are necessary to skip for the first GetFileListEscaped call
// and reach for the GetFileListUnescaped call when the wildcarded Exclude attribute results
// in a drive enumeration. Since we only want to check for the Exclude
// attribute here, we want to ensure that includeLocation is null - otherwise,
// Include wildcard attributes for the GetFileListEscaped calls would falsely appear
// with the Exclude attribute in the logged warning.
if (((!excludeFileSpecIsEmpty) && (!disableExcludeDriveEnumerationWarning)) ||
((includeLocation == null) && (excludeLocation != null)))
{
targetLoggingContext.LogWarning(
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.exclude,
XMakeElements.itemGroup,
excludeLocation.LocationString);
}
// Both conditions are necessary to reach for both GetFileListEscaped calls
// and skip for the GetFileListUnescaped call when the wildcarded Include attribute
// results in drive enumeration.
else if (excludeFileSpecIsEmpty && (includeLocation != null))
{
targetLoggingContext.LogWarning(
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
}
private static void LogDriveEnumerationWarningWithLoggingService(ILoggingService loggingService, IElementLocation includeLocation, BuildEventContext buildEventContext, string buildEventFileInfoFullPath, string filespecUnescaped)
{
if (buildEventContext != null && includeLocation != null)
{
loggingService.LogWarning(
buildEventContext,
string.Empty,
new BuildEventFileInfo(buildEventFileInfoFullPath),
DriveEnumeratingWildcardMessageResourceName,
filespecUnescaped,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
}
private static void LogDriveEnumerationWarningWithEvaluationLoggingContext(EvaluationLoggingContext evaluationLoggingContext, IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
{
if (importLocation != null)
{
evaluationLoggingContext.LogWarning(
DriveEnumeratingWildcardMessageResourceName,
filespecUnescaped,
XMakeAttributes.project,
XMakeElements.import,
importLocation.LocationString);
}
else if (excludeFileSpecIsEmpty && includeLocation != null)
{
evaluationLoggingContext.LogWarning(
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
else if (excludeLocation != null)
{
evaluationLoggingContext.LogWarning(
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.exclude,
XMakeElements.itemGroup,
excludeLocation.LocationString);
}
}
private static void ThrowDriveEnumerationExceptionWithTargetLoggingContext(IElementLocation includeLocation, IElementLocation excludeLocation, bool excludeFileSpecIsEmpty, string filespecUnescaped, string fileSpec)
{
// The first condition is necessary to reach for both GetFileListEscaped calls
// whenever the wildcarded Include attribute results in drive enumeration, and
// the second condition is necessary to skip for the GetFileListUnescaped call
// whenever the wildcarded Exclude attribute results in drive enumeration.
if (excludeFileSpecIsEmpty && (includeLocation != null))
{
ProjectErrorUtilities.ThrowInvalidProject(
includeLocation,
DriveEnumeratingWildcardMessageResourceName,
filespecUnescaped,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
// The first condition is necessary to reach for both GetFileListEscaped calls
// whenever the wildcarded Exclude attribute results in drive enumeration, and
// the second condition is necessary to reach for the GetFileListUnescaped call
// (also when the wildcarded Exclude attribute results in drive enumeration).
else if (((!excludeFileSpecIsEmpty) || (includeLocation == null)) && (excludeLocation != null))
{
ProjectErrorUtilities.ThrowInvalidProject(
excludeLocation,
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.exclude,
XMakeElements.itemGroup,
excludeLocation.LocationString);
}
}
private static void ThrowDriveEnumerationExceptionWithLoggingService(IElementLocation includeLocation, string filespecUnescaped)
{
ProjectErrorUtilities.ThrowInvalidProject(
includeLocation,
DriveEnumeratingWildcardMessageResourceName,
filespecUnescaped,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
private static void ThrowDriveEnumerationExceptionWithEvaluationLoggingContext(IElementLocation importLocation, IElementLocation includeLocation, IElementLocation excludeLocation, string filespecUnescaped, string fileSpec, bool excludeFileSpecIsEmpty)
{
if (importLocation != null)
{
ProjectErrorUtilities.ThrowInvalidProject(
importLocation,
DriveEnumeratingWildcardMessageResourceName,
filespecUnescaped,
XMakeAttributes.project,
XMakeElements.import,
importLocation.LocationString);
}
else if (excludeFileSpecIsEmpty && includeLocation != null)
{
ProjectErrorUtilities.ThrowInvalidProject(
includeLocation,
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.include,
XMakeElements.itemGroup,
includeLocation.LocationString);
}
else if (excludeLocation != null)
{
ProjectErrorUtilities.ThrowInvalidProject(
excludeLocation,
DriveEnumeratingWildcardMessageResourceName,
fileSpec,
XMakeAttributes.exclude,
XMakeElements.itemGroup,
excludeLocation.LocationString);
}
}
private static bool FilespecMatchesLazyWildcard(string filespecEscaped, bool forceEvaluateWildCards)
{
return Traits.Instance.UseLazyWildCardEvaluation && !forceEvaluateWildCards && MatchesLazyWildcard(filespecEscaped);
}
private static bool IsValidExclude(string exclude)
{
// TODO: assumption on legal path characters: https://github.com/dotnet/msbuild/issues/781
// Excludes that have both wildcards and non escaped wildcards will never be matched on Windows, because
// wildcard characters are invalid in Windows paths.
// Filtering these excludes early keeps the glob expander simpler. Otherwise unescaping logic would reach all the way down to
// filespec parsing (parse escaped string (to correctly ignore escaped wildcards) and then
// unescape the path fragments to unfold potentially escaped wildcard chars)
var hasBothWildcardsAndEscapedWildcards = FileMatcher.HasWildcards(exclude) && EscapingUtilities.ContainsEscapedWildcards(exclude);
return !hasBothWildcardsAndEscapedWildcards;
}
private static List<Regex> PopulateRegexFromEnvironment()
{
string wildCards = Environment.GetEnvironmentVariable("MsBuildSkipEagerWildCardEvaluationRegexes");
if (string.IsNullOrEmpty(wildCards))
{
return new List<Regex>(0);
}
else
{
List<Regex> regexes = new List<Regex>();
foreach (string regex in wildCards.Split(MSBuildConstants.SemicolonChar))
{
Regex item = new Regex(regex, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
// trigger a match first?
item.IsMatch("foo");
regexes.Add(item);
}
return regexes;
}
}
// TODO: assumption on file system case sensitivity: https://github.com/dotnet/msbuild/issues/781
private static readonly Lazy<ConcurrentDictionary<string, bool>> _regexMatchCache = new Lazy<ConcurrentDictionary<string, bool>>(() => new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase));
private static bool MatchesLazyWildcard(string fileSpec)
{
return _regexMatchCache.Value.GetOrAdd(fileSpec, file => s_lazyWildCardExpansionRegexes.Any(regex => regex.IsMatch(fileSpec)));
}
/// <summary>
/// Returns a Func that will return true IFF its argument matches any of the specified filespecs.
/// Assumes filespec may be escaped, so it unescapes it.
/// The returned function makes no escaping assumptions or escaping operations. Its callers should control escaping.
/// </summary>
/// <param name="filespecsEscaped"></param>
/// <param name="currentDirectory"></param>
/// <returns>A Func that will return true IFF its argument matches any of the specified filespecs.</returns>
internal static Func<string, bool> GetFileSpecMatchTester(IList<string> filespecsEscaped, string currentDirectory)
{
var matchers = filespecsEscaped
.Select(fs => new Lazy<FileSpecMatcherTester>(() => FileSpecMatcherTester.Parse(currentDirectory, fs)))
.ToList();
return file => matchers.Any(m => m.Value.IsMatch(file));
}
internal sealed class IOCache
{
private readonly Lazy<ConcurrentDictionary<string, bool>> existenceCache = new Lazy<ConcurrentDictionary<string, bool>>(() => new ConcurrentDictionary<string, bool>(), true);
public bool DirectoryExists(string directory)
{
return existenceCache.Value.GetOrAdd(directory, directory => Directory.Exists(directory));
}
}
}
}