[wasi-common]: move filetime module to yanix (#1255)

* Move filetime module to yanix

I've noticed that we could replace every occurrence of `crate::Result`
in `filetime` mods with `io::Result`, so I thought why not move it
to `yanix` and get rid off a lot of unnecessary code duplication
within `wasi-common`. Now, ideally I'd have our `filetime` modifications
backported to Alex's [`filetime`] crate, but one step at a time
(apologies Alex, I was meant to backport this ages ago, just didn't
find the time yet... :-().

Anyway, this commit does just that; i.e., moves the `filetime` modules
into `yanix` which seems a better fit for this type of code.

[`filetime`]: https://github.com/alexcrichton/filetime

There is one caveat here. On Emscripten, converting between `filetime::Filetime`
and `libc::timespec` appears to be lossy, at least as far as the
types are concerned. Now, `filetime::Filetime`'s seconds field is
`i64` while nanoseconds field is `u32`, while Emscripten's
`libc::timespec` requires both to be `i32` width. This might actually
not be a problem since I don't think it's possible to fill `filetime::Filetime`
struct with values of width wider than `i32` since Emscripten is 32bit
but just to be on the safe side, we do a `TryInto` conversion, log
the error (if any), and return `libc::EOVERFLOW`.

* Run cargo fmt

* Use i64::from instead of as cast
This commit is contained in:
Jakub Konka
2020-03-09 16:07:09 +01:00
committed by GitHub
parent 8f824a9fc1
commit 061390ee1b
26 changed files with 73 additions and 429 deletions

1
Cargo.lock generated
View File

@@ -2605,6 +2605,7 @@ version = "0.12.0"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cfg-if", "cfg-if",
"filetime",
"libc", "libc",
"log", "log",
] ]

View File

@@ -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<usize> {
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;
}

View File

@@ -1,6 +1,3 @@
pub(crate) mod filetime;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
pub(crate) mod oshandle; pub(crate) mod oshandle;
#[path = "../linux/utimesat.rs"]
pub(crate) mod utimesat;

View File

@@ -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())
}
}

View File

@@ -1,4 +1,3 @@
pub(crate) mod filetime;
pub(crate) mod host_impl; pub(crate) mod host_impl;
#[path = "../linux/hostcalls_impl.rs"] #[path = "../linux/hostcalls_impl.rs"]
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;

View File

@@ -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<libc::timespec> {
Ok(
libc::timespec {
tv_sec: ft.seconds(),
tv_nsec: ft.nanoseconds().try_into()?,
}
)
}
} else {
fn filetime_to_timespec(ft: &filetime::FileTime) -> Result<libc::timespec> {
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<libc::timespec> {
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)
}

View File

@@ -225,8 +225,8 @@ pub(crate) fn path_filestat_set_times(
st_mtim: wasi::__wasi_timestamp_t, st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t, fst_flags: wasi::__wasi_fstflags_t,
) -> Result<()> { ) -> Result<()> {
use super::super::filetime::*;
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
use yanix::filetime::*;
let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0; let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0;
let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0; let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0;

View File

@@ -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)
}

View File

@@ -1,5 +1,3 @@
pub(crate) mod filetime;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
pub(crate) mod oshandle; pub(crate) mod oshandle;
pub(crate) mod utimesat;

View File

@@ -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<fs::Metadata>,
) -> 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))
}

View File

