From 76ac602c3d79bb39c133c81a38425a77bc0b8b1f Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Sat, 25 Nov 2023 02:13:22 -0800 Subject: Some FUSE callbacks, some POSIX interface implementation --- src/disk/block.rs | 119 +++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 63 deletions(-) (limited to 'src/disk/block.rs') diff --git a/src/disk/block.rs b/src/disk/block.rs index d287ee6..0058b3e 100644 --- a/src/disk/block.rs +++ b/src/disk/block.rs @@ -1,7 +1,10 @@ use crate::disk::inode::Inode; +use std::ffi::{OsStr, OsString}; +use std::os::unix::ffi::OsStrExt; pub trait Block: Default + Clone {} +#[repr(C)] #[derive(Clone)] pub struct DataBlock(pub(crate) [u8; 4096]); @@ -13,6 +16,7 @@ impl Default for DataBlock { impl Block for DataBlock {} +#[repr(C)] #[derive(Clone)] pub struct InodeBlock { pub(crate) inodes: [Inode; 16], @@ -47,53 +51,70 @@ impl Block for InodeBlock {} const FULL_MAP: u32 = 0b111_111_111_111_111; +#[repr(C)] #[derive(Clone)] -pub struct DirectoryBlock { - entries: [[u8; 256]; 15], - inode_ids: [usize; 15], - occupancy_map: u32, - reserved: [u8; 132], +pub struct DirectoryEntry { + pub inode: u32, + pub record_len: u16, + pub name_len: u8, + pub file_type: u8, + pub name: [u8; 256], } -impl Default for DirectoryBlock { +impl DirectoryEntry { + pub(crate) fn name(&self) -> OsString { + let name = &self.name[0..self.name_len as usize]; + OsStr::from_bytes(name).to_os_string() + } +} + +impl Default for DirectoryEntry { fn default() -> Self { Self { - entries: [[0; 256]; 15], - inode_ids: [0; 15], - occupancy_map: 0, - reserved: [0xFF; 132], + inode: 0, + record_len: 0, + name_len: 0, + file_type: 0x0, + name: [0; 256], } } } -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!() - } +#[repr(C)] +#[derive(Clone)] +pub struct DirectoryBlock { + pub entries: [DirectoryEntry; 15], + reserved: [u8; 136], +} - /// 需要判断 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 +impl Default for DirectoryBlock { + fn default() -> Self { + Self { + entries: [ + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + DirectoryEntry::default(), + ], + reserved: [0xFF; 136], } } } +impl Block for DirectoryBlock {} + +#[repr(C)] #[derive(Clone)] pub struct IndirectBlock { pub entries: [u32; 1024], @@ -107,16 +128,7 @@ impl Default for IndirectBlock { impl Block for IndirectBlock {} -impl IndirectBlock { - pub fn full(&self) -> bool { - todo!() - } - - pub fn allocate(&mut self) -> Option { - todo!() - } -} - +#[repr(C)] #[derive(Clone)] pub struct DoubleIndirectBlock { pub indirect: [u32; 1024], @@ -132,16 +144,7 @@ impl Default for DoubleIndirectBlock { impl Block for DoubleIndirectBlock {} -impl DoubleIndirectBlock { - pub fn full(&self) -> bool { - todo!() - } - - pub fn allocate(&mut self) -> Option { - todo!() - } -} - +#[repr(C)] #[derive(Clone)] pub struct TripleIndirectBlock { pub double_indirect: [u32; 1024], @@ -156,13 +159,3 @@ impl Default for TripleIndirectBlock { } 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