Skip to content

Commit 5c2a20a

Browse files
committed
Fixed FileManager.attributesOfFileSystem tests (+1 squashed commit)
Squashed commits: [55e5c50] Fallback to statvfs in Linux for filesystem attributes
1 parent 4433310 commit 5c2a20a

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Foundation/FileManager.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,28 @@ open class FileManager : NSObject {
343343
This method replaces fileSystemAttributesAtPath:.
344344
*/
345345
open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
346-
var s = statfs()
347-
guard statfs(path, &s) == 0 else {
348-
throw _NSErrorWithErrno(errno, reading: true, path: path)
349-
}
346+
// statvfs(2) doesn't support 64bit inode on Darwin (apfs), fallback to statfs(2)
347+
#if os(OSX) || os(iOS)
348+
var s = statfs()
349+
guard statfs(path, &s) == 0 else {
350+
throw _NSErrorWithErrno(errno, reading: true, path: path)
351+
}
352+
#else
353+
var s = statvfs()
354+
guard statvfs(path, &s) == 0 else {
355+
throw _NSErrorWithErrno(errno, reading: true, path: path)
356+
}
357+
#endif
358+
350359

351360
var result = [FileAttributeKey : Any]()
352-
let blockSize = UInt64(s.f_bsize)
353-
result[.systemNumber] = NSNumber(value: UInt64(s.f_fsid.val.0))
361+
#if os(OSX) || os(iOS)
362+
let blockSize = UInt64(s.f_bsize)
363+
result[.systemNumber] = NSNumber(value: UInt64(s.f_fsid.val.0))
364+
#else
365+
let blockSize = UInt64(s.f_frsize)
366+
result[.systemNumber] = NSNumber(value: UInt64(s.f_fsid))
367+
#endif
354368
result[.systemSize] = NSNumber(value: blockSize * UInt64(s.f_blocks))
355369
result[.systemFreeSize] = NSNumber(value: blockSize * UInt64(s.f_bavail))
356370
result[.systemNodes] = NSNumber(value: UInt64(s.f_files))

TestFoundation/TestFileManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,23 @@ class TestFileManager : XCTestCase {
218218

219219
let systemNumber = attrs[.systemNumber] as? NSNumber
220220
XCTAssertNotNil(systemNumber)
221-
XCTAssertGreaterThan(systemNumber!.int64Value, 0)
221+
XCTAssertNotEqual(systemNumber!.uint64Value, 0)
222222

223223
let systemFreeSize = attrs[.systemFreeSize] as? NSNumber
224224
XCTAssertNotNil(systemFreeSize)
225-
XCTAssertGreaterThan(systemFreeSize!.int64Value, 0)
225+
XCTAssertNotEqual(systemFreeSize!.uint64Value, 0)
226226

227227
let systemSize = attrs[.systemSize] as? NSNumber
228228
XCTAssertNotNil(systemSize)
229-
XCTAssertGreaterThan(systemSize!.int64Value, systemFreeSize!.int64Value)
229+
XCTAssertGreaterThan(systemSize!.uint64Value, systemFreeSize!.uint64Value)
230230

231231
let systemFreeNodes = attrs[.systemFreeNodes] as? NSNumber
232232
XCTAssertNotNil(systemFreeNodes)
233-
XCTAssertGreaterThan(systemFreeNodes!.int64Value, 0)
233+
XCTAssertNotEqual(systemFreeNodes!.uint64Value, 0)
234234

235235
let systemNodes = attrs[.systemNodes] as? NSNumber
236236
XCTAssertNotNil(systemNodes)
237-
XCTAssertGreaterThan(systemNodes!.int64Value, systemFreeNodes!.int64Value)
237+
XCTAssertGreaterThan(systemNodes!.uint64Value, systemFreeNodes!.uint64Value)
238238

239239
} catch let err {
240240
XCTFail("\(err)")

0 commit comments

Comments
 (0)