Skip to content

Commit d73d7c1

Browse files
authored
FoundationEssentials: account for relative paths in iteration (#635)
We are not changing the working directory in between recursive calls. This is a problem when trying to enumerate the subpaths of a directory in the recursive call where the parent path was lost. Concatenate the parent path before recursing.
1 parent 39344d7 commit d73d7c1

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

Sources/FoundationEssentials/FileManager/FileManager+Directories.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ extension _FileManagerImpl {
163163
func subpathsOfDirectory(atPath path: String) throws -> [String] {
164164
#if os(Windows)
165165
var results: [String] = []
166-
let iterator = _Win32DirectoryContentsSequence(path: path, appendSlashForDirectory: true).makeIterator()
167-
while let item = iterator.next() {
166+
for item in _Win32DirectoryContentsSequence(path: path, appendSlashForDirectory: true) {
168167
results.append(item.fileName)
169168
if item.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == FILE_ATTRIBUTE_DIRECTORY &&
170169
item.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != FILE_ATTRIBUTE_REPARSE_POINT {
@@ -176,7 +175,7 @@ extension _FileManagerImpl {
176175
}
177176
defer { LocalFree(pwszSubPath) }
178177

179-
results.append(contentsOf: try subpathsOfDirectory(atPath: item.fileName).map {
178+
results.append(contentsOf: try subpathsOfDirectory(atPath: String(decodingCString: pwszSubPath!, as: UTF16.self)).map {
180179
var pwszFullPath: PWSTR? = nil
181180
_ = PathAllocCombine(item.fileName, $0, PATHCCH_ALLOW_LONG_PATHS, &pwszFullPath)
182181
defer { LocalFree(pwszFullPath) }

0 commit comments

Comments
 (0)