Skip to content

Commit 6134036

Browse files
authoredSep 17, 2019
Merge pull request swiftlang#2511 from gmittert/WindowsShouldBePOSIXCompatible
[Windows] Match error behavior of POSIX
2 parents fff2489 + 527d1e8 commit 6134036

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed
 

‎Foundation/FileManager+Win32.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,19 @@ extension FileManager {
541541
let length = wcsnlen_s(&fsrBuf, fsrBuf.count)
542542
let fsrPath = String(utf16CodeUnits: &fsrBuf, count: length)
543543

544-
let faAttributes = try windowsFileAttributes(atPath: fsrPath)
545-
544+
let faAttributes: WIN32_FILE_ATTRIBUTE_DATA
545+
do {
546+
faAttributes = try windowsFileAttributes(atPath: fsrPath)
547+
} catch {
548+
// removeItem on POSIX throws fileNoSuchFile rather than
549+
// fileReadNoSuchFile that windowsFileAttributes will
550+
// throw if it doesn't find the file.
551+
if (error as NSError).code == CocoaError.fileReadNoSuchFile.rawValue {
552+
throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [path])
553+
} else {
554+
throw error
555+
}
556+
}
546557
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY {
547558
let readableAttributes = faAttributes.dwFileAttributes & DWORD(bitPattern: ~FILE_ATTRIBUTE_READONLY)
548559
guard fsrPath.withCString(encodedAs: UTF16.self, { SetFileAttributesW($0, readableAttributes) }) else {

‎Foundation/FoundationErrors.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ internal func _NSErrorWithWindowsError(_ windowsError: DWORD, reading: Bool, pat
248248
// On an empty path, Windows will return FILE/PATH_NOT_FOUND
249249
// rather than invalid path as posix does
250250
cocoaError = paths?.contains("") ?? false
251-
? .fileReadInvalidFileName
252-
: .fileReadNoSuchFile
251+
? .fileWriteInvalidFileName
252+
: .fileNoSuchFile
253253
case ERROR_ACCESS_DENIED: cocoaError = .fileWriteNoPermission
254254
case ERROR_INVALID_ACCESS: cocoaError = .fileWriteNoPermission
255255
case ERROR_INVALID_DRIVE: cocoaError = .fileNoSuchFile

0 commit comments

Comments
 (0)