Skip to content

Commit

Permalink
Fix regression with rooted paths without volume labels on Windows
Browse files Browse the repository at this point in the history
Also explain why we're not just always calling Path.GetFullPath

xoofx#92
  • Loading branch information
Metapyziks committed Jul 30, 2024
1 parent a7e549f commit 86b2af6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Zio/FileSystems/PhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,12 @@ protected override UPath ConvertPathFromInternalImpl(string innerPath)
if (innerPath.StartsWith(@"\\", StringComparison.Ordinal) || innerPath.StartsWith(@"\?", StringComparison.Ordinal))
throw new NotSupportedException($"Path starting with `\\\\` or `\\?` are not supported -> `{innerPath}` ");

var absolutePath = Path.IsPathRooted(innerPath) ? innerPath : Path.GetFullPath(innerPath);
var driveIndex = absolutePath.IndexOf(":\\", StringComparison.Ordinal);
if (driveIndex != 1)
// We want to avoid using Path.GetFullPath unless absolutely necessary,
// because it can change the case of already rooted paths that contain a ~
var absolutePath = HasWindowsVolumeLabel(innerPath) ? innerPath : Path.GetFullPath(innerPath);

// Assert that Path.GetFullPath returned the format we expect
if (!HasWindowsVolumeLabel(absolutePath))
throw new ArgumentException($"Expecting a drive for the path `{absolutePath}`");

var builder = UPath.GetSharedStringBuilder();
Expand Down Expand Up @@ -1032,4 +1035,12 @@ private static bool IsDriveLetter(char c)
{
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}

private static bool HasWindowsVolumeLabel( string path )
{
if ( !IsOnWindows )
throw new NotSupportedException( $"{nameof( HasWindowsVolumeLabel )} is only supported on Windows platforms." );

return path.Length >= 3 && path[1] == ':' && path[2] is '\\' or '/';
}
}

0 comments on commit 86b2af6

Please sign in to comment.