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:
Jakub Konka
2019-12-12 01:25:13 +01:00
committed by Alex Crichton
parent ddd2300010
commit 95c2addf15
38 changed files with 541 additions and 434 deletions

View File

@@ -87,6 +87,19 @@ jobs:
- run: cargo fuzz run instantiate_translated --release --debug-assertions -- -runs=0 - run: cargo fuzz run instantiate_translated --release --debug-assertions -- -runs=0
- run: cargo fuzz run api_calls --release --debug-assertions -- -runs=0 - run: cargo fuzz run api_calls --release --debug-assertions -- -runs=0
# Install wasm32-unknown-emscripten target, and ensure `crates/wasi-common`
# compiles to Emscripten.
emscripten:
name: Emscripten
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
submodules: true
- uses: ./.github/actions/install-rust
- run: rustup target add wasm32-unknown-emscripten
- run: cargo build --target wasm32-unknown-emscripten -p wasi-common
# Perform all tests (debug mode) for `wasmtime`. This runs stable/beta/nightly # Perform all tests (debug mode) for `wasmtime`. This runs stable/beta/nightly
# channels of Rust as well as macOS/Linux/Windows. # channels of Rust as well as macOS/Linux/Windows.
test: test:

View File

@@ -149,6 +149,12 @@ impl From<str::Utf8Error> for Error {
} }
} }
impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
}
}
impl From<&ffi::NulError> for Error { impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self { fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::Wasi(WasiError::EILSEQ)

View File

@@ -149,6 +149,12 @@ impl From<str::Utf8Error> for Error {
} }
} }
impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
}
}
impl From<&ffi::NulError> for Error { impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self { fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::Wasi(WasiError::EILSEQ)

View File

@@ -1,6 +1,6 @@
//! This internal module consists of helper types and functions for dealing //! This internal 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 super::super::filetime::FileTime; use crate::old::snapshot_0::{sys::unix::filetime::FileTime, Result};
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;
@@ -41,8 +41,8 @@ pub(crate) fn utimensat(
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> io::Result<()> { ) -> Result<()> {
use super::super::filetime::{to_timespec, utimesat}; use crate::old::snapshot_0::sys::unix::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -56,16 +56,16 @@ 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) }; let rc = unsafe { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) };
if rc == 0 { if rc == 0 {
return Ok(()); return Ok(());
} else { } else {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error().into());
} }
} }
utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)
} }
/// Wraps `fetch` specifically targetting `utimensat` symbol. If the symbol exists /// Wraps `fetch` specifically targetting `utimensat` symbol. If the symbol exists

View File

@@ -0,0 +1,16 @@
use crate::old::snapshot_0::{wasi, Result};
use std::convert::TryFrom;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
wasi::__wasi_device_t::try_from(dev).map_err(Into::into)
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
wasi::__wasi_device_t::try_from(ino).map_err(Into::into)
}
pub(crate) fn stnlink_from_nix(nlink: libc::nlink_t) -> Result<wasi::__wasi_linkcount_t> {
Ok(nlink.into())
}

View File

@@ -1,18 +1,6 @@
pub(crate) mod filetime; pub(crate) mod filetime;
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 host_impl { pub(crate) mod utimesat;
use crate::old::snapshot_0::{wasi, Result};
use std::convert::TryFrom;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
wasi::__wasi_device_t::try_from(dev).map_err(Into::into)
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
wasi::__wasi_device_t::try_from(ino).map_err(Into::into)
}
}

View File

@@ -0,0 +1,36 @@
//! 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

@@ -0,0 +1,15 @@
use crate::old::snapshot_0::{wasi, Result};
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
Ok(wasi::__wasi_device_t::from(ino))
}
pub(crate) fn stnlink_from_nix(nlink: libc::nlink_t) -> Result<wasi::__wasi_linkcount_t> {
Ok(nlink)
}

View File

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

View File

