Implement fd_filestat_set_times using the filetime crate.
This commit is contained in:
committed by
Jakub Konka
parent
46c41abd60
commit
2f1afc4846
@@ -16,6 +16,7 @@ libc = "0.2"
|
|||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
cfg-if = "0.1.9"
|
cfg-if = "0.1.9"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
filetime = "0.2.6"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
nix = "0.13"
|
nix = "0.13"
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ use crate::sys::fdentry_impl::determine_type_rights;
|
|||||||
use crate::sys::hostcalls_impl::fs_helpers::path_open_rights;
|
use crate::sys::hostcalls_impl::fs_helpers::path_open_rights;
|
||||||
use crate::sys::{errno_from_ioerror, host_impl, hostcalls_impl};
|
use crate::sys::{errno_from_ioerror, host_impl, hostcalls_impl};
|
||||||
use crate::{host, wasm32, Result};
|
use crate::{host, wasm32, Result};
|
||||||
|
use filetime::{set_file_handle_times, FileTime};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
pub(crate) fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> Result<()> {
|
pub(crate) fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> Result<()> {
|
||||||
trace!("fd_close(fd={:?})", fd);
|
trace!("fd_close(fd={:?})", fd);
|
||||||
@@ -764,7 +766,34 @@ pub(crate) fn fd_filestat_set_times(
|
|||||||
let st_mtim = dec_timestamp(st_mtim);
|
let st_mtim = dec_timestamp(st_mtim);
|
||||||
let fst_flags = dec_fstflags(fst_flags);
|
let fst_flags = dec_fstflags(fst_flags);
|
||||||
|
|
||||||
hostcalls_impl::fd_filestat_set_times(fd, st_atim, st_mtim, fst_flags)
|
let set_atim = fst_flags & host::__WASI_FILESTAT_SET_MTIM != 0;
|
||||||
|
let set_atim_now = fst_flags & host::__WASI_FILESTAT_SET_MTIM != 0;
|
||||||
|
let set_mtim = fst_flags & host::__WASI_FILESTAT_SET_MTIM != 0;
|
||||||
|
let set_mtim_now = fst_flags & host::__WASI_FILESTAT_SET_MTIM != 0;
|
||||||
|
|
||||||
|
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||||
|
return Err(host::__WASI_EINVAL);
|
||||||
|
}
|
||||||
|
let atim = if set_atim {
|
||||||
|
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
|
||||||
|
Some(FileTime::from_system_time(time))
|
||||||
|
} else if set_atim_now {
|
||||||
|
let time = SystemTime::now();
|
||||||
|
Some(FileTime::from_system_time(time))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let mtim = if set_mtim {
|
||||||
|
let time = UNIX_EPOCH + Duration::from_nanos(st_mtim);
|
||||||
|
Some(FileTime::from_system_time(time))
|
||||||
|
} else if set_mtim_now {
|
||||||
|
let time = SystemTime::now();
|
||||||
|
Some(FileTime::from_system_time(time))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
set_file_handle_times(fd, atim, mtim).map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_filestat_set_size(
|
pub(crate) fn fd_filestat_set_size(
|
||||||
|
|||||||
@@ -363,49 +363,6 @@ fn filetype(metadata: &Metadata) -> host::__wasi_filetype_t {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_filestat_set_times(
|
|
||||||
fd: &File,
|
|
||||||
st_atim: host::__wasi_timestamp_t,
|
|
||||||
mut st_mtim: host::__wasi_timestamp_t,
|
|
||||||
fst_flags: host::__wasi_fstflags_t,
|
|
||||||
) -> Result<()> {
|
|
||||||
use nix::sys::time::{TimeSpec, TimeValLike};
|
|
||||||
|
|
||||||
if fst_flags & host::__WASI_FILESTAT_SET_MTIM_NOW != 0 {
|
|
||||||
let clock_id = libc::CLOCK_REALTIME;
|
|
||||||
let mut timespec = unsafe { std::mem::uninitialized::<libc::timespec>() };
|
|
||||||
let res = unsafe { libc::clock_gettime(clock_id, &mut timespec as *mut libc::timespec) };
|
|
||||||
if res != 0 {
|
|
||||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
|
||||||
}
|
|
||||||
st_mtim = (timespec.tv_sec as host::__wasi_timestamp_t)
|
|
||||||
.checked_mul(1_000_000_000)
|
|
||||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as host::__wasi_timestamp_t))
|
|
||||||
.ok_or(host::__WASI_EOVERFLOW)?;
|
|
||||||
}
|
|
||||||
let ts_atime = match fst_flags {
|
|
||||||
f if f & host::__WASI_FILESTAT_SET_ATIM_NOW != 0 => libc::timespec {
|
|
||||||
tv_sec: 0,
|
|
||||||
tv_nsec: utime_now(),
|
|
||||||
},
|
|
||||||
f if f & host::__WASI_FILESTAT_SET_ATIM != 0 => {
|
|
||||||
*TimeSpec::nanoseconds(st_atim as i64).as_ref()
|
|
||||||
}
|
|
||||||
_ => libc::timespec {
|
|
||||||
tv_sec: 0,
|
|
||||||
tv_nsec: utime_omit(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref();
|
|
||||||
let times = [ts_atime, ts_mtime];
|
|
||||||
let res = unsafe { libc::futimens(fd.as_raw_fd(), times.as_ptr()) };
|
|
||||||
if res != 0 {
|
|
||||||
Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn path_filestat_get(
|
pub(crate) fn path_filestat_get(
|
||||||
resolved: PathGet,
|
resolved: PathGet,
|
||||||
dirflags: host::__wasi_lookupflags_t,
|
dirflags: host::__wasi_lookupflags_t,
|
||||||
|
|||||||
@@ -267,15 +267,6 @@ fn filetype(metadata: &Metadata) -> io::Result<host::__wasi_filetype_t> {
|
|||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_filestat_set_times(
|
|
||||||
fd: &File,
|
|
||||||
st_atim: host::__wasi_timestamp_t,
|
|
||||||
mut st_mtim: host::__wasi_timestamp_t,
|
|
||||||
fst_flags: host::__wasi_fstflags_t,
|
|
||||||
) -> Result<()> {
|
|
||||||
unimplemented!("fd_filestat_set_times")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn path_filestat_get(
|
pub(crate) fn path_filestat_get(
|
||||||
resolved: PathGet,
|
resolved: PathGet,
|
||||||
dirflags: host::__wasi_lookupflags_t,
|
dirflags: host::__wasi_lookupflags_t,
|
||||||
|
|||||||
Reference in New Issue
Block a user