Skip to content

Commit

Permalink
stat: Simplify syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Oct 20, 2023
1 parent fa522fa commit 72f2ca1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
10 changes: 9 additions & 1 deletion cykusz-rs/src/kernel/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,15 @@ pub fn lookup_by_path_at(
path: Path,
lookup_mode: LookupMode,
) -> Result<DirEntryItem> {
lookup_by_path_from(path, lookup_mode, dir, false, 0)
if let Some(cur) = if !path.is_absolute() {
Some(dir)
} else {
root_dentry().cloned()
} {
lookup_by_path_from(path, lookup_mode, cur, false, 0)
} else {
return Err(FsError::NotSupported);
}
}

pub fn lookup_by_path(path: Path, lookup_mode: LookupMode) -> Result<DirEntryItem> {
Expand Down
4 changes: 1 addition & 3 deletions cykusz-rs/src/kernel/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64)
SYS_PIPE => sys::sys_pipe(a, b),
SYS_DUP => sys::sys_dup(a, b),
SYS_DUP2 => sys::sys_dup2(a, b, c),
SYS_STAT => sys::sys_stat(a, b, c),
SYS_FSTAT => sys::sys_fstat(a, b),
SYS_STAT => sys::sys_stat(a, b, c, d),
SYS_GETRLIMIT => sys::sys_getrlimit(a, b),
SYS_DEBUG => sys::sys_debug(a, b),
SYS_ACCESS => sys::sys_access(a, b, c, d, e),
Expand All @@ -88,7 +87,6 @@ pub fn syscall_handler(num: u64, a: u64, b: u64, c: u64, d: u64, e: u64, f: u64)
SYS_TICKSNS => sys::sys_ticksns(),
SYS_GETPPID => sys::sys_getppid(),
SYS_GETPGID => sys::sys_getpgid(a),
SYS_FSTATAT => sys::sys_fstatat(a, b, c, d),
a => {
logln!("NO SYS????? {}", a);
Err(SyscallError::ENOSYS)
Expand Down
48 changes: 13 additions & 35 deletions cykusz-rs/src/kernel/syscall/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,43 +1087,17 @@ pub fn sys_futex_wake(uaddr: u64) -> SyscallResult {
crate::kernel::futex::futex().wake(uaddr)
}

pub fn sys_stat(path: u64, path_len: u64, stat: u64) -> SyscallResult {
let _str = make_str(path, path_len);
let path = Path::new(make_str(path, path_len));

let stat = unsafe { VirtAddr(stat as usize).read_mut::<syscall_defs::stat::Stat>() };

let inode = lookup_by_path(path, LookupMode::None)?;

*stat = inode.inode().stat()?;

logln!("stat {}, {:?}", _str, stat);

Ok(0)
}

pub fn sys_fstat(fd: u64, stat: u64) -> SyscallResult {
let file = current_task_ref()
.filetable()
.get_handle(fd as usize)
.ok_or(SyscallError::EBADFD)?;

let stat = unsafe { VirtAddr(stat as usize).read_mut::<syscall_defs::stat::Stat>() };

*stat = file.inode.inode().stat()?;

logln!("fstat {}, {:?}", fd, stat);

Ok(0)
}

pub fn sys_fstatat(fd: u64, path: u64, path_len: u64, stat: u64) -> SyscallResult {
pub fn sys_stat(fd: u64, path: u64, path_len: u64, stat: u64) -> SyscallResult {
let fd = OpenFD::try_from(fd)?;
let path = Path::new(make_str(path, path_len));
let path = if path != 0 {
Some(Path::new(make_str(path, path_len)))
} else {
None
};

let task = current_task_ref();

let dirent = match fd {
let file_dir = match fd {
OpenFD::Fd(fd) => {
task.get_handle(fd).ok_or(SyscallError::EBADFD)?.inode.clone()
}
Expand All @@ -1137,9 +1111,13 @@ pub fn sys_fstatat(fd: u64, path: u64, path_len: u64, stat: u64) -> SyscallResul

let stat = unsafe { VirtAddr(stat as usize).read_mut::<syscall_defs::stat::Stat>() };

let file = lookup_by_path_at(dirent, path, LookupMode::None)?;
if let Some(path) = path {
let file = lookup_by_path_at(file_dir, path, LookupMode::None)?;

*stat = file.inode().stat()?;
*stat = file.inode().stat()?;
} else {
*stat = file_dir.inode().stat()?;
}

logln!("fstatat {:?}, {:?}", fd, stat);

Expand Down
2 changes: 0 additions & 2 deletions syscall-defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ pub const SYS_PIPE: usize = 47;
pub const SYS_DUP: usize = 48;
pub const SYS_DUP2: usize = 49;
pub const SYS_STAT: usize = 50;
pub const SYS_FSTAT: usize = 51;

pub const SYS_GETRLIMIT: usize = 52;
pub const SYS_DEBUG: usize = 53;
Expand All @@ -86,7 +85,6 @@ pub const SYS_TICKSNS: usize = 58;

pub const SYS_GETPPID: usize = 59;
pub const SYS_GETPGID: usize = 60;
pub const SYS_FSTATAT: usize = 61;

#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(u64)]
Expand Down

0 comments on commit 72f2ca1

Please sign in to comment.