[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

View File

@@ -0,0 +1,101 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to BSD-style *nixes.
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::Result;
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 fn utimensat(
dirfd: &File,
path: &str,
atime: FileTime,
mtime: FileTime,
symlink_nofollow: bool,
) -> Result<()> {
use crate::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)?];
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)
}
/// 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,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;

View File

@@ -0,0 +1,34 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to Emscripten.
use crate::filetime::FileTime;
use crate::from_success_code;
use std::fs::File;
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 fn utimensat(
dirfd: &File,
path: &str,
atime: FileTime,
mtime: FileTime,
symlink_nofollow: bool,
) -> Result<()> {
use crate::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)?];
from_success_code(unsafe {
libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags)
})
}

View File

@@ -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;

View File

@@ -0,0 +1,61 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to Linux.
use crate::filetime::FileTime;
use crate::from_success_code;
use std::fs::File;
use std::io::Result;
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 fn utimensat(
dirfd: &File,
path: &str,
atime: FileTime,
mtime: FileTime,
symlink_nofollow: bool,
) -> Result<()> {
use crate::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 res = from_success_code(unsafe {
libc::syscall(
libc::SYS_utimensat,
dirfd.as_raw_fd(),
p.as_ptr(),
times.as_ptr(),
flags,
)
});
let err = match res {
Ok(()) => return Ok(()),
Err(e) => e,
};
if err.raw_os_error().unwrap() == libc::ENOSYS {
INVALID.store(true, Relaxed);
}
return Err(err);
}
super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)
}

View File

@@ -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;

View File

@@ -0,0 +1,84 @@
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 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)];
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
/// [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))
}