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/cached_inode.rs | 87 ++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 29 deletions(-) (limited to 'src/memory/cached_inode.rs') diff --git a/src/memory/cached_inode.rs b/src/memory/cached_inode.rs index b1be2de..aa4f5e3 100644 --- a/src/memory/cached_inode.rs +++ b/src/memory/cached_inode.rs @@ -1,10 +1,8 @@ use crate::disk::block::{DirectoryBlock, DirectoryEntry, InodeBlock}; -use crate::disk::inode::{Inode, InodeMode, INODE_SIZE}; -use crate::utils::from_filetype; +use crate::disk::inode::{Inode, INODE_SIZE}; use crate::{utils, AyaFS}; use and_then_some::BoolExt; -use fuser::FileType; -use log::debug; +use libc::{c_int, EISDIR, ENOENT, ENOTDIR}; impl AyaFS { pub(crate) fn create_file( @@ -22,12 +20,14 @@ impl AyaFS { }) } + /// 根目录的 parent_inode_number 传入 None, 会直接以自己作为 .. 的 inode number pub(crate) fn create_directory( &mut self, permissions: u16, uid: u32, gid: u32, flags: u32, + parent_inode_number: Option, ) -> Option<(usize, &Inode)> { self.inode_bitmap.allocate().map(|inode_index| { // 创建 Inode @@ -53,7 +53,7 @@ impl AyaFS { // add dot dot entry directory_block.block.entries[1] = DirectoryEntry { - inode: 0, // TODO set this as parent inode number + inode: parent_inode_number.unwrap_or(inode_index as u32), record_len: 264, name_len: 2, file_type: 0x2, @@ -70,31 +70,60 @@ impl AyaFS { (inode_index, self.get_inode(inode_index).unwrap()) }) } + + pub(crate) fn remove_file(&mut self, inode_index: usize) -> Result { + if self.inode_bitmap.query(inode_index) { + self.inode_bitmap.deallocate(inode_index); + let (block_index, offset) = self.locate_inode(inode_index); + if let Some(cached_block) = self.cached_inodes.get_block::(block_index) { + let inode = cached_block.block.inodes[offset / INODE_SIZE].clone(); + if !inode.is_file() { + return Err(EISDIR); + } + self.deallocate_all_blocks_for(&inode).unwrap(); + } + Ok(true) + } else { + Err(ENOENT) + } + } + + pub(crate) fn remove_dir(&mut self, inode_index: usize) -> Result { + if self.inode_bitmap.query(inode_index) { + self.inode_bitmap.deallocate(inode_index); + let (block_index, offset) = self.locate_inode(inode_index); + if let Some(cached_block) = self.cached_inodes.get_block::(block_index) { + let inode = cached_block.block.inodes[offset / INODE_SIZE].clone(); + if !inode.is_dir() { + return Err(ENOTDIR); + } + // TODO 递归删除所有下面的 direntry 里的 inode, 注意排除 . 和 .. + } + Ok(true) + } else { + Err(ENOENT) + } + } - pub(crate) fn create_inode( - &mut self, - permissions: u16, - mode: InodeMode, - uid: u32, - gid: u32, - flags: u32, - ) -> Option { - self.inode_bitmap.allocate().map(|inode_index| { - self.get_inode_mut(inode_index).map(|inode| { - *inode = Inode::make_inode( - permissions, - mode, - uid, - gid, - utils::time_now(), - flags, - 0, - 0, - 0, - ); - }); - inode_index - }) + pub(crate) fn remove_inode(&mut self, inode_index: usize) -> bool { + if self.inode_bitmap.query(inode_index) { + self.inode_bitmap.deallocate(inode_index); + let (block_index, offset) = self.locate_inode(inode_index); + if let Some(cached_block) = self.cached_inodes.get_block::(block_index) { + let inode = &cached_block.block.inodes[offset / INODE_SIZE].clone(); + + if inode.is_file() { + self.deallocate_all_blocks_for(inode).unwrap(); + } else if inode.is_dir() { + // TODO 把 dir 下所有子目录 remove 掉 + } else { + unimplemented!("only file and dir are implemented!"); + } + } + true + } else { + false + } } pub(crate) fn update_inode(&mut self, inode_index: usize, inode: Inode) -> bool { -- cgit v1.2.3-70-g09d2