From 587ea4cfe113c6f78d3949754bd12c25cee61445 Mon Sep 17 00:00:00 2001 From: Greg Neagle Date: Thu, 8 Aug 2024 10:46:16 -0700 Subject: [PATCH] Add availableDiskSpace function to shared/info.swift --- code/cli/munki/shared/info.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/code/cli/munki/shared/info.swift b/code/cli/munki/shared/info.swift index c07154ff..5146a996 100644 --- a/code/cli/munki/shared/info.swift +++ b/code/cli/munki/shared/info.swift @@ -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.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) +}