Skip to content

Commit 9297b5d

Browse files
authored
Merge pull request #2329 from gmittert/ForcefullyRemoved
Force file removal in Windows _removeItem
2 parents 14e14b2 + a762b87 commit 9297b5d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ extension FileManager {
422422
let fsrPath = String(utf16CodeUnits: &fsrBuf, count: length)
423423

424424
let faAttributes = try windowsFileAttributes(atPath: fsrPath)
425+
426+
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY {
427+
let readableAttributes = faAttributes.dwFileAttributes & DWORD(bitPattern: ~FILE_ATTRIBUTE_READONLY)
428+
guard fsrPath.withCString(encodedAs: UTF16.self, { SetFileAttributesW($0, readableAttributes) }) else {
429+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
430+
}
431+
}
432+
425433
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == 0 {
426434
if !fsrPath.withCString(encodedAs: UTF16.self, DeleteFileW) {
427435
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
@@ -458,6 +466,14 @@ extension FileManager {
458466
count: MemoryLayout.size(ofValue: ffd.cFileName)))
459467
let file = String(decodingCString: fileArr, as: UTF16.self)
460468
itemPath = "\(currentDir)\\\(file)"
469+
470+
if ffd.dwFileAttributes & DWORD(FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY {
471+
let readableAttributes = ffd.dwFileAttributes & DWORD(bitPattern: ~FILE_ATTRIBUTE_READONLY)
472+
guard file.withCString(encodedAs: UTF16.self, { SetFileAttributesW($0, readableAttributes) }) else {
473+
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
474+
}
475+
}
476+
461477
if (ffd.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) != 0) {
462478
if file != "." && file != ".." {
463479
dirStack.append(itemPath)

0 commit comments

Comments
 (0)