Skip to content

Commit

Permalink
add utimensat, fix touch
Browse files Browse the repository at this point in the history
  • Loading branch information
function2-llx committed Apr 25, 2020
1 parent db9ef21 commit ff9b0cc
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Cargo.lock
modules/*/objs
# Rust
modules/*/target
test/
2 changes: 2 additions & 0 deletions kernel/src/fs/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ const F_LINUX_SPECIFIC_BASE: usize = 1024;
pub const F_DUPFD_CLOEXEC: usize = F_LINUX_SPECIFIC_BASE + 6;

pub const O_CLOEXEC: usize = 02000000; /* set close_on_exec */

pub const AT_SYMLINK_NOFOLLOW: usize = 0x100;
2 changes: 1 addition & 1 deletion kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ lazy_static! {
};
}

pub const FOLLOW_MAX_DEPTH: usize = 1;
pub const FOLLOW_MAX_DEPTH: usize = 3;

pub trait INodeExt {
fn read_as_vec(&self) -> Result<Vec<u8>>;
Expand Down
9 changes: 6 additions & 3 deletions kernel/src/process/structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::Arc, sync::Weak, vec::Vec, collections::BTreeSet};
use alloc::{
boxed::Box, collections::BTreeMap, collections::BTreeSet, string::String, sync::Arc,
sync::Weak, vec::Vec,
};
use core::fmt;

use core::str;
Expand All @@ -18,14 +21,14 @@ use crate::ipc::SemProc;
use crate::memory::{
ByFrame, Delay, File, GlobalFrameAlloc, KernelStack, MemoryAttr, MemorySet, Read,
};
use crate::sync::{Condvar, SpinNoIrqLock as Mutex, SpinLock};
use crate::sync::{Condvar, SpinLock, SpinNoIrqLock as Mutex};

use super::abi::{self, ProcInitInfo};
use crate::process::thread_manager;
use bitflags::_core::cell::Ref;
use core::mem::MaybeUninit;
use rcore_fs::vfs::INode;
use rcore_thread::std_thread::yield_now;
use bitflags::_core::cell::Ref;

#[allow(dead_code)]
pub struct Thread {
Expand Down
70 changes: 57 additions & 13 deletions kernel/src/syscall/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::*;
use crate::fs::epoll::EpollInstance;
use crate::fs::FileLike;
use crate::process::Process;
use crate::syscall::SysError::EINVAL;
use rcore_fs::vfs::PollStatus;

impl Syscall<'_> {
Expand Down Expand Up @@ -963,7 +964,12 @@ impl Syscall<'_> {
self.sys_symlinkat(target, AT_FDCWD, linkpath)
}

pub fn sys_symlinkat(&mut self, target: *const u8, newdirfd: usize, linkpath: *const u8) -> SysResult {
pub fn sys_symlinkat(
&mut self,
target: *const u8,
newdirfd: usize,
linkpath: *const u8,
) -> SysResult {
let proc = self.process();
let target = check_and_clone_cstr(target)?;
let linkpath = check_and_clone_cstr(linkpath)?;
Expand All @@ -976,19 +982,15 @@ impl Syscall<'_> {

// If linkpath exists, it will not be overwritten.
match dir_inode.find(filename) {
Ok(_) => {
Err(SysError::EEXIST)
}
Err(e) => {
match e {
FsError::EntryNotFound => {
let symlink = dir_inode.create(filename, FileType::SymLink, 0o777)?;
symlink.write_at(0, target.as_bytes())?;
Ok(0)
},
_ => Err(e.into())
Ok(_) => Err(SysError::EEXIST),
Err(e) => match e {
FsError::EntryNotFound => {
let symlink = dir_inode.create(filename, FileType::SymLink, 0o777)?;
symlink.write_at(0, target.as_bytes())?;
Ok(0)
}
}
_ => Err(e.into()),
},
}
}

Expand Down Expand Up @@ -1050,6 +1052,48 @@ impl Syscall<'_> {
Ok(0)
}

pub fn sys_utimensat(
&mut self,
dirfd: usize,
pathname: *const u8,
times: *const TimeSpec,
flags: usize,
) -> SysResult {
info!(
"utimensat(raw): dirfd: {}, pathname: {}, times: {}, flags: {:#x}",
dirfd as i64, pathname as usize, times as usize, flags
);
let pathname = check_and_clone_cstr(pathname)?;
let proc = self.process();
let times = if times.is_null() {
[TimeSpec::get_epoch(), TimeSpec::get_epoch()]
} else {
let times = unsafe { self.vm().check_read_array(times, 2)? };
[times[0], times[1]]
};
info!(
"utimensat: dirfd: {}, pathname: {}, times: {:?}, flags: {:#x}",
dirfd as i64, pathname, times, flags
);
let follow = match flags {
0 => true,
fcntl::AT_SYMLINK_NOFOLLOW => false,
_ => return Err(EINVAL),
};
let inode = proc.lookup_inode_at(dirfd, &pathname, follow)?;
let mut metadata = inode.metadata()?;
metadata.atime = Timespec {
sec: times[0].sec as i64,
nsec: times[0].nsec as i32,
};
metadata.mtime = Timespec {
sec: times[1].sec as i64,
nsec: times[1].nsec as i32,
};
inode.set_metadata(&metadata)?;
Ok(0)
}

pub fn sys_sync(&mut self) -> SysResult {
ROOT_INODE.fs().sync()?;
Ok(0)
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/syscall/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Syscall<'_> {
let flags = MmapFlags::from_bits_truncate(flags);
info!(
"mmap: addr={:#x}, size={:#x}, prot={:?}, flags={:?}, fd={}, offset={:#x}",
addr, len, prot, flags, fd, offset
addr, len, prot, flags, fd as isize, offset
);

let mut proc = self.process();
Expand Down
11 changes: 9 additions & 2 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ impl Syscall<'_> {
args[4],
),
SYS_UNLINKAT => self.sys_unlinkat(args[0], args[1] as *const u8, args[2]),
SYS_SYMLINKAT => self.sys_symlinkat(args[0] as *const u8, args[1] as usize, args[2] as *const u8),
SYS_SYMLINKAT => {
self.sys_symlinkat(args[0] as *const u8, args[1] as usize, args[2] as *const u8)
}
SYS_READLINKAT => {
self.sys_readlinkat(args[0], args[1] as *const u8, args[2] as *mut u8, args[3])
}
Expand All @@ -146,7 +148,12 @@ impl Syscall<'_> {
SYS_FACCESSAT => self.sys_faccessat(args[0], args[1] as *const u8, args[2], args[3]),
SYS_DUP3 => self.sys_dup3(args[0], args[1], args[2]),
SYS_PIPE2 => self.sys_pipe(args[0] as *mut u32), // TODO: handle `flags`
SYS_UTIMENSAT => self.unimplemented("utimensat", Ok(0)),
SYS_UTIMENSAT => self.sys_utimensat(
args[0],
args[1] as *const u8,
args[2] as *const TimeSpec,
args[3],
),
SYS_COPY_FILE_RANGE => self.sys_copy_file_range(
args[0],
args[1] as *mut usize,
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/syscall/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl TimeVal {
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TimeSpec {
sec: usize,
nsec: usize,
pub sec: usize,
pub nsec: usize,
}

impl TimeSpec {
Expand Down

0 comments on commit ff9b0cc

Please sign in to comment.