Skip to content

Commit

Permalink
rafs: cleanup raw os error with error macros
Browse files Browse the repository at this point in the history
With error macros, debug mode can show more informations such as stack trace.

Signed-off-by: Liu Bo <liub.liubo@gmail.com>
  • Loading branch information
liubogithub committed Dec 13, 2024
1 parent 33022a7 commit 5234d61
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ define_libc_error_macro!(ealready, EALREADY);
define_libc_error_macro!(enosys, ENOSYS);
define_libc_error_macro!(epipe, EPIPE);
define_libc_error_macro!(eio, EIO);
define_libc_error_macro!(erange, ERANGE);
define_libc_error_macro!(enodata, ENODATA);

/// Return EINVAL error with formatted error message.
#[macro_export]
Expand Down
10 changes: 5 additions & 5 deletions rafs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ impl FileSystem for Rafs {
let mut recorder = FopRecorder::settle(Getxattr, inode, &self.ios);

if !self.xattr_supported() {
return Err(std::io::Error::from_raw_os_error(libc::ENOSYS));
return Err(enosys!());
}

let name = OsStr::from_bytes(name.to_bytes());
Expand All @@ -744,15 +744,15 @@ impl FileSystem for Rafs {
let r = match value {
Some(value) => match size {
0 => Ok(GetxattrReply::Count((value.len() + 1) as u32)),
x if x < value.len() as u32 => Err(std::io::Error::from_raw_os_error(libc::ERANGE)),
x if x < value.len() as u32 => Err(erange!()),
_ => Ok(GetxattrReply::Value(value)),
},
None => {
// TODO: Hopefully, we can have a 'decorator' procedure macro in
// the future to wrap this method thus to handle different reasonable
// errors in a clean way.
recorder.mark_success(0);
Err(std::io::Error::from_raw_os_error(libc::ENODATA))
Err(enodata!())
}
};

Expand All @@ -765,7 +765,7 @@ impl FileSystem for Rafs {
fn listxattr(&self, _ctx: &Context, inode: u64, size: u32) -> Result<ListxattrReply> {
let mut rec = FopRecorder::settle(Listxattr, inode, &self.ios);
if !self.xattr_supported() {
return Err(std::io::Error::from_raw_os_error(libc::ENOSYS));
return Err(enosys!());
}

let inode = self.sb.get_inode(inode, false)?;
Expand All @@ -783,7 +783,7 @@ impl FileSystem for Rafs {

match size {
0 => Ok(ListxattrReply::Count(count as u32)),
x if x < count as u32 => Err(std::io::Error::from_raw_os_error(libc::ERANGE)),
x if x < count as u32 => Err(erange!()),
_ => Ok(ListxattrReply::Names(buf)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion rafs/src/metadata/direct_v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl DirectSuperBlockV6 {
let block_size = state.block_size();
let unit_size = size_of::<RafsV5ChunkInfo>();
if size % unit_size != 0 {
return Err(std::io::Error::from_raw_os_error(libc::EINVAL));
return Err(einval!());
}

for idx in 0..(size / unit_size) {
Expand Down

0 comments on commit 5234d61

Please sign in to comment.