Skip to content

Commit a55589e

Browse files
authored
Merge pull request #2117 from jmittert/TreeRemovalSplit
2 parents 6baf10d + 0d155e3 commit a55589e

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,62 @@ extension FileManager {
401401
guard alreadyConfirmed || shouldRemoveItemAtPath(path, isURL: isURL) else {
402402
return
403403
}
404+
let faAttributes = try! windowsFileAttributes(atPath: path)
405+
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == 0 {
406+
if path.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
407+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
408+
}
409+
return
410+
}
411+
var dirStack = [path]
412+
var itemPath = ""
413+
while let currentDir = dirStack.popLast() {
414+
do {
415+
itemPath = currentDir
416+
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
417+
continue
418+
}
419+
guard path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) == 0 else {
420+
continue
421+
}
422+
guard GetLastError() == ERROR_DIR_NOT_EMPTY else {
423+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
424+
}
425+
dirStack.append(path)
426+
var ffd: WIN32_FIND_DATAW = WIN32_FIND_DATAW()
427+
let h: HANDLE = (path + "\\*").withCString(encodedAs: UTF16.self, {
428+
FindFirstFileW($0, &ffd)
429+
})
430+
guard h != INVALID_HANDLE_VALUE else {
431+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
432+
}
433+
defer { FindClose(h) }
404434

405-
NSUnimplemented()
435+
repeat {
436+
let fileArr = Array<WCHAR>(
437+
UnsafeBufferPointer(start: &ffd.cFileName.0,
438+
count: MemoryLayout.size(ofValue: ffd.cFileName)))
439+
let file = String(decodingCString: fileArr, as: UTF16.self)
440+
itemPath = "\(currentDir)\\\(file)"
441+
if (ffd.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) != 0) {
442+
if file != "." && file != ".." {
443+
dirStack.append(itemPath)
444+
}
445+
} else {
446+
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
447+
continue
448+
}
449+
if itemPath.withCString(encodedAs: UTF16.self, DeleteFileW) == 0 {
450+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
451+
}
452+
}
453+
} while(FindNextFileW(h, &ffd) != 0)
454+
} catch {
455+
if !shouldProceedAfterError(error, removingItemAtPath: itemPath, isURL: isURL) {
456+
throw error
457+
}
458+
}
459+
}
406460
}
407461

408462
internal func _currentDirectoryPath() -> String {

0 commit comments

Comments
 (0)