Skip to content

[Windows] Fix Interpretation of Volume Name Array #2575

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
Dec 6, 2019
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
22 changes: 14 additions & 8 deletions Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ extension FileManager {
continue
}

var pPath: DWORD = 0
repeat {
let path: String = String(decodingCString: &wszPathNames[Int(pPath)], as: UTF16.self)
if path.length == 0 {
break
// GetVolumePathNamesForVolumeNameW writes an array of
// null terminated wchar strings followed by an additional
// null terminator.
// e.g. [ "C", ":", "\\", "\0", "D", ":", "\\", "\0", "\0"]
var remaining = wszPathNames[...]
while !remaining.isEmpty {
let path = remaining.withUnsafeBufferPointer {
String(decodingCString: $0.baseAddress!, as: UTF16.self)
}
urls.append(URL(fileURLWithPath: path, isDirectory: true))
pPath += DWORD(path.length + 1)
} while pPath < dwCChReturnLength

if !path.isEmpty {
urls.append(URL(fileURLWithPath: path, isDirectory: true))
}
remaining = remaining.dropFirst(path.count + 1)
}
} while FindNextVolumeW(hVolumes, &wszVolumeName, DWORD(wszVolumeName.count))

return urls
Expand Down
5 changes: 5 additions & 0 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,12 @@ class TestFileManager : XCTestCase {
return
}
XCTAssertNotEqual(0, volumes.count)
#if os(Windows)
let url = URL(fileURLWithPath: String(NSTemporaryDirectory().prefix(3)))
XCTAssertTrue(volumes.contains(url))
#else
XCTAssertTrue(volumes.contains(URL(fileURLWithPath: "/")))
#endif
#if os(macOS)
// On macOS, .skipHiddenVolumes should hide 'nobrowse' volumes of which there should be at least one
guard let visibleVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [], options: [.skipHiddenVolumes]) else {
Expand Down