@@ -1,24 +1,11 @@
use crate::old::snapshot_0::fdentry::{Descriptor, OsHandleRef}; use crate::old::snapshot_0::fdentry::{Descriptor, OsHandleRef};
use crate::old::snapshot_0::{wasi, Error, Result}; use crate::old::snapshot_0::{sys::unix::sys_impl, wasi, Error, Result};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd}; use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
cfg_if::cfg_if! { pub(crate) use sys_impl::oshandle::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::linux::oshandle::*;
} 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::oshandle::*;
}
}
impl AsRawFd for Descriptor { impl AsRawFd for Descriptor {
fn as_raw_fd(&self) -> RawFd { fn as_raw_fd(&self) -> RawFd {

View File

@@ -6,21 +6,30 @@
//! Kudos @alexcrichton! //! Kudos @alexcrichton!
//! //!
//! [filetime]: https://github.com/alexcrichton/filetime //! [filetime]: https://github.com/alexcrichton/filetime
use std::fs::{self, File}; use crate::old::snapshot_0::Result;
use std::io; use std::convert::TryInto;
pub(crate) use super::sys_impl::filetime::*;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] { if #[cfg(not(target_os = "emscripten"))] {
pub(crate) use super::linux::filetime::*; fn filetime_to_timespec(ft: &filetime::FileTime) -> Result<libc::timespec> {
} else if #[cfg(any( Ok(
target_os = "macos", libc::timespec {
target_os = "netbsd", tv_sec: ft.seconds(),
target_os = "freebsd", tv_nsec: ft.nanoseconds().try_into()?,
target_os = "openbsd", }
target_os = "ios", )
target_os = "dragonfly" }
))] { } else {
pub(crate) use super::bsd::filetime::*; 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), 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 /// 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 /// 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 /// `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]: https://github.com/alexcrichton/filetime
/// [filetime::unix::to_timespec]: https://github.com/alexcrichton/filetime/blob/master/src/unix/mod.rs#L30 /// [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 { pub(crate) fn to_timespec(ft: &FileTime) -> Result<libc::timespec> {
match ft { let ts = match ft {
FileTime::Now => libc::timespec { FileTime::Now => libc::timespec {
tv_sec: 0, tv_sec: 0,
tv_nsec: UTIME_NOW, tv_nsec: UTIME_NOW,
@@ -137,13 +61,7 @@ pub(crate) fn to_timespec(ft: &FileTime) -> libc::timespec {
tv_sec: 0, tv_sec: 0,
tv_nsec: UTIME_OMIT, tv_nsec: UTIME_OMIT,
}, },
// `filetime::FileTime`'s fields are normalised by definition. `ft.seconds()` return the number FileTime::FileTime(ft) => filetime_to_timespec(ft)?,
// 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 Ok(ts)
// `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 _,
},
}
} }

View File

@@ -3,7 +3,7 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use crate::old::snapshot_0::host::FileType; use crate::old::snapshot_0::host::FileType;
use crate::old::snapshot_0::{helpers, wasi, Error, Result}; use crate::old::snapshot_0::{helpers, sys::unix::sys_impl, wasi, Error, Result};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use yanix::{ use yanix::{
@@ -11,20 +11,7 @@ use yanix::{
Errno, Errno,
}; };
cfg_if::cfg_if! { pub(crate) use sys_impl::host_impl::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::linux::host_impl::*;
} 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::host_impl::*;
}
}
pub(crate) fn errno_from_nix(errno: Errno) -> Error { pub(crate) fn errno_from_nix(errno: Errno) -> Error {
match errno { match errno {
@@ -197,7 +184,7 @@ pub(crate) fn filestat_from_nix(filestat: libc::stat) -> Result<wasi::__wasi_fil
Ok(wasi::__wasi_filestat_t { Ok(wasi::__wasi_filestat_t {
dev, dev,
ino, ino,
nlink: filestat.st_nlink as wasi::__wasi_linkcount_t, nlink: stnlink_from_nix(filestat.st_nlink)?,
size: filestat.st_size as wasi::__wasi_filesize_t, size: filestat.st_size as wasi::__wasi_filesize_t,
atim, atim,
ctim, ctim,

View File

@@ -3,27 +3,14 @@
use crate::old::snapshot_0::helpers::systemtime_to_timestamp; use crate::old::snapshot_0::helpers::systemtime_to_timestamp;
use crate::old::snapshot_0::host::{Dirent, FileType}; use crate::old::snapshot_0::host::{Dirent, FileType};
use crate::old::snapshot_0::hostcalls_impl::PathGet; use crate::old::snapshot_0::hostcalls_impl::PathGet;
use crate::old::snapshot_0::sys::{fdentry_impl::OsHandle, host_impl}; use crate::old::snapshot_0::sys::{fdentry_impl::OsHandle, host_impl, unix::sys_impl};
use crate::old::snapshot_0::{wasi, Error, Result}; use crate::old::snapshot_0::{wasi, Error, Result};
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
use std::os::unix::fs::FileExt; use std::os::unix::fs::FileExt;
use std::os::unix::prelude::{AsRawFd, FromRawFd}; use std::os::unix::prelude::{AsRawFd, FromRawFd};
cfg_if::cfg_if! { pub(crate) use sys_impl::hostcalls_impl::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::super::linux::hostcalls_impl::*;
} 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::super::bsd::hostcalls_impl::*;
}
}
pub(crate) fn fd_pread( pub(crate) fn fd_pread(
file: &File, file: &File,
@@ -342,7 +329,7 @@ pub(crate) fn fd_readdir<'a>(
dir.rewind(); dir.rewind();
} else { } else {
log::trace!(" | fd_readdir: doing seekdir to {}", cookie); log::trace!(" | fd_readdir: doing seekdir to {}", cookie);
let loc = unsafe { SeekLoc::from_raw(cookie as i64) }; let loc = unsafe { SeekLoc::from_raw(cookie as i64)? };
dir.seek(loc); dir.seek(loc);
} }
@@ -356,7 +343,7 @@ pub(crate) fn fd_readdir<'a>(
.to_owned(), .to_owned(),
ino: entry.ino(), ino: entry.ino(),
ftype: entry.file_type().into(), ftype: entry.file_type().into(),
cookie: entry.seek_loc().to_raw().try_into()?, cookie: entry.seek_loc()?.to_raw().try_into()?,
}) })
})) }))
} }

