Skip to content

Commit

Permalink
Added methods to query filesystem for size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
attermann committed Aug 13, 2024
1 parent 4dcfa3b commit 585debc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 585debc

Please sign in to comment.