diff --git a/Cargo.lock b/Cargo.lock index 8f99fb1591..0141c8afc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2605,6 +2605,7 @@ version = "0.12.0" dependencies = [ "bitflags", "cfg-if", + "filetime", "libc", "log", ] diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/filetime.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/filetime.rs deleted file mode 100644 index 943aba9f7b..0000000000 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/filetime.rs +++ /dev/null @@ -1,103 +0,0 @@ -//! This internal module consists of helper types and functions for dealing -//! with setting the file times specific to BSD-style *nixes. -use crate::old::snapshot_0::{sys::unix::filetime::FileTime, Result}; -use cfg_if::cfg_if; -use std::ffi::CStr; -use std::fs::File; -use std::io; -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; - -cfg_if! { - if #[cfg(any( - target_os = "macos", - target_os = "freebsd", - target_os = "ios", - target_os = "dragonfly" - ))] { - pub(crate) const UTIME_NOW: i64 = -1; - pub(crate) const UTIME_OMIT: i64 = -2; - } else if #[cfg(target_os = "openbsd")] { - // These are swapped compared to macos, freebsd, ios, and dragonfly. - // https://github.com/openbsd/src/blob/master/sys/sys/stat.h#L187 - pub(crate) const UTIME_NOW: i64 = -2; - pub(crate) const UTIME_OMIT: i64 = -1; - } else if #[cfg(target_os = "netbsd" )] { - // These are the same as for Linux. - // http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/stat.h?rev=1.69&content-type=text/x-cvsweb-markup&only_with_tag=MAIN - pub(crate) const UTIME_NOW: i64 = 1_073_741_823; - pub(crate) const UTIME_OMIT: i64 = 1_073_741_822; - } -} - -/// Wrapper for `utimensat` syscall, however, with an added twist such that `utimensat` symbol -/// is firstly resolved (i.e., we check whether it exists on the host), and only used if that is -/// the case. Otherwise, the syscall resorts to a less accurate `utimesat` emulated syscall. -/// The original implementation can be found here: [filetime::unix::macos::set_times] -/// -/// [filetime::unix::macos::set_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/macos.rs#L49 -pub(crate) fn utimensat( - dirfd: &File, - path: &str, - atime: FileTime, - mtime: FileTime, - symlink_nofollow: bool, -) -> Result<()> { - use crate::old::snapshot_0::sys::unix::filetime::to_timespec; - use std::ffi::CString; - use std::os::unix::prelude::*; - - // Attempt to use the `utimensat` syscall, but if it's not supported by the - // current kernel then fall back to an older syscall. - if let Some(func) = fetch_utimensat() { - let flags = if symlink_nofollow { - libc::AT_SYMLINK_NOFOLLOW - } else { - 0 - }; - - let p = CString::new(path.as_bytes())?; - let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; - if rc == 0 { - return Ok(()); - } else { - return Err(io::Error::last_os_error().into()); - } - } - - super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) -} - -/// Wraps `fetch` specifically targetting `utimensat` symbol. If the symbol exists -/// on the host, then returns an `Some(unsafe fn)`. -fn fetch_utimensat() -> Option< - unsafe extern "C" fn( - libc::c_int, - *const libc::c_char, - *const libc::timespec, - libc::c_int, - ) -> libc::c_int, -> { - static ADDR: AtomicUsize = AtomicUsize::new(0); - unsafe { - fetch(&ADDR, CStr::from_bytes_with_nul_unchecked(b"utimensat\0")) - .map(|sym| std::mem::transmute(sym)) - } -} - -/// Fetches a symbol by `name` and stores it in `cache`. -fn fetch(cache: &AtomicUsize, name: &CStr) -> Option { - match cache.load(SeqCst) { - 0 => {} - 1 => return None, - n => return Some(n), - } - let sym = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) }; - let (val, ret) = if sym.is_null() { - (1, None) - } else { - (sym as usize, Some(sym as usize)) - }; - cache.store(val, SeqCst); - return ret; -} diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs index 39a4046a74..b47019d43d 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/mod.rs @@ -1,6 +1,3 @@ -pub(crate) mod filetime; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; pub(crate) mod oshandle; -#[path = "../linux/utimesat.rs"] -pub(crate) mod utimesat; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/filetime.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/filetime.rs deleted file mode 100644 index adc48a3493..0000000000 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/filetime.rs +++ /dev/null @@ -1,36 +0,0 @@ -//! This internal module consists of helper types and functions for dealing -//! with setting the file times specific to Emscripten. -use crate::old::snapshot_0::{sys::unix::filetime::FileTime, Result}; -use std::fs::File; -use std::io; - -pub(crate) const UTIME_NOW: i32 = 1_073_741_823; -pub(crate) const UTIME_OMIT: i32 = 1_073_741_822; - -/// Wrapper for `utimensat` syscall. In Emscripten, there is no point in dynamically resolving -/// if `utimensat` is available as it always was and will be. -pub(crate) fn utimensat( - dirfd: &File, - path: &str, - atime: FileTime, - mtime: FileTime, - symlink_nofollow: bool, -) -> Result<()> { - use crate::old::snapshot_0::sys::unix::filetime::to_timespec; - use std::ffi::CString; - use std::os::unix::prelude::*; - - let flags = if symlink_nofollow { - libc::AT_SYMLINK_NOFOLLOW - } else { - 0 - }; - let p = CString::new(path.as_bytes())?; - let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; - if rc == 0 { - Ok(()) - } else { - Err(io::Error::last_os_error().into()) - } -} diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/mod.rs index c9c3841b9a..0b3cfa38fe 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/emscripten/mod.rs @@ -1,4 +1,3 @@ -pub(crate) mod filetime; pub(crate) mod host_impl; #[path = "../linux/hostcalls_impl.rs"] pub(crate) mod hostcalls_impl; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/filetime.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/filetime.rs deleted file mode 100644 index 15530e1f0c..0000000000 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/filetime.rs +++ /dev/null @@ -1,67 +0,0 @@ -//! This internal module consists of helper types and functions for dealing -//! with setting the file times (mainly in `path_filestat_set_times` syscall for now). -//! -//! The vast majority of the code contained within and in platform-specific implementations -//! (`super::linux::filetime` and `super::bsd::filetime`) is based on the [filetime] crate. -//! Kudos @alexcrichton! -//! -//! [filetime]: https://github.com/alexcrichton/filetime -use crate::old::snapshot_0::Result; -use std::convert::TryInto; - -pub(crate) use super::sys_impl::filetime::*; - -cfg_if::cfg_if! { - if #[cfg(not(target_os = "emscripten"))] { - fn filetime_to_timespec(ft: &filetime::FileTime) -> Result { - Ok( - libc::timespec { - tv_sec: ft.seconds(), - tv_nsec: ft.nanoseconds().try_into()?, - } - ) - } - } else { - fn filetime_to_timespec(ft: &filetime::FileTime) -> Result { - Ok( - libc::timespec { - tv_sec: ft.seconds().try_into()?, - tv_nsec: ft.nanoseconds().try_into()?, - } - ) - } - } -} - -/// A wrapper `enum` around `filetime::FileTime` struct, but unlike the original, this -/// type allows the possibility of specifying `FileTime::Now` as a valid enumeration which, -/// in turn, if `utimensat` is available on the host, will use a special const setting -/// `UTIME_NOW`. -#[derive(Debug, Copy, Clone)] -pub(crate) enum FileTime { - Now, - Omit, - FileTime(filetime::FileTime), -} - -/// Converts `FileTime` to `libc::timespec`. If `FileTime::Now` variant is specified, this -/// resolves to `UTIME_NOW` special const, `FileTime::Omit` variant resolves to `UTIME_OMIT`, and -/// `FileTime::FileTime(ft)` where `ft := filetime::FileTime` uses [filetime] crate's original -/// implementation which can be found here: [filetime::unix::to_timespec]. -/// -/// [filetime]: https://github.com/alexcrichton/filetime -/// [filetime::unix::to_timespec]: https://github.com/alexcrichton/filetime/blob/master/src/unix/mod.rs#L30 -pub(crate) fn to_timespec(ft: &FileTime) -> Result { - let ts = match ft { - FileTime::Now => libc::timespec { - tv_sec: 0, - tv_nsec: UTIME_NOW, - }, - FileTime::Omit => libc::timespec { - tv_sec: 0, - tv_nsec: UTIME_OMIT, - }, - FileTime::FileTime(ft) => filetime_to_timespec(ft)?, - }; - Ok(ts) -} diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs index cace76529e..d22d711c6b 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs @@ -225,8 +225,8 @@ pub(crate) fn path_filestat_set_times( st_mtim: wasi::__wasi_timestamp_t, fst_flags: wasi::__wasi_fstflags_t, ) -> Result<()> { - use super::super::filetime::*; use std::time::{Duration, UNIX_EPOCH}; + use yanix::filetime::*; let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0; let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/filetime.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/filetime.rs deleted file mode 100644 index 8162b391fa..0000000000 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/filetime.rs +++ /dev/null @@ -1,61 +0,0 @@ -//! This internal module consists of helper types and functions for dealing -//! with setting the file times specific to Linux. -use crate::old::snapshot_0::{sys::unix::filetime::FileTime, Result}; -use std::fs::File; -use std::io; -use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; - -pub(crate) const UTIME_NOW: i64 = 1_073_741_823; -pub(crate) const UTIME_OMIT: i64 = 1_073_741_822; - -/// Wrapper for `utimensat` syscall, however, with an added twist such that `utimensat` symbol -/// is firstly resolved (i.e., we check whether it exists on the host), and only used if that is -/// the case. Otherwise, the syscall resorts to a less accurate `utimesat` emulated syscall. -/// The original implementation can be found here: [filetime::unix::linux::set_times] -/// -/// [filetime::unix::linux::set_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/linux.rs#L64 -pub(crate) fn utimensat( - dirfd: &File, - path: &str, - atime: FileTime, - mtime: FileTime, - symlink_nofollow: bool, -) -> Result<()> { - use crate::old::snapshot_0::sys::unix::filetime::to_timespec; - use std::ffi::CString; - use std::os::unix::prelude::*; - - let flags = if symlink_nofollow { - libc::AT_SYMLINK_NOFOLLOW - } else { - 0 - }; - - // Attempt to use the `utimensat` syscall, but if it's not supported by the - // current kernel then fall back to an older syscall. - static INVALID: AtomicBool = AtomicBool::new(false); - if !INVALID.load(Relaxed) { - let p = CString::new(path.as_bytes())?; - let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { - libc::syscall( - libc::SYS_utimensat, - dirfd.as_raw_fd(), - p.as_ptr(), - times.as_ptr(), - flags, - ) - }; - if rc == 0 { - return Ok(()); - } - let err = io::Error::last_os_error(); - if err.raw_os_error() == Some(libc::ENOSYS) { - INVALID.store(true, Relaxed); - } else { - return Err(err.into()); - } - } - - super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) -} diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs index 0b4b8fd0b9..b47019d43d 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/mod.rs @@ -1,5 +1,3 @@ -pub(crate) mod filetime; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; pub(crate) mod oshandle; -pub(crate) mod utimesat; diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/utimesat.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/utimesat.rs deleted file mode 100644 index 155d994578..0000000000 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/linux/utimesat.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::old::snapshot_0::sys::unix::filetime::FileTime; -use crate::old::snapshot_0::Result; -use std::{fs, io}; - -/// Combines `openat` with `utimes` to emulate `utimensat` on platforms where it is -/// not available. The logic for setting file times is based on [filetime::unix::set_file_handles_times]. -/// -/// [filetime::unix::set_file_handles_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/utimes.rs#L24 -pub(crate) fn utimesat( - dirfd: &fs::File, - path: &str, - atime: FileTime, - mtime: FileTime, - symlink_nofollow: bool, -) -> Result<()> { - use std::ffi::CString; - use std::os::unix::prelude::*; - // emulate *at syscall by reading the path from a combination of - // (fd, path) - let p = CString::new(path.as_bytes())?; - let mut flags = libc::O_RDWR; - if symlink_nofollow { - flags |= libc::O_NOFOLLOW; - } - let fd = unsafe { libc::openat(dirfd.as_raw_fd(), p.as_ptr(), flags) }; - let f = unsafe { fs::File::from_raw_fd(fd) }; - let (atime, mtime) = get_times(atime, mtime, || f.metadata().map_err(Into::into))?; - let times = [to_timeval(atime), to_timeval(mtime)]; - let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }; - if rc == 0 { - Ok(()) - } else { - Err(io::Error::last_os_error().into()) - } -} - -/// Converts `filetime::FileTime` to `libc::timeval`. This function was taken directly from -/// [filetime] crate. -/// -/// [filetime]: https://github.com/alexcrichton/filetime/blob/master/src/unix/utimes.rs#L93 -fn to_timeval(ft: filetime::FileTime) -> libc::timeval { - libc::timeval { - tv_sec: ft.seconds(), - tv_usec: (ft.nanoseconds() / 1000) as libc::suseconds_t, - } -} - -/// For a provided pair of access and modified `FileTime`s, converts the input to -/// `filetime::FileTime` used later in `utimensat` function. For variants `FileTime::Now` -/// and `FileTime::Omit`, this function will make two syscalls: either accessing current -/// system time, or accessing the file's metadata. -/// -/// The original implementation can be found here: [filetime::unix::get_times]. -/// -/// [filetime::unix::get_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/utimes.rs#L42 -fn get_times( - atime: FileTime, - mtime: FileTime, - current: impl Fn() -> Result, -) -> Result<(filetime::FileTime, filetime::FileTime)> { - use std::time::SystemTime; - - let atime = match atime { - FileTime::Now => { - let time = SystemTime::now(); - filetime::FileTime::from_system_time(time) - } - FileTime::Omit => { - let meta = current()?; - filetime::FileTime::from_last_access_time(&meta) - } - FileTime::FileTime(ft) => ft, - }; - - let mtime = match mtime { - FileTime::Now => { - let time = SystemTime::now(); - filetime::FileTime::from_system_time(time) - } - FileTime::Omit => { - let meta = current()?; - filetime::FileTime::from_last_modification_time(&meta) - } - FileTime::FileTime(ft) => ft, - }; - - Ok((atime, mtime)) -} diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/mod.rs index 723c878950..d7e8288323 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/mod.rs @@ -2,8 +2,6 @@ pub(crate) mod fdentry_impl; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; -mod filetime; - cfg_if::cfg_if! { if #[cfg(target_os = "linux")] { mod linux; diff --git a/crates/wasi-common/src/sys/unix/bsd/mod.rs b/crates/wasi-common/src/sys/unix/bsd/mod.rs index 39a4046a74..b47019d43d 100644 --- a/crates/wasi-common/src/sys/unix/bsd/mod.rs +++ b/crates/wasi-common/src/sys/unix/bsd/mod.rs @@ -1,6 +1,3 @@ -pub(crate) mod filetime; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; pub(crate) mod oshandle; -#[path = "../linux/utimesat.rs"] -pub(crate) mod utimesat; diff --git a/crates/wasi-common/src/sys/unix/emscripten/mod.rs b/crates/wasi-common/src/sys/unix/emscripten/mod.rs index 82a7ed93a8..04a3a8316d 100644 --- a/crates/wasi-common/src/sys/unix/emscripten/mod.rs +++ b/crates/wasi-common/src/sys/unix/emscripten/mod.rs @@ -1,4 +1,3 @@ -pub(crate) mod filetime; #[path = "../linux/host_impl.rs"] pub(crate) mod host_impl; #[path = "../linux/hostcalls_impl.rs"] diff --git a/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs b/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs index 2a7c01db74..85ac6bb1c2 100644 --- a/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs @@ -225,8 +225,8 @@ pub(crate) fn path_filestat_set_times( st_mtim: wasi::__wasi_timestamp_t, fst_flags: wasi::__wasi_fstflags_t, ) -> Result<()> { - use super::super::filetime::*; use std::time::{Duration, UNIX_EPOCH}; + use yanix::filetime::*; let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0; let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0; diff --git a/crates/wasi-common/src/sys/unix/linux/mod.rs b/crates/wasi-common/src/sys/unix/linux/mod.rs index 0b4b8fd0b9..b47019d43d 100644 --- a/crates/wasi-common/src/sys/unix/linux/mod.rs +++ b/crates/wasi-common/src/sys/unix/linux/mod.rs @@ -1,5 +1,3 @@ -pub(crate) mod filetime; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; pub(crate) mod oshandle; -pub(crate) mod utimesat; diff --git a/crates/wasi-common/src/sys/unix/mod.rs b/crates/wasi-common/src/sys/unix/mod.rs index 60b08ab355..b34024676b 100644 --- a/crates/wasi-common/src/sys/unix/mod.rs +++ b/crates/wasi-common/src/sys/unix/mod.rs @@ -2,8 +2,6 @@ pub(crate) mod fdentry_impl; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; -mod filetime; - cfg_if::cfg_if! { if #[cfg(target_os = "linux")] { mod linux; diff --git a/crates/wasi-common/yanix/Cargo.toml b/crates/wasi-common/yanix/Cargo.toml index e5e30f7d72..4ea001dec8 100644 --- a/crates/wasi-common/yanix/Cargo.toml +++ b/crates/wasi-common/yanix/Cargo.toml @@ -12,6 +12,7 @@ log = "0.4" libc = { version = "0.2", features = ["extra_traits"] } bitflags = "1.2" cfg-if = "0.1.9" +filetime = "0.2.7" [badges] maintenance = { status = "actively-developed" } diff --git a/crates/wasi-common/src/sys/unix/filetime.rs b/crates/wasi-common/yanix/src/filetime.rs similarity index 61% rename from crates/wasi-common/src/sys/unix/filetime.rs rename to crates/wasi-common/yanix/src/filetime.rs index 9a70740a36..d712036c0e 100644 --- a/crates/wasi-common/src/sys/unix/filetime.rs +++ b/crates/wasi-common/yanix/src/filetime.rs @@ -1,4 +1,4 @@ -//! This internal module consists of helper types and functions for dealing +//! This module consists of helper types and functions for dealing //! with setting the file times (mainly in `path_filestat_set_times` syscall for now). //! //! The vast majority of the code contained within and in platform-specific implementations @@ -6,10 +6,9 @@ //! Kudos @alexcrichton! //! //! [filetime]: https://github.com/alexcrichton/filetime -use crate::Result; -use std::convert::TryInto; +use std::io::Result; -pub(crate) use super::sys_impl::filetime::*; +pub use super::sys::filetime::*; cfg_if::cfg_if! { if #[cfg(not(target_os = "emscripten"))] { @@ -17,18 +16,33 @@ cfg_if::cfg_if! { Ok( libc::timespec { tv_sec: ft.seconds(), - tv_nsec: ft.nanoseconds().try_into()?, + tv_nsec: i64::from(ft.nanoseconds()), } ) } } else { fn filetime_to_timespec(ft: &filetime::FileTime) -> Result { - Ok( - libc::timespec { - tv_sec: ft.seconds().try_into()?, - tv_nsec: ft.nanoseconds().try_into()?, + use std::convert::TryInto; + use std::io::Error; + // Emscripten expects both `tv_sec` and `tv_nsec` fields to be `i32`. + // Here however `ft.seconds() -> i64` and `ft.nanoseconds() -> u32` so + // a simple `as` cast may be insufficient. So, perform a checked conversion, + // log error if any, and convert to libc::EOVERFLOW. + let tv_sec = match ft.seconds().try_into() { + Ok(sec) => sec, + Err(_) => { + log::debug!("filetime_to_timespec failed converting seconds to required width"); + return Err(Error::from_raw_os_error(libc::EOVERFLOW)); } - ) + }; + let tv_nsec = match ft.nanoseconds().try_into() { + Ok(nsec) => nsec, + Err(_) => { + log::debug!("filetime_to_timespec failed converting nanoseconds to required width"); + return Err(Error::from_raw_os_error(libc::EOVERFLOW)); + } + }; + Ok(libc::timespec { tv_sec, tv_nsec }) } } } @@ -38,7 +52,7 @@ cfg_if::cfg_if! { /// in turn, if `utimensat` is available on the host, will use a special const setting /// `UTIME_NOW`. #[derive(Debug, Copy, Clone)] -pub(crate) enum FileTime { +pub enum FileTime { Now, Omit, FileTime(filetime::FileTime), diff --git a/crates/wasi-common/yanix/src/lib.rs b/crates/wasi-common/yanix/src/lib.rs index fded31d6cd..f4caea998d 100644 --- a/crates/wasi-common/yanix/src/lib.rs +++ b/crates/wasi-common/yanix/src/lib.rs @@ -13,6 +13,7 @@ pub mod clock; pub mod dir; pub mod fcntl; pub mod file; +pub mod filetime; pub mod poll; pub mod socket; diff --git a/crates/wasi-common/src/sys/unix/bsd/filetime.rs b/crates/wasi-common/yanix/src/sys/bsd/filetime.rs similarity index 88% rename from crates/wasi-common/src/sys/unix/bsd/filetime.rs rename to crates/wasi-common/yanix/src/sys/bsd/filetime.rs index 4fb40c926b..8f71db0e4c 100644 --- a/crates/wasi-common/src/sys/unix/bsd/filetime.rs +++ b/crates/wasi-common/yanix/src/sys/bsd/filetime.rs @@ -1,10 +1,11 @@ -//! This internal module consists of helper types and functions for dealing +//! This module consists of helper types and functions for dealing //! with setting the file times specific to BSD-style *nixes. -use crate::{sys::unix::filetime::FileTime, Result}; +use crate::filetime::FileTime; +use crate::from_success_code; use cfg_if::cfg_if; use std::ffi::CStr; use std::fs::File; -use std::io; +use std::io::Result; use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; cfg_if! { @@ -35,14 +36,14 @@ cfg_if! { /// The original implementation can be found here: [filetime::unix::macos::set_times] /// /// [filetime::unix::macos::set_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/macos.rs#L49 -pub(crate) fn utimensat( +pub fn utimensat( dirfd: &File, path: &str, atime: FileTime, mtime: FileTime, symlink_nofollow: bool, ) -> Result<()> { - use crate::sys::unix::filetime::to_timespec; + use crate::filetime::to_timespec; use std::ffi::CString; use std::os::unix::prelude::*; @@ -57,12 +58,9 @@ pub(crate) fn utimensat( let p = CString::new(path.as_bytes())?; let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; - if rc == 0 { - return Ok(()); - } else { - return Err(io::Error::last_os_error().into()); - } + return from_success_code(unsafe { + func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) + }); } super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) diff --git a/crates/wasi-common/yanix/src/sys/bsd/mod.rs b/crates/wasi-common/yanix/src/sys/bsd/mod.rs index 3e6611f7b9..a8f4d43631 100644 --- a/crates/wasi-common/yanix/src/sys/bsd/mod.rs +++ b/crates/wasi-common/yanix/src/sys/bsd/mod.rs @@ -1,3 +1,6 @@ pub(crate) mod dir; pub(crate) mod fadvise; pub(crate) mod file; +pub(crate) mod filetime; +#[path = "../linux/utimesat.rs"] +pub(crate) mod utimesat; diff --git a/crates/wasi-common/src/sys/unix/emscripten/filetime.rs b/crates/wasi-common/yanix/src/sys/emscripten/filetime.rs similarity index 64% rename from crates/wasi-common/src/sys/unix/emscripten/filetime.rs rename to crates/wasi-common/yanix/src/sys/emscripten/filetime.rs index 08f7c24a79..c8de04d399 100644 --- a/crates/wasi-common/src/sys/unix/emscripten/filetime.rs +++ b/crates/wasi-common/yanix/src/sys/emscripten/filetime.rs @@ -1,22 +1,23 @@ -//! This internal module consists of helper types and functions for dealing +//! This module consists of helper types and functions for dealing //! with setting the file times specific to Emscripten. -use crate::{sys::unix::filetime::FileTime, Result}; +use crate::filetime::FileTime; +use crate::from_success_code; use std::fs::File; -use std::io; +use std::io::Result; pub(crate) const UTIME_NOW: i32 = 1_073_741_823; pub(crate) const UTIME_OMIT: i32 = 1_073_741_822; /// Wrapper for `utimensat` syscall. In Emscripten, there is no point in dynamically resolving /// if `utimensat` is available as it always was and will be. -pub(crate) fn utimensat( +pub fn utimensat( dirfd: &File, path: &str, atime: FileTime, mtime: FileTime, symlink_nofollow: bool, ) -> Result<()> { - use crate::sys::unix::filetime::to_timespec; + use crate::filetime::to_timespec; use std::ffi::CString; use std::os::unix::prelude::*; @@ -27,10 +28,7 @@ pub(crate) fn utimensat( }; let p = CString::new(path.as_bytes())?; let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; - if rc == 0 { - Ok(()) - } else { - Err(io::Error::last_os_error().into()) - } + from_success_code(unsafe { + libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) + }) } diff --git a/crates/wasi-common/yanix/src/sys/emscripten/mod.rs b/crates/wasi-common/yanix/src/sys/emscripten/mod.rs index 1ee0bdbda4..8511b5d377 100644 --- a/crates/wasi-common/yanix/src/sys/emscripten/mod.rs +++ b/crates/wasi-common/yanix/src/sys/emscripten/mod.rs @@ -4,6 +4,7 @@ pub(crate) mod dir; pub(crate) mod fadvise; #[path = "../linux/file.rs"] pub(crate) mod file; +pub(crate) mod filetime; use crate::dir::SeekLoc; use std::convert::TryInto; diff --git a/crates/wasi-common/src/sys/unix/linux/filetime.rs b/crates/wasi-common/yanix/src/sys/linux/filetime.rs similarity index 78% rename from crates/wasi-common/src/sys/unix/linux/filetime.rs rename to crates/wasi-common/yanix/src/sys/linux/filetime.rs index 6ffd6b8ddf..36b7687386 100644 --- a/crates/wasi-common/src/sys/unix/linux/filetime.rs +++ b/crates/wasi-common/yanix/src/sys/linux/filetime.rs @@ -1,8 +1,9 @@ -//! This internal module consists of helper types and functions for dealing +//! This module consists of helper types and functions for dealing //! with setting the file times specific to Linux. -use crate::{sys::unix::filetime::FileTime, Result}; +use crate::filetime::FileTime; +use crate::from_success_code; use std::fs::File; -use std::io; +use std::io::Result; use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; pub(crate) const UTIME_NOW: i64 = 1_073_741_823; @@ -14,14 +15,14 @@ pub(crate) const UTIME_OMIT: i64 = 1_073_741_822; /// The original implementation can be found here: [filetime::unix::linux::set_times] /// /// [filetime::unix::linux::set_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/linux.rs#L64 -pub(crate) fn utimensat( +pub fn utimensat( dirfd: &File, path: &str, atime: FileTime, mtime: FileTime, symlink_nofollow: bool, ) -> Result<()> { - use crate::sys::unix::filetime::to_timespec; + use crate::filetime::to_timespec; use std::ffi::CString; use std::os::unix::prelude::*; @@ -37,7 +38,7 @@ pub(crate) fn utimensat( if !INVALID.load(Relaxed) { let p = CString::new(path.as_bytes())?; let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; - let rc = unsafe { + let res = from_success_code(unsafe { libc::syscall( libc::SYS_utimensat, dirfd.as_raw_fd(), @@ -45,16 +46,15 @@ pub(crate) fn utimensat( times.as_ptr(), flags, ) + }); + let err = match res { + Ok(()) => return Ok(()), + Err(e) => e, }; - if rc == 0 { - return Ok(()); - } - let err = io::Error::last_os_error(); - if err.raw_os_error() == Some(libc::ENOSYS) { + if err.raw_os_error().unwrap() == libc::ENOSYS { INVALID.store(true, Relaxed); - } else { - return Err(err.into()); } + return Err(err); } super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) diff --git a/crates/wasi-common/yanix/src/sys/linux/mod.rs b/crates/wasi-common/yanix/src/sys/linux/mod.rs index e386fccad2..311155a6de 100644 --- a/crates/wasi-common/yanix/src/sys/linux/mod.rs +++ b/crates/wasi-common/yanix/src/sys/linux/mod.rs @@ -1,6 +1,8 @@ pub(crate) mod dir; pub(crate) mod fadvise; pub(crate) mod file; +pub(crate) mod filetime; +pub(crate) mod utimesat; use crate::dir::SeekLoc; use std::io::Result; diff --git a/crates/wasi-common/src/sys/unix/linux/utimesat.rs b/crates/wasi-common/yanix/src/sys/linux/utimesat.rs similarity index 91% rename from crates/wasi-common/src/sys/unix/linux/utimesat.rs rename to crates/wasi-common/yanix/src/sys/linux/utimesat.rs index c99e6e0610..26025e0772 100644 --- a/crates/wasi-common/src/sys/unix/linux/utimesat.rs +++ b/crates/wasi-common/yanix/src/sys/linux/utimesat.rs @@ -1,12 +1,13 @@ -use crate::sys::unix::filetime::FileTime; -use crate::Result; -use std::{fs, io}; +use crate::filetime::FileTime; +use crate::from_success_code; +use std::fs; +use std::io::Result; /// Combines `openat` with `utimes` to emulate `utimensat` on platforms where it is /// not available. The logic for setting file times is based on [filetime::unix::set_file_handles_times]. /// /// [filetime::unix::set_file_handles_times]: https://github.com/alexcrichton/filetime/blob/master/src/unix/utimes.rs#L24 -pub(crate) fn utimesat( +pub fn utimesat( dirfd: &fs::File, path: &str, atime: FileTime, @@ -26,12 +27,7 @@ pub(crate) fn utimesat( let f = unsafe { fs::File::from_raw_fd(fd) }; let (atime, mtime) = get_times(atime, mtime, || f.metadata().map_err(Into::into))?; let times = [to_timeval(atime), to_timeval(mtime)]; - let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }; - if rc == 0 { - Ok(()) - } else { - Err(io::Error::last_os_error().into()) - } + from_success_code(unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }) } /// Converts `filetime::FileTime` to `libc::timeval`. This function was taken directly from