summaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-25 02:13:22 -0800
commit76ac602c3d79bb39c133c81a38425a77bc0b8b1f (patch)
treea1de9a03d16bd38fc6ab6b9568d2df562a913d96 /src/utils/mod.rs
parentb8afa7cfb02b32278e268924e189170496f81c1b (diff)
downloadmyfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.tar.gz
myfs-76ac602c3d79bb39c133c81a38425a77bc0b8b1f.zip
Some FUSE callbacks, some POSIX interface implementation
Diffstat (limited to 'src/utils/mod.rs')
-rw-r--r--src/utils/mod.rs61
1 files changed, 60 insertions, 1 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 60b09f2..0a9b825 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -1,7 +1,10 @@
+mod constants;
+pub mod permissions;
+
use crate::block_device::BLOCK_SIZE;
use crate::disk::inode::{Inode, INODE_SIZE};
use crate::{AyaFS, INODE_PER_BLOCK};
-use fuser::FileAttr;
+use fuser::{FileAttr, FileType};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
pub(crate) fn time_now() -> u32 {
@@ -42,6 +45,62 @@ pub(crate) fn to_systime(time: u32) -> SystemTime {
UNIX_EPOCH + Duration::from_secs(time as u64)
}
+// File type code, one of:
+// 0x0 Unknown.
+// 0x1 Regular file.
+// 0x2 Directory.
+// 0x3 Character device file.
+// 0x4 Block device file.
+// 0x5 FIFO.
+// 0x6 Socket.
+// 0x7 Symbolic link.
+
+pub(crate) fn from_filetype(file_type: FileType) -> u8 {
+ match file_type {
+ FileType::NamedPipe => 0x5,
+ FileType::CharDevice => 0x3,
+ FileType::BlockDevice => 0x4,
+ FileType::Directory => 0x2,
+ FileType::RegularFile => 0x1,
+ FileType::Symlink => 0x7,
+ FileType::Socket => 0x6,
+ }
+}
+
+pub(crate) fn to_filetype(file_type: u8) -> Option<FileType> {
+ match file_type {
+ 0x0 => None,
+ 0x1 => Some(FileType::RegularFile),
+ 0x2 => Some(FileType::Directory),
+ 0x3 => Some(FileType::CharDevice),
+ 0x4 => Some(FileType::BlockDevice),
+ 0x5 => Some(FileType::NamedPipe),
+ 0x6 => Some(FileType::Socket),
+ 0x7 => Some(FileType::Symlink),
+ _ => panic!("bad filetype"),
+ }
+}
+
+pub(crate) fn to_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.mtime),
+ 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,
+ }
+}
+
impl AyaFS {
/// 输入 inode 编号, 返回它对应的 block number 和 block 内 offset
pub(crate) fn locate_inode(&self, inode_index: usize) -> (usize, usize) {