Browse Source

fix: cargo clippy

liujingx 1 month ago
parent
commit
3c3e03a1b6

+ 4 - 4
src/error.rs

@@ -98,19 +98,19 @@ impl Ext4Error {
 #[macro_export]
 macro_rules! format_error {
     ($code: expr, $message: expr) => {
-        crate::error::Ext4Error::with_message($code, format!($message))
+        $crate::error::Ext4Error::with_message($code, format!($message))
     };
     ($code: expr, $fmt: expr,  $($args:tt)*) => {
-        crate::error::Ext4Error::with_message($code, format!($fmt, $($args)*))
+        $crate::error::Ext4Error::with_message($code, format!($fmt, $($args)*))
     };
 }
 
 #[macro_export]
 macro_rules! return_error {
     ($code: expr, $message: expr) => {
-        return Err(crate::format_error!($code, $message));
+        return Err($crate::format_error!($code, $message));
     };
     ($code: expr, $fmt: expr,  $($args:tt)*) => {
-        return Err(crate::format_error!($code, $fmt, $($args)*));
+        return Err($crate::format_error!($code, $fmt, $($args)*));
     }
 }

+ 2 - 2
src/ext4/alloc.rs

@@ -49,7 +49,7 @@ impl Ext4 {
     /// Free an allocated inode and all data blocks allocated for it
     pub(super) fn free_inode(&self, inode: &mut InodeRef) -> Result<()> {
         // Free the data blocks allocated for the inode
-        let pblocks = self.extent_all_data_blocks(&inode);
+        let pblocks = self.extent_all_data_blocks(inode);
         for pblock in pblocks {
             // Deallocate the block
             self.dealloc_block(inode, pblock)?;
@@ -57,7 +57,7 @@ impl Ext4 {
             self.write_block(&Block::new(pblock, [0; BLOCK_SIZE]));
         }
         // Free extent tree
-        let pblocks = self.extent_all_tree_blocks(&inode);
+        let pblocks = self.extent_all_tree_blocks(inode);
         for pblock in pblocks {
             // Deallocate the block
             self.dealloc_block(inode, pblock)?;

+ 3 - 3
src/ext4/dir.rs

@@ -60,7 +60,7 @@ impl Ext4 {
                     dir.inode.generation(),
                 );
                 // Write the block back to disk
-                self.write_block(&dir_block.block());
+                self.write_block(dir_block.block());
                 return Ok(());
             }
             // Current block has no enough space
@@ -82,7 +82,7 @@ impl Ext4 {
             dir.inode.generation(),
         );
         // Write the block back to disk
-        self.write_block(&new_dir_block.block());
+        self.write_block(new_dir_block.block());
 
         Ok(())
     }
@@ -107,7 +107,7 @@ impl Ext4 {
                     dir.inode.generation(),
                 );
                 // Write the block back to disk
-                self.write_block(&dir_block.block());
+                self.write_block(dir_block.block());
                 return Ok(());
             }
             // Current block has no enough space

+ 1 - 1
src/ext4/extent.rs

@@ -173,7 +173,7 @@ impl Ext4 {
     fn insert_extent(
         &self,
         inode_ref: &mut InodeRef,
-        path: &Vec<ExtentSearchStep>,
+        path: &[ExtentSearchStep],
         new_ext: &Extent,
     ) -> Result<()> {
         let leaf = path.last().unwrap();

+ 3 - 3
src/ext4/high_level.rs

@@ -69,7 +69,7 @@ impl Ext4 {
             if !cur.inode.is_dir() {
                 return_error!(ErrCode::ENOTDIR, "Parent {} is not a directory", cur.id);
             }
-            match self.dir_find_entry(&cur, &path) {
+            match self.dir_find_entry(&cur, path) {
                 Ok(id) => {
                     if i == search_path.len() - 1 {
                         // Reach the object and it already exists
@@ -115,7 +115,7 @@ impl Ext4 {
         // Get the parent directory inode
         let parent_id = self.generic_lookup(root, &parent_path)?;
         // Get the child inode
-        let child_id = self.lookup(parent_id, &file_name)?;
+        let child_id = self.lookup(parent_id, file_name)?;
         let mut parent = self.read_inode(parent_id);
         let mut child = self.read_inode(child_id);
         // Check if child is a non-empty directory
@@ -151,7 +151,7 @@ impl Ext4 {
         let src_parent_id = self.generic_lookup(root, &src_parent_path)?;
         let dst_parent_id = self.generic_lookup(root, &dst_parent_path)?;
         // Move the file
-        self.rename(src_parent_id, &src_file_name, dst_parent_id, &dst_file_name)
+        self.rename(src_parent_id, src_file_name, dst_parent_id, dst_file_name)
     }
 
     /// A helper function to split a path by '/'

+ 1 - 1
src/ext4/link.rs

@@ -43,7 +43,7 @@ impl Ext4 {
         if child.inode.is_dir() {
             // Child is a directory
             // Unlink "child/.."
-            self.dir_remove_entry(&child, "..")?;
+            self.dir_remove_entry(child, "..")?;
             parent.inode.set_link_count(parent.inode.link_count() - 1);
             self.write_inode_with_csum(parent);
         }

+ 6 - 6
src/ext4/low_level.rs

@@ -90,7 +90,7 @@ impl Ext4 {
         }
         if let Some(size) = size {
             // If size increases, allocate new blocks if needed.
-            let required_blocks = (size as usize + INODE_BLOCK_SIZE - 1) / INODE_BLOCK_SIZE;
+            let required_blocks = (size as usize).div_ceil(INODE_BLOCK_SIZE);
             for _ in inode.inode.block_count()..required_blocks as u64 {
                 self.inode_append_block(&mut inode)?;
             }
@@ -161,13 +161,13 @@ impl Ext4 {
     /// * `EISDIR` - `file` is not a regular file
     pub fn read(&self, file: InodeId, offset: usize, buf: &mut [u8]) -> Result<usize> {
         // Get the inode of the file
-        let mut file = self.read_inode(file);
+        let file = self.read_inode(file);
         if !file.inode.is_file() {
             return_error!(ErrCode::EISDIR, "Inode {} is not a file", file.id);
         }
 
         // Read no bytes
-        if buf.len() == 0 {
+        if buf.is_empty() {
             return Ok(0);
         }
         // Calc the actual size to read
@@ -182,7 +182,7 @@ impl Ext4 {
         // Read first block
         if misaligned > 0 {
             let read_len = min(BLOCK_SIZE - misaligned, read_size);
-            let fblock = self.extent_query(&mut file, start_iblock).unwrap();
+            let fblock = self.extent_query(&file, start_iblock).unwrap();
             let block = self.read_block(fblock);
             // Copy data from block to the user buffer
             buf[cursor..cursor + read_len].copy_from_slice(block.read_offset(misaligned, read_len));
@@ -192,7 +192,7 @@ impl Ext4 {
         // Continue with full block reads
         while cursor < read_size {
             let read_len = min(BLOCK_SIZE, read_size - cursor);
-            let fblock = self.extent_query(&mut file, iblock).unwrap();
+            let fblock = self.extent_query(&file, iblock).unwrap();
             let block = self.read_block(fblock);
             // Copy data from block to the user buffer
             buf[cursor..cursor + read_len].copy_from_slice(block.read_offset(0, read_len));
@@ -241,7 +241,7 @@ impl Ext4 {
         let mut iblock = start_iblock;
         while cursor < write_size {
             let write_len = min(BLOCK_SIZE, write_size - cursor);
-            let fblock = self.extent_query(&mut file, iblock)?;
+            let fblock = self.extent_query(&file, iblock)?;
             let mut block = self.read_block(fblock);
             block.write_offset(
                 (offset + cursor) % BLOCK_SIZE,

+ 1 - 1
src/ext4/mod.rs

@@ -37,7 +37,7 @@ impl Ext4 {
             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 {
+        if sb.desc_size() != SB_GOOD_DESC_SIZE {
             return_error!(
                 ErrCode::EINVAL,
                 "Invalid block group desc size {}",

+ 3 - 3
src/ext4/rw.rs

@@ -47,8 +47,8 @@ impl Ext4 {
     pub(super) fn read_inode(&self, inode_id: InodeId) -> InodeRef {
         let (block_id, offset) = self.inode_disk_pos(inode_id);
         let block = self.read_block(block_id);
-        let inode = InodeRef::new(inode_id, block.read_offset_as(offset));
-        inode
+        
+        InodeRef::new(inode_id, block.read_offset_as(offset))
     }
 
     /// Read the root inode from block device
@@ -115,7 +115,7 @@ impl Ext4 {
         let inodes_per_group = super_block.inodes_per_group();
 
         let bg_id = ((inode_id - 1) / inodes_per_group) as BlockGroupId;
-        let inode_size = super_block.inode_size() as usize;
+        let inode_size = super_block.inode_size();
         let bg = self.read_block_group(bg_id);
         let id_in_bg = ((inode_id - 1) % inodes_per_group) as usize;
 

+ 2 - 8
src/ext4_defs/bitmap.rs

@@ -24,19 +24,13 @@ impl<'a> Bitmap<'a> {
     /// Find the first clear bit in the range `[start, end)`
     pub fn first_clear_bit(&self, start: usize, end: usize) -> Option<usize> {
         let end = core::cmp::min(end, self.0.len() * 8);
-        for i in start..end {
-            if self.is_bit_clear(i) {
-                return Some(i);
-            }
-        }
-        None
+        (start..end).find(|&i| self.is_bit_clear(i))
     }
 
     /// Find the first clear bit in the range `[start, end)` and set it if found
     pub fn find_and_set_first_clear_bit(&mut self, start: usize, end: usize) -> Option<usize> {
-        self.first_clear_bit(start, end).map(|bit| {
+        self.first_clear_bit(start, end).inspect(|&bit| {
             self.set_bit(bit);
-            bit
         })
     }
 }

+ 9 - 9
src/ext4_defs/block_group.rs

@@ -59,19 +59,19 @@ impl BlockGroupDesc {
     const MAX_BLOCK_GROUP_DESC_SIZE: usize = 64;
 
     pub fn block_bitmap_block(&self) -> PBlockId {
-        (self.block_bitmap_hi as PBlockId) << 32 | self.block_bitmap_lo as PBlockId
+        ((self.block_bitmap_hi as PBlockId) << 32) | self.block_bitmap_lo as PBlockId
     }
 
     pub fn inode_bitmap_block(&self) -> PBlockId {
-        (self.inode_bitmap_hi as PBlockId) << 32 | self.inode_bitmap_lo as PBlockId
+        ((self.inode_bitmap_hi as PBlockId) << 32) | self.inode_bitmap_lo as PBlockId
     }
 
     pub fn itable_unused(&self) -> u32 {
-        (self.itable_unused_hi as u32) << 16 | self.itable_unused_lo as u32
+        ((self.itable_unused_hi as u32) << 16) | self.itable_unused_lo as u32
     }
 
     pub fn used_dirs_count(&self) -> u32 {
-        (self.used_dirs_count_hi as u32) << 16 | self.used_dirs_count_lo as u32
+        ((self.used_dirs_count_hi as u32) << 16) | self.used_dirs_count_lo as u32
     }
 
     pub fn set_used_dirs_count(&mut self, cnt: u32) {
@@ -107,15 +107,15 @@ impl BlockGroupDesc {
     }
 
     pub fn set_inode_bitmap_csum(&mut self, uuid: &[u8], bitmap: &Bitmap) {
-        let mut csum = crc32(CRC32_INIT, &uuid);
-        csum = crc32(csum, &bitmap.as_bytes());
+        let mut csum = crc32(CRC32_INIT, uuid);
+        csum = crc32(csum, bitmap.as_bytes());
         self.inode_bitmap_csum_lo = csum as u16;
         self.block_bitmap_csum_hi = (csum >> 16) as u16;
     }
 
     pub fn set_block_bitmap_csum(&mut self, uuid: &[u8], bitmap: &Bitmap) {
-        let mut csum = crc32(CRC32_INIT, &uuid);
-        csum = crc32(csum, &bitmap.as_bytes());
+        let mut csum = crc32(CRC32_INIT, uuid);
+        csum = crc32(csum, bitmap.as_bytes());
         self.block_bitmap_csum_lo = csum as u16;
         self.block_bitmap_csum_hi = (csum >> 16) as u16;
     }
@@ -138,7 +138,7 @@ impl BlockGroupRef {
     pub fn set_checksum(&mut self, uuid: &[u8]) {
         let mut checksum = crc32(CRC32_INIT, uuid);
         checksum = crc32(checksum, &self.id.to_le_bytes());
-        checksum = crc32(checksum, &self.desc.to_bytes());
+        checksum = crc32(checksum, self.desc.to_bytes());
         self.desc.checksum = checksum as u16;
     }
 }

+ 3 - 3
src/ext4_defs/dir.rs

@@ -66,7 +66,7 @@ impl DirEntry {
     /// Create a new directory entry
     pub fn new(inode: InodeId, rec_len: u16, name: &str, file_type: FileType) -> Self {
         let mut name_bytes = [0u8; 255];
-        let name_len = name.as_bytes().len();
+        let name_len = name.len();
         name_bytes[..name_len].copy_from_slice(name.as_bytes());
         Self {
             inode,
@@ -150,7 +150,7 @@ impl DirEntryTail {
     }
 
     pub fn set_checksum(&mut self, uuid: &[u8], ino: InodeId, ino_gen: u32, block: &Block) {
-        let mut csum = crc32(CRC32_INIT, &uuid);
+        let mut csum = crc32(CRC32_INIT, uuid);
         csum = crc32(csum, &ino.to_le_bytes());
         csum = crc32(csum, &ino_gen.to_le_bytes());
         self.checksum = crc32(csum, &block.data[..size_of::<DirEntryTail>()]);
@@ -224,7 +224,7 @@ impl DirBlock {
             // Compare size
             if free_size < required_size {
                 // No enough space, try next dir ent
-                offset = offset + rec_len;
+                offset += rec_len;
                 continue;
             }
             // Has enough space

+ 10 - 9
src/ext4_defs/extent.rs

@@ -124,7 +124,7 @@ impl ExtentIndex {
 
     /// The physical block number of the extent node that is the next level lower in the tree
     pub fn leaf(&self) -> PBlockId {
-        (self.leaf_hi as PBlockId) << 32 | self.leaf_lo as PBlockId
+        ((self.leaf_hi as PBlockId) << 32) | self.leaf_lo as PBlockId
     }
 }
 
@@ -205,7 +205,7 @@ impl Extent {
 
     /// Mark the extent as unwritten
     pub fn mark_unwritten(&mut self) {
-        (*self).block_count |= Self::INIT_MAX_LEN;
+        self.block_count |= Self::INIT_MAX_LEN;
     }
 
     /// Check whether the `ex2` extent can be appended to the `ex1` extent
@@ -215,13 +215,14 @@ impl Extent {
         }
         if ex1.is_unwritten() && ex1.block_count() + ex2.block_count() > 65535 as LBlockId {
             return false;
-        } else if ex1.block_count() + ex2.block_count() > Self::INIT_MAX_LEN as LBlockId {
+        }
+        if ex1.block_count() + ex2.block_count() > Self::INIT_MAX_LEN as LBlockId {
             return false;
         }
-        if ex1.first_block + ex1.block_count() as u32 != ex2.first_block {
+        if ex1.first_block + ex1.block_count() != ex2.first_block {
             return false;
         }
-        return true;
+        true
     }
 }
 
@@ -300,9 +301,9 @@ impl<'a> ExtentNode<'a> {
                 break;
             }
         }
-        let res = Err(i);
+        
         // debug!("Search res: {:?}", res);
-        return res;
+        Err(i)
     }
 
     /// Find the extent index that covers the given logical block number. The extent index
@@ -321,9 +322,9 @@ impl<'a> ExtentNode<'a> {
             }
             i += 1;
         }
-        let res = Ok(i - 1);
+        
         // debug!("Search res: {:?}", res);
-        return res;
+        Ok(i - 1)
     }
 
     pub fn print(&self) {

+ 5 - 5
src/ext4_defs/inode.rs

@@ -13,7 +13,7 @@ use crate::prelude::*;
 use crate::FileType;
 
 bitflags! {
-    #[derive(PartialEq, Debug, Clone, Copy)]
+    #[derive(PartialEq, Eq, Debug, Clone, Copy)]
     pub struct InodeMode: u16 {
         // Premission
         const PERM_MASK = 0xFFF;
@@ -322,7 +322,7 @@ impl Inode {
     }
 
     pub fn xattr_block(&self) -> PBlockId {
-        (self.osd2.l_file_acl_hi as u64) << 32 | self.file_acl as u64
+        ((self.osd2.l_file_acl_hi as u64) << 32) | self.file_acl as u64
     }
 
     pub fn set_xattr_block(&mut self, block: PBlockId) {
@@ -335,14 +335,14 @@ impl Inode {
     /// Get the immutable extent root node
     pub fn extent_root(&self) -> ExtentNode {
         ExtentNode::from_bytes(unsafe {
-            core::slice::from_raw_parts(self.block.as_ptr() as *const u8, 60)
+            core::slice::from_raw_parts(self.block.as_ptr(), 60)
         })
     }
 
     /// Get the mutable extent root node
     pub fn extent_root_mut(&mut self) -> ExtentNodeMut {
         ExtentNodeMut::from_bytes(unsafe {
-            core::slice::from_raw_parts_mut(self.block.as_mut_ptr() as *mut u8, 60)
+            core::slice::from_raw_parts_mut(self.block.as_mut_ptr(), 60)
         })
     }
 
@@ -371,7 +371,7 @@ impl InodeRef {
         let mut checksum = crc32(CRC32_INIT, uuid);
         checksum = crc32(checksum, &self.id.to_le_bytes());
         checksum = crc32(checksum, &self.inode.generation.to_le_bytes());
-        checksum = crc32(checksum, &self.inode.to_bytes());
+        checksum = crc32(checksum, self.inode.to_bytes());
         self.inode.osd2.l_checksum_lo = checksum as u16;
         self.inode.checksum_hi = (checksum >> 16) as u16;
     }

+ 1 - 1
src/ext4_defs/super_block.rs

@@ -153,7 +153,7 @@ impl SuperBlock {
 
     /// The number of block groups.
     pub fn block_group_count(&self) -> u32 {
-        ((self.block_count() + self.blocks_per_group as u64 - 1) / self.blocks_per_group as u64)
+        self.block_count().div_ceil(self.blocks_per_group as u64)
             as u32
     }
 

+ 3 - 3
src/ext4_defs/xattr.rs

@@ -124,7 +124,7 @@ impl XattrEntry {
     pub fn new(name: &str, value_size: usize, value_offset: usize) -> Self {
         let mut name_bytes = [0u8; 255];
         let (name_index, name) = Self::match_name(name);
-        let name_len = name.as_bytes().len();
+        let name_len = name.len();
         name_bytes[..name_len].copy_from_slice(name.as_bytes());
         Self {
             name_len: name.len() as u8,
@@ -192,8 +192,8 @@ impl XattrEntry {
             ("system.", 7),
         ];
         for (prefix, index) in prefixes {
-            if name.starts_with(prefix) {
-                return (index, &name[prefix.len()..]);
+            if let Some(stripped) = name.strip_prefix(prefix) {
+                return (index, stripped);
             }
         }
         (0, name)