From 585debc1d73c45b3d26282fec14f06865d001f89 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Tue, 13 Aug 2024 11:42:21 -0600 Subject: [PATCH] Added methods to query filesystem for size Added methods totalBytes() and usedBytes() for obtaining flash filesystem overall size and available space, like those found in SPIFFS. Also added usedBlocks() method for good measure. --- .../src/Adafruit_LittleFS.cpp | 26 +++++++++++++++++++ .../Adafruit_LittleFS/src/Adafruit_LittleFS.h | 4 +++ 2 files changed, 30 insertions(+) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index ec5ef73af..ebddecff9 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -239,6 +239,32 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath) return LFS_ERR_OK == err; } +static int _lfs_count(void *p, lfs_block_t block) { + *(size_t *)p += 1; + return 0; +} + +size_t Adafruit_LittleFS::usedBlocks() { + _lockFS(); + + size_t blocks = 0; + lfs_traverse(&_lfs, _lfs_count, &blocks); + + _unlockFS(); + return blocks; +} + +size_t Adafruit_LittleFS::usedBytes() { + if (nullptr == _lfs_cfg) return 0; + const size_t blocks_used = usedBlocks(); + return _lfs_cfg->block_size * blocks_used; +} + +size_t Adafruit_LittleFS::totalBytes() { + if (nullptr == _lfs_cfg) return 0; + return _lfs_cfg->block_size * _lfs_cfg->block_count; +} + //------------- Debug -------------// #if CFG_DEBUG diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h index 3eff8320d..07fb8acac 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h @@ -70,6 +70,10 @@ class Adafruit_LittleFS // format file system bool format (void); + size_t usedBlocks(); + size_t usedBytes(); + size_t totalBytes(); + /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY * Although declare as public, it is meant to be invoked by internal