summaryrefslogtreecommitdiff
path: root/src/disk/inode.rs
blob: f38508a81b1ebe2c10957012c5de4497f8a14084 (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
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
use bitflags::bitflags;

const DIRECT_NUMBER: usize = 15;

#[derive(Debug)]
pub struct InodeMode(u16);

bitflags! {
    impl InodeMode: u16 {
        const IXOTH = 0x0001;
        const IWOTH = 0x0002;
        const IROTH = 0x0004;
        const IXGRP = 0x0008;
        const IWGRP = 0x0010;
        const IRGRP = 0x0020;
        const IXUSR = 0x0040;
        const IWUSR = 0x0080;
        const IRUSR = 0x0100;
        const ISVTX = 0x0200;
        const ISGID = 0x0400;
        const ISUID = 0x0800;
        // These are mutually-exclusive:
        const IFIFO = 0x1000;
        const IFCHR = 0x2000;
        const IFDIR = 0x4000;
        const IFBLK = 0x6000;
        const IFREG = 0x8000;
        const IFLNK = 0xA000;
        const IFSOCK = 0xC000;
    }
}

/// Pretty much the same with ext2, with minor changes:
/// - removed OS dependent attributes (osd1 & osd2)
/// - removed i_faddr since fragmentation is not supported
/// - changed uid and gid from u16 to u32
/// - added more direct blocks for a total size of 128 bytes
/// TODO: do we need to extend time precision?
#[repr(C)]
#[derive(Debug)]
pub struct Inode {
    mode: InodeMode,
    uid: u32,
    size: u32,
    atime: u32, // time in seconds
    ctime: u32,
    mtime: u32,
    dtime: u32,
    gid: u32,
    n_links: u16,
    n_blocks: u32,
    flags: u32, // TODO: do we actually need this? maybe just return 0
    direct: [u32; DIRECT_NUMBER],
    single_indirect: u32,
    double_indirect: u32,
    triple_indirect: u32,
    generation: u32,
    file_acl: u32,
    dir_acl: u32, // TODO do we have to implement ACL......?
}

impl Inode {
    pub fn directory() -> Self {
        Self {
            mode: InodeMode::IFDIR
                | InodeMode::IRUSR
                | InodeMode::IWUSR
                | InodeMode::IXUSR
                | InodeMode::IRGRP
                | InodeMode::IXGRP
                | InodeMode::IROTH
                | InodeMode::IXOTH,
            // Directory, 755 permissions
            uid: 0,
            size: 0,
            atime: 0,
            ctime: 0,
            mtime: 0,
            dtime: 0,
            gid: 0,
            n_links: 0,
            n_blocks: 0,
            flags: 0,
            direct: [0; DIRECT_NUMBER],
            single_indirect: 0,
            double_indirect: 0,
            triple_indirect: 0,
            generation: 0,
            file_acl: 0,
            dir_acl: 0,
        }
    }

    pub fn file() -> Self {
        Self {
            mode: InodeMode::IFREG
                | InodeMode::IRUSR
                | InodeMode::IWUSR
                | InodeMode::IXUSR
                | InodeMode::IRGRP
                | InodeMode::IXGRP
                | InodeMode::IROTH
                | InodeMode::IXOTH,
            // RegularFile, 755 permissions
            uid: 0,
            size: 0,
            atime: 0,
            ctime: 0,
            mtime: 0,
            dtime: 0,
            gid: 0,
            n_links: 0,
            n_blocks: 0,
            flags: 0,
            direct: [0; DIRECT_NUMBER],
            single_indirect: 0,
            double_indirect: 0,
            triple_indirect: 0,
            generation: 0,
            file_acl: 0,
            dir_acl: 0,
        }
    }
}

//
// #[repr(C)]
// #[derive(Debug, Default)]
// pub struct FileInode {
//     file_size: u32,
//     direct_blocks: [u32; DIRECT_NUMBER],
//     indirect_block: u32,
//     doubly_indirect_block: u32,
// } // sizeof(FileInode) == 124 bytes
//
// #[repr(C)]
// #[derive(Debug, Default)]
// pub struct DirectoryInode {
//     child_number: u32,
//     direct_blocks: [u32; DIRECT_NUMBER],
//     indirect_block: u32,
//     doubly_indirect_block: u32,
// } // sizeof(FileInode) == 124 bytes
//
// #[repr(C)]
// #[derive(Debug)]
// pub enum Inode {
//     File(FileInode),
//     Directory(DirectoryInode),
// } // sizeof(Inode) == 128 bytes

pub const INODE_SIZE: usize = std::mem::size_of::<Inode>();