Skip to content

Commit cef3bbd

Browse files
authored
Merge pull request #2203 from gmittert/removeErrors
2 parents 9ba604d + 3f006d1 commit cef3bbd

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,30 +405,36 @@ extension FileManager {
405405
guard alreadyConfirmed || shouldRemoveItemAtPath(path, isURL: isURL) else {
406406
return
407407
}
408-
let faAttributes = try windowsFileAttributes(atPath: path)
408+
let url = URL(fileURLWithPath: path)
409+
var fsrBuf: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH))
410+
_CFURLGetWideFileSystemRepresentation(url._cfObject, false, &fsrBuf, Int(MAX_PATH))
411+
let length = wcsnlen_s(&fsrBuf, fsrBuf.count)
412+
let fsrPath = String(utf16CodeUnits: &fsrBuf, count: length)
413+
414+
let faAttributes = try windowsFileAttributes(atPath: fsrPath)
409415
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == 0 {
410-
if !path.withCString(encodedAs: UTF16.self, DeleteFileW) {
416+
if !fsrPath.withCString(encodedAs: UTF16.self, DeleteFileW) {
411417
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
412418
}
413419
return
414420
}
415-
var dirStack = [path]
421+
var dirStack = [fsrPath]
416422
var itemPath = ""
417423
while let currentDir = dirStack.popLast() {
418424
do {
419425
itemPath = currentDir
420426
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
421427
continue
422428
}
423-
guard !path.withCString(encodedAs: UTF16.self, RemoveDirectoryW) else {
429+
guard !itemPath.withCString(encodedAs: UTF16.self, RemoveDirectoryW) else {
424430
continue
425431
}
426432
guard GetLastError() == ERROR_DIR_NOT_EMPTY else {
427433
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
428434
}
429-
dirStack.append(path)
435+
dirStack.append(itemPath)
430436
var ffd: WIN32_FIND_DATAW = WIN32_FIND_DATAW()
431-
let h: HANDLE = (path + "\\*").withCString(encodedAs: UTF16.self, {
437+
let h: HANDLE = (itemPath + "\\*").withCString(encodedAs: UTF16.self, {
432438
FindFirstFileW($0, &ffd)
433439
})
434440
guard h != INVALID_HANDLE_VALUE else {

0 commit comments

Comments
 (0)