From 7af0771f9a3031acc36a6990d07bdb92a61c0c75 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Wed, 29 Nov 2023 03:29:34 -0800 Subject: symlink, probably not working --- src/memory/cached_inode.rs | 92 ++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 56 deletions(-) (limited to 'src/memory/cached_inode.rs') diff --git a/src/memory/cached_inode.rs b/src/memory/cached_inode.rs index a9c92f5..441d0fb 100644 --- a/src/memory/cached_inode.rs +++ b/src/memory/cached_inode.rs @@ -1,8 +1,14 @@ -use crate::disk::block::{DirectoryBlock, DirectoryEntry, InodeBlock}; +use std::ffi::OsStr; +use std::os::unix::ffi::OsStrExt; +use std::path::Path; +use std::slice; +use crate::disk::block::InodeBlock; use crate::disk::inode::{Inode, INODE_SIZE}; use crate::{utils, AyaFS}; use and_then_some::BoolExt; -use libc::{c_int, EISDIR, ENOENT, ENOTDIR, ENOTEMPTY}; +use fuser::FileType; +use libc::{c_int, EIO, EISDIR, ENOENT, ENOTDIR, ENOTEMPTY}; +use log::{debug, error}; impl AyaFS { pub(crate) fn create_file( @@ -20,6 +26,21 @@ impl AyaFS { }) } + pub(crate) fn create_symlink( + &mut self, + permissions: u16, + uid: u32, + gid: u32, + flags: u32, + ) -> Option<(usize, &Inode)> { + self.inode_bitmap.allocate().map(|inode_index| { + self.get_inode_mut(inode_index).map(|inode| { + *inode = Inode::symlink(permissions, uid, gid, utils::time_now(), flags, 0, 0, 0); + }); + (inode_index, self.get_inode(inode_index).unwrap()) + }) + } + /// 根目录的 parent_inode_number 传入 None, 会直接以自己作为 .. 的 inode number pub(crate) fn create_directory( &mut self, @@ -44,36 +65,6 @@ impl AyaFS { 0x2, ) .unwrap(); - - // // 分配第一个 direct block - // (new_inode.direct[0], _) = self.allocate_block_for(&mut new_inode).unwrap(); - // new_inode.size = 2; - // // 在 direct block 里分配 . 和 .. - // if let Some(directory_block) = - // self.get_block_mut::(new_inode.direct[0] as usize) - // { - // let dot = '.'.to_ascii_lowercase() as u8; - // // add dot entry - // directory_block.block.entries[0] = DirectoryEntry { - // inode: inode_index as u32, - // record_len: 264, - // name_len: 1, - // file_type: 0x2, - // name: [0; 256], - // }; - // directory_block.block.entries[0].name[0] = dot; - // - // // add dot dot entry - // directory_block.block.entries[1] = DirectoryEntry { - // inode: parent_inode_number.unwrap_or(inode_index as u32), - // record_len: 264, - // name_len: 2, - // file_type: 0x2, - // name: [0; 256], - // }; - // directory_block.block.entries[1].name[0] = dot; - // directory_block.block.entries[1].name[1] = dot; - // } // 把 inode 放到指定位置 self.get_inode_mut(inode_index).map(|inode| { *inode = new_inode; @@ -88,10 +79,16 @@ impl AyaFS { 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); + match inode.file_type() { + FileType::RegularFile => self.deallocate_all_blocks_for(&inode).unwrap(), + FileType::Symlink => { + if inode.size >= 60 { + self.deallocate_all_blocks_for(&inode).unwrap(); + } + }, + FileType::Directory => return Err(EISDIR), + _ => return Err(EIO), } - self.deallocate_all_blocks_for(&inode).unwrap(); } self.inode_bitmap.deallocate(inode_index); Ok(true) @@ -125,27 +122,6 @@ impl AyaFS { } } - 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 { if self.inode_bitmap.query(inode_index) { let (block_index, offset) = self.locate_inode(inode_index); @@ -162,6 +138,7 @@ impl AyaFS { pub(crate) fn get_inode(&mut self, inode_index: usize) -> Option<&Inode> { self.inode_bitmap.query(inode_index).and_then(|| { let (block_index, offset) = self.locate_inode(inode_index); + // debug!("get_inode(inode_index: {}) -> block_index: {}, offset: {}", inode_index, block_index, offset); self.cached_inodes .get_block::(block_index) .map(|cached_block| &cached_block.block.inodes[offset / INODE_SIZE]) @@ -171,6 +148,7 @@ impl AyaFS { pub(crate) fn get_inode_mut(&mut self, inode_index: usize) -> Option<&mut Inode> { self.inode_bitmap.query(inode_index).and_then(|| { let (block_index, offset) = self.locate_inode(inode_index); + // debug!("get_inode_mut(inode_index: {}) -> block_index: {}, offset: {}", inode_index, block_index, offset); self.cached_inodes .get_block_mut::(block_index) .map(|cached_block| { @@ -180,6 +158,7 @@ impl AyaFS { }) } + #[allow(unused)] pub(crate) fn peek_inode(&self, inode_index: usize) -> Option<&Inode> { self.inode_bitmap.query(inode_index).and_then(|| { let (block_index, offset) = self.locate_inode(inode_index); @@ -189,6 +168,7 @@ impl AyaFS { }) } + #[allow(unused)] pub(crate) fn peek_inode_mut(&mut self, inode_index: usize) -> Option<&mut Inode> { self.inode_bitmap.query(inode_index).and_then(|| { let (block_index, offset) = self.locate_inode(inode_index); -- cgit v1.2.3-70-g09d2