View File

@@ -1,6 +1,6 @@
//! This internal module consists of helper types and functions for dealing //! This internal 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 super::super::filetime::FileTime; use crate::old::snapshot_0::{sys::unix::filetime::FileTime, Result};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
@@ -20,8 +20,8 @@ pub(crate) fn utimensat(
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> io::Result<()> { ) -> Result<()> {
use super::super::filetime::{to_timespec, utimesat}; use crate::old::snapshot_0::sys::unix::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -53,9 +53,9 @@ pub(crate) fn utimensat(
if err.raw_os_error() == Some(libc::ENOSYS) { if err.raw_os_error() == Some(libc::ENOSYS) {
INVALID.store(true, Relaxed); INVALID.store(true, Relaxed);
} else { } else {
return Err(err); return Err(err.into());
} }
} }
utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)
} }

View File

@@ -0,0 +1,16 @@
use crate::old::snapshot_0::{wasi, Result};
use std::convert::TryInto;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
Ok(wasi::__wasi_device_t::from(ino))
}
pub(crate) fn stnlink_from_nix(nlink: libc::nlink_t) -> Result<wasi::__wasi_linkcount_t> {
nlink.try_into().map_err(Into::into)
}

View File

@@ -1,17 +1,5 @@
pub(crate) mod filetime; pub(crate) mod filetime;
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;
pub(crate) mod host_impl {
use crate::old::snapshot_0::{wasi, Result};
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
Ok(wasi::__wasi_device_t::from(ino))
}
}

View File

@@ -0,0 +1,88 @@
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

@@ -4,17 +4,23 @@ pub(crate) mod hostcalls_impl;
mod filetime; mod filetime;
#[cfg(any( cfg_if::cfg_if! {
target_os = "macos", if #[cfg(target_os = "linux")] {
mod linux;
use self::linux as sys_impl;
} else if #[cfg(target_os = "emscripten")] {
mod emscripten;
use self::emscripten as sys_impl;
} else if #[cfg(any(target_os = "macos",
target_os = "netbsd", target_os = "netbsd",
target_os = "freebsd", target_os = "freebsd",
target_os = "openbsd", target_os = "openbsd",
target_os = "ios", target_os = "ios",
target_os = "dragonfly" target_os = "dragonfly"))] {
))]
mod bsd; mod bsd;
#[cfg(target_os = "linux")] use self::bsd as sys_impl;
mod linux; }
}
use crate::old::snapshot_0::Result; use crate::old::snapshot_0::Result;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};

View File

