Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dbd63bb

Browse files
committed
Handle escaping in Unix file enumeration (#20784)
* Handle escaping in Unix file enumeration We try and align with Windows, so we'll escape out [ and /. Adds a bunch of tests and issues for other cases that we don't match Windows behavior. * Clarify comments, move one case For completeness I ran everything through RtlIsNameInExpression to validate the "correct" results. I've commented appropriately. I moved one test to the "normal" block that I misplaced. * Optimize the escaping a bit * Improve perf further * Whoops, miscopied * More feedback
1 parent d7fd784 commit dbd63bb

File tree

3 files changed

+424
-0
lines changed

3 files changed

+424
-0
lines changed

src/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,21 @@ internal FileSystemEnumerable(
504504
}
505505
searchPattern = searchPattern.Substring(lastSlash + 1);
506506
}
507+
508+
// Typically we shouldn't see either of these cases, an upfront check is much faster
509+
foreach (char c in searchPattern)
510+
{
511+
if (c == '\\' || c == '[')
512+
{
513+
// We need to escape any escape characters in the search pattern
514+
searchPattern = searchPattern.Replace(@"\", @"\\");
515+
516+
// And then escape '[' to prevent it being picked up as a wildcard
517+
searchPattern = searchPattern.Replace(@"[", @"\[");
518+
break;
519+
}
520+
}
521+
507522
string fullPath = Path.GetFullPath(userPath);
508523

509524
// Store everything for the enumerator
@@ -638,6 +653,7 @@ private static string NormalizeSearchPattern(string searchPattern)
638653
{
639654
searchPattern += "*";
640655
}
656+
641657
return searchPattern;
642658
}
643659

src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ public virtual string[] GetEntries(string dirName)
2020
return Directory.GetFileSystemEntries(dirName);
2121
}
2222

23+
/// <summary>
24+
/// Create a file at the given path or directory if GetEntries doesn't return files
25+
/// </summary>
26+
protected void CreateItem(string path)
27+
{
28+
if (TestFiles)
29+
File.WriteAllText(path, path);
30+
else
31+
Directory.CreateDirectory(path);
32+
}
33+
2334
#endregion
2435

2536
#region UniversalTests

0 commit comments

Comments
 (0)