27 lines
1.4 KiB
Swift
27 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
nonisolated enum DiskSMARTReader {
|
|
static func readReport() async -> DiskHealthMetrics? {
|
|
await withCheckedContinuation { continuation in
|
|
DispatchQueue.global(qos: .utility).async {
|
|
guard let values = try? URL(fileURLWithPath: NSHomeDirectory()).resourceValues(forKeys: [.volumeURLKey]),
|
|
let volume = values.volume else { continuation.resume(returning: nil); return }
|
|
let process = Process()
|
|
process.executableURL = URL(fileURLWithPath: "/usr/sbin/diskutil")
|
|
process.arguments = ["info", "-plist", volume.path]
|
|
let pipe = Pipe()
|
|
process.standardOutput = pipe
|
|
process.standardError = FileHandle.nullDevice
|
|
do { try process.run() }
|
|
catch { continuation.resume(returning: nil); return }
|
|
let deadline = DispatchWorkItem { if process.isRunning { process.terminate() } }
|
|
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 10, execute: deadline)
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
process.waitUntilExit()
|
|
deadline.cancel()
|
|
continuation.resume(returning: process.terminationStatus == 0 && data.count <= 262_144 ? DiskHealthReport.parse(data) : nil)
|
|
}
|
|
}
|
|
}
|
|
}
|