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