Skip to content

Commit 04ff380

Browse files
committed
FileManager: setAttributes(ofItemAtPath:) should throw if chmod fails.
- chmod() failing should throw an error and not call fatalError(). - Behaviour now matches Darwin. (cherry picked from commit 5995272)
1 parent 0c9e76b commit 04ff380

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Foundation/FileManager.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ open class FileManager : NSObject {
524524
#elseif os(Linux) || os(Android) || CYGWIN
525525
let modeT = number.uint32Value
526526
#endif
527-
_fileSystemRepresentation(withPath: path, {
528-
if chmod($0, mode_t(modeT)) != 0 {
529-
fatalError("errno \(errno)")
527+
try _fileSystemRepresentation(withPath: path, {
528+
guard chmod($0, mode_t(modeT)) == 0 else {
529+
throw _NSErrorWithErrno(errno, reading: false, path: path)
530530
}
531531
})
532532
} else {

TestFoundation/TestFileManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,15 @@ class TestFileManager : XCTestCase {
434434
} catch {
435435
XCTFail("Failed to clean up files")
436436
}
437+
438+
// test non existant file
439+
let noSuchFile = NSTemporaryDirectory() + "fileThatDoesntExist"
440+
try? fm.removeItem(atPath: noSuchFile)
441+
do {
442+
try fm.setAttributes([.posixPermissions: 0], ofItemAtPath: noSuchFile)
443+
XCTFail("Setting permissions of non-existant file should throw")
444+
} catch {
445+
}
437446
}
438447

439448
func test_pathEnumerator() {

0 commit comments

Comments
 (0)