Skip to content

Commit

Permalink
Fix task_info and fstat
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf2019 committed Mar 7, 2024
1 parent 64a1621 commit b106c0a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/bin/ch3_taskinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use user_lib::{
#[no_mangle]
pub fn main() -> usize {
let t1 = get_time() as usize;
let info = TaskInfo::new();
let mut info = TaskInfo::new();
get_time();
sleep(500);
let t2 = get_time() as usize;
// 注意本次 task info 调用也计入
assert_eq!(0, task_info(&info));
assert_eq!(0, task_info(&mut info));
let t3 = get_time() as usize;
assert!(3 <= info.syscall_times[SYSCALL_GETTIMEOFDAY]);
assert_eq!(1, info.syscall_times[SYSCALL_TASK_INFO]);
Expand All @@ -30,7 +30,7 @@ pub fn main() -> usize {
// 想想为什么 write 调用是两次
println!("string from task info test\n");
let t4 = get_time() as usize;
assert_eq!(0, task_info(&info));
assert_eq!(0, task_info(&mut info));
let t5 = get_time() as usize;
assert!(5 <= info.syscall_times[SYSCALL_GETTIMEOFDAY]);
assert_eq!(2, info.syscall_times[SYSCALL_TASK_INFO]);
Expand Down
4 changes: 2 additions & 2 deletions src/bin/ch6_file1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn main() -> i32 {
let fd = open(fname, OpenFlags::CREATE | OpenFlags::WRONLY);
assert!(fd > 0);
let fd = fd as usize;
let stat: Stat = Stat::new();
let ret = fstat(fd, &stat);
let mut stat: Stat = Stat::new();
let ret = fstat(fd, &mut stat);
assert_eq!(ret, 0);
assert_eq!(stat.mode, StatMode::FILE);
assert_eq!(stat.nlink, 1);
Expand Down
12 changes: 6 additions & 6 deletions src/bin/ch6_file2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ pub fn main() -> i32 {
let (lname0, lname1, lname2) = ("linkname0\0", "linkname1\0", "linkname2\0");
let fd = open(fname, OpenFlags::CREATE | OpenFlags::WRONLY) as usize;
link(fname, lname0);
let stat = Stat::new();
fstat(fd, &stat);
let mut stat = Stat::new();
fstat(fd, &mut stat);
assert_eq!(stat.nlink, 2);
link(fname, lname1);
link(fname, lname2);
fstat(fd, &stat);
fstat(fd, &mut stat);
assert_eq!(stat.nlink, 4);
write(fd, test_str.as_bytes());
close(fd);

unlink(fname);
let fd = open(lname0, OpenFlags::RDONLY) as usize;
let stat2 = Stat::new();
let mut stat2 = Stat::new();
let mut buf = [0u8; 100];
let read_len = read(fd, &mut buf) as usize;
assert_eq!(test_str, core::str::from_utf8(&buf[..read_len]).unwrap(),);
fstat(fd, &stat2);
fstat(fd, &mut stat2);
assert_eq!(stat2.dev, stat.dev);
assert_eq!(stat2.ino, stat.ino);
assert_eq!(stat2.nlink, 3);
unlink(lname1);
unlink(lname2);
fstat(fd, &stat2);
fstat(fd, &mut stat2);
assert_eq!(stat2.nlink, 1);
close(fd);
unlink(lname0);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub fn unlink(path: &str) -> isize {
sys_unlinkat(AT_FDCWD as usize, path, 0)
}

pub fn fstat(fd: usize, st: &Stat) -> isize {
pub fn fstat(fd: usize, st: &mut Stat) -> isize {
sys_fstat(fd, st)
}

Expand Down Expand Up @@ -307,7 +307,7 @@ pub fn pipe(pipe_fd: &mut [usize]) -> isize {
sys_pipe(pipe_fd)
}

pub fn task_info(info: &TaskInfo) -> isize {
pub fn task_info(info: &mut TaskInfo) -> isize {
sys_task_info(info)
}

Expand Down
4 changes: 2 additions & 2 deletions src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn sys_unlinkat(dirfd: usize, path: &str, flags: usize) -> isize {
syscall(SYSCALL_UNLINKAT, [dirfd, path.as_ptr() as usize, flags])
}

pub fn sys_fstat(fd: usize, st: &Stat) -> isize {
pub fn sys_fstat(fd: usize, st: &mut Stat) -> isize {
syscall(SYSCALL_FSTAT, [fd, st as *const _ as usize, 0])
}

Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn sys_pipe(pipe: &mut [usize]) -> isize {
syscall(SYSCALL_PIPE, [pipe.as_mut_ptr() as usize, 0, 0])
}

pub fn sys_task_info(info: &TaskInfo) -> isize {
pub fn sys_task_info(info: &mut TaskInfo) -> isize {
syscall(SYSCALL_TASK_INFO, [info as *const _ as usize, 0, 0])
}

Expand Down

0 comments on commit b106c0a

Please sign in to comment.