소스 검색

feat: remove file check existence

liujingx 11 달 전
부모
커밋
da873910d4
3개의 변경된 파일17개의 추가작업 그리고 15개의 파일을 삭제
  1. 1 0
      ext4_test/src/main.rs
  2. 6 12
      src/ext4/dir.rs
  3. 10 3
      src/ext4/file.rs

+ 1 - 0
ext4_test/src/main.rs

@@ -112,6 +112,7 @@ fn remove_file_test(ext4: &mut Ext4) {
     ext4.open("d3/f1", "r", true).expect_err("open failed");
     ext4.remove_file("f1").expect("remove file failed");
     ext4.open("f1", "r", true).expect_err("open failed");
+    ext4.remove_file("d1/not_exist").expect_err("remove file failed");
 }
 
 fn main() {

+ 6 - 12
src/ext4/dir.rs

@@ -66,13 +66,8 @@ impl Ext4 {
         Ok(())
     }
 
-    /// Remove a entry from a directory, return the inode id that
-    /// the removed entry points to.
-    pub(super) fn dir_remove_entry(
-        &mut self,
-        parent: &mut InodeRef,
-        name: &str,
-    ) -> Result<InodeId> {
+    /// Remove a entry from a directory
+    pub(super) fn dir_remove_entry(&mut self, parent: &mut InodeRef, name: &str) -> Result<()> {
         info!("Dir remove entry: parent {}, path {}", parent.id, name);
         let inode_size = parent.inode.size();
         let total_blocks = inode_size as u32 / BLOCK_SIZE as u32;
@@ -85,9 +80,9 @@ impl Ext4 {
             // Load the block from disk
             let mut block = self.read_block(fblock);
             // Try removing the entry
-            if let Ok(inode) = Self::remove_entry_from_block(&mut block, name) {
+            if let Ok(()) = Self::remove_entry_from_block(&mut block, name) {
                 self.write_block(&block);
-                return Ok(inode);
+                return Ok(());
             }
             // Current block has no enough space
             iblock += 1;
@@ -113,17 +108,16 @@ impl Ext4 {
     }
 
     /// Remove a directory entry that matches a given name from a given block
-    fn remove_entry_from_block(block: &mut Block, name: &str) -> Result<InodeId> {
+    fn remove_entry_from_block(block: &mut Block, name: &str) -> Result<()> {
         info!("Dir remove entry {} from block {}", name, block.block_id);
         let mut offset = 0;
         while offset < BLOCK_SIZE {
             let mut de: DirEntry = block.read_offset_as(offset);
             if !de.unused() && de.compare_name(name) {
-                let inode = de.inode();
                 // Mark the target entry as unused
                 de.set_unused();
                 block.write_offset_as(offset, &de);
-                return Ok(inode);
+                return Ok(());
             }
             offset += de.rec_len() as usize;
         }

+ 10 - 3
src/ext4/file.rs

@@ -186,15 +186,22 @@ impl Ext4 {
         let file_name = &search_path.split_off(search_path.len() - 1)[0];
         let parent_path = search_path.join("/");
         // Get the parent directory inode
-        let parent_id = self.generic_open(
+        let parent_inode_id = self.generic_open(
             EXT4_ROOT_INO,
             &parent_path,
             FileType::Directory,
             OpenFlags::O_RDONLY,
         )?;
-        let mut parent_inode = self.read_inode(parent_id);
+        // Get the file inode, check the existence and type
+        let child_inode_id = self.generic_open(
+            parent_inode_id,
+            file_name,
+            FileType::RegularFile,
+            OpenFlags::O_RDONLY,
+        )?;
         // Remove the file from the parent directory
-        let child_inode_id = self.dir_remove_entry(&mut parent_inode, &file_name)?;
+        let mut parent_inode = self.read_inode(parent_inode_id);
+        self.dir_remove_entry(&mut parent_inode, &file_name)?;
         // Free the inode of the file
         self.free_inode(child_inode_id)
     }