Add support for shrinking a filesystem

This PR adds a new `lfs_fs_shrink`, which functions similarly to
`lfs_fs_grow`, but supports reducing the block count.

This functions first checks that none of the removed block are in use.
If it is the case, it will fail.
This commit is contained in:
Sosthène Guédon
2025-04-16 17:46:34 +02:00
parent 8ed63b27be
commit 2105e502c5
4 changed files with 288 additions and 22 deletions

11
lfs.h
View File

@@ -772,6 +772,17 @@ int lfs_fs_gc(lfs_t *lfs);
int lfs_fs_grow(lfs_t *lfs, lfs_size_t block_count);
#endif
#ifndef LFS_READONLY
// Shrinks the filesystem to a new size, updating the superblock with the new
// block count.
//
// Note: This first checks that none of the blocks that are being removed are in use
// and will fail if it is the case
//
// Returns a negative error code on failure.
int lfs_fs_shrink(lfs_t *lfs, lfs_size_t block_count);
#endif
#ifndef LFS_READONLY
#ifdef LFS_MIGRATE
// Attempts to migrate a previous version of littlefs