File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -188,9 +188,11 @@ open class FileManager : NSObject {
188
188
#elseif os(Linux) || os(Android) || CYGWIN
189
189
let modeT = number. uint32Value
190
190
#endif
191
- if chmod ( path, mode_t ( modeT) ) != 0 {
192
- fatalError ( " errno \( errno) " )
193
- }
191
+ try _fileSystemRepresentation ( withPath: path, {
192
+ guard chmod ( $0, mode_t ( modeT) ) == 0 else {
193
+ throw _NSErrorWithErrno ( errno, reading: false , path: path)
194
+ }
195
+ } )
194
196
} else {
195
197
fatalError ( " Attribute type not implemented: \( attribute) " )
196
198
}
@@ -964,7 +966,13 @@ open class FileManager : NSObject {
964
966
}
965
967
return UnsafePointer ( buf)
966
968
}
967
-
969
+
970
+ internal func _fileSystemRepresentation< ResultType> ( withPath path: String , _ body: ( UnsafePointer < Int8 > ) throws -> ResultType ) rethrows -> ResultType {
971
+ let fsRep = fileSystemRepresentation ( withPath: path)
972
+ defer { fsRep. deallocate ( ) }
973
+ return try body ( fsRep)
974
+ }
975
+
968
976
/* stringWithFileSystemRepresentation:length: returns an NSString created from an array of bytes that are in the filesystem representation.
969
977
*/
970
978
open func string( withFileSystemRepresentation str: UnsafePointer < Int8 > , length len: Int ) -> String {
Original file line number Diff line number Diff line change @@ -326,6 +326,15 @@ class TestFileManager : XCTestCase {
326
326
} catch {
327
327
XCTFail ( " Failed to clean up files " )
328
328
}
329
+
330
+ // test non existant file
331
+ let noSuchFile = NSTemporaryDirectory ( ) + " fileThatDoesntExist "
332
+ try ? fm. removeItem ( atPath: noSuchFile)
333
+ do {
334
+ try fm. setAttributes ( [ . posixPermissions: 0 ] , ofItemAtPath: noSuchFile)
335
+ XCTFail ( " Setting permissions of non-existant file should throw " )
336
+ } catch {
337
+ }
329
338
}
330
339
331
340
func test_pathEnumerator( ) {
You can’t perform that action at this time.
0 commit comments