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
use alloc::string::String;

#[repr(C)]
pub struct DirTable {
    pub items : &'static mut [DirItem],
}

#[repr(C)] // 32 字节
#[derive(Debug, Clone, Copy)]
pub struct DirItem {
    pub name : [u8;15],
    pub attr : Attribute,
    pub start_block : u64,
    pub length : u64,
}

impl DirItem {
    pub fn new_file(filename : &String, start_block:usize, length:usize)->Self {
        let mut name = [0;15];
        for (idx, c) in filename.as_bytes().iter().enumerate() {
            if idx >= 15 {
                break;
            }
            name[idx] = *c;
        }
        Self {
            name,
            attr:Attribute::File,
            start_block:start_block as u64,
            length: length as u64,
        }
    }

    pub fn empty(&self)->bool {
        self.attr == Attribute::Free
    }

    pub fn new_dir(dirname : &String, start_block:usize, length:usize)->Self {
        assert!(dirname.len() <= 15);
        let mut name = [0;15];
        for (idx, c) in dirname.as_bytes().iter().enumerate() {
            name[idx] = *c;
        }
        Self {
            name,
            attr:Attribute::Directory,
            start_block:start_block as u64,
            length: length as u64,
        }
    }

    pub fn is_file(&self)->bool {
        self.attr == Attribute::File
    }

    pub fn is_dir(&self)->bool {
        self.attr == Attribute::Directory
    }
}


#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub enum Attribute {
    Free = 0,
    File = 1,
    Directory = 1 << 1,
}