summaryrefslogtreecommitdiff
path: root/ayafs/src/utils
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
commitfd125947c9db0b33761414e65e919f73d9bf1815 (patch)
treec4c66d95ba85601427928aa7f23659590055d464 /ayafs/src/utils
parent1eac97eea4ec0bcef0be061a2cba93a584355283 (diff)
downloadmyfs-fd125947c9db0b33761414e65e919f73d9bf1815.tar.gz
myfs-fd125947c9db0b33761414e65e919f73d9bf1815.zip
Refactor workspace
Diffstat (limited to 'ayafs/src/utils')
-rw-r--r--ayafs/src/utils/constants.rs1
-rw-r--r--ayafs/src/utils/mod.rs92
-rw-r--r--ayafs/src/utils/permissions.rs62
3 files changed, 0 insertions, 155 deletions
diff --git a/ayafs/src/utils/constants.rs b/ayafs/src/utils/constants.rs
deleted file mode 100644
index 8b13789..0000000
--- a/ayafs/src/utils/constants.rs
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/ayafs/src/utils/mod.rs b/ayafs/src/utils/mod.rs
deleted file mode 100644
index 468ebdb..0000000
--- a/ayafs/src/utils/mod.rs
+++ /dev/null
@@ -1,92 +0,0 @@
-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, FileType};
-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 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) {
- 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)
- }
-}
diff --git a/ayafs/src/utils/permissions.rs b/ayafs/src/utils/permissions.rs
deleted file mode 100644
index 6773511..0000000
--- a/ayafs/src/utils/permissions.rs
+++ /dev/null
@@ -1,62 +0,0 @@
-use crate::disk::inode::InodeMode;
-use libc::{F_OK, S_ISGID, S_ISUID, S_IXGRP, X_OK};
-use std::fs::File;
-use std::io::BufRead;
-
-pub(crate) fn get_groups(pid: u32) -> Vec<u32> {
- let file = File::open(format!("/proc/{pid}/task/{pid}/status"))
- .expect(format!("pid {pid} incorrect!").as_str());
- for line in std::io::BufReader::new(file).lines() {
- let line = line.unwrap();
- if line.starts_with("Groups:") {
- return line["Groups: ".len()..]
- .split(' ')
- .filter(|x| !x.trim().is_empty())
- .map(|x| x.parse::<u32>().unwrap())
- .collect();
- }
- }
-
- Vec::new()
-}
-
-pub(crate) fn clear_suid_sgid(mut mode: InodeMode) -> InodeMode {
- mode.0 &= !S_ISUID as u16;
- if mode.0 & S_IXGRP as u16 != 0 {
- mode.0 &= !S_ISGID as u16;
- }
- mode
-}
-
-pub(crate) fn check_access(
- incoming_uid: u32,
- incoming_gid: u32,
- uid: u32,
- gid: u32,
- mode: InodeMode,
- mut mask: i32,
-) -> bool {
- if mask == F_OK {
- return true;
- }
-
- let perm = i32::from(mode.0);
- // root
- if incoming_uid == 0 {
- // 读写任何东西都是可以的, 执行只有 IXOTH/IXGRP/IXUSR 之一设置才可以
- mask &= X_OK;
- mask -= mask & (perm >> 6);
- mask -= mask & (perm >> 3);
- mask -= mask & perm;
- return mask == 0;
- }
-
- if incoming_uid == uid {
- mask -= mask & (perm >> 6);
- } else if incoming_gid == gid {
- mask -= mask & (perm >> 3);
- } else {
- mask -= mask & perm;
- }
- mask == 0
-}