Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not Path.Join user directory if it's "C:" #94587

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ public void WindowsInvalidCharsPath_Core(string invalid)
Assert.Throws<IOException>(() => GetEntries(invalid));
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsRelativeToCurrentDrivePath()
{
RemoteExecutor.Invoke(() =>
{
string testRootDir = GetTestFilePath();
Directory.CreateDirectory(testRootDir);
Directory.SetCurrentDirectory(testRootDir);

const string TestSubdir = "foo";
Directory.CreateDirectory(Path.Combine(testRootDir, TestSubdir));

string currentDrive = testRootDir.Substring(0, 2);
string[] results = GetEntries(currentDrive);

string result = Assert.Single(results);
Assert.Equal(currentDrive + TestSubdir, result);
}).Dispose();
}

[Theory,
InlineData(" "),
InlineData(" "),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,11 @@ public FileSystemInfo ToFileSystemInfo()
/// </summary>
public string ToFullPath() =>
new string(FullPath);

private static string Join(
ReadOnlySpan<char> originalRootDirectory,
ReadOnlySpan<char> relativePath,
ReadOnlySpan<char> fileName) =>
Path.Join(originalRootDirectory, relativePath, fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,18 @@ public FileSystemInfo ToFileSystemInfo()
/// <returns>A string representing the full path.</returns>
public string ToFullPath() =>
Path.Join(Directory, FileName);

private static string Join(
ReadOnlySpan<char> originalRootDirectory,
ReadOnlySpan<char> relativePath,
ReadOnlySpan<char> fileName)
{
if (originalRootDirectory.Length == 2 && originalRootDirectory[1] == Path.VolumeSeparatorChar)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for a Windows drive to have more than one letter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not as far as I know and I expect that would break assumptions elsewhere if it could.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When A-Z alphabets are all assigned, user either needs to switch to UNC paths via GUIDs \\?\Volume{GUID} to access the next device, or mount it as directory (via Disk management). That device would not show up in explorer by default.

{
return string.Concat(originalRootDirectory, Path.Join(relativePath, fileName));
}

return Path.Join(originalRootDirectory, relativePath, fileName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public string ToSpecifiedFullPath()
if (Path.EndsInDirectorySeparator(OriginalRootDirectory) && PathInternal.StartsWithDirectorySeparator(relativePath))
relativePath = relativePath.Slice(1);

return Path.Join(OriginalRootDirectory, relativePath, FileName);
return Join(OriginalRootDirectory, relativePath, FileName);
}
}
}
Loading