Skip to content

Commit 01e274a

Browse files
committed
Refactor a little
1 parent b848a72 commit 01e274a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/PathVerifier.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class PathVerifier
1616
private static readonly char[] AdditionalInvalidPathChars = { '*', '?' };
1717
private readonly IMockFileDataAccessor _mockFileDataAccessor;
1818

19+
// Windows supports extended-length paths with a `\\?\` prefix, to work around low path length limits.
20+
// Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
21+
private const string WINDOWS_EXTENDED_LENGTH_PATH_PREFIX = @"\\?\";
22+
1923
/// <summary>
2024
/// Creates a new verifier instance.
2125
/// </summary>
@@ -65,10 +69,10 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName)
6569

6670
private static bool IsValidUseOfVolumeSeparatorChar(string path)
6771
{
68-
const string EXTENDED_LENGTH_PATH_PREFIX = @"\\?\";
69-
if (path.StartsWith(EXTENDED_LENGTH_PATH_PREFIX))
72+
if (XFS.IsWindowsPlatform() && path.StartsWith(WINDOWS_EXTENDED_LENGTH_PATH_PREFIX))
7073
{
71-
path = path.Substring(EXTENDED_LENGTH_PATH_PREFIX.Length);
74+
// Skip over the `\\?\` prefix if there is one.
75+
path = path.Substring(WINDOWS_EXTENDED_LENGTH_PATH_PREFIX.Length);
7276
}
7377

7478
var lastVolSepIndex = path.LastIndexOf(Path.VolumeSeparatorChar);
@@ -107,11 +111,9 @@ public bool HasIllegalCharacters(string path, bool checkAdditional)
107111
// AdditionalInvalidPathChars includes '?', but this character is allowed in extended-length
108112
// windows path prefixes (`\\?\`). If we're dealing with such a path, check for invalid
109113
// characters after the prefix.
110-
// Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
111-
const string EXTENDED_LENGTH_PATH_PREFIX = @"\\?\";
112-
if (XFS.IsWindowsPlatform() && path.StartsWith(EXTENDED_LENGTH_PATH_PREFIX))
114+
if (XFS.IsWindowsPlatform() && path.StartsWith(WINDOWS_EXTENDED_LENGTH_PATH_PREFIX))
113115
{
114-
path = path.Substring(EXTENDED_LENGTH_PATH_PREFIX.Length);
116+
path = path.Substring(WINDOWS_EXTENDED_LENGTH_PATH_PREFIX.Length);
115117
}
116118

117119
return path.IndexOfAny(invalidPathChars.Concat(AdditionalInvalidPathChars).ToArray()) >= 0;

0 commit comments

Comments
 (0)