summaryrefslogtreecommitdiff
path: root/src/memory/cached_inode.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-26 02:33:01 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-26 02:33:01 -0800
commit9d1368b0ea380a9446b4697af668d1685464b6c7 (patch)
tree2103af418264cc70addc9cfc6ed7acac640f5151 /src/memory/cached_inode.rs
parent777d5e01a34b8ebe6f1a5751b593266f93e88499 (diff)
downloadmyfs-9d1368b0ea380a9446b4697af668d1685464b6c7.tar.gz
myfs-9d1368b0ea380a9446b4697af668d1685464b6c7.zip
more code idk what they are im so tired ohno
Diffstat (limited to 'src/memory/cached_inode.rs')
-rw-r--r--src/memory/cached_inode.rs87
1 files changed, 58 insertions, 29 deletions
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<u32>,
) -> 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<bool, c_int> {
+ 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::<InodeBlock>(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<bool, c_int> {
+ 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::<InodeBlock>(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<usize> {
- 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::<InodeBlock>(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 {