Compile wasi-common to Emscripten (#688)
* Compile wasi-common to Emscripten This commit enables cross-compiling of `wasi-common` to Emscripten. To achieve this, this commit does quite a bit reshuffling in the existing codebase. Namely, * rename `linux` modules in `wasi-common` and `yanix` to `linux_like` -- this is needed so that we can separate out logic specific to Linux and Emscripten out * tweak `dir` module in `yanix` to support Emscripten -- in particular, the main change involves `SeekLoc::from_raw` which has to be now host-specific, and is now fallible * tweak `filetime` so that in Emscripten we never check for existence of `utimensat` at runtime since we are guaranteed for it to exist by design * since `utimes` and `futimes` are not present in Emscripten, move them into a separate module, `utimesat`, and tag it cfg-non-emscripten only * finally, `to_timespec` is now fallible since on Emscripten we have to cast number of seconds, `FileTime::seconds` from `i64` to `libc::c_long` which resolves to `i32` unlike on other nixes * Fix macos build * Verify wasi-common compiles to Emscripten This commit adds `emscripten` job to Github Actions which installs `wasm32-unknown-emscripten` target, and builds `wasi-common` crate. * Use #[path] to cherry-pick mods for Emscripten This commit effectively reverses the reorg introduced in 145f4a5 in that it ditches `linux_like` mod for separate mods `linux` and `emscripten` which are now on the same crate level, and instead, pulls in common bits from `linux` using the `#[path = ..]` proc macro.
This commit is contained in:
committed by
Alex Crichton
parent
ddd2300010
commit
95c2addf15
@@ -6,21 +6,30 @@
|
||||
//! Kudos @alexcrichton!
|
||||
//!
|
||||
//! [filetime]: https://github.com/alexcrichton/filetime
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use crate::Result;
|
||||
use std::convert::TryInto;
|
||||
|
||||
pub(crate) use super::sys_impl::filetime::*;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
pub(crate) use super::linux::filetime::*;
|
||||
} else if #[cfg(any(
|
||||
target_os = "macos",
|
||||
target_os = "netbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "ios",
|
||||
target_os = "dragonfly"
|
||||
))] {
|
||||
pub(crate) use super::bsd::filetime::*;
|
||||
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()?,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,91 +44,6 @@ pub(crate) enum FileTime {
|
||||
FileTime(filetime::FileTime),
|
||||
}
|
||||
|
||||
/// 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() -> io::Result<fs::Metadata>,
|
||||
) -> io::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))
|
||||
}
|
||||
|
||||
/// 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: &File,
|
||||
path: &str,
|
||||
atime: FileTime,
|
||||
mtime: FileTime,
|
||||
symlink_nofollow: bool,
|
||||
) -> io::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 { File::from_raw_fd(fd) };
|
||||
let (atime, mtime) = get_times(atime, mtime, || f.metadata())?;
|
||||
let times = [to_timeval(atime), to_timeval(mtime)];
|
||||
let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) };
|
||||
return if rc == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(io::Error::last_os_error())
|
||||
};
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
@@ -127,8 +51,8 @@ fn to_timeval(ft: filetime::FileTime) -> libc::timeval {
|
||||
///
|
||||
/// [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) -> libc::timespec {
|
||||
match ft {
|
||||
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,
|
||||
@@ -137,13 +61,7 @@ pub(crate) fn to_timespec(ft: &FileTime) -> libc::timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: UTIME_OMIT,
|
||||
},
|
||||
// `filetime::FileTime`'s fields are normalised by definition. `ft.seconds()` return the number
|
||||
// of whole seconds, while `ft.nanoseconds()` returns only fractional part expressed in
|
||||
// nanoseconds, as underneath it uses `std::time::Duration::subsec_nanos` to populate the
|
||||
// `filetime::FileTime::nanoseconds` field. It is, therefore, OK to do an `as` cast here.
|
||||
FileTime::FileTime(ft) => libc::timespec {
|
||||
tv_sec: ft.seconds(),
|
||||
tv_nsec: ft.nanoseconds() as _,
|
||||
},
|
||||
}
|
||||
FileTime::FileTime(ft) => filetime_to_timespec(ft)?,
|
||||
};
|
||||
Ok(ts)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user