diff --git a/src/fs.rs b/src/fs.rs index 6ef0cd87..3337718c 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -231,7 +231,7 @@ impl Filesystem<'_, Storage> { let fs = Filesystem::new(alloc, storage); let mut alloc = fs.alloc.borrow_mut(); let return_code = unsafe { ll::lfs_format(&mut alloc.state, &alloc.config) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } // TODO: check if this is equivalent to `is_formatted`. @@ -281,7 +281,7 @@ impl Filesystem<'_, Storage> { /// by this method available, at any given time. pub fn available_blocks(&self) -> Result { let return_code = unsafe { ll::lfs_fs_size( &mut self.alloc.borrow_mut().state) }; - io::result_from(return_code).map(|blocks| self.total_blocks() - blocks as usize) + io::result_from(return_code, return_code).map(|blocks| self.total_blocks() - blocks as usize) } /// Available number of unused bytes in the filesystem @@ -299,7 +299,7 @@ impl Filesystem<'_, Storage> { &mut self.alloc.borrow_mut().state, path.as_ptr(), ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } /// Remove a file or directory. @@ -365,7 +365,7 @@ impl Filesystem<'_, Storage> { from.as_ptr(), to.as_ptr(), ) }; - io::result_from(return_code).map(drop) + io::result_from((),return_code) } /// Given a path, query the filesystem to get information about a file or directory. @@ -389,7 +389,7 @@ impl Filesystem<'_, Storage> { ) }; - io::result_from(return_code).map(drop).map(|_| info.into()) + io::result_from((), return_code).map(|_| info.into()) } pub fn create_file_and_then( @@ -455,7 +455,7 @@ impl Filesystem<'_, Storage> { return Ok(None) } - io::result_from(return_code).map(drop)?; + io::result_from((), return_code)?; // TODO: get rid of this unreachable!(); } @@ -471,7 +471,7 @@ impl Filesystem<'_, Storage> { path.as_ptr(), id, ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } /// Set attribute. @@ -490,7 +490,7 @@ impl Filesystem<'_, Storage> { attribute.size as u32, ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } @@ -746,7 +746,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> &mut self.fs.alloc.borrow_mut().state, &mut self.alloc.borrow_mut().state, ); - io::result_from(return_code).map(drop) + io::result_from((), return_code) } /// Synchronize file contents to storage. @@ -755,7 +755,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> &mut self.fs.alloc.borrow_mut().state, &mut self.alloc.borrow_mut().state, ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } /// Size of the file in bytes. @@ -764,7 +764,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> &mut self.fs.alloc.borrow_mut().state, &mut self.alloc.borrow_mut().state ) }; - io::result_from(return_code).map(|e| e as usize) + io::result_from(return_code as usize, return_code) } /// Truncates or extends the underlying file, updating the size of this file to become size. @@ -778,7 +778,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> &mut self.alloc.borrow_mut().state, size as u32, ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } // This belongs in `io::Read` but really don't want that to have a generic parameter @@ -863,7 +863,7 @@ impl OpenOptions { fs, }; - io::result_from(return_code).map(drop).map(|_| file) + io::result_from(file, return_code) } /// (Hopefully) safe abstraction around `open`. @@ -951,7 +951,7 @@ impl io::Read for File<'_, '_, S> buf.as_mut_ptr() as *mut cty::c_void, buf.len() as u32, ) }; - io::result_from(return_code).map(|read| read as usize) + io::result_from(return_code as usize, return_code) } } @@ -964,7 +964,7 @@ impl io::Seek for File<'_, '_, S> pos.off(), pos.whence(), ) }; - io::result_from(return_code).map(|position| position as usize) + io::result_from(return_code as usize, return_code) } } @@ -977,7 +977,7 @@ impl io::Write for File<'_, '_, S> buf.as_ptr() as *const cty::c_void, buf.len() as u32, ) }; - io::result_from(return_code).map(|written| written as usize) + io::result_from(return_code as usize, return_code) } fn flush(&self) -> Result<()> { Ok(()) } @@ -1086,7 +1086,7 @@ impl<'a, 'b, S: driver::Storage> Iterator for ReadDir<'a, 'b, S> return None } - Some(Err(io::result_from(return_code).map(drop).unwrap_err())) + Some(Err(io::result_from((), return_code).unwrap_err())) } } @@ -1112,7 +1112,7 @@ impl ReadDir<'_, '_, S> { &mut self.fs.alloc.borrow_mut().state, &mut self.alloc.borrow_mut().state, ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } } @@ -1157,7 +1157,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> { path: PathBuf::from(path), }; - io::result_from(return_code).map(drop).map(|_| read_dir) + io::result_from(read_dir, return_code) } } @@ -1169,12 +1169,11 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> { alloc: &'a mut Allocation, storage: &'a mut Storage, ) -> Result { - let fs = Self::new(alloc, storage); let mut alloc = fs.alloc.borrow_mut(); let return_code = unsafe { ll::lfs_mount(&mut alloc.state, &alloc.config) }; drop(alloc); - io::result_from(return_code).map(drop).map(move |_| { fs } ) + io::result_from(fs, return_code) } // Not public, user should use `mount`, possibly after `format` @@ -1206,7 +1205,7 @@ impl<'a, Storage: driver::Storage> Filesystem<'a, Storage> { &mut self.alloc.borrow_mut().state, path.as_ptr(), ) }; - io::result_from(return_code).map(drop) + io::result_from((), return_code) } /// Recursively create a directory and all of its parent components if they are missing. diff --git a/src/io.rs b/src/io.rs index c875655b..a41333e2 100644 --- a/src/io.rs +++ b/src/io.rs @@ -98,6 +98,8 @@ pub type Result = core::result::Result; /// Definition of errors that might be returned by filesystem functionality. #[derive(Clone,Copy,Debug,PartialEq)] pub enum Error { + /// Error code was >=0, operation was successful. + Success, /// Input / output error occurred. Io, /// File or filesystem was corrupt. @@ -136,26 +138,35 @@ impl From for Error { } } -pub fn result_from(error_code: ll::lfs_error) -> Result { - match error_code { - n if n >= 0 => Ok(n as u32), - // negative codes - ll::lfs_error_LFS_ERR_IO => Err(Error::Io), - ll::lfs_error_LFS_ERR_CORRUPT => Err(Error::Corruption), - ll::lfs_error_LFS_ERR_NOENT => Err(Error::NoSuchEntry), - ll::lfs_error_LFS_ERR_EXIST => Err(Error::EntryAlreadyExisted), - ll::lfs_error_LFS_ERR_NOTDIR => Err(Error::PathNotDir), - ll::lfs_error_LFS_ERR_ISDIR => Err(Error::PathIsDir), - ll::lfs_error_LFS_ERR_NOTEMPTY => Err(Error::DirNotEmpty), - ll::lfs_error_LFS_ERR_BADF => Err(Error::BadFileDescriptor), - ll::lfs_error_LFS_ERR_FBIG => Err(Error::FileTooBig), - ll::lfs_error_LFS_ERR_INVAL => Err(Error::Invalid), - ll::lfs_error_LFS_ERR_NOSPC => Err(Error::NoSpace), - ll::lfs_error_LFS_ERR_NOMEM => Err(Error::NoMemory), - ll::lfs_error_LFS_ERR_NOATTR => Err(Error::NoAttribute), - ll::lfs_error_LFS_ERR_NAMETOOLONG => Err(Error::FilenameTooLong), - // positive codes should always indicate success - _ => Err(Error::Unknown(error_code)), +impl From for Error { + fn from(error_code: i32) -> Error { + match error_code { + n if n >= 0 => Error::Success, + // negative codes + ll::lfs_error_LFS_ERR_IO => Error::Io, + ll::lfs_error_LFS_ERR_CORRUPT => Error::Corruption, + ll::lfs_error_LFS_ERR_NOENT => Error::NoSuchEntry, + ll::lfs_error_LFS_ERR_EXIST => Error::EntryAlreadyExisted, + ll::lfs_error_LFS_ERR_NOTDIR => Error::PathNotDir, + ll::lfs_error_LFS_ERR_ISDIR => Error::PathIsDir, + ll::lfs_error_LFS_ERR_NOTEMPTY => Error::DirNotEmpty, + ll::lfs_error_LFS_ERR_BADF => Error::BadFileDescriptor, + ll::lfs_error_LFS_ERR_FBIG => Error::FileTooBig, + ll::lfs_error_LFS_ERR_INVAL => Error::Invalid, + ll::lfs_error_LFS_ERR_NOSPC => Error::NoSpace, + ll::lfs_error_LFS_ERR_NOMEM => Error::NoMemory, + ll::lfs_error_LFS_ERR_NOATTR => Error::NoAttribute, + ll::lfs_error_LFS_ERR_NAMETOOLONG => Error::FilenameTooLong, + // positive codes should always indicate success + _ => Error::Unknown(error_code), + } } } +pub fn result_from(return_value: T, error_code: ll::lfs_error) -> Result { + let error: Error = error_code.into(); + match error { + Error::Success => Ok(return_value), + _ => Err(error) + } +}