summaryrefslogtreecommitdiff
path: root/src/disk/block.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/disk/block.rs')
-rw-r--r--src/disk/block.rs119
1 files changed, 56 insertions, 63 deletions
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<usize> {
- 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<usize> {
- 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<usize> {
- 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<usize> {
- 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<usize> {
- todo!()
- }
-}