summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mod.rs49
1 files changed, 41 insertions, 8 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 2ea25e7..60b09f2 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -1,15 +1,48 @@
-use crate::disk::inode::INODE_SIZE;
+use crate::block_device::BLOCK_SIZE;
+use crate::disk::inode::{Inode, INODE_SIZE};
use crate::{AyaFS, INODE_PER_BLOCK};
-use std::time::{SystemTime, UNIX_EPOCH};
+use fuser::FileAttr;
+use std::time::{Duration, SystemTime, UNIX_EPOCH};
-impl AyaFS {
- 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 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 =