Skip to content

URL.fileSystemPath should strip leading slash for Windows drive letters #964

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

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,32 @@ public struct URL: Equatable, Sendable, Hashable {
}
}

private static func windowsPath(for posixPath: String) -> String {
let utf8 = posixPath.utf8
guard utf8.count >= 4 else {
return posixPath
}
// "C:\" is standardized to "/C:/" on initialization
let array = Array(utf8)
if array[0] == ._slash,
array[1].isAlpha,
array[2] == ._colon,
array[3] == ._slash {
return String(Substring(utf8.dropFirst()))
}
Copy link
Member

Choose a reason for hiding this comment

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

This is unfortunately insufficient. \\server\share is considered a root, and therefore, //server/share should be returned similarly. I think that you should do something like if array.count > 1, path.substring(1).withCString(encodedAs: UTF16.self, PathIsRootW) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I don't think I understand. Do we need to strip leading slashes from \\server\share? A URL like URL(filePath: "\\\\server\\share") is stored internally as file:////server/share, so URL.fileSystemPath will return //server/share in that case.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, I think that we can get away with this then. The root classification is a bit tricky as [A-Z]: is not a root, [A-Z]:\ is a root, \\server is not a root, but \\server\share is a root.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that's interesting, I wasn't aware of the \\server case. There's likely room for improvement on how we initialize a URL like URL(filePath: "\\server") on Windows. Currently, this is treated as absolute (.absoluteString == "file:////server", .path == "//server"), but it sounds like this might be relative instead?

I don't think it blocks this PR to strip the leading slash from Windows drive letter RFC paths, but it's something we should consider after.

return posixPath
}

private static func fileSystemPath(for urlPath: String) -> String {
let charsToLeaveEncoded: Set<UInt8> = [._slash, 0]
return Parser.percentDecode(urlPath._droppingTrailingSlashes, excluding: charsToLeaveEncoded) ?? ""
guard let posixPath = Parser.percentDecode(urlPath._droppingTrailingSlashes, excluding: charsToLeaveEncoded) else {
return ""
}
#if os(Windows)
return windowsPath(for: posixPath)
#else
return posixPath
#endif
}

var fileSystemPath: String {
Expand Down
12 changes: 12 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ final class URLTests : XCTestCase {
try FileManager.default.removeItem(at: URL(filePath: "\(tempDirectory.path)/tmp-dir"))
}

#if os(Windows)
func testURLWindowsDriveLetterPath() throws {
let url = URL(filePath: "C:\\test\\path", directoryHint: .notDirectory)
// .absoluteString and .path() use the RFC 8089 URL path
XCTAssertEqual(url.absoluteString, "file:///C:/test/path")
XCTAssertEqual(url.path(), "/C:/test/path")
// .path and .fileSystemPath strip the leading slash
XCTAssertEqual(url.path, "C:/test/path")
XCTAssertEqual(url.fileSystemPath, "C:/test/path")
}
#endif

func testURLFilePathRelativeToBase() throws {
try FileManagerPlayground {
Directory("dir") {
Expand Down