lethe_gadget_fat/
filesystem.rs

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use layout::{io::SeekFrom, ApplyLayout, Frame, Read, Seek, Write, IO};

use crate::{
    block_io::BlockIO,
    schema::{self, FATEntry, FileSystemFrame},
};

#[derive(Debug)]
pub enum FSError<E> {
    ObjNotFound,
    Full,
    OutOfBounds,
    UnexpectedEof,
    WriteZero,
    IO(E),
}

impl<E> From<E> for FSError<E> {
    fn from(e: E) -> Self {
        FSError::IO(e)
    }
}

pub struct FileSystem<S> {
    pub disk: S, // todo pub
}

impl<S: Read + Write + Seek + IO> FileSystem<S> {
    const MAGIC_NUM: u64 = 0x1e0_15_c001;

    pub fn open(disk: S) -> Self {
        Self { disk }
    }

    pub fn create(mut disk: S, block_size: u32) -> Result<Self, S::Error> {
        let block_count = disk.stream_len()? / block_size as u64;

        let mut frame = schema::FileSystem::apply_layout(&mut disk, 0)?;

        let super_block = schema::Superblock {
            magic: Self::MAGIC_NUM,
            block_size,
            block_count,
        };

        frame.set_super_block(&super_block)?;
        frame.fat()?.set_len(block_count)?;
        frame.set_super_block_cp(&super_block)?;

        let padding = block_size as u64 - frame.obj_lookup()?.offset() % block_size as u64;
        let mut obj_lookup = frame.obj_lookup()?;
        obj_lookup.set_len(padding + block_size as u64)?;
        for i in 0..obj_lookup.len() {
            obj_lookup.set(i, FATEntry::None)?;
        }

        let reserved_len = frame.rest()?.offset() / block_size as u64 + 1;
        let mut fat = frame.fat()?;
        for i in 0..reserved_len {
            fat.set(i, FATEntry::Reserved)?;
        }
        for i in reserved_len..block_count {
            fat.set(i, FATEntry::None)?;
        }

        Ok(Self { disk })
    }

    pub fn frame(&mut self) -> Result<FileSystemFrame<'_, S>, S::Error> {
        schema::FileSystem::apply_layout(&mut self.disk, 0)
    }

    pub(crate) fn alloc_block(&mut self) -> Result<u64, FSError<S::Error>> {
        let mut frame = self.frame()?;
        let mut fat = frame.fat()?;

        let free_head = fat.get(0)?.unwrap().ok_or(FSError::Full)?;
        let next_free = fat.get(free_head)?;
        fat.set(0, next_free)?;
        fat.set(free_head, FATEntry::None)?;

        Ok(free_head)
    }

    pub(crate) fn free_block(&mut self, block: u64) -> Result<(), FSError<S::Error>> {
        let mut frame = self.frame()?;
        let mut fat = frame.fat()?;

        let free_head = fat.get(0)?;
        fat.set(block, free_head)?;
        fat.set(0, FATEntry::Block(block))?;

        Ok(())
    }

    pub fn create_object(&mut self, obj_id: u128, size: u64) -> Result<bool, FSError<S::Error>> {
        let mut frame = self.frame()?;
        let mut obj_lookup = frame.obj_lookup()?;

        let hash = obj_id as u64 % obj_lookup.len();
        let bucket_start = obj_lookup.get(hash)?.unwrap();

        let mut new = false;
        let mut bucket_blocks = match bucket_start {
            Some(bucket_start) => BlockIO::from_block(self, bucket_start, false)?,
            None => {
                let bio = BlockIO::create(self, None)?.start_block(); // TODO

                self.frame()?
                    .obj_lookup()?
                    .set(hash, FATEntry::Block(bio))?;

                new = true;

                BlockIO::from_block(self, bio, false)?
            }
        };

        let bucket_start = bucket_blocks.start_block();

        let mut bucket = bucket_blocks.as_frame::<schema::ObjLookupBucket>()?;
        if new {
            bucket.set_len(0)?;
        }

        for i in 0..bucket.len() {
            let entry = bucket.get(i)?;
            if entry.object_id == obj_id {
                return Ok(false);
            }
        }

        let obj_start_block = self.alloc_block()?;
        // let mut oio = BlockIO::from_block(self, obj_start_block, false)?;
        // oio.write_all(&[obj_id as u8].repeat(size as usize))?;

        let mut bucket_blocks = BlockIO::from_block(self, bucket_start, false)?;
        let mut bucket = bucket_blocks.as_frame::<schema::ObjLookupBucket>()?;

        bucket.set_len(bucket.len() + 1)?;
        bucket.set(
            bucket.len() - 1,
            schema::ONode {
                object_id: obj_id,
                size,
                first_block: obj_start_block,
                reserved: Default::default(),
            },
        )?;

        Ok(true)
    }

    pub fn read_exact(
        &mut self,
        obj_id: u128,
        buf: &mut [u8],
        off: u64,
    ) -> Result<(), FSError<S::Error>> {
        let mut frame = self.frame()?;
        let mut obj_lookup = frame.obj_lookup()?;
        let bucket_start = obj_lookup.get(obj_id as u64 % obj_lookup.len())?;

        let bucket_start = match bucket_start {
            FATEntry::Block(b) => b,
            _ => return Err(FSError::ObjNotFound),
        };

        let mut bucket_blocks = BlockIO::from_block(self, bucket_start, true)?;
        let mut bucket = bucket_blocks.as_frame::<schema::ObjLookupBucket>()?;

        let mut data_start = None;
        for i in 0..bucket.len() {
            let entry = bucket.get(i)?;
            if entry.object_id == obj_id {
                data_start = Some(entry.first_block);
                break;
            }
        }

        let mut data_blocks =
            BlockIO::from_block(self, data_start.ok_or(FSError::ObjNotFound)?, true)?;
        data_blocks.seek(SeekFrom::Start(off))?;
        data_blocks.read_exact(buf)?;

        Ok(())
    }

    pub fn write_all(
        &mut self,
        obj_id: u128,
        buf: &[u8],
        off: u64,
    ) -> Result<(), FSError<S::Error>> {
        let mut frame = self.frame()?;
        let mut obj_lookup = frame.obj_lookup()?;
        let bucket_start = obj_lookup.get(obj_id as u64 % obj_lookup.len())?;

        let bucket_start = match bucket_start {
            FATEntry::Block(b) => b,
            _ => return Err(FSError::ObjNotFound),
        };

        let mut bucket_blocks = BlockIO::from_block(self, bucket_start, true)?;
        let mut bucket = bucket_blocks.as_frame::<schema::ObjLookupBucket>()?;

        let mut data_start = None;
        for i in 0..bucket.len() {
            let entry = bucket.get(i)?;
            if entry.object_id == obj_id {
                data_start = Some(entry.first_block);
                break;
            }
        }

        let mut data_blocks =
            BlockIO::from_block(self, data_start.ok_or(FSError::ObjNotFound)?, true)?;
        data_blocks.seek(SeekFrom::Start(off))?;
        data_blocks.write_all(buf)?;

        Ok(())
    }
}