@@ -2,8 +2,6 @@ pub(crate) mod fdentry_impl;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
mod filetime;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] { if #[cfg(target_os = "linux")] {
mod linux; mod linux;

View File

@@ -1,6 +1,3 @@
pub(crate) mod filetime;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
pub(crate) mod oshandle; pub(crate) mod oshandle;
#[path = "../linux/utimesat.rs"]
pub(crate) mod utimesat;

View File

@@ -1,4 +1,3 @@
pub(crate) mod filetime;
#[path = "../linux/host_impl.rs"] #[path = "../linux/host_impl.rs"]
pub(crate) mod host_impl; pub(crate) mod host_impl;
#[path = "../linux/hostcalls_impl.rs"] #[path = "../linux/hostcalls_impl.rs"]

View File

@@ -225,8 +225,8 @@ pub(crate) fn path_filestat_set_times(
st_mtim: wasi::__wasi_timestamp_t, st_mtim: wasi::__wasi_timestamp_t,
fst_flags: wasi::__wasi_fstflags_t, fst_flags: wasi::__wasi_fstflags_t,
) -> Result<()> { ) -> Result<()> {
use super::super::filetime::*;
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
use yanix::filetime::*;
let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0; let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0;
let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0; let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0;

View File

@@ -1,5 +1,3 @@
pub(crate) mod filetime;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
pub(crate) mod oshandle; pub(crate) mod oshandle;
pub(crate) mod utimesat;

View File

@@ -2,8 +2,6 @@ pub(crate) mod fdentry_impl;
pub(crate) mod host_impl; pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl; pub(crate) mod hostcalls_impl;
mod filetime;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] { if #[cfg(target_os = "linux")] {
mod linux; mod linux;

View File

@@ -12,6 +12,7 @@ log = "0.4"
libc = { version = "0.2", features = ["extra_traits"] } libc = { version = "0.2", features = ["extra_traits"] }
bitflags = "1.2" bitflags = "1.2"
cfg-if = "0.1.9" cfg-if = "0.1.9"
filetime = "0.2.7"
[badges] [badges]
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }

View File

@@ -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). //! 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 //! The vast majority of the code contained within and in platform-specific implementations
@@ -6,10 +6,9 @@
//! Kudos @alexcrichton! //! Kudos @alexcrichton!
//! //!
//! [filetime]: https://github.com/alexcrichton/filetime //! [filetime]: https://github.com/alexcrichton/filetime
use crate::Result; use std::io::Result;
use std::convert::TryInto;
pub(crate) use super::sys_impl::filetime::*; pub use super::sys::filetime::*;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(not(target_os = "emscripten"))] { if #[cfg(not(target_os = "emscripten"))] {
@@ -17,18 +16,33 @@ cfg_if::cfg_if! {
Ok( Ok(
libc::timespec { libc::timespec {
tv_sec: ft.seconds(), tv_sec: ft.seconds(),
tv_nsec: ft.nanoseconds().try_into()?, tv_nsec: i64::from(ft.nanoseconds()),
} }
) )
} }
} else { } else {
fn filetime_to_timespec(ft: &filetime::FileTime) -> Result<libc::timespec> { fn filetime_to_timespec(ft: &filetime::FileTime) -> Result<libc::timespec> {
Ok( use std::convert::TryInto;
libc::timespec { use std::io::Error;
tv_sec: ft.seconds().try_into()?, // Emscripten expects both `tv_sec` and `tv_nsec` fields to be `i32`.
tv_nsec: ft.nanoseconds().try_into()?, // 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 /// in turn, if `utimensat` is available on the host, will use a special const setting
/// `UTIME_NOW`. /// `UTIME_NOW`.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub(crate) enum FileTime { pub enum FileTime {
Now, Now,
Omit, Omit,
FileTime(filetime::FileTime), FileTime(filetime::FileTime),

View File

@@ -13,6 +13,7 @@ pub mod clock;
pub mod dir; pub mod dir;
pub mod fcntl; pub mod fcntl;
pub mod file; pub mod file;
pub mod filetime;
pub mod poll; pub mod poll;
pub mod socket; pub mod socket;

View File

@@ -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. //! 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 cfg_if::cfg_if;
use std::ffi::CStr; use std::ffi::CStr;
use std::fs::File; use std::fs::File;
use std::io; use std::io::Result;
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
cfg_if! { cfg_if! {
@@ -35,14 +36,14 @@ cfg_if! {
/// The original implementation can be found here: [filetime::unix::macos::set_times] /// 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 /// [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, dirfd: &File,
path: &str, path: &str,
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> Result<()> { ) -> Result<()> {
use crate::sys::unix::filetime::to_timespec; use crate::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -57,12 +58,9 @@ pub(crate) fn utimensat(
let p = CString::new(path.as_bytes())?; let p = CString::new(path.as_bytes())?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let rc = unsafe { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; return from_success_code(unsafe {
if rc == 0 { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags)
return Ok(()); });
} else {
return Err(io::Error::last_os_error().into());
}
} }
super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)

View File

@@ -1,3 +1,6 @@
pub(crate) mod dir; pub(crate) mod dir;
pub(crate) mod fadvise; pub(crate) mod fadvise;
pub(crate) mod file; pub(crate) mod file;
pub(crate) mod filetime;
#[path = "../linux/utimesat.rs"]
pub(crate) mod utimesat;

View File

@@ -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. //! 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::fs::File;
use std::io; use std::io::Result;
pub(crate) const UTIME_NOW: i32 = 1_073_741_823; pub(crate) const UTIME_NOW: i32 = 1_073_741_823;
pub(crate) const UTIME_OMIT: i32 = 1_073_741_822; pub(crate) const UTIME_OMIT: i32 = 1_073_741_822;
/// Wrapper for `utimensat` syscall. In Emscripten, there is no point in dynamically resolving /// Wrapper for `utimensat` syscall. In Emscripten, there is no point in dynamically resolving
/// if `utimensat` is available as it always was and will be. /// if `utimensat` is available as it always was and will be.
pub(crate) fn utimensat( pub fn utimensat(
dirfd: &File, dirfd: &File,
path: &str, path: &str,
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> Result<()> { ) -> Result<()> {
use crate::sys::unix::filetime::to_timespec; use crate::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -27,10 +28,7 @@ pub(crate) fn utimensat(
}; };
let p = CString::new(path.as_bytes())?; let p = CString::new(path.as_bytes())?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let rc = unsafe { libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) }; from_success_code(unsafe {
if rc == 0 { libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags)
Ok(()) })
} else {
Err(io::Error::last_os_error().into())
}
} }

View File

@@ -4,6 +4,7 @@ pub(crate) mod dir;
pub(crate) mod fadvise; pub(crate) mod fadvise;
#[path = "../linux/file.rs"] #[path = "../linux/file.rs"]
pub(crate) mod file; pub(crate) mod file;
pub(crate) mod filetime;
use crate::dir::SeekLoc; use crate::dir::SeekLoc;
use std::convert::TryInto; use std::convert::TryInto;

View File

@@ -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. //! 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::fs::File;
use std::io; use std::io::Result;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
pub(crate) const UTIME_NOW: i64 = 1_073_741_823; 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] /// 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 /// [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, dirfd: &File,
path: &str, path: &str,
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> Result<()> { ) -> Result<()> {
use crate::sys::unix::filetime::to_timespec; use crate::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -37,7 +38,7 @@ pub(crate) fn utimensat(
if !INVALID.load(Relaxed) { if !INVALID.load(Relaxed) {
let p = CString::new(path.as_bytes())?; let p = CString::new(path.as_bytes())?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?]; let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let rc = unsafe { let res = from_success_code(unsafe {
libc::syscall( libc::syscall(
libc::SYS_utimensat, libc::SYS_utimensat,
dirfd.as_raw_fd(), dirfd.as_raw_fd(),
@@ -45,16 +46,15 @@ pub(crate) fn utimensat(
times.as_ptr(), times.as_ptr(),
flags, flags,
) )
});
let err = match res {
Ok(()) => return Ok(()),
Err(e) => e,
}; };
if rc == 0 { if err.raw_os_error().unwrap() == libc::ENOSYS {
return Ok(());
}
let err = io::Error::last_os_error();
if err.raw_os_error() == Some(libc::ENOSYS) {
INVALID.store(true, Relaxed); INVALID.store(true, Relaxed);
} else {
return Err(err.into());
} }
return Err(err);
} }
super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)

View File

@@ -1,6 +1,8 @@
pub(crate) mod dir; pub(crate) mod dir;
pub(crate) mod fadvise; pub(crate) mod fadvise;
pub(crate) mod file; pub(crate) mod file;
pub(crate) mod filetime;
pub(crate) mod utimesat;
use crate::dir::SeekLoc; use crate::dir::SeekLoc;
use std::io::Result; use std::io::Result;

View File

@@ -1,12 +1,13 @@
use crate::sys::unix::filetime::FileTime; use crate::filetime::FileTime;
use crate::Result; use crate::from_success_code;
use std::{fs, io}; use std::fs;
use std::io::Result;
/// Combines `openat` with `utimes` to emulate `utimensat` on platforms where it is /// 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]. /// 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 /// [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, dirfd: &fs::File,
path: &str, path: &str,
atime: FileTime, atime: FileTime,
@@ -26,12 +27,7 @@ pub(crate) fn utimesat(
let f = unsafe { fs::File::from_raw_fd(fd) }; let f = unsafe { fs::File::from_raw_fd(fd) };
let (atime, mtime) = get_times(atime, mtime, || f.metadata().map_err(Into::into))?; let (atime, mtime) = get_times(atime, mtime, || f.metadata().map_err(Into::into))?;
let times = [to_timeval(atime), to_timeval(mtime)]; let times = [to_timeval(atime), to_timeval(mtime)];
let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }; from_success_code(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 /// Converts `filetime::FileTime` to `libc::timeval`. This function was taken directly from