From 9d1368b0ea380a9446b4697af668d1685464b6c7 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Sun, 26 Nov 2023 02:33:01 -0800 Subject: more code idk what they are im so tired ohno --- src/memory/dir_entry.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'src/memory/dir_entry.rs') diff --git a/src/memory/dir_entry.rs b/src/memory/dir_entry.rs index 0ade23f..2d961db 100644 --- a/src/memory/dir_entry.rs +++ b/src/memory/dir_entry.rs @@ -1,15 +1,21 @@ -use std::ffi::OsStr; -use std::os::unix::ffi::OsStrExt; -use libc::{c_int, ENOENT, ENOSPC}; use crate::disk::block::{DirectoryBlock, DirectoryEntry}; -use crate::disk::inode::{Inode}; +use crate::disk::inode::Inode; use crate::AyaFS; +use libc::{c_int, ENOENT, ENOSPC}; +use std::ffi::OsStr; +use std::os::unix::ffi::OsStrExt; impl AyaFS { + /// 删除所有的 dir entry, 递归 + pub(crate) fn clear_direntry(&mut self, parent_inode: &mut Inode) -> Result<(), c_int> { + todo!() + } + + /// 删除第 entry_index 个 dir entry pub(crate) fn remove_direntry( &mut self, parent_inode: &mut Inode, - entry_index: u32 + entry_index: u32, ) -> Result<(), c_int> { let block_index_within_inode = (entry_index / 15) as usize; let entry_index_within_block = (entry_index % 15) as usize; @@ -18,7 +24,8 @@ impl AyaFS { Some(directory_block) => { if directory_block.block.query(entry_index_within_block) { directory_block.block.deallocate(entry_index_within_block); - directory_block.block.entries[entry_index_within_block] = DirectoryEntry::default(); + directory_block.block.entries[entry_index_within_block] = + DirectoryEntry::default(); Ok(()) } else { Err(ENOENT) @@ -31,7 +38,7 @@ impl AyaFS { pub(crate) fn add_direntry( &mut self, parent_inode: &mut Inode, - child_inode_index: u32, + child_inode_index: usize, child_inode_name: &OsStr, child_inode: &Inode, ) -> Result { @@ -45,7 +52,9 @@ impl AyaFS { } } // 寻找当前块里有没有空闲空间 - if let Some(directory_block) = self.access_block_mut::(parent_inode, block_index_within_inode) { + if let Some(directory_block) = + self.access_block_mut::(parent_inode, block_index_within_inode) + { if let Some(entry_index_within_block) = directory_block.block.allocate() { // 如果有空闲空间, 可以分配一个块 let name_len = child_inode_name.len() as u8; @@ -54,7 +63,7 @@ impl AyaFS { name.copy_from_slice(child_inode_name.as_bytes()); let dir_entry = DirectoryEntry { - inode: child_inode_index, + inode: child_inode_index as u32, record_len: 264, name_len, file_type, @@ -66,12 +75,12 @@ impl AyaFS { } } block_index_within_inode += 1; - }; + } } pub(crate) fn get_direntry( &mut self, parent_inode: &Inode, - entry_index: u32 + entry_index: u32, ) -> Option { let block_index_within_inode = (entry_index / 15) as usize; let entry_index_within_block = (entry_index % 15) as usize; @@ -87,13 +96,18 @@ impl AyaFS { } // TODO 实现一个带 cache 的版本 - pub fn lookup_name(&mut self, parent_inode: &Inode, name: &OsStr) -> Result<(u32, u32, Inode), c_int> { + /// 返回 inode_index, inode 在 parent 里的 index, inode 本身 + pub fn lookup_name( + &mut self, + parent_inode: &Inode, + name: &OsStr, + ) -> Result<(u32, u32, Inode), c_int> { let mut entry_index = 0; while entry_index < parent_inode.size { if let Some(entry) = self.get_direntry(parent_inode, entry_index) { if entry.name() == name { let inode = self.get_inode(entry.inode as usize).unwrap().clone(); - return Ok((entry.inode, entry_index, inode)) + return Ok((entry.inode, entry_index, inode)); } } entry_index += 1; -- cgit v1.2.3-70-g09d2