diff --git a/src/fs.rs b/src/fs.rs index 873d54302..50c475649 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -16,7 +16,7 @@ pub type Bytes = generic_array::GenericArray; use crate::{ driver, - io::{self, OpenSeekFrom, Result}, + io::{self, Error, OpenSeekFrom, Result}, path, path::{Path, PathBuf}, }; @@ -1179,10 +1179,28 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> { impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> { pub fn mount(alloc: &'a mut Allocation, storage: &'a mut Storage) -> Result { let fs = Self::new(alloc, storage); - let mut alloc = fs.alloc.borrow_mut(); + fs.raw_mount()?; + Ok(fs) + } + + /// Mount the filesystem or, if that fails, call `f` with the mount error and the storage and then try again. + pub fn try_mount(alloc: &'a mut Allocation, storage: &'a mut Storage, f: F) -> Result + where + F: FnOnce(Error, &mut Storage) -> Result<()>, + { + let fs = Self::new(alloc, storage); + if let Err(err) = fs.raw_mount() { + f(err, fs.storage)?; + fs.raw_mount()?; + } + Ok(fs) + } + + fn raw_mount(&self) -> Result<()> { + let mut alloc = self.alloc.borrow_mut(); let return_code = unsafe { ll::lfs_mount(&mut alloc.state, &alloc.config) }; drop(alloc); - io::result_from(fs, return_code) + io::result_from((), return_code) } // Not public, user should use `mount`, possibly after `format`