summaryrefslogtreecommitdiff
path: root/src/memory/cached_inode.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
commit76ac602c3d79bb39c133c81a38425a77bc0b8b1f (patch)
treea1de9a03d16bd38fc6ab6b9568d2df562a913d96 /src/memory/cached_inode.rs
parentb8afa7cfb02b32278e268924e189170496f81c1b (diff)
downloadmyfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.tar.gz
myfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.zip
Some FUSE callbacks, some POSIX interface implementation
Diffstat (limited to 'src/memory/cached_inode.rs')
-rw-r--r--src/memory/cached_inode.rs71
1 files changed, 67 insertions, 4 deletions
diff --git a/src/memory/cached_inode.rs b/src/memory/cached_inode.rs
index dd5e5c3..be87850 100644
--- a/src/memory/cached_inode.rs
+++ b/src/memory/cached_inode.rs
@@ -1,10 +1,76 @@
-use crate::disk::block::InodeBlock;
+use crate::disk::block::{DirectoryBlock, DirectoryEntry, InodeBlock};
use crate::disk::inode::{Inode, InodeMode, INODE_SIZE};
+use crate::utils::from_filetype;
use crate::{utils, AyaFS};
use and_then_some::BoolExt;
+use fuser::FileType;
use log::debug;
impl AyaFS {
+ pub(crate) fn create_file(
+ &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::file(permissions, uid, gid, utils::time_now(), flags, 0, 0, 0);
+ });
+ (inode_index, self.get_inode(inode_index).unwrap())
+ })
+ }
+
+ pub(crate) fn create_directory(
+ &mut self,
+ permissions: u16,
+ uid: u32,
+ gid: u32,
+ flags: u32,
+ ) -> Option<(usize, &Inode)> {
+ self.inode_bitmap.allocate().map(|inode_index| {
+ // 创建 Inode
+ let mut new_inode =
+ Inode::directory(permissions, uid, gid, utils::time_now(), flags, 0, 0, 0);
+ // 分配第一个 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::<DirectoryBlock>(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: 0, // TODO set this as parent inode number
+ 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;
+ });
+
+ (inode_index, self.get_inode(inode_index).unwrap())
+ })
+ }
+
pub(crate) fn create_inode(
&mut self,
permissions: u16,
@@ -13,9 +79,7 @@ impl AyaFS {
gid: u32,
flags: u32,
) -> Option<usize> {
- debug!("create inode");
self.inode_bitmap.allocate().map(|inode_index| {
- debug!("creating inode");
self.get_inode_mut(inode_index).map(|inode| {
*inode = Inode::make_inode(
permissions,
@@ -29,7 +93,6 @@ impl AyaFS {
0,
);
});
- debug!("inode created");
inode_index
})
}