From 012dbcad76c09a8d12d70fe1f5c348519f51f530 Mon Sep 17 00:00:00 2001 From: giovannilentini Date: Tue, 17 Nov 2026 06:53:12 +0100 Subject: [PATCH] PhysicalDisk (macOS): adds health detection Ref: #2726 --- CHANGELOG.md | 2 +- src/detection/physicaldisk/physicaldisk_apple.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 840c2b532..ad326f5df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Features: * Added glob and environment variable expansion to logo sources on Windows. `%VAR%`, `$VAR` and `${VAR}` are supported, and a glob matching several files picks one randomly. (#2718, Logo, Windows) -* Added opt-in drive health detection for NVMe disks via udisks2, which doesn't require root. (#2726, PhysicalDisk, Linux) +* Added opt-in drive health detection for NVMe disks, via udisks2 on Linux (no root required) and NVMe SMART on macOS. (#2726, PhysicalDisk, Linux / macOS) * Enable it with `"health": true`. The default output appends `Health <100 - percentage used>%` and any critical warnings. * New format args: `{health}`, `{percent-used}`, `{available-spare}` and `{critical-warning}`. diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index e56bbb4f1..295390216 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -21,7 +21,7 @@ static inline void wrapIoDestroyPlugInInterface(IOCFPlugInInterface*** pluginInf } #endif -static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) { +static const char* detectNvmeSmart(io_service_t entryPhysical, FFPhysicalDiskResult* device, FFPhysicalDiskOptions* options) { #ifdef MAC_OS_X_VERSION_10_15 [[gnu::cleanup(wrapIoDestroyPlugInInterface)]] IOCFPlugInInterface** pluginInf = nullptr; int32_t score; @@ -39,7 +39,14 @@ static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) { NVMeSMARTData smartData; const char* error = nullptr; if ((*smartInf)->SMARTReadData(smartInf, &smartData) == kIOReturnSuccess) { - *temp = smartData.TEMPERATURE - 273; + if (options->temp) { + device->temperature = smartData.TEMPERATURE - 273; + } + if (options->health) { + device->percentUsed = (uint8_t) (smartData.PERCENTAGE_USED > 254 ? 254 : smartData.PERCENTAGE_USED); + device->availableSpare = (uint8_t) (smartData.AVAILABLE_SPARE > 100 ? 100 : smartData.AVAILABLE_SPARE); + device->criticalWarning = (FFPhysicalDiskWarning) (smartData.CRITICAL_WARNING & 0x3F); + } } else { error = "SMARTReadData() failed"; } @@ -47,7 +54,7 @@ static const char* detectSsdTemp(io_service_t entryPhysical, double* temp) { (*pluginInf)->Release(smartInf); return error; #else - FF_DEBUG("SSD temperature detection needs macOS 10.15 or newer"); + FF_DEBUG("NVMe SMART detection needs macOS 10.15 or newer"); return "No support for old MacOS version"; #endif } @@ -186,10 +193,10 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) } #ifdef MAC_OS_X_VERSION_10_15 - if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && options->temp) { + if (!(device->type & FF_PHYSICALDISK_TYPE_VIRTUAL) && (options->temp || options->health)) { FF_CFTYPE_AUTO_RELEASE CFBooleanRef nvmeSMARTCapable = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyNVMeSMARTCapableKey), nullptr, kNilOptions); if (nvmeSMARTCapable && CFBooleanGetValue(nvmeSMARTCapable)) { - detectSsdTemp(entryPhysical, &device->temperature); + detectNvmeSmart(entryPhysical, device, options); } } #endif