Skip to content

Verify contents of the file attributes dictionary #32

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 1 commit into from
Dec 6, 2015
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
11 changes: 6 additions & 5 deletions Foundation/NSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ public class NSFileManager : NSObject {

This method replaces fileAttributesAtPath:traverseLink:.
*/
public func attributesOfItemAtPath(path: String) throws -> [String : AnyObject] {
/// - Experiment: Note that the return type of this function is different than on Darwin Foundation (Any instead of AnyObject). This is likely to change once we have a more complete story for bridging in place.
public func attributesOfItemAtPath(path: String) throws -> [String : Any] {
var s = stat()
guard lstat(path, &s) == 0 else {
throw _NSErrorWithErrno(errno, reading: true, path: path)
}
var result = [String : AnyObject]()
var result = [String : Any]()
result[NSFileSize] = NSNumber(unsignedLongLong: UInt64(s.st_size))

#if os(OSX) || os(iOS)
Expand All @@ -233,14 +234,14 @@ public class NSFileManager : NSObject {

let pwd = getpwuid(s.st_uid)
if pwd != nil && pwd.memory.pw_name != nil {
if let name = NSString(bytes: pwd.memory.pw_name, length: Int(strlen(pwd.memory.pw_name)), encoding: NSUTF8StringEncoding) {
if let name = String.fromCString(pwd.memory.pw_name) {
result[NSFileOwnerAccountName] = name
}
}

let grd = getgrgid(s.st_gid)
if grd != nil && grd.memory.gr_name != nil {
if let name = NSString(bytes: grd.memory.gr_name, length: Int(strlen(grd.memory.gr_name)), encoding: NSUTF8StringEncoding) {
if let name = String.fromCString(grd.memory.gr_name) {
result[NSFileGroupOwnerAccountID] = name
}
}
Expand All @@ -255,7 +256,7 @@ public class NSFileManager : NSObject {
case S_IFSOCK: type = NSFileTypeSocket
default: type = NSFileTypeUnknown
}
result[NSFileType] = NSString(type)
result[NSFileType] = type

if type == NSFileTypeBlockSpecial || type == NSFileTypeCharacterSpecial {
result[NSFileDeviceIdentifier] = NSNumber(unsignedLongLong: UInt64(s.st_rdev))
Expand Down
27 changes: 26 additions & 1 deletion TestFoundation/TestNSFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,33 @@ class TestNSFileManger : XCTestCase {

do {
let attrs = try fm.attributesOfItemAtPath(path)
// TODO: Actually verify the contents of the dictionary.

XCTAssertTrue(attrs.count > 0)

let fileSize = attrs[NSFileSize] as? NSNumber
XCTAssertEqual(fileSize!.longLongValue, 0)

let fileModificationDate = attrs[NSFileModificationDate] as? NSDate
XCTAssertGreaterThan(NSDate().timeIntervalSince1970, fileModificationDate!.timeIntervalSince1970)

let filePosixPermissions = attrs[NSFilePosixPermissions] as? NSNumber
XCTAssertNotEqual(filePosixPermissions!.longLongValue, 0)

let fileReferenceCount = attrs[NSFileReferenceCount] as? NSNumber
XCTAssertEqual(fileReferenceCount!.longLongValue, 1)

let fileSystemNumber = attrs[NSFileSystemNumber] as? NSNumber
XCTAssertNotEqual(fileSystemNumber!.longLongValue, 0)

let fileSystemFileNumber = attrs[NSFileSystemFileNumber] as? NSNumber
XCTAssertNotEqual(fileSystemFileNumber!.longLongValue, 0)

let fileType = attrs[NSFileType] as? String
XCTAssertEqual(fileType!, NSFileTypeRegular)

let fileOwnerAccountID = attrs[NSFileOwnerAccountID] as? NSNumber
XCTAssertNotEqual(fileOwnerAccountID!.longLongValue, 0)

} catch let err {
XCTFail("\(err)")
}
Expand Down