From 95d8d84eef645b52d92fd3fb8fdea7aed1f6d474 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Fri, 17 Nov 2023 11:40:33 -0800 Subject: Layer 1 incomplete --- src/disk/data_block.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/disk/data_block.rs (limited to 'src/disk/data_block.rs') diff --git a/src/disk/data_block.rs b/src/disk/data_block.rs new file mode 100644 index 0000000..2160b91 --- /dev/null +++ b/src/disk/data_block.rs @@ -0,0 +1,94 @@ +pub trait Block: Default {} + +#[derive(Default)] +pub struct DataBlock([u8; 4096]); +impl Block for DataBlock {} + +const FULL_MAP: u32 = 0b111_111_111_111_111; + +#[derive(Default)] +pub struct DirectoryBlock { + entries: [[u8; 256]; 15], + inode_ids: [usize; 15], + occupancy_map: u32, + reserved: [u8; 132], +} + +impl Block for DirectoryBlock {} + +impl DirectoryBlock { + fn vacant(&self) -> bool { + self.occupancy_map & FULL_MAP != FULL_MAP + } + + fn first_free(&self) -> Option { + todo!() + } + + fn mark_busy(&mut self, entry_id: usize) { + todo!() + } + + /// 需要判断 entry_name.len() <= 255 + pub fn write_entry(&mut self, entry_name: &[u8], entry_inode_id: usize) -> Option { + if let Some(entry_id) = self.first_free() { + self.mark_busy(entry_id); + self.entries[entry_id].copy_from_slice(entry_name); + self.inode_ids[entry_id] = entry_inode_id; + Some(entry_id) + } else { + None + } + } +} + +#[derive(Default)] +pub struct IndirectBlock { + pub entries: [u32; 1024], +} + +impl Block for IndirectBlock {} + +impl IndirectBlock { + pub fn full(&self) -> bool { + todo!() + } + + pub fn allocate(&mut self) -> Option { + todo!() + } +} + +#[derive(Default)] +pub struct DoubleIndirectBlock { + pub indirect: [u32; 1024], +} + +impl Block for DoubleIndirectBlock {} + +impl DoubleIndirectBlock { + pub fn full(&self) -> bool { + todo!() + } + + pub fn allocate(&mut self) -> Option { + todo!() + } +} + +#[derive(Default)] +pub struct TripleIndirectBlock { + pub double_indirect: [u32; 1024], +} + +impl Block for TripleIndirectBlock {} + +impl TripleIndirectBlock { + pub fn full(&self) -> bool { + todo!() + } + + pub fn allocate(&mut self) -> Option { + todo!() + } +} -- cgit v1.2.3-70-g09d2