Skip to content

Commit 55e5c50

Browse files
committed
Fallback to statvfs in Linux for filesystem attributes
1 parent 4433310 commit 55e5c50

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
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))

0 commit comments

Comments
 (0)