Skip to content

Commit 4433310

Browse files
committed
Implemented FileManager.attributesOfFileSystem()
1 parent 42d5ddf commit 4433310

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Foundation/FileManager.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,20 @@ open class FileManager : NSObject {
343343
This method replaces fileSystemAttributesAtPath:.
344344
*/
345345
open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
346-
NSUnimplemented()
346+
var s = statfs()
347+
guard statfs(path, &s) == 0 else {
348+
throw _NSErrorWithErrno(errno, reading: true, path: path)
349+
}
350+
351+
var result = [FileAttributeKey : Any]()
352+
let blockSize = UInt64(s.f_bsize)
353+
result[.systemNumber] = NSNumber(value: UInt64(s.f_fsid.val.0))
354+
result[.systemSize] = NSNumber(value: blockSize * UInt64(s.f_blocks))
355+
result[.systemFreeSize] = NSNumber(value: blockSize * UInt64(s.f_bavail))
356+
result[.systemNodes] = NSNumber(value: UInt64(s.f_files))
357+
result[.systemFreeNodes] = NSNumber(value: UInt64(s.f_ffree))
358+
359+
return result
347360
}
348361

349362
/* createSymbolicLinkAtPath:withDestination:error: returns YES if the symbolic link that point at 'destPath' was able to be created at the location specified by 'path'. If this method returns NO, the link was unable to be created and an NSError will be returned by reference in the 'error' parameter. This method does not traverse a terminal symlink.

TestFoundation/TestFileManager.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TestFileManager : XCTestCase {
2424
("test_moveFile", test_moveFile),
2525
("test_fileSystemRepresentation", test_fileSystemRepresentation),
2626
("test_fileAttributes", test_fileAttributes),
27+
("test_fileSystemAttributes", test_fileSystemAttributes),
2728
("test_setFileAttributes", test_setFileAttributes),
2829
("test_directoryEnumerator", test_directoryEnumerator),
2930
("test_pathEnumerator",test_pathEnumerator),
@@ -206,6 +207,40 @@ class TestFileManager : XCTestCase {
206207
}
207208
}
208209

210+
func test_fileSystemAttributes() {
211+
let fm = FileManager.default
212+
let path = NSTemporaryDirectory()
213+
214+
do {
215+
let attrs = try fm.attributesOfFileSystem(forPath: path)
216+
217+
XCTAssertTrue(attrs.count > 0)
218+
219+
let systemNumber = attrs[.systemNumber] as? NSNumber
220+
XCTAssertNotNil(systemNumber)
221+
XCTAssertGreaterThan(systemNumber!.int64Value, 0)
222+
223+
let systemFreeSize = attrs[.systemFreeSize] as? NSNumber
224+
XCTAssertNotNil(systemFreeSize)
225+
XCTAssertGreaterThan(systemFreeSize!.int64Value, 0)
226+
227+
let systemSize = attrs[.systemSize] as? NSNumber
228+
XCTAssertNotNil(systemSize)
229+
XCTAssertGreaterThan(systemSize!.int64Value, systemFreeSize!.int64Value)
230+
231+
let systemFreeNodes = attrs[.systemFreeNodes] as? NSNumber
232+
XCTAssertNotNil(systemFreeNodes)
233+
XCTAssertGreaterThan(systemFreeNodes!.int64Value, 0)
234+
235+
let systemNodes = attrs[.systemNodes] as? NSNumber
236+
XCTAssertNotNil(systemNodes)
237+
XCTAssertGreaterThan(systemNodes!.int64Value, systemFreeNodes!.int64Value)
238+
239+
} catch let err {
240+
XCTFail("\(err)")
241+
}
242+
}
243+
209244
func test_setFileAttributes() {
210245
let path = NSTemporaryDirectory() + "test_setFileAttributes\(NSUUID().uuidString)"
211246
let fm = FileManager.default

0 commit comments

Comments
 (0)