Skip to content

Foundation: improve relative path computation for iteration on Windows #4797

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
Jul 19, 2023
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
29 changes: 15 additions & 14 deletions Sources/Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1075,23 +1075,24 @@ extension FileManager.NSPathDirectoryEnumerator {
internal func _nextObject() -> Any? {
guard let url = innerEnumerator.nextObject() as? URL else { return nil }

var relativePath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH))

guard baseURL._withUnsafeWideFileSystemRepresentation({ baseUrlFsr in
url._withUnsafeWideFileSystemRepresentation { urlFsr in
let fromAttrs = GetFileAttributesW(baseUrlFsr)
let toAttrs = GetFileAttributesW(urlFsr)
guard fromAttrs != INVALID_FILE_ATTRIBUTES, toAttrs != INVALID_FILE_ATTRIBUTES else {
return false
let path: String? = try? baseURL.withUnsafeNTPath { pwszBasePath in
let dwBaseAttrs = GetFileAttributesW(pwszBasePath)
if dwBaseAttrs == INVALID_FILE_ATTRIBUTES { return nil }

return try? url.withUnsafeNTPath { pwszPath in
let dwAttrs = GetFileAttributesW(pwszPath)
if dwAttrs == INVALID_FILE_ATTRIBUTES { return nil }

return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(MAX_PATH)) {
guard PathRelativePathToW($0.baseAddress, pwszBasePath, dwBaseAttrs, pwszPath, dwAttrs) else { return nil }
// Drop the leading ".\" from the path
return String(decodingCString: $0.baseAddress!.advanced(by: 2), as: UTF16.self)
}
return PathRelativePathToW(&relativePath, baseUrlFsr, fromAttrs, urlFsr, toAttrs)
}
}) else { return nil }
}

let path = String(decodingCString: &relativePath, as: UTF16.self)
// Drop the leading ".\" from the path
_currentItemPath = String(path.dropFirst(2))
return _currentItemPath
_currentItemPath = path ?? _currentItemPath
return path
}

}
Expand Down
8 changes: 0 additions & 8 deletions Sources/Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,6 @@ public struct URL : ReferenceConvertible, Equatable {
return try block(fsRep)
}

#if os(Windows)
internal func _withUnsafeWideFileSystemRepresentation<ResultType>(_ block: (UnsafePointer<UInt16>?) throws -> ResultType) rethrows -> ResultType {
let fsr: UnsafePointer<UInt16> = _url._wideFileSystemRepresentation
defer { fsr.deallocate() }
return try block(fsr)
}
#endif

// MARK: -
// MARK: Path manipulation

Expand Down