-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implemented _getFileSystemRepresentation for Windows #2366
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ public func NSTemporaryDirectory() -> String { | |
guard GetTempPathW(DWORD(wszPath.count), &wszPath) <= cchLength else { | ||
preconditionFailure("GetTempPathW mutation race") | ||
} | ||
return String(decodingCString: wszPath, as: UTF16.self) | ||
return String(decodingCString: wszPath, as: UTF16.self).standardizingPath | ||
#else | ||
#if canImport(Darwin) | ||
let safe_confstr = { (name: Int32, buf: UnsafeMutablePointer<Int8>?, len: Int) -> Int in | ||
|
@@ -348,14 +348,32 @@ extension NSString { | |
|
||
return result | ||
} | ||
|
||
|
||
#if os(Windows) | ||
// Convert to a posix style '/' separated path | ||
internal var unixPath: String { | ||
var droppedPrefix = self as String | ||
// If there is anything before the drive letter, | ||
// e.g. "\\?\, \\host\, \??\", remove it | ||
if isAbsolutePath, let idx = droppedPrefix.firstIndex(of: ":") { | ||
droppedPrefix.removeSubrange(..<droppedPrefix.index(before: idx)) | ||
} | ||
let slashesConverted = String(droppedPrefix.map({ $0 == "\\" ? "/" : $0 })) | ||
let compressTrailing = slashesConverted._stringByFixingSlashes(stripTrailing: false) | ||
return compressTrailing | ||
} | ||
#endif | ||
|
||
public var standardizingPath: String { | ||
#if os(Windows) | ||
let expanded = unixPath.expandingTildeInPath | ||
#else | ||
let expanded = expandingTildeInPath | ||
var resolved = expanded._bridgeToObjectiveC().resolvingSymlinksInPath | ||
#endif | ||
let resolved = expanded._bridgeToObjectiveC().resolvingSymlinksInPath | ||
|
||
let automount = "/var/automount" | ||
resolved = resolved._tryToRemovePathPrefix(automount) ?? resolved | ||
return resolved | ||
return resolved._tryToRemovePathPrefix(automount) ?? resolved | ||
} | ||
|
||
public var resolvingSymlinksInPath: String { | ||
|
@@ -554,11 +572,61 @@ extension NSString { | |
} | ||
|
||
public func getFileSystemRepresentation(_ cname: UnsafeMutablePointer<Int8>, maxLength max: Int) -> Bool { | ||
#if os(Windows) | ||
let fsr = UnsafeMutablePointer<WCHAR>.allocate(capacity: max) | ||
defer { fsr.deallocate() } | ||
guard _getFileSystemRepresentation(fsr, maxLength: max) else { return false } | ||
return String(decodingCString: fsr, as: UTF16.self).withCString() { | ||
let chars = strnlen_s($0, max) | ||
guard chars < max else { return false } | ||
cname.assign(from: $0, count: chars + 1) | ||
return true | ||
} | ||
#else | ||
return _getFileSystemRepresentation(cname, maxLength: max) | ||
#endif | ||
} | ||
|
||
internal func _getFileSystemRepresentation(_ cname: UnsafeMutablePointer<NativeFSRCharType>, maxLength max: Int) -> Bool { | ||
guard self.length > 0 else { | ||
gmittert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false | ||
} | ||
|
||
#if os(Windows) | ||
var fsr = self._swiftObject | ||
let idx = fsr.startIndex | ||
|
||
// If we have an RFC 8089 style path e.g. `/[drive-letter]:/...`, drop the | ||
// leading /, otherwise, a leading slash indicates a rooted path on the | ||
// drive for the current working directory | ||
if fsr.count >= 3 && fsr[idx] == "/" && fsr[fsr.index(after: idx)].isLetter && fsr[fsr.index(idx, offsetBy: 2)] == ":" { | ||
fsr.removeFirst() | ||
} | ||
|
||
// Windows APIS that go through the path parser can handle | ||
// forward slashes in paths. However, symlinks created with | ||
// forward slashes do not resolve properly, so we normalize | ||
// the path separators anyways. | ||
fsr = fsr.replacingOccurrences(of: "/", with: "\\") | ||
|
||
// Drop trailing slashes unless it follows a drive letter. On | ||
// Windows the path `C:\` indicates the root directory of the | ||
// `C:` drive. The path `C:` indicates the current working | ||
// directory on the `C:` drive. | ||
while fsr.count > 1 | ||
&& (fsr[fsr.index(before: fsr.endIndex)] == "\\") | ||
&& !(fsr.count == 3 && fsr[fsr.index(fsr.endIndex, offsetBy: -2)] == ":") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish there was a more elegant way to write this. |
||
fsr.removeLast() | ||
} | ||
|
||
return fsr.withCString(encodedAs: UTF16.self) { | ||
let wchars = wcsnlen_s($0, max) | ||
guard wchars < max else { return false } | ||
cname.assign(from: $0, count: wchars + 1) | ||
return true | ||
} | ||
#else | ||
return CFStringGetFileSystemRepresentation(self._cfObject, cname, max) | ||
gmittert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.