diff options
author | Chuyan Zhang <me@zcy.moe> | 2023-12-03 00:44:48 -0800 |
---|---|---|
committer | Chuyan Zhang <me@zcy.moe> | 2023-12-03 00:44:48 -0800 |
commit | 21a9ce8e53224100f331d9a666bc00c630964724 (patch) | |
tree | a57b71974becd920120241b6e947f1082870fee6 /ayafs-core/src/memory | |
parent | f5c92834f79dfdf8007daa019f401c5e51a7596f (diff) | |
download | myfs-21a9ce8e53224100f331d9a666bc00c630964724.tar.gz myfs-21a9ce8e53224100f331d9a666bc00c630964724.zip |
fix directory entry counting
Diffstat (limited to 'ayafs-core/src/memory')
-rw-r--r-- | ayafs-core/src/memory/cached_inode.rs | 4 | ||||
-rw-r--r-- | ayafs-core/src/memory/dir_entry.rs | 38 |
2 files changed, 23 insertions, 19 deletions
diff --git a/ayafs-core/src/memory/cached_inode.rs b/ayafs-core/src/memory/cached_inode.rs index 70579c7..c1e1909 100644 --- a/ayafs-core/src/memory/cached_inode.rs +++ b/ayafs-core/src/memory/cached_inode.rs @@ -4,6 +4,7 @@ use crate::{utils, AyaFS}; use and_then_some::BoolExt; use fuser::FileType; use libc::{c_int, EIO, EISDIR, ENOENT, ENOTDIR, ENOTEMPTY}; +use log::debug; impl AyaFS { pub(crate) fn create_file( @@ -103,7 +104,10 @@ impl AyaFS { // 不是 dir -> ENOTDIR return Err(ENOTDIR); } + + let direntry_map = self.dir_entry_map.get(&inode_index).unwrap(); if inode.size > 2 { + debug!("deleting inode {} failed, having {} directory entries", inode_index, direntry_map.len()); // 有 . 和 .. 以外的 entry -> ENOTEMPTY return Err(ENOTEMPTY); } diff --git a/ayafs-core/src/memory/dir_entry.rs b/ayafs-core/src/memory/dir_entry.rs index ee363b2..9a11d2a 100644 --- a/ayafs-core/src/memory/dir_entry.rs +++ b/ayafs-core/src/memory/dir_entry.rs @@ -72,6 +72,7 @@ impl AyaFS { parent_inode: &mut Inode, dir_entry_map: IndexMap<OsString, DirectoryEntry>, ) -> Result<(), c_int> { + parent_inode.size = dir_entry_map.len() as u64; for (entry_index, (name, dir_entry)) in dir_entry_map.into_iter().enumerate() { let block_index_within_inode = entry_index / 15; let entry_index_within_block = entry_index % 15; @@ -117,32 +118,36 @@ impl AyaFS { new_parent_inode: &mut Inode, new_name: T, ) -> Result<(), c_int> { + let name: &OsStr = name.as_ref(); + let new_name: &OsStr = new_name.as_ref(); self.load_direntry_map(parent_index, parent_inode)?; self.load_direntry_map(new_parent_index, new_parent_inode)?; + if parent_index != new_parent_index { + parent_inode.size -= 1; + new_parent_inode.size += 1; + } + let dir_entry = self .dir_entry_map .get(&parent_index) .ok_or(ENOENT)? - .get(name.as_ref()) + .get(name) .cloned() .ok_or(ENOENT)?; let new_dir_entry = self .dir_entry_map .get(&new_parent_index) .ok_or(ENOENT)? - .get(new_name.as_ref()) + .get(new_name) .cloned() .ok_or(ENOENT)?; - let name: &OsStr = name.as_ref(); - let new_name: &OsStr = new_name.as_ref(); - self.dir_entry_map .get_mut(&parent_index) .map(|dir_entry_map| { if let Occupied(mut entry) = dir_entry_map.entry(name.to_os_string()) { - *entry.get_mut() = new_dir_entry.clone(); + *entry.get_mut() = new_dir_entry; } }) .ok_or(ENOENT)?; @@ -150,7 +155,7 @@ impl AyaFS { .get_mut(&new_parent_index) .map(|new_dir_entry_map| { if let Occupied(mut entry) = new_dir_entry_map.entry(new_name.to_os_string()) { - *entry.get_mut() = dir_entry.clone(); + *entry.get_mut() = dir_entry; } }) .ok_or(ENOENT)?; @@ -164,12 +169,12 @@ impl AyaFS { parent_index: usize, parent_inode: &mut Inode, name: T, - _entry_index: u32, ) -> Result<(), c_int> { self.load_direntry_map(parent_index, parent_inode)?; if let Some(dir_entry_map) = self.dir_entry_map.get_mut(&parent_index) { - trace!(" remove_direntry(ino: {}) using hashmap", parent_index); if dir_entry_map.shift_remove(name.as_ref()).is_some() { + parent_inode.size = dir_entry_map.len() as u64; + // trace!("remove_direntry(parent: {:#x}, name: {:?}), parent inode size is {}, direntry_map size is {}", parent_index, name.as_ref(), parent_inode.size, dir_entry_map.len()); Ok(()) } else { Err(ENOENT) @@ -191,11 +196,9 @@ impl AyaFS { if let Some(dir_entry_map) = self.dir_entry_map.get_mut(&parent_index) { let (entry_index, _) = dir_entry_map.insert_full(child_inode_name.to_os_string(), dir_entry); - trace!( - " add_direntry(ino: {}) using hashmap, entry {}", - parent_index, entry_index - ); - parent_inode.size += 1; + // trace!("parent inode size {} before +1", parent_inode.size); + parent_inode.size = dir_entry_map.len() as u64; + // trace!("add_direntry_2(parent: {:#x}, name: {:?}), parent inode size is {}, direntry_map size is {}", parent_index, child_inode_name, parent_inode.size, dir_entry_map.len()); Ok(entry_index as u32) } else { Err(ENOENT) @@ -227,11 +230,8 @@ impl AyaFS { let (entry_index, _) = dir_entry_map.insert_full(child_inode_name.to_os_string(), dir_entry); - trace!( - " add_direntry(ino: {}) using hashmap, entry {}", - parent_index, entry_index - ); - parent_inode.size += 1; + parent_inode.size = dir_entry_map.len() as u64; + trace!("add_direntry(parent: {:#x}, name: {:?}), parent inode size is {}, direntry_map size is {}", parent_index, child_inode_name, parent_inode.size, dir_entry_map.len()); Ok(entry_index as u32) } else { Err(ENOENT) |