Skip to main content

bux_e2fs/
error.rs

1//! Error types for ext4 filesystem operations.
2
3/// Errors returned by ext4 operations.
4#[non_exhaustive]
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7    /// A libext2fs function returned a non-zero error code.
8    #[error("{op}: {}", describe_ext2fs_error(*.code))]
9    Ext2fs {
10        /// Name of the libext2fs operation that failed.
11        op: &'static str,
12        /// The raw `errcode_t` value.
13        code: i64,
14    },
15
16    /// A path could not be converted to a C string.
17    #[error("invalid path: {0}")]
18    InvalidPath(String),
19
20    /// An I/O error occurred outside of libext2fs.
21    #[error(transparent)]
22    Io(#[from] std::io::Error),
23}
24
25/// Convenience alias for `std::result::Result<T, Error>`.
26pub type Result<T> = std::result::Result<T, Error>;
27
28/// Maps common libext2fs error codes to human-readable descriptions.
29///
30/// Error codes are defined in `ext2_err.h` (base 2133571328 = 0x7F2C0000).
31fn describe_ext2fs_error(code: i64) -> String {
32    // ext2fs error table base: EXT2_ET_BASE = 2133571328
33    const BASE: i64 = 2_133_571_328;
34    match code - BASE {
35        1 => "bad magic number in superblock".into(),
36        2 => "filesystem revision too high".into(),
37        4 => "illegal block number".into(),
38        5 => "illegal inode number".into(),
39        6 => "internal error in ext2fs_open_icount".into(),
40        7 => "cannot write to an fs opened read-only".into(),
41        8 => "block bitmap not loaded".into(),
42        9 => "inode bitmap not loaded".into(),
43        10 => "no free blocks".into(),
44        11 => "no free inodes".into(),
45        12 => "directory block not found".into(),
46        16 => "inode already allocated".into(),
47        17 => "block already allocated".into(),
48        22 => "filesystem not open".into(),
49        23 => "device is read-only".into(),
50        24 => "directory corrupted".into(),
51        25 => "short read".into(),
52        26 => "short write".into(),
53        28 => "filesystem too large".into(),
54        _ => format!("libext2fs error {code:#x}"),
55    }
56}