Skip to content

Commit 746b909

Browse files
committed
Added lfs_fs_size for finding a count of used blocks
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.
1 parent 93244a3 commit 746b909

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lfs.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,3 +3183,19 @@ int lfs_fs_setattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count) {
31833183

31843184
return lfs_dir_setattrs(lfs, &dir, &entry, attrs, count);
31853185
}
3186+
3187+
static int lfs_fs_size_count(void *p, lfs_block_t block) {
3188+
lfs_size_t *size = p;
3189+
*size += 1;
3190+
return 0;
3191+
}
3192+
3193+
lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
3194+
lfs_size_t size = 0;
3195+
int err = lfs_traverse(lfs, lfs_fs_size_count, &size);
3196+
if (err) {
3197+
return err;
3198+
}
3199+
3200+
return size;
3201+
}

lfs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ int lfs_fs_getattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count);
590590
// Returns a negative error code on failure.
591591
int lfs_fs_setattrs(lfs_t *lfs, const struct lfs_attr *attrs, int count);
592592

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

594602
/// Miscellaneous littlefs specific operations ///
595603

0 commit comments

Comments
 (0)