[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:
@@ -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::{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::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)
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
use crate::sys::unix::filetime::FileTime;
|
||||
use crate::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))
|
||||
}
|
||||
Reference in New Issue
Block a user