littlefs2
littlefs2 copied to clipboard
Add Filesystem::try_mount
When mounting a filesystem, often code like this is used:
if !Filesystem::is_mountable(alloc, storage) {
Filesystem::format(storage).ok();
}
Filesystem::mount(alloc, storage)
This mounts the filesystem twice because Filesystem::is_mountable is equivalent to Filesystem::mount(...).is_ok(). Depending on the storage implementation, mounting the filesystem can have significant cost. But directly calling Filesystem::mount and re-mounting in the error case is prohibited by the borrow checker.
This patch adds a try_mount method that accepts a callable that is called on mount error. Afterwards, mounting is re-tried.
I’m not entirely happy with try_mount
either, but I did not see a better alternative. mount_with_prepare
sounds like it would always call the prepare function. Maybe mount_with_recover
?
Is this going to be used with anything other than format
?
I would have initially just gone with mount_or_format
.
Even if it’s just called with format
, I would like to be able to know whether the initial mount worked. This could also encoded in the return type, though it would be more complicated.
A more interesting case would be journaling. Here we first want to try to mount the filesystem, and if it fails, recover from the journal. I’m not sure if we will end up using this method for IFS recovery in the nitrokey-3-firmware, but I think it would make sense to support the use case.
@sosthene-nitrokey How about mount_or_else
?
Looks good!