littlefs-project / littlefs

A little fail-safe filesystem designed for microcontrollers
BSD 3-Clause "New" or "Revised" License
4.92k stars 774 forks source link

How to find out each and everyone of the blocks whether they are allocated? #853

Open test-sg opened 1 year ago

test-sg commented 1 year ago

Does anyone has the sample code to print out the 'status' of all the fs blocks, whether each of the block has been allocated? For example, like below, all the 128 blocks?

image

geky commented 11 months ago

Hi @test-sg, this can be found using the lfs_fs_traverse function, which takes a callback and calls it with every block address currently in use by the filesystem:

https://github.com/littlefs-project/littlefs/blob/611c9b20db2b99faee261daa7cc9bbe175d3eaca/lfs.h#L699-L706

As for finding all of the blocks not in use, littlefs doesn't really know this. It needs to be constructed by looking at each block and seeing if it is in-use.

littlefs does this a window at a time, which is what the lookahead_size configuration option controls.