diff --git a/src/fs.rs b/src/fs.rs index 2cfa60dd9..bcf74aaa5 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -511,9 +511,7 @@ impl Filesystem<'_, Storage> { let off = (block * block_size + off) as usize; let buf: &mut [u8] = unsafe { slice::from_raw_parts_mut(buffer as *mut u8, size as usize) }; - // TODO - storage.read(off, buf).unwrap(); - 0 + io::error_code_from(storage.read(off, buf)) } /// C callback interface used by LittleFS to program data with the lower level system below the @@ -533,9 +531,7 @@ impl Filesystem<'_, Storage> { let off = (block * block_size + off) as usize; let buf: &[u8] = unsafe { slice::from_raw_parts(buffer as *const u8, size as usize) }; - // TODO - storage.write(off, buf).unwrap(); - 0 + io::error_code_from(storage.write(off, buf)) } /// C callback interface used by LittleFS to erase data with the lower level system below the @@ -548,9 +544,7 @@ impl Filesystem<'_, Storage> { let storage = unsafe { &mut *((*c).context as *mut Storage) }; let off = block as usize * Storage::BLOCK_SIZE as usize; - // TODO - storage.erase(off, Storage::BLOCK_SIZE as usize).unwrap(); - 0 + io::error_code_from(storage.erase(off, Storage::BLOCK_SIZE as usize)) } /// C callback interface used by LittleFS to sync data with the lower level interface below the diff --git a/src/io.rs b/src/io.rs index a41333e29..64d4cc7b0 100644 --- a/src/io.rs +++ b/src/io.rs @@ -138,6 +138,29 @@ impl From for Error { } } +impl From for ll::lfs_error { + fn from(error: Error) -> Self { + match error { + Error::Success => ll::lfs_error_LFS_ERR_OK, + Error::Io => ll::lfs_error_LFS_ERR_IO, + Error::Corruption => ll::lfs_error_LFS_ERR_CORRUPT, + Error::NoSuchEntry => ll::lfs_error_LFS_ERR_NOENT, + Error::EntryAlreadyExisted => ll::lfs_error_LFS_ERR_EXIST, + Error::PathNotDir => ll::lfs_error_LFS_ERR_NOTDIR, + Error::PathIsDir => ll::lfs_error_LFS_ERR_ISDIR, + Error::DirNotEmpty => ll::lfs_error_LFS_ERR_NOTEMPTY, + Error::BadFileDescriptor => ll::lfs_error_LFS_ERR_BADF, + Error::FileTooBig => ll::lfs_error_LFS_ERR_FBIG, + Error::Invalid => ll::lfs_error_LFS_ERR_INVAL, + Error::NoSpace => ll::lfs_error_LFS_ERR_NOSPC, + Error::NoMemory => ll::lfs_error_LFS_ERR_NOMEM, + Error::NoAttribute => ll::lfs_error_LFS_ERR_NOATTR, + Error::FilenameTooLong => ll::lfs_error_LFS_ERR_NAMETOOLONG, + Error::Unknown(error_code) => error_code, + } + } +} + impl From for Error { fn from(error_code: i32) -> Error { match error_code { @@ -163,6 +186,10 @@ impl From for Error { } } +pub fn error_code_from(result: Result) -> ll::lfs_error { + result.err().unwrap_or(Error::Success).into() +} + pub fn result_from(return_value: T, error_code: ll::lfs_error) -> Result { let error: Error = error_code.into(); match error {