summaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
blob: 60b09f21cda864a62a3b6a6c8e865b3216bc00c8 (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
use crate::block_device::BLOCK_SIZE;
use crate::disk::inode::{Inode, INODE_SIZE};
use crate::{AyaFS, INODE_PER_BLOCK};
use fuser::FileAttr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub(crate) fn time_now() -> u32 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("How can current time be earlier than UNIX_EPOCH?")
        .as_secs() as u32
}

pub(crate) fn from_systime(system_time: SystemTime) -> u32 {
    system_time
        .duration_since(UNIX_EPOCH)
        .expect("How can current time be earlier than UNIX_EPOCH?")
        .as_secs() as u32
}

pub(crate) fn make_fileattr(inode_index: usize, inode: &Inode) -> FileAttr {
    FileAttr {
        ino: inode_index as u64,
        size: inode.size as u64,
        blocks: inode.n_blocks as u64,
        atime: to_systime(inode.atime),
        mtime: to_systime(inode.atime),
        ctime: to_systime(inode.ctime),
        crtime: 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,
    }
}

pub(crate) fn to_systime(time: u32) -> SystemTime {
    UNIX_EPOCH + Duration::from_secs(time as u64)
}

impl AyaFS {
    /// 输入 inode 编号, 返回它对应的 block number 和 block 内 offset
    pub(crate) fn locate_inode(&self, inode_index: usize) -> (usize, usize) {
        let block_number =
            inode_index / INODE_PER_BLOCK + 1 + self.inode_bitmap.length + self.data_bitmap.length;
        let block_offset = inode_index % INODE_PER_BLOCK * INODE_SIZE;
        (block_number, block_offset)
    }
}