feat(storage): 新增只读 SMART 辅助服务与寿命通电详情

This commit is contained in:
2026-10-30 16:02:46 +08:00
parent 069e2e100f
commit 7e9728899d
25 changed files with 515 additions and 15 deletions
+26
View File
@@ -0,0 +1,26 @@
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)
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>CFBundleDevelopmentRegion</key><string>en</string>
<key>CFBundleExecutable</key><string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key><string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key><string>6.0</string>
<key>CFBundleName</key><string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key><string>XPC!</string>
<key>CFBundleShortVersionString</key><string>1.0</string>
<key>CFBundleVersion</key><string>1</string>
<key>XPCService</key><dict><key>ServiceType</key><string>Application</string></dict>
</dict></plist>
+32
View File
@@ -0,0 +1,32 @@
import Foundation
final class SMARTService: NSObject, SMARTServiceProtocol {
func readSMART(withReply reply: @escaping (NSNumber?, NSNumber?) -> Void) {
Task {
let result = await DiskSMARTReader.readReport()
reply(result?.remainingLife.map(NSNumber.init(value:)), result?.powerOnHours.map(NSNumber.init(value:)))
}
}
}
final class ServiceDelegate: NSObject, NSXPCListenerDelegate {
// Bind the peer to the containing app's actual designated signature, including ad-hoc builds.
private let requirement: String? = {
let host = Bundle.main.bundleURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
return CodeIdentity.requirement(at: host)
}()
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection connection: NSXPCConnection) -> Bool {
guard connection.effectiveUserIdentifier == getuid(), let requirement else { return false }
connection.setCodeSigningRequirement(requirement)
connection.exportedInterface = NSXPCInterface(with: SMARTServiceProtocol.self)
connection.exportedObject = SMARTService()
connection.resume()
return true
}
}
let delegate = ServiceDelegate()
let listener = NSXPCListener.service()
listener.delegate = delegate
listener.resume()