Skip to content

Commit

Permalink
Added lfs_fs_size for finding a count of used blocks
Browse files Browse the repository at this point in the history
This has existed for some time in the form of the lfs_traverse
function, through which a user could provide a simple callback that
would just count the number of blocks lfs_traverse finds. However,
this approach is relatively unconventional and has proven to be confusing
for most users.
  • Loading branch information
geky committed Oct 10, 2018
1 parent 93244a3 commit 746b909
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3183,3 +3183,19 @@ int lfs_fs_setattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count) {

return lfs_dir_setattrs(lfs, &dir, &entry, attrs, count);
}

static int lfs_fs_size_count(void *p, lfs_block_t block) {
lfs_size_t *size = p;
*size += 1;
return 0;
}

lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
lfs_size_t size = 0;
int err = lfs_traverse(lfs, lfs_fs_size_count, &size);
if (err) {
return err;
}

return size;
}
8 changes: 8 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,14 @@ int lfs_fs_getattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count);
// Returns a negative error code on failure.
int lfs_fs_setattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count);

// Finds the current size of the filesystem
//
// Note: Result is best effort. If files share COW structures, the returned
// size may be larger than the filesystem actually is.
//
// Returns the number of allocated blocks, or a negative error code on failure.
lfs_ssize_t lfs_fs_size(lfs_t *lfs);


/// Miscellaneous littlefs specific operations ///

Expand Down

0 comments on commit 746b909

Please sign in to comment.