Skip to content

Commit a7835c6

Browse files
authored
Merge pull request #830 from attermann/lfs_size
Added methods to query filesystem for size
2 parents fa129c2 + de1f7df commit a7835c6

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,34 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
239239
return LFS_ERR_OK == err;
240240
}
241241

242+
static int _lfs_count(void *p, lfs_block_t block) {
243+
(void) block;
244+
*(size_t *)p += 1;
245+
return 0;
246+
}
247+
248+
size_t Adafruit_LittleFS::usedBlocks() {
249+
_lockFS();
250+
251+
size_t block_used = 0;
252+
lfs_traverse(&_lfs, _lfs_count, &block_used);
253+
254+
_unlockFS();
255+
return block_used;
256+
}
257+
258+
size_t Adafruit_LittleFS::usedBytes() {
259+
if (nullptr == _lfs_cfg)
260+
return 0;
261+
return _lfs_cfg->block_size * usedBlocks();
262+
}
263+
264+
size_t Adafruit_LittleFS::totalBytes() {
265+
if (nullptr == _lfs_cfg)
266+
return 0;
267+
return _lfs_cfg->block_size * _lfs_cfg->block_count;
268+
}
269+
242270
//------------- Debug -------------//
243271
#if CFG_DEBUG
244272

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class Adafruit_LittleFS
7070
// format file system
7171
bool format (void);
7272

73+
size_t usedBlocks();
74+
size_t usedBytes();
75+
size_t totalBytes();
76+
7377
/*------------------------------------------------------------------*/
7478
/* INTERNAL USAGE ONLY
7579
* Although declare as public, it is meant to be invoked by internal

0 commit comments

Comments
 (0)