From 3e7a8bf8ff47432ee88a4b79fad4a59c4be37541 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Mon, 16 Mar 2026 20:33:58 +0100 Subject: [PATCH] Relocate logic --- lib/hal/HalStorage.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/hal/HalStorage.cpp b/lib/hal/HalStorage.cpp index 9d5b50f5..1828aee5 100644 --- a/lib/hal/HalStorage.cpp +++ b/lib/hal/HalStorage.cpp @@ -54,8 +54,24 @@ bool HalStorage::writeFile(const char* path, const String& content) { bool HalStorage::ensureDirectoryExists(const char* path) { HAL_STORAGE_WRAPPED_CALL(ensureDirectoryExists, path); } -uint64_t HalStorage::sdTotalBytes() const { HAL_STORAGE_WRAPPED_CALL(totalBytes); } -uint64_t HalStorage::sdUsedBytes() const { HAL_STORAGE_WRAPPED_CALL(usedBytes); } +uint64_t HalStorage::sdTotalBytes() const { + StorageLock lock; + if (!SDCard.ready()) return 0; + const auto* vol = FsVolume::cwv(); + if (!vol) return 0; + return (uint64_t)vol->clusterCount() * vol->bytesPerCluster(); +} + +uint64_t HalStorage::sdUsedBytes() const { + StorageLock lock; + if (!SDCard.ready()) return 0; + const auto* vol = FsVolume::cwv(); + if (!vol) return 0; + const int32_t freeClusters = vol->freeClusterCount(); + if (freeClusters < 0) return 0; // error reading FAT + const uint64_t bytesPerCluster = vol->bytesPerCluster(); + return ((uint64_t)vol->clusterCount() - freeClusters) * bytesPerCluster; +} class HalFile::Impl { public: