aboutsummaryrefslogtreecommitdiff
path: root/src/client/fs/file.rs
blob: 5d51913fc5d731b489ba051632f108d077944575 (plain)
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
use super::ino::Ino;
use crate::server::blobref::BlobRef;
use fuser::{FileAttr, FileType};
use std::{
    ffi::{OsStr, OsString},
    time::SystemTime,
};

const DEFAULT_PERMISSIONS: u16 = 0o644;

#[derive(Debug)]
pub struct File {
    // Files only have a blobref if they were written to. No blob is created if the
    // files is only `touch`ed. This means empty files will disappear on `umount`.
    pub blobref: Option<BlobRef>,
    parent: Option<Ino>,
    pub attr: FileAttr,
    name: OsString,
}

impl File {
    fn new(ino: Ino, name: &OsStr, kind: FileType) -> Self {
        let now = SystemTime::now();

        let attr = FileAttr {
            ino: ino.into(),
            size: 0,
            blocks: 0,
            atime: now,
            mtime: now,
            ctime: now,
            crtime: now,
            kind,
            perm: DEFAULT_PERMISSIONS,
            nlink: 0,
            uid: 0,
            gid: 0,
            rdev: 0,
            flags: 0,
            blksize: 0,
        };

        File {
            blobref: None,
            parent: None,
            attr,
            name: name.into(),
        }
    }

    pub fn new_regular_file<T: Into<OsString>>(ino: Ino, name: T) -> Self {
        Self::new(ino, &name.into(), FileType::RegularFile)
    }

    pub fn new_directory<T: Into<OsString>>(ino: Ino, name: T) -> Self {
        Self::new(ino, &name.into(), FileType::Directory)
    }

    pub fn set_parent(&mut self, ino: Ino) {
        self.parent = Some(ino);
    }

    pub fn name(&self) -> OsString {
        self.name.clone()
    }

    pub fn parent(&self) -> Option<Ino> {
        self.parent
    }

    pub fn ino(&self) -> Ino {
        self.attr.ino.into()
    }

    pub fn size(&self) -> usize {
        self.attr.size as usize
    }
}