summaryrefslogtreecommitdiff
path: root/src/filesystem/trait_impl.rs
blob: a25a383aa17bdaa808ed634ce9e3189c6b4804a7 (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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::block_device::BLOCK_SIZE;
use crate::disk::inode::InodeMode;
use crate::utils::make_fileattr;
use crate::{utils, AyaFS, TTL};
use fuser::{
    FileAttr, FileType, Filesystem, KernelConfig, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty,
    ReplyEntry, ReplyLseek, ReplyWrite, Request, TimeOrNow,
};
use libc::{c_int, ENAMETOOLONG, ENOENT, ENOSPC, ENOSYS, ENOTDIR};
use log::debug;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::time::SystemTime;
use users::{get_current_gid, get_current_uid};

impl Filesystem for AyaFS {
    fn init(&mut self, _req: &Request<'_>, _config: &mut KernelConfig) -> Result<(), c_int> {
        debug!("Filesystem::init called.");
        Ok(())
    }

    fn destroy(&mut self) {
        debug!("Filesystem::destroy()");
    }

    fn lookup(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEntry) {
        let parent = parent as usize;
        if let Some(inode) = self.get_inode(parent) {
            // debug!("{:?}", inode);
        }
        // if self.inode_active(parent) {
        //     let (block, offset) = self.locate_inode(parent);
        //     let inode = self.get_inode(block, offset);
        //     debug!("{:?}", inode);
        // }
        reply.error(ENOENT);
    }

    fn forget(&mut self, _req: &Request<'_>, _ino: u64, _nlookup: u64) {
        debug!("Filesystem::forget()");
        todo!("This is a dumb implementation")
    }

    fn getattr(&mut self, _req: &Request<'_>, ino: u64, reply: ReplyAttr) {
        debug!("Filesystem::getattr(ino: {})", ino);
        if let Some(inode) = self.get_inode(ino as usize) {
            let attr = FileAttr {
                ino,
                size: inode.size as u64,
                blocks: inode.n_blocks as u64,
                atime: utils::to_systime(inode.atime),
                mtime: utils::to_systime(inode.mtime),
                ctime: utils::to_systime(inode.ctime),
                crtime: utils::to_systime(inode.crtime),
                kind: inode.mode.into(),
                perm: inode.mode.perm(),
                nlink: inode.n_links as u32,
                uid: inode.uid,
                gid: inode.gid,
                rdev: 0,
                blksize: BLOCK_SIZE as u32,
                flags: inode.flags,
            };
            reply.attr(&TTL, &attr);
        } else {
            reply.error(ENOENT);
        }
    }

    fn setattr(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        mode: Option<u32>,
        uid: Option<u32>,
        gid: Option<u32>,
        size: Option<u64>, // TODO 为什么 setattr 还可以设置 size??
        atime: Option<TimeOrNow>,
        mtime: Option<TimeOrNow>,
        ctime: Option<SystemTime>,
        fh: Option<u64>,
        crtime: Option<SystemTime>,
        _chgtime: Option<SystemTime>,
        _bkuptime: Option<SystemTime>,
        flags: Option<u32>,
        reply: ReplyAttr,
    ) {
        if let Some(inode) = self.get_inode_mut(ino as usize) {
            let mode = mode
                .and_then(|mode_value| {
                    InodeMode::validate(mode_value as u16) // high 16 bit truncated
                })
                .unwrap_or(inode.mode);
            inode.mode = mode;
            // 如果传入的 InodeMode 是合法结果, 那么得到 Some(mode), 否则 None
            // 需要检查权限吗? 如果需要, 如何知道 invoke 这个 callback 的 UID/GID?

            let size = size.unwrap_or(inode.size as u64);
            inode.size = size as u32;
            // TODO 为什么可以设置 size 呢……

            let uid = uid.unwrap_or(inode.uid);
            let gid = gid.unwrap_or(inode.gid);
            inode.uid = uid;
            inode.gid = gid;

            let (atime, atime_inner) = atime
                .map(|atime| {
                    let system_time = match atime {
                        TimeOrNow::SpecificTime(system_time) => system_time,
                        TimeOrNow::Now => SystemTime::now(),
                    };
                    (system_time, utils::from_systime(system_time))
                })
                .unwrap_or((utils::to_systime(inode.atime), inode.atime));
            inode.atime = atime_inner;

            let (mtime, mtime_inner) = mtime
                .map(|mtime| {
                    let system_time = match mtime {
                        TimeOrNow::SpecificTime(system_time) => system_time,
                        TimeOrNow::Now => SystemTime::now(),
                    };
                    (system_time, utils::from_systime(system_time))
                })
                .unwrap_or((utils::to_systime(inode.mtime), inode.mtime));
            inode.mtime = mtime_inner;

            let (ctime, ctime_inner) = ctime
                .map(|ctime| (ctime, utils::from_systime(ctime)))
                .unwrap_or((utils::to_systime(inode.ctime), inode.ctime));
            inode.ctime = ctime_inner;

            let (crtime, crtime_inner) = crtime
                .map(|crtime| (crtime, utils::from_systime(crtime)))
                .unwrap_or((utils::to_systime(inode.crtime), inode.crtime));
            inode.crtime = crtime_inner;

            let flags = flags.unwrap_or(inode.flags);
            inode.flags = flags;

            let attr = FileAttr {
                ino,
                size,
                blocks: inode.n_blocks as u64,
                atime,
                mtime,
                ctime,
                crtime,
                kind: mode.into(),
                perm: mode.perm(),
                nlink: inode.n_links as u32,
                uid,
                gid,
                rdev: 0,
                blksize: BLOCK_SIZE as u32,
                flags,
            };
            reply.attr(&TTL, &attr);
        } else {
            reply.error(ENOSYS);
        }
    }

    fn readlink(&mut self, _req: &Request<'_>, ino: u64, reply: ReplyData) {
        debug!("[Not Implemented] readlink(ino: {})", ino);
        reply.error(ENOSYS);
    }

    fn mknod(
        &mut self,
        _req: &Request<'_>,
        parent: u64,
        name: &OsStr,
        mode: u32,
        _umask: u32, // umask 是用不到的
        _rdev: u32,  // the device number (only valid if created file is a device)
        reply: ReplyEntry,
    ) {
        debug!(
            "Filesystem::mknod(parent: {}, name: {:?}, mode: {}, umask: {})",
            parent, name, mode, _umask
        );
        if let Some(inode) = self.get_inode(parent as usize) {
            if inode.is_dir() {
                let name = name.as_bytes();
                if name.len() <= 255 {
                    let permissions = (mode & 0o777) as u16;
                    if let Some(inode_index) = self.create_inode(
                        permissions,
                        InodeMode::IFREG,
                        get_current_uid(),
                        get_current_gid(),
                        0,
                    ) {
                        let inode = self.get_inode(inode_index).unwrap();
                        let file_attr = make_fileattr(inode_index, inode);
                        // TODO 把 inode 挂到 parent 下
                        reply.entry(&TTL, &file_attr, 0);
                    } else {
                        // create_inode 失败 -> no enough space
                        reply.error(ENOSPC);
                    }
                } else {
                    // name 长度超过 255 -> File name too long
                    reply.error(ENAMETOOLONG);
                }
            } else {
                // parent 不是 IFDIR -> Not a directory
                reply.error(ENOTDIR);
            }
        } else {
            // parent 不存在 -> No such file or directory
            reply.error(ENOENT);
        }
    }

    fn mkdir(
        &mut self,
        _req: &Request<'_>,
        parent: u64,
        name: &OsStr,
        mode: u32,
        _umask: u32, // umask 应该也是用不到的
        reply: ReplyEntry,
    ) {
        if let Some(inode) = self.get_inode(parent as usize) {
            if inode.is_dir() {
                let name = name.as_bytes();
                if name.len() <= 255 {
                    let permissions = (mode & 0o777) as u16;
                    if let Some(inode_index) = self.create_inode(
                        permissions,
                        InodeMode::IFDIR,
                        get_current_uid(),
                        get_current_gid(),
                        0,
                    ) {
                        let inode = self.get_inode(inode_index).unwrap();
                        let file_attr = make_fileattr(inode_index, inode);
                        // TODO 把 inode 挂到 parent 下
                        reply.entry(&TTL, &file_attr, 0);
                    } else {
                        // create_inode 失败 -> no enough space
                        reply.error(ENOSPC);
                    }
                } else {
                    // name 长度超过 255 -> File name too long
                    reply.error(ENAMETOOLONG);
                }
            } else {
                // parent 不是 IFDIR -> Not a directory
                reply.error(ENOTDIR);
            }
        } else {
            // parent 不存在 -> No such file or directory
            reply.error(ENOENT);
        }
    }

    fn unlink(&mut self, _req: &Request<'_>, parent: u64, name: &OsStr, reply: ReplyEmpty) {
        debug!(
            "unlink(parent: {:#x?}, name: {:?})",
            parent, name,
        );
        if let Some(inode) = self.get_inode_mut(parent as usize) {
            // TODO 找到这个 inode 并且删掉
            reply.ok();
        } else {
            reply.error(ENOENT);
        }
    }

    fn read(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        _fh: u64,
        offset: i64,
        _size: u32,
        _flags: i32,
        _lock_owner: Option<u64>,
        reply: ReplyData,
    ) {
        todo!()
    }

    fn readdir(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        _fh: u64,
        offset: i64,
        mut reply: ReplyDirectory,
    ) {
        if let Some(inode) = self.get_inode(ino as usize) {
        } else {
            reply.error(ENOENT);
        }

        // if ino != 1 {
        //     reply.error(ENOENT);
        //     return;
        // }
        //
        // let entries = vec![
        //     (1, FileType::Directory, "."),
        //     (1, FileType::Directory, ".."),
        //     (2, FileType::RegularFile, "hello.txt"),
        // ];
        //
        // for (i, entry) in entries.into_iter().enumerate().skip(offset as usize) {
        //     // i + 1 means the index of the next entry
        //     if reply.add(entry.0, (i + 1) as i64, entry.1, entry.2) {
        //         break;
        //     }
        // }
        // reply.ok();
    }

    fn access(&mut self, _req: &Request<'_>, ino: u64, mask: i32, reply: ReplyEmpty) {
        debug!("Filesystem::getattr(ino: {}, mask: {})", ino, mask);
        if let Some(inode) = self.get_inode(ino as usize) {
            reply.ok()
        } else {
            reply.error(ENOENT)
        }
    }

    fn lseek(
        &mut self,
        _req: &Request<'_>,
        ino: u64,
        fh: u64,
        offset: i64,
        whence: i32,
        reply: ReplyLseek,
    ) {
        reply.error(ENOSYS);
    }
}