@@ -1,6 +1,6 @@
//! This internal module consists of helper types and functions for dealing //! This internal 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 super::super::filetime::FileTime; use crate::{sys::unix::filetime::FileTime, Result};
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;
@@ -41,8 +41,8 @@ pub(crate) fn utimensat(
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> io::Result<()> { ) -> Result<()> {
use super::super::filetime::{to_timespec, utimesat}; use crate::sys::unix::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -56,16 +56,16 @@ 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) }; let rc = unsafe { func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags) };
if rc == 0 { if rc == 0 {
return Ok(()); return Ok(());
} else { } else {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error().into());
} }
} }
utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)
} }
/// Wraps `fetch` specifically targetting `utimensat` symbol. If the symbol exists /// Wraps `fetch` specifically targetting `utimensat` symbol. If the symbol exists

View File

@@ -0,0 +1,12 @@
use crate::{wasi, Result};
use std::convert::TryFrom;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
wasi::__wasi_device_t::try_from(dev).map_err(Into::into)
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
wasi::__wasi_device_t::try_from(ino).map_err(Into::into)
}

View File

@@ -1,18 +1,6 @@
pub(crate) mod filetime; pub(crate) mod filetime;
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 host_impl { pub(crate) mod utimesat;
use crate::{wasi, Result};
use std::convert::TryFrom;
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
wasi::__wasi_device_t::try_from(dev).map_err(Into::into)
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
wasi::__wasi_device_t::try_from(ino).map_err(Into::into)
}
}

View File

@@ -0,0 +1,36 @@
//! This internal 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 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::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

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

View File

@@ -1,24 +1,11 @@
use crate::fdentry::{Descriptor, OsHandleRef}; use crate::fdentry::{Descriptor, OsHandleRef};
use crate::{wasi, Error, Result}; use crate::{sys::unix::sys_impl, wasi, Error, Result};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd}; use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
cfg_if::cfg_if! { pub(crate) use sys_impl::oshandle::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::linux::oshandle::*;
} 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::oshandle::*;
}
}
impl AsRawFd for Descriptor { impl AsRawFd for Descriptor {
fn as_raw_fd(&self) -> RawFd { fn as_raw_fd(&self) -> RawFd {

View File

@@ -6,21 +6,30 @@
//! Kudos @alexcrichton! //! Kudos @alexcrichton!
//! //!
//! [filetime]: https://github.com/alexcrichton/filetime //! [filetime]: https://github.com/alexcrichton/filetime
use std::fs::{self, File}; use crate::Result;
use std::io; use std::convert::TryInto;
pub(crate) use super::sys_impl::filetime::*;
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] { if #[cfg(not(target_os = "emscripten"))] {
pub(crate) use super::linux::filetime::*; fn filetime_to_timespec(ft: &filetime::FileTime) -> Result<libc::timespec> {
} else if #[cfg(any( Ok(
target_os = "macos", libc::timespec {
target_os = "netbsd", tv_sec: ft.seconds(),
target_os = "freebsd", tv_nsec: ft.nanoseconds().try_into()?,
target_os = "openbsd", }
target_os = "ios", )
target_os = "dragonfly" }
))] { } else {
pub(crate) use super::bsd::filetime::*; 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), 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 /// 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 /// 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 /// `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]: https://github.com/alexcrichton/filetime
/// [filetime::unix::to_timespec]: https://github.com/alexcrichton/filetime/blob/master/src/unix/mod.rs#L30 /// [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 { pub(crate) fn to_timespec(ft: &FileTime) -> Result<libc::timespec> {
match ft { let ts = match ft {
FileTime::Now => libc::timespec { FileTime::Now => libc::timespec {
tv_sec: 0, tv_sec: 0,
tv_nsec: UTIME_NOW, tv_nsec: UTIME_NOW,
@@ -137,13 +61,7 @@ pub(crate) fn to_timespec(ft: &FileTime) -> libc::timespec {
tv_sec: 0, tv_sec: 0,
tv_nsec: UTIME_OMIT, tv_nsec: UTIME_OMIT,
}, },
// `filetime::FileTime`'s fields are normalised by definition. `ft.seconds()` return the number FileTime::FileTime(ft) => filetime_to_timespec(ft)?,
// 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 Ok(ts)
// `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 _,
},
}
} }

View File

