summaryrefslogtreecommitdiff
path: root/src/disk/data_block.rs
blob: 2160b9195e5f56bedbbe8cf7ace09da9623c930d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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<usize> {
        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<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
        }
    }
}

#[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<usize> {
        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<usize> {
        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<usize> {
        todo!()
    }
}