Add availableDiskSpace function to shared/info.swift

This commit is contained in:
Greg Neagle
2024-08-08 10:46:16 -07:00
parent c8fdc11de0
commit 587ea4cfe1
+16
View File
@@ -6,6 +6,7 @@
//
import CoreServices.LaunchServices
import Darwin
import Foundation
import IOKit
@@ -257,3 +258,18 @@ func hasIntel64Support() -> Bool {
}
return buffer[0] == 1
}
func availableDiskSpace(volumePath _: String = "/") -> Int {
// Returns available diskspace in KBytes.
// Value should be very close to `df -k` output
// Returns negative values of there is an error
let buffer = UnsafeMutablePointer<statvfs>.allocate(capacity: 1024)
defer { buffer.deallocate() }
let err = statvfs("/", buffer)
if err != 0 {
return -1
}
let f_frsize = Int(buffer.pointee.f_frsize)
let f_bavail = Int(buffer.pointee.f_bavail)
return Int(f_frsize * f_bavail / 1024)
}