@@ -3,7 +3,7 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use crate::host::FileType; use crate::host::FileType;
use crate::{helpers, wasi, Error, Result}; use crate::{helpers, sys::unix::sys_impl, wasi, Error, Result};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use yanix::{ use yanix::{
@@ -11,20 +11,7 @@ use yanix::{
Errno, Errno,
}; };
cfg_if::cfg_if! { pub(crate) use sys_impl::host_impl::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::linux::host_impl::*;
} 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::host_impl::*;
}
}
pub(crate) fn errno_from_nix(errno: Errno) -> Error { pub(crate) fn errno_from_nix(errno: Errno) -> Error {
match errno { match errno {

View File

@@ -3,27 +3,14 @@
use crate::helpers::systemtime_to_timestamp; use crate::helpers::systemtime_to_timestamp;
use crate::host::{Dirent, FileType}; use crate::host::{Dirent, FileType};
use crate::hostcalls_impl::PathGet; use crate::hostcalls_impl::PathGet;
use crate::sys::{fdentry_impl::OsHandle, host_impl}; use crate::sys::{fdentry_impl::OsHandle, host_impl, unix::sys_impl};
use crate::{wasi, Error, Result}; use crate::{wasi, Error, Result};
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
use std::os::unix::fs::FileExt; use std::os::unix::fs::FileExt;
use std::os::unix::prelude::{AsRawFd, FromRawFd}; use std::os::unix::prelude::{AsRawFd, FromRawFd};
cfg_if::cfg_if! { pub(crate) use sys_impl::hostcalls_impl::*;
if #[cfg(target_os = "linux")] {
pub(crate) use super::super::linux::hostcalls_impl::*;
} 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::super::bsd::hostcalls_impl::*;
}
}
pub(crate) fn fd_pread( pub(crate) fn fd_pread(
file: &File, file: &File,
@@ -342,7 +329,7 @@ pub(crate) fn fd_readdir<'a>(
dir.rewind(); dir.rewind();
} else { } else {
log::trace!(" | fd_readdir: doing seekdir to {}", cookie); log::trace!(" | fd_readdir: doing seekdir to {}", cookie);
let loc = unsafe { SeekLoc::from_raw(cookie as i64) }; let loc = unsafe { SeekLoc::from_raw(cookie as i64)? };
dir.seek(loc); dir.seek(loc);
} }
@@ -356,7 +343,7 @@ pub(crate) fn fd_readdir<'a>(
.to_owned(), .to_owned(),
ino: entry.ino(), ino: entry.ino(),
ftype: entry.file_type().into(), ftype: entry.file_type().into(),
cookie: entry.seek_loc().to_raw().try_into()?, cookie: entry.seek_loc()?.to_raw().try_into()?,
}) })
})) }))
} }

View File

@@ -1,6 +1,6 @@
//! This internal module consists of helper types and functions for dealing //! This internal 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 super::super::filetime::FileTime; use crate::{sys::unix::filetime::FileTime, Result};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
@@ -20,8 +20,8 @@ pub(crate) fn utimensat(
atime: FileTime, atime: FileTime,
mtime: FileTime, mtime: FileTime,
symlink_nofollow: bool, symlink_nofollow: bool,
) -> io::Result<()> { ) -> Result<()> {
use super::super::filetime::{to_timespec, utimesat}; use crate::sys::unix::filetime::to_timespec;
use std::ffi::CString; use std::ffi::CString;
use std::os::unix::prelude::*; use std::os::unix::prelude::*;
@@ -53,9 +53,9 @@ pub(crate) fn utimensat(
if err.raw_os_error() == Some(libc::ENOSYS) { if err.raw_os_error() == Some(libc::ENOSYS) {
INVALID.store(true, Relaxed); INVALID.store(true, Relaxed);
} else { } else {
return Err(err); return Err(err.into());
} }
} }
utimesat(dirfd, path, atime, mtime, symlink_nofollow) super::utimesat::utimesat(dirfd, path, atime, mtime, symlink_nofollow)
} }

View File

@@ -0,0 +1,11 @@
use crate::{wasi, Result};
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
Ok(wasi::__wasi_device_t::from(ino))
}

View File

@@ -1,17 +1,5 @@
pub(crate) mod filetime; pub(crate) mod filetime;
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;
pub(crate) mod host_impl {
use crate::{wasi, Result};
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> Result<wasi::__wasi_device_t> {
Ok(wasi::__wasi_device_t::from(dev))
}
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> Result<wasi::__wasi_inode_t> {
Ok(wasi::__wasi_device_t::from(ino))
}
}

View File

@@ -0,0 +1,88 @@
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))
}

View File

