Browse Source

style: error code explanations

liujingx 10 months ago
parent
commit
5b05bf0cfe
4 changed files with 69 additions and 39 deletions
  1. 47 24
      src/error.rs
  2. 7 4
      src/ext4/mod.rs
  3. 10 11
      src/ext4_defs/block_device.rs
  4. 5 0
      src/ext4_defs/dir.rs

+ 47 - 24
src/error.rs

@@ -5,31 +5,54 @@ use crate::prelude::*;
 
 /// Ext4Error number.
 #[repr(i32)]
-#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 pub enum ErrCode {
-    EPERM = 1,       /* Operation not permitted */
-    ENOENT = 2,      /* No such file or directory */
-    EIO = 5,         /* I/O error */
-    ENXIO = 6,       /* No such device or address */
-    E2BIG = 7,       /* Argument list too long */
-    ENOMEM = 12,     /* Out of memory */
-    EACCES = 13,     /* Permission denied */
-    EFAULT = 14,     /* Bad address */
-    EEXIST = 17,     /* File exists */
-    ENODEV = 19,     /* No such device */
-    ENOTDIR = 20,    /* Not a directory */
-    EISDIR = 21,     /* Is a directory */
-    EINVAL = 22,     /* Invalid argument */
-    EFBIG = 27,      /* File too large */
-    ENOSPC = 28,     /* No space left on device */
-    EROFS = 30,      /* Read-only file system */
-    EMLINK = 31,     /* Too many links */
-    ERANGE = 34,     /* Math result not representable */
-    ENOTEMPTY = 39,  /* Directory not empty */
-    ENODATA = 61,    /* No data available */
-    ENOTSUP = 95,    /* Not supported */
-    ELINKFAIL = 97,  /* Link failed */
-    EALLOCFIAL = 98, /* Inode alloc failed */
+    /// Operation not permitted.
+    EPERM = 1, 
+    /// No such file or directory.
+    ENOENT = 2,
+    /// I/O error.
+    EIO = 5,
+    /// No such device or address.
+    ENXIO = 6,
+    /// Argument list too long.
+    E2BIG = 7,
+    /// Out of memory.
+    ENOMEM = 12,
+    /// Permission denied.
+    EACCES = 13,
+    /// Bad address.
+    EFAULT = 14,
+    /// File exists.
+    EEXIST = 17,
+    /// No such device.
+    ENODEV = 19,
+    /// Not a directory.
+    ENOTDIR = 20,
+    /// Is a directory.
+    EISDIR = 21,
+    /// Invalid argument.
+    EINVAL = 22,
+    /// File too large.
+    EFBIG = 27,
+    /// No space left on device.
+    ENOSPC = 28,
+    /// Read-only file system.
+    EROFS = 30,
+    /// Too many links.
+    EMLINK = 31,
+    /// Math result not representable.
+    ERANGE = 34,
+    /// Directory not empty.
+    ENOTEMPTY = 39,
+    /// No data available.
+    ENODATA = 61,
+    /// Not supported.
+    ENOTSUP = 95,
+    /// Link failed.
+    ELINKFAIL = 97,
+    /// Inode alloc failed.
+    EALLOCFAIL = 98,
 }
 
 /// error used in this crate

+ 7 - 4
src/ext4/mod.rs

@@ -12,7 +12,6 @@ mod link;
 mod low_level;
 mod rw;
 
-#[derive(Debug)]
 pub struct Ext4 {
     block_device: Arc<dyn BlockDevice>,
 }
@@ -24,18 +23,22 @@ impl Ext4 {
         // TODO: if the main superblock is corrupted, should we load the backup?
         let block = block_device.read_block(0);
         let sb = block.read_offset_as::<SuperBlock>(BASE_OFFSET);
-        log::debug!("Ext4 loaded: {:?}", sb);
+        log::debug!("Load Ext4 Superblock: {:?}", sb);
         // Check magic number
         if !sb.check_magic() {
             return_error!(ErrCode::EINVAL, "Invalid magic number");
         }
         // Check inode size
         if sb.inode_size() != SB_GOOD_INODE_SIZE {
-            return_error!(ErrCode::EINVAL, "Invalid inode size");
+            return_error!(ErrCode::EINVAL, "Invalid inode size {}", sb.inode_size());
         }
         // Check block group desc size
         if sb.desc_size() as usize != SB_GOOD_DESC_SIZE {
-            return_error!(ErrCode::EINVAL, "Invalid block group desc size");
+            return_error!(
+                ErrCode::EINVAL,
+                "Invalid block group desc size {}",
+                sb.desc_size()
+            );
         }
         // Create Ext4 instance
         Ok(Self { block_device })

+ 10 - 11
src/ext4_defs/block_device.rs

@@ -1,7 +1,6 @@
 use crate::constants::*;
 use crate::prelude::*;
 use core::any::Any;
-use core::fmt::Debug;
 
 /// Interface for serializing and deserializing objects to and from bytes.
 ///
@@ -24,7 +23,7 @@ where
     }
 }
 
-/// Common data block descriptor
+/// Common data block descriptor.
 pub struct Block {
     /// Physical block id
     pub id: PBlockId,
@@ -42,17 +41,17 @@ impl Default for Block {
 }
 
 impl Block {
-    /// Create new block with given physical block id and data
+    /// Create new block with given physical block id and data.
     pub fn new(block_id: PBlockId, data: [u8; BLOCK_SIZE]) -> Self {
         Self { id: block_id, data }
     }
 
-    /// Read `size` bytes from `offset` in block data
+    /// Read `size` bytes from `offset` in block data.
     pub fn read_offset(&self, offset: usize, size: usize) -> &[u8] {
         &self.data[offset..offset + size]
     }
 
-    /// Read bytes from `offset` in block data and interpret it as `T`
+    /// Read bytes from `offset` in block data and interpret it as `T`.
     pub fn read_offset_as<'a, T>(&self, offset: usize) -> T
     where
         T: AsBytes,
@@ -60,12 +59,12 @@ impl Block {
         T::from_bytes(&self.data[offset..])
     }
 
-    /// Write block data to `offset` with `size`
+    /// Write block data to `offset` with `size`.
     pub fn write_offset(&mut self, offset: usize, data: &[u8]) {
         self.data[offset..offset + data.len()].copy_from_slice(data);
     }
 
-    /// Transform `T` to bytes and write it to `offset`
+    /// Transform `T` to bytes and write it to `offset`.
     pub fn write_offset_as<T>(&mut self, offset: usize, value: &T)
     where
         T: AsBytes,
@@ -74,10 +73,10 @@ impl Block {
     }
 }
 
-/// Common interface for block devices
-pub trait BlockDevice: Send + Sync + Any + Debug {
-    /// Read a block from disk
+/// Common interface for block devices.
+pub trait BlockDevice: Send + Sync + Any {
+    /// Read a block from disk.
     fn read_block(&self, block_id: PBlockId) -> Block;
-    /// Write a block to disk
+    /// Write a block to disk.
     fn write_block(&self, block: &Block);
 }

+ 5 - 0
src/ext4_defs/dir.rs

@@ -105,6 +105,11 @@ impl DirEntry {
         self.inode = 0
     }
 
+    /// Get the dir entry's file type
+    pub fn file_type(&self) -> FileType {
+        self.file_type
+    }
+
     /// Set the dir entry's file type
     pub fn set_type(&mut self, file_type: FileType) {
         self.file_type = file_type;