Skip to content

Implement setAttributesOfItemAtPath for NSFilePosixPermissions attribute #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,24 @@ public class NSFileManager : NSObject {
This method replaces changeFileAttributes:atPath:.
*/
public func setAttributes(attributes: [String : AnyObject], ofItemAtPath path: String) throws {
NSUnimplemented()
for attribute in attributes.keys {
switch attribute {
case NSFilePosixPermissions:
guard let number = attributes[attribute] as? NSNumber else {
fatalError("Can't set file permissions to \(attributes[attribute])")
}
#if os(OSX) || os(iOS)
let modeT = number.unsignedShortValue
#elseif os(Linux)
let modeT = number.unsignedIntValue
#endif
if chmod(path, modeT) != 0 {
fatalError("errno \(errno)")
}
default:
fatalError("Attribute type not implemented: \(attribute)")
}
}
}

/* createDirectoryAtPath:withIntermediateDirectories:attributes:error: creates a directory at the specified path. If you pass 'NO' for createIntermediates, the directory must not exist at the time this call is made. Passing 'YES' for 'createIntermediates' will create any necessary intermediate directories. This method returns YES if all directories specified in 'path' were created and attributes were set. Directories are created with attributes specified by the dictionary passed to 'attributes'. If no dictionary is supplied, directories are created according to the umask of the process. This method returns NO if a failure occurs at any stage of the operation. If an error parameter was provided, a presentable NSError will be returned by reference.
Expand Down
23 changes: 22 additions & 1 deletion TestFoundation/TestNSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestNSFileManger : XCTestCase {
("test_createFile", test_createFile ),
("test_fileSystemRepresentation", test_fileSystemRepresentation),
("test_fileAttributes", test_fileAttributes),
("test_setFileAttributes", test_setFileAttributes),
("test_directoryEnumerator", test_directoryEnumerator),
("test_pathEnumerator",test_pathEnumerator),
("test_contentsOfDirectoryAtPath", test_contentsOfDirectoryAtPath),
Expand Down Expand Up @@ -91,7 +92,7 @@ class TestNSFileManger : XCTestCase {

func test_fileAttributes() {
let fm = NSFileManager.defaultManager()
let path = "/tmp/testfile"
let path = "/tmp/test_fileAttributes\(NSUUID().UUIDString)"

ignoreError { try fm.removeItemAtPath(path) }

Expand Down Expand Up @@ -137,6 +138,26 @@ class TestNSFileManger : XCTestCase {
}
}

func test_setFileAttributes() {
let path = "/tmp/test_setFileAttributes\(NSUUID().UUIDString)"
let fm = NSFileManager.defaultManager()

ignoreError { try fm.removeItemAtPath(path) }
XCTAssertTrue(fm.createFileAtPath(path, contents: NSData(), attributes: nil))

do {
try fm.setAttributes([NSFilePosixPermissions:NSNumber(short: 0o0600)], ofItemAtPath: path)
}
catch { XCTFail("\(error)") }

//read back the attributes
do {
let attributes = try fm.attributesOfItemAtPath(path)
XCTAssert((attributes[NSFilePosixPermissions] as? NSNumber)?.shortValue == 0o0600)
}
catch { XCTFail("\(error)") }
}

func test_pathEnumerator() {
let fm = NSFileManager.defaultManager()
let basePath = "/tmp/testdir"
Expand Down