summaryrefslogtreecommitdiff
path: root/src/memory/cached_inode.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-18 02:43:01 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-18 02:43:01 -0800
commit886df6daf6bb6b922276157dba1cc099e897a9ea (patch)
tree300b135bddd8ce8631dfd3ec45a9bf3d021a24df /src/memory/cached_inode.rs
parentcd0163da154367f5437ae1423bc97c450d74adf7 (diff)
downloadmyfs-886df6daf6bb6b922276157dba1cc099e897a9ea.tar.gz
myfs-886df6daf6bb6b922276157dba1cc099e897a9ea.zip
Major refactor of file hierarchy
Diffstat (limited to 'src/memory/cached_inode.rs')
-rw-r--r--src/memory/cached_inode.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/memory/cached_inode.rs b/src/memory/cached_inode.rs
new file mode 100644
index 0000000..b51d279
--- /dev/null
+++ b/src/memory/cached_inode.rs
@@ -0,0 +1,51 @@
+use and_then_some::BoolExt;
+use crate::AyaFS;
+use crate::disk::data_block::InodeBlock;
+use crate::disk::inode::{Inode, INODE_SIZE, InodeMode};
+
+impl AyaFS {
+ 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,
+ Self::time_now(),
+ flags,
+ 0,
+ 0,
+ 0,
+ );
+ });
+ inode_index
+ })
+ }
+
+ 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);
+ self.get_block::<InodeBlock>(block_index)
+ .map(|cached_block| &cached_block.block.inodes[offset / INODE_SIZE])
+ })
+ }
+
+ 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);
+ self.get_block_mut::<InodeBlock>(block_index)
+ .map(|cached_block| {
+ cached_block.dirty = true; // 保守一些, 只要返回了 &mut Inode 这个页一定标记为脏
+ &mut cached_block.block.inodes[offset / INODE_SIZE]
+ })
+ })
+ }
+} \ No newline at end of file