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

GetPathRoot cannot handle UNC paths on unix. #109594

Open
crone66 opened this issue Nov 6, 2024 · 2 comments
Open

GetPathRoot cannot handle UNC paths on unix. #109594

crone66 opened this issue Nov 6, 2024 · 2 comments
Labels
area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@crone66
Copy link

crone66 commented Nov 6, 2024

Description

System.IO.Path.GetPathRoot() seem to have a strange behavior when using UNC paths on a unix system. Maybe I just don't know how UNC paths are supposed to look like on UNIX but maybe someone can explain whats going on here.

Let's assume we have the UNC path @"\\server\share". If I pass the path to GetPathRoot(path) I would assume the same path as result. On windows this works perfectly but on Unix (tested with Ubuntu 24.4) the result is "". My initial thought was maybe I have to use forward slashes since unix and backward slashs might not work. But when using forward slashes (@"//server/share") the result is even more strange. Since the directory seperator is normalized when using the string overload, windows returns @"\\server\share" but unix returns "/", From my point of view the UNIX result doesn't make sense. I checked the code and the implementations are very different for both platforms. For Unix we just check if the the first character is a forward slash and if yes just return a forward slash not taking into account UNC path.

From what I've read online on multiple sources e.g. https://en.linuxportal.info/encyclopedia/u/unc-universal-naming-convention "//server/share" should be a valid UNC path and therefore be handled as such when using System.IO.Path methods.

Was this a oversight or are my assumptions about UNC paths on UNIX incorrect or is there any other reason for this behavior that I'm not aware of?

Thank you.

Reproduction Steps

Execute the following line on Windows and a Unix system (tested with Ubuntu 24.4)

Console.WriteLine(System.IO.Path.GetPathRoot(@"\\server\share"));

Result:
Windows: "\server\share"
Unix: ""

Console.WriteLine(System.IO.Path.GetPathRoot(@"//server/share"));

Result:
Windows: "\server\share"
Unix: "/"

Expected behavior

Console.WriteLine(System.IO.Path.GetPathRoot(@"\\server\share"));

Result:
Windows: "\server\share"
Unix: "" might be okay but maybe "//server/share" or "\server\share" would be good to

Console.WriteLine(System.IO.Path.GetPathRoot(@"//server/share"));

Result:
Windows: "\server\share"
Unix: "//server/share" or "\server\share" whatever makes more sense and fits the specification.

Actual behavior

Console.WriteLine(System.IO.Path.GetPathRoot(@"\\server\share"));

Result:
Windows: "\server\share"
Unix: ""

Console.WriteLine(System.IO.Path.GetPathRoot(@"//server/share"));

Result:
Windows: "\server\share"
Unix: "/"

Regression?

No response

Known Workarounds

No response

Configuration

Tested on Windows 10 & 11 64 Bit and Ubuntu 24.4 (WSL)

Other information

System.IO.Path.GetPathRoot() is used by other methods such as System.IO.Path.GetDirectoryName() which currently causes weird path modification on UNIX.

Example:
path: "//server/share" -> result: "/server/share"

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 6, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@karakasa
Copy link
Contributor

karakasa commented Nov 7, 2024

The current doc seemingly implies the method works with UNC paths only on Windows.

  • "\ComputerName\SharedFolder" (Windows: a UNC path).

don't know if this is a design choice, as Linux requires SMB path to be mounted to become "valid".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants