Compare commits
2 commits
3a57c25f66
...
0c951ff896
Author | SHA1 | Date | |
---|---|---|---|
0c951ff896 | |||
ef2094cc69 |
3 changed files with 68 additions and 0 deletions
|
@ -149,3 +149,59 @@ fn efs_test() -> std::io::Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mac_test() -> std::io::Result<()> {
|
||||||
|
const BLOCK_SZ: usize = 512;
|
||||||
|
|
||||||
|
let block_file = Arc::new(BlockFile(Mutex::new({
|
||||||
|
let f = OpenOptions::new()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.open("target/fs_mac.img")?;
|
||||||
|
f.set_len(8192 * BLOCK_SZ).unwrap();
|
||||||
|
f
|
||||||
|
})));
|
||||||
|
|
||||||
|
EasyFileSystem::create(block_file.clone(), 4096, 1);
|
||||||
|
let efs = EasyFileSystem::open(block_file.clone());
|
||||||
|
let root_inode = EasyFileSystem::root_inode(&efs);
|
||||||
|
|
||||||
|
root_inode.create("root_file");
|
||||||
|
root_inode.create("public_file");
|
||||||
|
|
||||||
|
let secret_inode = root_inode.find("root_file").unwrap();
|
||||||
|
secret_inode.write_at(0, b"TOP SECRET: root only!");
|
||||||
|
let public_inode = root_inode.find("public_file").unwrap();
|
||||||
|
public_inode.write_at(0, b"This file is public.");
|
||||||
|
|
||||||
|
let check_permission = |user: &str, filename: &str| -> bool {
|
||||||
|
if filename == "root_file" && user != "root" {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let users = ["root", "nonroot"];
|
||||||
|
for user in users.iter() {
|
||||||
|
println!("{} task:", user);
|
||||||
|
|
||||||
|
for filename in ["root_file", "public_file"].iter() {
|
||||||
|
if check_permission(user, filename) {
|
||||||
|
let inode = root_inode.find(filename).unwrap();
|
||||||
|
let mut buf = [0u8; 128];
|
||||||
|
let len = inode.read_at(0, &mut buf);
|
||||||
|
let content = core::str::from_utf8(&buf[..len]).unwrap();
|
||||||
|
println!("{}: Opened successfully");
|
||||||
|
} else {
|
||||||
|
println!("{}: Permission denied");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -47,6 +47,13 @@ pub fn sys_open(path: *const u8, flags: u32) -> isize {
|
||||||
let task = current_task().unwrap();
|
let task = current_task().unwrap();
|
||||||
let token = current_user_token();
|
let token = current_user_token();
|
||||||
let path = translated_str(token, path);
|
let path = translated_str(token, path);
|
||||||
|
|
||||||
|
// 简单用户检查示例:非 root 用户不能打开 /root 下文件
|
||||||
|
let username = task.inner_exclusive_access().user.clone();
|
||||||
|
if path.starts_with("/root") && username != "root" {
|
||||||
|
return -1; // Permission denied
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(inode) = open_file(path.as_str(), OpenFlags::from_bits(flags).unwrap()) {
|
if let Some(inode) = open_file(path.as_str(), OpenFlags::from_bits(flags).unwrap()) {
|
||||||
let mut inner = task.inner_exclusive_access();
|
let mut inner = task.inner_exclusive_access();
|
||||||
let fd = inner.alloc_fd();
|
let fd = inner.alloc_fd();
|
||||||
|
@ -57,6 +64,7 @@ pub fn sys_open(path: *const u8, flags: u32) -> isize {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn sys_close(fd: usize) -> isize {
|
pub fn sys_close(fd: usize) -> isize {
|
||||||
let task = current_task().unwrap();
|
let task = current_task().unwrap();
|
||||||
let mut inner = task.inner_exclusive_access();
|
let mut inner = task.inner_exclusive_access();
|
||||||
|
|
|
@ -30,6 +30,9 @@ pub struct TaskControlBlockInner {
|
||||||
pub children: Vec<Arc<TaskControlBlock>>,
|
pub children: Vec<Arc<TaskControlBlock>>,
|
||||||
pub exit_code: i32,
|
pub exit_code: i32,
|
||||||
pub fd_table: Vec<Option<Arc<dyn File + Send + Sync>>>,
|
pub fd_table: Vec<Option<Arc<dyn File + Send + Sync>>>,
|
||||||
|
|
||||||
|
// New: User
|
||||||
|
pub user: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TaskControlBlockInner {
|
impl TaskControlBlockInner {
|
||||||
|
@ -166,6 +169,7 @@ impl TaskControlBlock {
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
exit_code: 0,
|
exit_code: 0,
|
||||||
fd_table: new_fd_table,
|
fd_table: new_fd_table,
|
||||||
|
user: username.to_string(), // Init User name
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue