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 { 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::(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::(block_index) .map(|cached_block| { cached_block.dirty = true; // 保守一些, 只要返回了 &mut Inode 这个页一定标记为脏 &mut cached_block.block.inodes[offset / INODE_SIZE] }) }) } }