Skip to content

Commit 0381c45

Browse files
authored
Merge pull request #2575 from gmittert/SaveToTheC_____Drive
[Windows] Fix Interpretation of Volume Name Array
2 parents b048f4e + ddadd5e commit 0381c45

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ extension FileManager {
4343
continue
4444
}
4545

46-
var pPath: DWORD = 0
47-
repeat {
48-
let path: String = String(decodingCString: &wszPathNames[Int(pPath)], as: UTF16.self)
49-
if path.length == 0 {
50-
break
46+
// GetVolumePathNamesForVolumeNameW writes an array of
47+
// null terminated wchar strings followed by an additional
48+
// null terminator.
49+
// e.g. [ "C", ":", "\\", "\0", "D", ":", "\\", "\0", "\0"]
50+
var remaining = wszPathNames[...]
51+
while !remaining.isEmpty {
52+
let path = remaining.withUnsafeBufferPointer {
53+
String(decodingCString: $0.baseAddress!, as: UTF16.self)
5154
}
52-
urls.append(URL(fileURLWithPath: path, isDirectory: true))
53-
pPath += DWORD(path.length + 1)
54-
} while pPath < dwCChReturnLength
55+
56+
if !path.isEmpty {
57+
urls.append(URL(fileURLWithPath: path, isDirectory: true))
58+
}
59+
remaining = remaining.dropFirst(path.count + 1)
60+
}
5561
} while FindNextVolumeW(hVolumes, &wszVolumeName, DWORD(wszVolumeName.count))
5662

5763
return urls

TestFoundation/TestFileManager.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,12 @@ class TestFileManager : XCTestCase {
975975
return
976976
}
977977
XCTAssertNotEqual(0, volumes.count)
978+
#if os(Windows)
979+
let url = URL(fileURLWithPath: String(NSTemporaryDirectory().prefix(3)))
980+
XCTAssertTrue(volumes.contains(url))
981+
#else
978982
XCTAssertTrue(volumes.contains(URL(fileURLWithPath: "/")))
983+
#endif
979984
#if os(macOS)
980985
// On macOS, .skipHiddenVolumes should hide 'nobrowse' volumes of which there should be at least one
981986
guard let visibleVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: [], options: [.skipHiddenVolumes]) else {

0 commit comments

Comments
 (0)