Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods to query filesystem for size #830

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading