import Foundation nonisolated struct DiskMetrics: Sendable { let total: UInt64 let available: UInt64 var usage: Double { total == 0 ? 0 : Double(total - min(total, available)) / Double(total) } } nonisolated enum DiskMonitor { static func sample() -> DiskMetrics? { guard let values = try? URL(fileURLWithPath: NSHomeDirectory()).resourceValues(forKeys: [.volumeTotalCapacityKey, .volumeAvailableCapacityKey]), let total = values.volumeTotalCapacity, let available = values.volumeAvailableCapacity, total > 0, available >= 0 else { return nil } return DiskMetrics(total: UInt64(total), available: UInt64(available)) } } nonisolated struct VolumeMetrics: Sendable { let path: String let name: String let capacity: DiskMetrics } nonisolated extension DiskMonitor { static func volumes() -> [VolumeMetrics]? { let keys: Set = [.volumeNameKey, .volumeTotalCapacityKey, .volumeAvailableCapacityKey, .volumeIsLocalKey] guard let urls = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: Array(keys), options: [.skipHiddenVolumes]) else { return nil } return urls.compactMap { url in guard let values = try? url.resourceValues(forKeys: keys), values.volumeIsLocal == true, let total = values.volumeTotalCapacity, total > 0, let available = values.volumeAvailableCapacity, available >= 0 else { return nil } return VolumeMetrics(path: url.path, name: values.volumeName ?? url.lastPathComponent, capacity: DiskMetrics(total: UInt64(total), available: UInt64(available))) }.sorted { $0.path < $1.path } } }