feat(storage): 新增只读 SMART 辅助服务与寿命通电详情
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import Foundation
|
||||
import Security
|
||||
|
||||
nonisolated enum CodeIdentity {
|
||||
static func requirement(at url: URL) -> String? {
|
||||
var code: SecStaticCode?
|
||||
guard SecStaticCodeCreateWithPath(url as CFURL, [], &code) == errSecSuccess,
|
||||
let code else { return nil }
|
||||
var requirement: SecRequirement?
|
||||
guard SecCodeCopyDesignatedRequirement(code, [], &requirement) == errSecSuccess,
|
||||
let requirement else { return nil }
|
||||
var text: CFString?
|
||||
guard SecRequirementCopyString(requirement, [], &text) == errSecSuccess else { return nil }
|
||||
return text as String?
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import Foundation
|
||||
|
||||
nonisolated enum SMARTServiceClient {
|
||||
static func read() async -> DiskHealthMetrics? {
|
||||
let url = Bundle.main.bundleURL.appendingPathComponent("Contents/XPCServices/SMARTService.xpc")
|
||||
guard let requirement = CodeIdentity.requirement(at: url) else { return nil }
|
||||
return await withCheckedContinuation { continuation in
|
||||
let connection = NSXPCConnection(serviceName: "cn.airnan.kymo.SMARTService")
|
||||
connection.setCodeSigningRequirement(requirement)
|
||||
connection.remoteObjectInterface = NSXPCInterface(with: SMARTServiceProtocol.self)
|
||||
let reply = Reply(connection: connection, continuation: continuation)
|
||||
connection.invalidationHandler = { reply.finish(nil) }
|
||||
connection.interruptionHandler = { reply.finish(nil) }
|
||||
connection.resume()
|
||||
guard let proxy = connection.remoteObjectProxyWithErrorHandler({ _ in reply.finish(nil) }) as? SMARTServiceProtocol else {
|
||||
reply.finish(nil); return
|
||||
}
|
||||
proxy.readSMART { remaining, hours in
|
||||
let life = remaining?.doubleValue
|
||||
let validLife = life.flatMap { $0.isFinite && (0...1).contains($0) ? $0 : nil }
|
||||
let validHours = hours.flatMap { UInt64($0.stringValue) }
|
||||
reply.finish(validLife != nil || validHours != nil ? DiskHealthMetrics(remainingLife: validLife, powerOnHours: validHours) : nil)
|
||||
}
|
||||
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 15) { reply.finish(nil) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Error, interruption, timeout and reply may race; resume exactly once.
|
||||
private final class Reply: @unchecked Sendable {
|
||||
private let lock = NSLock()
|
||||
private var continuation: CheckedContinuation<DiskHealthMetrics?, Never>?
|
||||
private let connection: NSXPCConnection
|
||||
|
||||
init(connection: NSXPCConnection, continuation: CheckedContinuation<DiskHealthMetrics?, Never>) {
|
||||
self.connection = connection
|
||||
self.continuation = continuation
|
||||
}
|
||||
|
||||
func finish(_ value: DiskHealthMetrics?) {
|
||||
lock.lock()
|
||||
let callback = continuation
|
||||
continuation = nil
|
||||
lock.unlock()
|
||||
guard let callback else { return }
|
||||
connection.invalidationHandler = nil
|
||||
connection.interruptionHandler = nil
|
||||
connection.invalidate()
|
||||
callback.resume(returning: value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import Foundation
|
||||
|
||||
/// Fixed read-only operation: no caller-supplied paths or commands.
|
||||
@objc protocol SMARTServiceProtocol {
|
||||
nonisolated func readSMART(withReply reply: @escaping (NSNumber?, NSNumber?) -> Void)
|
||||
}
|
||||
@@ -21,6 +21,8 @@ nonisolated enum SamplingDiagnostics {
|
||||
payload["networkPath"] = snapshot.networkPath.map { String(describing: $0) }
|
||||
payload["networkDownload"] = snapshot.downloadRate
|
||||
payload["networkInterfaces"] = snapshot.network?.map(\.name)
|
||||
payload["diskRemainingLife"] = snapshot.diskHealth?.remainingLife
|
||||
payload["diskPowerOnHours"] = snapshot.diskHealth?.powerOnHours
|
||||
payload["diskRead"] = snapshot.diskIO?.read
|
||||
payload["diskWrite"] = snapshot.diskIO?.written
|
||||
payload["volumeCount"] = snapshot.volumes?.count
|
||||
|
||||
Reference in New Issue
Block a user