@@ -4,17 +4,23 @@ pub(crate) mod hostcalls_impl;
mod filetime; mod filetime;
#[cfg(any( cfg_if::cfg_if! {
target_os = "macos", if #[cfg(target_os = "linux")] {
mod linux;
use self::linux as sys_impl;
} else if #[cfg(target_os = "emscripten")] {
mod emscripten;
use self::emscripten as sys_impl;
} else if #[cfg(any(target_os = "macos",
target_os = "netbsd", target_os = "netbsd",
target_os = "freebsd", target_os = "freebsd",
target_os = "openbsd", target_os = "openbsd",
target_os = "ios", target_os = "ios",
target_os = "dragonfly" target_os = "dragonfly"))] {
))]
mod bsd; mod bsd;
#[cfg(target_os = "linux")] use self::bsd as sys_impl;
mod linux; }
}
use crate::Result; use crate::Result;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};

View File

@@ -84,14 +84,10 @@ impl Entry {
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct SeekLoc(libc::c_long); pub struct SeekLoc(pub(crate) libc::c_long);
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
impl SeekLoc { impl SeekLoc {
pub unsafe fn from_raw(loc: i64) -> Self {
Self(loc.into())
}
pub fn to_raw(&self) -> i64 { pub fn to_raw(&self) -> i64 {
self.0.into() self.0.into()
} }

View File

@@ -46,7 +46,14 @@ impl EntryExt for Entry {
self.0.d_ino.into() self.0.d_ino.into()
} }
fn seek_loc(&self) -> SeekLoc { fn seek_loc(&self) -> Result<SeekLoc> {
self.0.loc Ok(self.0.loc)
}
}
impl SeekLoc {
pub unsafe fn from_raw(loc: i64) -> Result<Self> {
let loc = loc.into();
Ok(Self(loc))
} }
} }

View File

@@ -0,0 +1,16 @@
#[path = "../linux/dir.rs"]
pub(crate) mod dir;
#[path = "../linux/fadvise.rs"]
pub(crate) mod fadvise;
#[path = "../linux/file.rs"]
pub(crate) mod file;
use crate::{dir::SeekLoc, Result};
use std::convert::TryInto;
impl SeekLoc {
pub unsafe fn from_raw(loc: i64) -> Result<Self> {
let loc = loc.try_into()?;
Ok(Self(loc))
}
}

View File

@@ -20,7 +20,7 @@ impl EntryExt for Entry {
self.0.d_ino.into() self.0.d_ino.into()
} }
fn seek_loc(&self) -> SeekLoc { fn seek_loc(&self) -> Result<SeekLoc> {
unsafe { SeekLoc::from_raw(self.0.d_off) } unsafe { SeekLoc::from_raw(self.0.d_off) }
} }
} }

View File

@@ -1,3 +1,12 @@
pub(crate) mod dir; pub(crate) mod dir;
pub(crate) mod fadvise; pub(crate) mod fadvise;
pub(crate) mod file; pub(crate) mod file;
use crate::{dir::SeekLoc, Result};
impl SeekLoc {
pub unsafe fn from_raw(loc: i64) -> Result<Self> {
let loc = loc.into();
Ok(Self(loc))
}
}

View File

@@ -1,14 +1,15 @@
use crate::dir::SeekLoc; use crate::{dir::SeekLoc, Result};
use cfg_if::cfg_if; use cfg_if::cfg_if;
cfg_if! { cfg_if! {
if #[cfg(any(target_os = "linux", if #[cfg(any(target_os = "linux",
target_os = "android", target_os = "android"))] {
target_os = "emscripten"))] {
mod linux; mod linux;
pub(crate) use self::linux::*; pub(crate) use self::linux::*;
} } else if #[cfg(target_os = "emscripten")] {
else if #[cfg(any(target_os = "macos", mod emscripten;
pub(crate) use self::emscripten::*;
} else if #[cfg(any(target_os = "macos",
target_os = "ios", target_os = "ios",
target_os = "freebsd", target_os = "freebsd",
target_os = "netbsd", target_os = "netbsd",
@@ -23,5 +24,5 @@ cfg_if! {
pub trait EntryExt { pub trait EntryExt {
fn ino(&self) -> u64; fn ino(&self) -> u64;
fn seek_loc(&self) -> SeekLoc; fn seek_loc(&self) -> Result<SeekLoc>;
} }