It's wiggle time! (#1202)
* Use wiggle in place of wig in wasi-common This is a rather massive commit that introduces `wiggle` into the picture. We still use `wig`'s macro in `old` snapshot and to generate `wasmtime-wasi` glue, but everything else is now autogenerated by `wiggle`. In summary, thanks to `wiggle`, we no longer need to worry about serialising and deserialising to and from the guest memory, and all guest (WASI) types are now proper idiomatic Rust types. While we're here, in preparation for the ephemeral snapshot, I went ahead and reorganised the internal structure of the crate. Instead of modules like `hostcalls_impl` or `hostcalls_impl::fs`, the structure now resembles that in ephemeral with modules like `path`, `fd`, etc. Now, I'm not requiring we leave it like this, but I reckon it looks cleaner this way after all. * Fix wig to use new first-class access to caller's mem * Ignore warning in proc_exit for the moment * Group unsafes together in args and environ calls * Simplify pwrite; more unsafe blocks * Simplify fd_read * Bundle up unsafes in fd_readdir * Simplify fd_write * Add comment to path_readlink re zero-len buffers * Simplify unsafes in random_get * Hide GuestPtr<str> to &str in path::get * Rewrite pread and pwrite using SeekFrom and read/write_vectored I've left the implementation of VirtualFs pretty much untouched as I don't feel that comfortable in changing the API too much. Having said that, I reckon `pread` and `pwrite` could be refactored out, and `preadv` and `pwritev` could be entirely rewritten using `seek` and `read_vectored` and `write_vectored`. * Add comment about VirtFs unsafety * Fix all mentions of FdEntry to Entry * Fix warnings on Win * Add aux struct EntryTable responsible for Fds and Entries This commit adds aux struct `EntryTable` which is private to `WasiCtx` and is basically responsible for `Fd` alloc/dealloc as well as storing matching `Entry`s. This struct is entirely private to `WasiCtx` and as such as should remain transparent to `WasiCtx` users. * Remove redundant check for empty buffer in path_readlink * Preserve and rewind file cursor in pread/pwrite * Use GuestPtr<[u8]>::copy_from_slice wherever copying bytes directly * Use GuestPtr<[u8]>::copy_from_slice in fd_readdir * Clean up unsafes around WasiCtx accessors * Fix bugs in args_get and environ_get * Fix conflicts after rebase
This commit is contained in:
26
crates/wasi-common/src/sys/unix/bsd/fd.rs
Normal file
26
crates/wasi-common/src/sys/unix/bsd/fd.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::wasi::Result;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use yanix::dir::Dir;
|
||||
|
||||
pub(crate) fn get_dir_from_os_handle<'a>(
|
||||
os_handle: &'a mut OsHandle,
|
||||
) -> Result<MutexGuard<'a, Dir>> {
|
||||
let dir = match os_handle.dir {
|
||||
Some(ref mut dir) => dir,
|
||||
None => {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// Upon successful return from fdopendir(), the file descriptor is under
|
||||
// control of the system, and if any attempt is made to close the file
|
||||
// descriptor, or to modify the state of the associated description other
|
||||
// than by means of closedir(), readdir(), readdir_r(), or rewinddir(),
|
||||
// the behaviour is undefined.
|
||||
let fd = (*os_handle).try_clone()?;
|
||||
let dir = Dir::from(fd)?;
|
||||
os_handle.dir.get_or_insert(Mutex::new(dir))
|
||||
}
|
||||
};
|
||||
// Note that from this point on, until the end of the parent scope (i.e., enclosing this
|
||||
// function), we're locking the `Dir` member of this `OsHandle`.
|
||||
Ok(dir.lock().unwrap())
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
use crate::wasi::{self, WasiResult};
|
||||
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) -> WasiResult<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) -> WasiResult<wasi::__wasi_inode_t> {
|
||||
wasi::__wasi_device_t::try_from(ino).map_err(Into::into)
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod oshandle;
|
||||
pub(crate) mod path;
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::wasi::{WasiError, WasiResult};
|
||||
use crate::path::PathGet;
|
||||
use crate::wasi::{Errno, Result};
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
|
||||
pub(crate) fn unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
match unsafe {
|
||||
unlinkat(
|
||||
@@ -32,7 +32,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
|
||||
} {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Directory {
|
||||
return Err(WasiError::EISDIR);
|
||||
return Err(Errno::Isdir);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -47,7 +47,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()> {
|
||||
pub(crate) fn symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use yanix::file::{fstatat, symlinkat, AtFlag};
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
@@ -69,7 +69,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()>
|
||||
AtFlag::SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} {
|
||||
Ok(_) => return Err(WasiError::EEXIST),
|
||||
Ok(_) => return Err(Errno::Exist),
|
||||
Err(err) => {
|
||||
log::debug!("path_symlink fstatat error: {:?}", err);
|
||||
}
|
||||
@@ -81,7 +81,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()>
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiResult<()> {
|
||||
pub(crate) fn rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use yanix::file::{fstatat, renameat, AtFlag};
|
||||
match unsafe {
|
||||
renameat(
|
||||
@@ -113,9 +113,9 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiR
|
||||
Ok(_) => {
|
||||
// check if destination contains a trailing slash
|
||||
if resolved_new.path().contains('/') {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
return Err(Errno::Notdir);
|
||||
} else {
|
||||
return Err(WasiError::ENOENT);
|
||||
return Err(Errno::Noent);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -129,32 +129,3 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiR
|
||||
Ok(()) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod fd_readdir_impl {
|
||||
use crate::sys::entry_impl::OsHandle;
|
||||
use crate::wasi::WasiResult;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use yanix::dir::Dir;
|
||||
|
||||
pub(crate) fn get_dir_from_os_handle<'a>(
|
||||
os_handle: &'a mut OsHandle,
|
||||
) -> WasiResult<MutexGuard<'a, Dir>> {
|
||||
let dir = match os_handle.dir {
|
||||
Some(ref mut dir) => dir,
|
||||
None => {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// Upon successful return from fdopendir(), the file descriptor is under
|
||||
// control of the system, and if any attempt is made to close the file
|
||||
// descriptor, or to modify the state of the associated description other
|
||||
// than by means of closedir(), readdir(), readdir_r(), or rewinddir(),
|
||||
// the behaviour is undefined.
|
||||
let fd = (*os_handle).try_clone()?;
|
||||
let dir = Dir::from(fd)?;
|
||||
os_handle.dir.get_or_insert(Mutex::new(dir))
|
||||
}
|
||||
};
|
||||
// Note that from this point on, until the end of the parent scope (i.e., enclosing this
|
||||
// function), we're locking the `Dir` member of this `OsHandle`.
|
||||
Ok(dir.lock().unwrap())
|
||||
}
|
||||
}
|
||||
35
crates/wasi-common/src/sys/unix/clock.rs
Normal file
35
crates/wasi-common/src/sys/unix/clock.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use yanix::clock::{clock_getres, clock_gettime, ClockId};
|
||||
|
||||
pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
let clock_id: ClockId = clock_id.into();
|
||||
let timespec = clock_getres(clock_id)?;
|
||||
|
||||
// convert to nanoseconds, returning EOVERFLOW in case of overflow;
|
||||
// this is freelancing a bit from the spec but seems like it'll
|
||||
// be an unusual situation to hit
|
||||
(timespec.tv_sec as types::Timestamp)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
|
||||
.map_or(Err(Errno::Overflow), |resolution| {
|
||||
// a supported clock can never return zero; this case will probably never get hit, but
|
||||
// make sure we follow the spec
|
||||
if resolution == 0 {
|
||||
Err(Errno::Inval)
|
||||
} else {
|
||||
Ok(resolution)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn time_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
let clock_id: ClockId = clock_id.into();
|
||||
let timespec = clock_gettime(clock_id)?;
|
||||
|
||||
// convert to nanoseconds, returning EOVERFLOW in case of overflow; this is freelancing a bit
|
||||
// from the spec but seems like it'll be an unusual situation to hit
|
||||
(timespec.tv_sec as types::Timestamp)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
|
||||
.map_or(Err(Errno::Overflow), Ok)
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
#[path = "../linux/host_impl.rs"]
|
||||
pub(crate) mod host_impl;
|
||||
#[path = "../linux/hostcalls_impl.rs"]
|
||||
pub(crate) mod hostcalls_impl;
|
||||
#[path = "../linux/fd.rs"]
|
||||
pub(crate) mod fd;
|
||||
#[path = "../linux/oshandle.rs"]
|
||||
pub(crate) mod oshandle;
|
||||
#[path = "../linux/path.rs"]
|
||||
pub(crate) mod path;
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::entry::{Descriptor, OsHandleRef};
|
||||
use crate::{sys::unix::sys_impl, wasi};
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
|
||||
|
||||
pub(crate) use sys_impl::oshandle::*;
|
||||
pub(crate) use super::sys_impl::oshandle::*;
|
||||
|
||||
impl AsRawFd for Descriptor {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
@@ -33,20 +33,16 @@ pub(crate) fn descriptor_as_oshandle<'lifetime>(
|
||||
/// This function is unsafe because it operates on a raw file descriptor.
|
||||
pub(crate) unsafe fn determine_type_and_access_rights<Fd: AsRawFd>(
|
||||
fd: &Fd,
|
||||
) -> io::Result<(
|
||||
wasi::__wasi_filetype_t,
|
||||
wasi::__wasi_rights_t,
|
||||
wasi::__wasi_rights_t,
|
||||
)> {
|
||||
) -> io::Result<(types::Filetype, types::Rights, types::Rights)> {
|
||||
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(fd)?;
|
||||
|
||||
use yanix::{fcntl, file::OFlag};
|
||||
let flags = fcntl::get_status_flags(fd.as_raw_fd())?;
|
||||
let accmode = flags & OFlag::ACCMODE;
|
||||
if accmode == OFlag::RDONLY {
|
||||
rights_base &= !wasi::__WASI_RIGHTS_FD_WRITE;
|
||||
rights_base &= !types::Rights::FD_WRITE;
|
||||
} else if accmode == OFlag::WRONLY {
|
||||
rights_base &= !wasi::__WASI_RIGHTS_FD_READ;
|
||||
rights_base &= !types::Rights::FD_READ;
|
||||
}
|
||||
|
||||
Ok((file_type, rights_base, rights_inheriting))
|
||||
@@ -57,11 +53,7 @@ pub(crate) unsafe fn determine_type_and_access_rights<Fd: AsRawFd>(
|
||||
/// This function is unsafe because it operates on a raw file descriptor.
|
||||
pub(crate) unsafe fn determine_type_rights<Fd: AsRawFd>(
|
||||
fd: &Fd,
|
||||
) -> io::Result<(
|
||||
wasi::__wasi_filetype_t,
|
||||
wasi::__wasi_rights_t,
|
||||
wasi::__wasi_rights_t,
|
||||
)> {
|
||||
) -> io::Result<(types::Filetype, types::Rights, types::Rights)> {
|
||||
let (file_type, rights_base, rights_inheriting) = {
|
||||
// we just make a `File` here for convenience; we don't want it to close when it drops
|
||||
let file = std::mem::ManuallyDrop::new(std::fs::File::from_raw_fd(fd.as_raw_fd()));
|
||||
@@ -69,62 +61,62 @@ pub(crate) unsafe fn determine_type_rights<Fd: AsRawFd>(
|
||||
if ft.is_block_device() {
|
||||
log::debug!("Host fd {:?} is a block device", fd.as_raw_fd());
|
||||
(
|
||||
wasi::__WASI_FILETYPE_BLOCK_DEVICE,
|
||||
wasi::RIGHTS_BLOCK_DEVICE_BASE,
|
||||
wasi::RIGHTS_BLOCK_DEVICE_INHERITING,
|
||||
types::Filetype::BlockDevice,
|
||||
types::Rights::block_device_base(),
|
||||
types::Rights::block_device_inheriting(),
|
||||
)
|
||||
} else if ft.is_char_device() {
|
||||
log::debug!("Host fd {:?} is a char device", fd.as_raw_fd());
|
||||
use yanix::file::isatty;
|
||||
if isatty(fd.as_raw_fd())? {
|
||||
(
|
||||
wasi::__WASI_FILETYPE_CHARACTER_DEVICE,
|
||||
wasi::RIGHTS_TTY_BASE,
|
||||
wasi::RIGHTS_TTY_BASE,
|
||||
types::Filetype::CharacterDevice,
|
||||
types::Rights::tty_base(),
|
||||
types::Rights::tty_base(),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
wasi::__WASI_FILETYPE_CHARACTER_DEVICE,
|
||||
wasi::RIGHTS_CHARACTER_DEVICE_BASE,
|
||||
wasi::RIGHTS_CHARACTER_DEVICE_INHERITING,
|
||||
types::Filetype::CharacterDevice,
|
||||
types::Rights::character_device_base(),
|
||||
types::Rights::character_device_inheriting(),
|
||||
)
|
||||
}
|
||||
} else if ft.is_dir() {
|
||||
log::debug!("Host fd {:?} is a directory", fd.as_raw_fd());
|
||||
(
|
||||
wasi::__WASI_FILETYPE_DIRECTORY,
|
||||
wasi::RIGHTS_DIRECTORY_BASE,
|
||||
wasi::RIGHTS_DIRECTORY_INHERITING,
|
||||
types::Filetype::Directory,
|
||||
types::Rights::directory_base(),
|
||||
types::Rights::directory_inheriting(),
|
||||
)
|
||||
} else if ft.is_file() {
|
||||
log::debug!("Host fd {:?} is a file", fd.as_raw_fd());
|
||||
(
|
||||
wasi::__WASI_FILETYPE_REGULAR_FILE,
|
||||
wasi::RIGHTS_REGULAR_FILE_BASE,
|
||||
wasi::RIGHTS_REGULAR_FILE_INHERITING,
|
||||
types::Filetype::RegularFile,
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
)
|
||||
} else if ft.is_socket() {
|
||||
log::debug!("Host fd {:?} is a socket", fd.as_raw_fd());
|
||||
use yanix::socket::{get_socket_type, SockType};
|
||||
match get_socket_type(fd.as_raw_fd())? {
|
||||
SockType::Datagram => (
|
||||
wasi::__WASI_FILETYPE_SOCKET_DGRAM,
|
||||
wasi::RIGHTS_SOCKET_BASE,
|
||||
wasi::RIGHTS_SOCKET_INHERITING,
|
||||
types::Filetype::SocketDgram,
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_inheriting(),
|
||||
),
|
||||
SockType::Stream => (
|
||||
wasi::__WASI_FILETYPE_SOCKET_STREAM,
|
||||
wasi::RIGHTS_SOCKET_BASE,
|
||||
wasi::RIGHTS_SOCKET_INHERITING,
|
||||
types::Filetype::SocketStream,
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_inheriting(),
|
||||
),
|
||||
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
|
||||
}
|
||||
} else if ft.is_fifo() {
|
||||
log::debug!("Host fd {:?} is a fifo", fd.as_raw_fd());
|
||||
(
|
||||
wasi::__WASI_FILETYPE_UNKNOWN,
|
||||
wasi::RIGHTS_REGULAR_FILE_BASE,
|
||||
wasi::RIGHTS_REGULAR_FILE_INHERITING,
|
||||
types::Filetype::Unknown,
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
)
|
||||
} else {
|
||||
log::debug!("Host fd {:?} is unknown", fd.as_raw_fd());
|
||||
78
crates/wasi-common/src/sys/unix/fd.rs
Normal file
78
crates/wasi-common/src/sys/unix/fd.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use super::sys_impl::fd::get_dir_from_os_handle;
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::wasi::{self, types, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::fs::File;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn fdstat_get(fd: &File) -> Result<types::Fdflags> {
|
||||
let fdflags = unsafe { yanix::fcntl::get_status_flags(fd.as_raw_fd())? };
|
||||
Ok(fdflags.into())
|
||||
}
|
||||
|
||||
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::Fdflags) -> Result<Option<OsHandle>> {
|
||||
unsafe { yanix::fcntl::set_status_flags(fd.as_raw_fd(), fdflags.into())? };
|
||||
// TODO why are we returning Ok(None) here?
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn advise(
|
||||
file: &File,
|
||||
advice: types::Advice,
|
||||
offset: types::Filesize,
|
||||
len: types::Filesize,
|
||||
) -> Result<()> {
|
||||
use yanix::fadvise::{posix_fadvise, PosixFadviseAdvice};
|
||||
let offset = offset.try_into()?;
|
||||
let len = len.try_into()?;
|
||||
let host_advice = match advice {
|
||||
types::Advice::Dontneed => PosixFadviseAdvice::DontNeed,
|
||||
types::Advice::Sequential => PosixFadviseAdvice::Sequential,
|
||||
types::Advice::Willneed => PosixFadviseAdvice::WillNeed,
|
||||
types::Advice::Noreuse => PosixFadviseAdvice::NoReuse,
|
||||
types::Advice::Random => PosixFadviseAdvice::Random,
|
||||
types::Advice::Normal => PosixFadviseAdvice::Normal,
|
||||
};
|
||||
unsafe { posix_fadvise(file.as_raw_fd(), offset, len, host_advice)? };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_get(file: &std::fs::File) -> Result<types::Filestat> {
|
||||
use yanix::file::fstat;
|
||||
let stat = unsafe { fstat(file.as_raw_fd())? };
|
||||
Ok(stat.try_into()?)
|
||||
}
|
||||
|
||||
pub(crate) fn readdir<'a>(
|
||||
os_handle: &'a mut OsHandle,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<impl Iterator<Item = Result<(types::Dirent, String)>> + 'a> {
|
||||
use yanix::dir::{DirIter, Entry, EntryExt, SeekLoc};
|
||||
|
||||
// Get an instance of `Dir`; this is host-specific due to intricasies
|
||||
// of managing a dir stream between Linux and BSD *nixes
|
||||
let mut dir = get_dir_from_os_handle(os_handle)?;
|
||||
|
||||
// Seek if needed. Unless cookie is wasi::__WASI_DIRCOOKIE_START,
|
||||
// new items may not be returned to the caller.
|
||||
if cookie == wasi::DIRCOOKIE_START {
|
||||
log::trace!(" | fd_readdir: doing rewinddir");
|
||||
dir.rewind();
|
||||
} else {
|
||||
log::trace!(" | fd_readdir: doing seekdir to {}", cookie);
|
||||
let loc = unsafe { SeekLoc::from_raw(cookie as i64)? };
|
||||
dir.seek(loc);
|
||||
}
|
||||
|
||||
Ok(DirIter::new(dir).map(|entry| {
|
||||
let entry: Entry = entry?;
|
||||
let name = entry.file_name().to_str()?.to_owned();
|
||||
let dirent = types::Dirent {
|
||||
d_next: entry.seek_loc()?.to_raw().try_into()?,
|
||||
d_ino: entry.ino(),
|
||||
d_namlen: name.len().try_into()?,
|
||||
d_type: entry.file_type().into(),
|
||||
};
|
||||
Ok((dirent, name))
|
||||
}))
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
//! WASI host types specific to *nix host.
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(dead_code)]
|
||||
use crate::host::FileType;
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use crate::{helpers, sys::unix::sys_impl};
|
||||
use std::ffi::OsStr;
|
||||
use std::io;
|
||||
use std::os::unix::prelude::OsStrExt;
|
||||
use yanix::file::OFlag;
|
||||
|
||||
pub(crate) use sys_impl::host_impl::*;
|
||||
|
||||
impl From<io::Error> for WasiError {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code {
|
||||
libc::EPERM => Self::EPERM,
|
||||
libc::ENOENT => Self::ENOENT,
|
||||
libc::ESRCH => Self::ESRCH,
|
||||
libc::EINTR => Self::EINTR,
|
||||
libc::EIO => Self::EIO,
|
||||
libc::ENXIO => Self::ENXIO,
|
||||
libc::E2BIG => Self::E2BIG,
|
||||
libc::ENOEXEC => Self::ENOEXEC,
|
||||
libc::EBADF => Self::EBADF,
|
||||
libc::ECHILD => Self::ECHILD,
|
||||
libc::EAGAIN => Self::EAGAIN,
|
||||
libc::ENOMEM => Self::ENOMEM,
|
||||
libc::EACCES => Self::EACCES,
|
||||
libc::EFAULT => Self::EFAULT,
|
||||
libc::EBUSY => Self::EBUSY,
|
||||
libc::EEXIST => Self::EEXIST,
|
||||
libc::EXDEV => Self::EXDEV,
|
||||
libc::ENODEV => Self::ENODEV,
|
||||
libc::ENOTDIR => Self::ENOTDIR,
|
||||
libc::EISDIR => Self::EISDIR,
|
||||
libc::EINVAL => Self::EINVAL,
|
||||
libc::ENFILE => Self::ENFILE,
|
||||
libc::EMFILE => Self::EMFILE,
|
||||
libc::ENOTTY => Self::ENOTTY,
|
||||
libc::ETXTBSY => Self::ETXTBSY,
|
||||
libc::EFBIG => Self::EFBIG,
|
||||
libc::ENOSPC => Self::ENOSPC,
|
||||
libc::ESPIPE => Self::ESPIPE,
|
||||
libc::EROFS => Self::EROFS,
|
||||
libc::EMLINK => Self::EMLINK,
|
||||
libc::EPIPE => Self::EPIPE,
|
||||
libc::EDOM => Self::EDOM,
|
||||
libc::ERANGE => Self::ERANGE,
|
||||
libc::EDEADLK => Self::EDEADLK,
|
||||
libc::ENAMETOOLONG => Self::ENAMETOOLONG,
|
||||
libc::ENOLCK => Self::ENOLCK,
|
||||
libc::ENOSYS => Self::ENOSYS,
|
||||
libc::ENOTEMPTY => Self::ENOTEMPTY,
|
||||
libc::ELOOP => Self::ELOOP,
|
||||
libc::ENOMSG => Self::ENOMSG,
|
||||
libc::EIDRM => Self::EIDRM,
|
||||
libc::ENOLINK => Self::ENOLINK,
|
||||
libc::EPROTO => Self::EPROTO,
|
||||
libc::EMULTIHOP => Self::EMULTIHOP,
|
||||
libc::EBADMSG => Self::EBADMSG,
|
||||
libc::EOVERFLOW => Self::EOVERFLOW,
|
||||
libc::EILSEQ => Self::EILSEQ,
|
||||
libc::ENOTSOCK => Self::ENOTSOCK,
|
||||
libc::EDESTADDRREQ => Self::EDESTADDRREQ,
|
||||
libc::EMSGSIZE => Self::EMSGSIZE,
|
||||
libc::EPROTOTYPE => Self::EPROTOTYPE,
|
||||
libc::ENOPROTOOPT => Self::ENOPROTOOPT,
|
||||
libc::EPROTONOSUPPORT => Self::EPROTONOSUPPORT,
|
||||
libc::EAFNOSUPPORT => Self::EAFNOSUPPORT,
|
||||
libc::EADDRINUSE => Self::EADDRINUSE,
|
||||
libc::EADDRNOTAVAIL => Self::EADDRNOTAVAIL,
|
||||
libc::ENETDOWN => Self::ENETDOWN,
|
||||
libc::ENETUNREACH => Self::ENETUNREACH,
|
||||
libc::ENETRESET => Self::ENETRESET,
|
||||
libc::ECONNABORTED => Self::ECONNABORTED,
|
||||
libc::ECONNRESET => Self::ECONNRESET,
|
||||
libc::ENOBUFS => Self::ENOBUFS,
|
||||
libc::EISCONN => Self::EISCONN,
|
||||
libc::ENOTCONN => Self::ENOTCONN,
|
||||
libc::ETIMEDOUT => Self::ETIMEDOUT,
|
||||
libc::ECONNREFUSED => Self::ECONNREFUSED,
|
||||
libc::EHOSTUNREACH => Self::EHOSTUNREACH,
|
||||
libc::EALREADY => Self::EALREADY,
|
||||
libc::EINPROGRESS => Self::EINPROGRESS,
|
||||
libc::ESTALE => Self::ESTALE,
|
||||
libc::EDQUOT => Self::EDQUOT,
|
||||
libc::ECANCELED => Self::ECANCELED,
|
||||
libc::EOWNERDEAD => Self::EOWNERDEAD,
|
||||
libc::ENOTRECOVERABLE => Self::ENOTRECOVERABLE,
|
||||
libc::ENOTSUP => Self::ENOTSUP,
|
||||
x => {
|
||||
log::debug!("Unknown errno value: {}", x);
|
||||
Self::EIO
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::EIO
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn nix_from_fdflags(fdflags: wasi::__wasi_fdflags_t) -> OFlag {
|
||||
let mut nix_flags = OFlag::empty();
|
||||
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
|
||||
nix_flags.insert(OFlag::APPEND);
|
||||
}
|
||||
if fdflags & wasi::__WASI_FDFLAGS_DSYNC != 0 {
|
||||
nix_flags.insert(OFlag::DSYNC);
|
||||
}
|
||||
if fdflags & wasi::__WASI_FDFLAGS_NONBLOCK != 0 {
|
||||
nix_flags.insert(OFlag::NONBLOCK);
|
||||
}
|
||||
if fdflags & wasi::__WASI_FDFLAGS_RSYNC != 0 {
|
||||
nix_flags.insert(O_RSYNC);
|
||||
}
|
||||
if fdflags & wasi::__WASI_FDFLAGS_SYNC != 0 {
|
||||
nix_flags.insert(OFlag::SYNC);
|
||||
}
|
||||
nix_flags
|
||||
}
|
||||
|
||||
pub(crate) fn fdflags_from_nix(oflags: OFlag) -> wasi::__wasi_fdflags_t {
|
||||
let mut fdflags = 0;
|
||||
if oflags.contains(OFlag::APPEND) {
|
||||
fdflags |= wasi::__WASI_FDFLAGS_APPEND;
|
||||
}
|
||||
if oflags.contains(OFlag::DSYNC) {
|
||||
fdflags |= wasi::__WASI_FDFLAGS_DSYNC;
|
||||
}
|
||||
if oflags.contains(OFlag::NONBLOCK) {
|
||||
fdflags |= wasi::__WASI_FDFLAGS_NONBLOCK;
|
||||
}
|
||||
if oflags.contains(O_RSYNC) {
|
||||
fdflags |= wasi::__WASI_FDFLAGS_RSYNC;
|
||||
}
|
||||
if oflags.contains(OFlag::SYNC) {
|
||||
fdflags |= wasi::__WASI_FDFLAGS_SYNC;
|
||||
}
|
||||
fdflags
|
||||
}
|
||||
|
||||
pub(crate) fn nix_from_oflags(oflags: wasi::__wasi_oflags_t) -> OFlag {
|
||||
let mut nix_flags = OFlag::empty();
|
||||
if oflags & wasi::__WASI_OFLAGS_CREAT != 0 {
|
||||
nix_flags.insert(OFlag::CREAT);
|
||||
}
|
||||
if oflags & wasi::__WASI_OFLAGS_DIRECTORY != 0 {
|
||||
nix_flags.insert(OFlag::DIRECTORY);
|
||||
}
|
||||
if oflags & wasi::__WASI_OFLAGS_EXCL != 0 {
|
||||
nix_flags.insert(OFlag::EXCL);
|
||||
}
|
||||
if oflags & wasi::__WASI_OFLAGS_TRUNC != 0 {
|
||||
nix_flags.insert(OFlag::TRUNC);
|
||||
}
|
||||
nix_flags
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_from_nix(filestat: libc::stat) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
use std::convert::TryInto;
|
||||
|
||||
fn filestat_to_timestamp(secs: u64, nsecs: u64) -> WasiResult<wasi::__wasi_timestamp_t> {
|
||||
secs.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_nsec| sec_nsec.checked_add(nsecs))
|
||||
.ok_or(WasiError::EOVERFLOW)
|
||||
}
|
||||
|
||||
let filetype = yanix::file::FileType::from_stat_st_mode(filestat.st_mode);
|
||||
let dev = stdev_from_nix(filestat.st_dev)?;
|
||||
let ino = stino_from_nix(filestat.st_ino)?;
|
||||
let atim = filestat_to_timestamp(
|
||||
filestat.st_atime.try_into()?,
|
||||
filestat.st_atime_nsec.try_into()?,
|
||||
)?;
|
||||
let ctim = filestat_to_timestamp(
|
||||
filestat.st_ctime.try_into()?,
|
||||
filestat.st_ctime_nsec.try_into()?,
|
||||
)?;
|
||||
let mtim = filestat_to_timestamp(
|
||||
filestat.st_mtime.try_into()?,
|
||||
filestat.st_mtime_nsec.try_into()?,
|
||||
)?;
|
||||
|
||||
Ok(wasi::__wasi_filestat_t {
|
||||
dev,
|
||||
ino,
|
||||
nlink: wasi::__wasi_linkcount_t::from(filestat.st_nlink),
|
||||
size: filestat.st_size as wasi::__wasi_filesize_t,
|
||||
atim,
|
||||
ctim,
|
||||
mtim,
|
||||
filetype: FileType::from(filetype).to_wasi(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_ERRNO_ILSEQ` error is returned.
|
||||
pub(crate) fn path_from_host<S: AsRef<OsStr>>(s: S) -> WasiResult<String> {
|
||||
helpers::path_from_slice(s.as_ref().as_bytes()).map(String::from)
|
||||
}
|
||||
|
||||
impl From<yanix::file::FileType> for FileType {
|
||||
fn from(ft: yanix::file::FileType) -> Self {
|
||||
use yanix::file::FileType::*;
|
||||
match ft {
|
||||
RegularFile => Self::RegularFile,
|
||||
Symlink => Self::Symlink,
|
||||
Directory => Self::Directory,
|
||||
BlockDevice => Self::BlockDevice,
|
||||
CharacterDevice => Self::CharacterDevice,
|
||||
/* Unknown | Socket | Fifo */
|
||||
_ => Self::Unknown,
|
||||
// TODO how to discriminate between STREAM and DGRAM?
|
||||
// Perhaps, we should create a more general WASI filetype
|
||||
// such as __WASI_FILETYPE_SOCKET, and then it would be
|
||||
// up to the client to check whether it's actually
|
||||
// STREAM or DGRAM?
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,329 +0,0 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use crate::entry::Descriptor;
|
||||
use crate::host::Dirent;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::sys::entry_impl::OsHandle;
|
||||
use crate::sys::{host_impl, unix::sys_impl};
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use std::convert::TryInto;
|
||||
use std::fs::File;
|
||||
use std::os::unix::fs::FileExt;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
|
||||
pub(crate) use sys_impl::hostcalls_impl::*;
|
||||
|
||||
pub(crate) fn fd_pread(
|
||||
file: &File,
|
||||
buf: &mut [u8],
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<usize> {
|
||||
file.read_at(buf, offset).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_pwrite(
|
||||
file: &File,
|
||||
buf: &[u8],
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<usize> {
|
||||
file.write_at(buf, offset).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_get(fd: &File) -> WasiResult<wasi::__wasi_fdflags_t> {
|
||||
unsafe { yanix::fcntl::get_status_flags(fd.as_raw_fd()) }
|
||||
.map(host_impl::fdflags_from_nix)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd: &File,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> WasiResult<Option<OsHandle>> {
|
||||
let nix_flags = host_impl::nix_from_fdflags(fdflags);
|
||||
unsafe { yanix::fcntl::set_status_flags(fd.as_raw_fd(), nix_flags) }
|
||||
.map(|_| None)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
file: &File,
|
||||
advice: wasi::__wasi_advice_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
len: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<()> {
|
||||
use yanix::fadvise::{posix_fadvise, PosixFadviseAdvice};
|
||||
let offset = offset.try_into()?;
|
||||
let len = len.try_into()?;
|
||||
let host_advice = match advice {
|
||||
wasi::__WASI_ADVICE_DONTNEED => PosixFadviseAdvice::DontNeed,
|
||||
wasi::__WASI_ADVICE_SEQUENTIAL => PosixFadviseAdvice::Sequential,
|
||||
wasi::__WASI_ADVICE_WILLNEED => PosixFadviseAdvice::WillNeed,
|
||||
wasi::__WASI_ADVICE_NOREUSE => PosixFadviseAdvice::NoReuse,
|
||||
wasi::__WASI_ADVICE_RANDOM => PosixFadviseAdvice::Random,
|
||||
wasi::__WASI_ADVICE_NORMAL => PosixFadviseAdvice::Normal,
|
||||
_ => return Err(WasiError::EINVAL),
|
||||
};
|
||||
unsafe { posix_fadvise(file.as_raw_fd(), offset, len, host_advice) }.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_create_directory(base: &File, path: &str) -> WasiResult<()> {
|
||||
use yanix::file::{mkdirat, Mode};
|
||||
unsafe { mkdirat(base.as_raw_fd(), path, Mode::from_bits_truncate(0o777)) }.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_link(
|
||||
resolved_old: PathGet,
|
||||
resolved_new: PathGet,
|
||||
follow_symlinks: bool,
|
||||
) -> WasiResult<()> {
|
||||
use yanix::file::{linkat, AtFlag};
|
||||
let flags = if follow_symlinks {
|
||||
AtFlag::SYMLINK_FOLLOW
|
||||
} else {
|
||||
AtFlag::empty()
|
||||
};
|
||||
unsafe {
|
||||
linkat(
|
||||
resolved_old.dirfd().as_raw_fd(),
|
||||
resolved_old.path(),
|
||||
resolved_new.dirfd().as_raw_fd(),
|
||||
resolved_new.path(),
|
||||
flags,
|
||||
)
|
||||
}
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_open(
|
||||
resolved: PathGet,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: wasi::__wasi_oflags_t,
|
||||
fs_flags: wasi::__wasi_fdflags_t,
|
||||
) -> WasiResult<Descriptor> {
|
||||
use yanix::file::{fstatat, openat, AtFlag, FileType, Mode, OFlag};
|
||||
|
||||
let mut nix_all_oflags = if read && write {
|
||||
OFlag::RDWR
|
||||
} else if write {
|
||||
OFlag::WRONLY
|
||||
} else {
|
||||
OFlag::RDONLY
|
||||
};
|
||||
|
||||
// on non-Capsicum systems, we always want nofollow
|
||||
nix_all_oflags.insert(OFlag::NOFOLLOW);
|
||||
|
||||
// convert open flags
|
||||
nix_all_oflags.insert(host_impl::nix_from_oflags(oflags));
|
||||
|
||||
// convert file descriptor flags
|
||||
nix_all_oflags.insert(host_impl::nix_from_fdflags(fs_flags));
|
||||
|
||||
// Call openat. Use mode 0o666 so that we follow whatever the user's
|
||||
// umask is, but don't set the executable flag, because it isn't yet
|
||||
// meaningful for WASI programs to create executable files.
|
||||
|
||||
log::debug!("path_open resolved = {:?}", resolved);
|
||||
log::debug!("path_open oflags = {:?}", nix_all_oflags);
|
||||
|
||||
let fd_no = unsafe {
|
||||
openat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
nix_all_oflags,
|
||||
Mode::from_bits_truncate(0o666),
|
||||
)
|
||||
};
|
||||
let new_fd = match fd_no {
|
||||
Ok(fd) => fd,
|
||||
Err(e) => {
|
||||
match e.raw_os_error().unwrap() {
|
||||
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
|
||||
libc::ENXIO => {
|
||||
match unsafe {
|
||||
fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Socket {
|
||||
return Err(WasiError::ENOTSUP);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("path_open fstatat error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY
|
||||
// on a symlink.
|
||||
libc::ENOTDIR
|
||||
if !(nix_all_oflags & (OFlag::NOFOLLOW | OFlag::DIRECTORY)).is_empty() =>
|
||||
{
|
||||
match unsafe {
|
||||
fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Symlink {
|
||||
return Err(WasiError::ELOOP);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("path_open fstatat error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on
|
||||
// a symlink.
|
||||
libc::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
|
||||
return Err(WasiError::ELOOP);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
|
||||
log::debug!("path_open (host) new_fd = {:?}", new_fd);
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
Ok(OsHandle::from(unsafe { File::from_raw_fd(new_fd) }).into())
|
||||
}
|
||||
|
||||
pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> WasiResult<usize> {
|
||||
use std::cmp::min;
|
||||
use yanix::file::readlinkat;
|
||||
let read_link = unsafe { readlinkat(resolved.dirfd().as_raw_fd(), resolved.path()) }
|
||||
.map_err(Into::into)
|
||||
.and_then(host_impl::path_from_host)?;
|
||||
let copy_len = min(read_link.len(), buf.len());
|
||||
if copy_len > 0 {
|
||||
buf[..copy_len].copy_from_slice(&read_link.as_bytes()[..copy_len]);
|
||||
}
|
||||
Ok(copy_len)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(file: &std::fs::File) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
use yanix::file::fstat;
|
||||
unsafe { fstat(file.as_raw_fd()) }
|
||||
.map_err(Into::into)
|
||||
.and_then(host_impl::filestat_from_nix)
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_get(
|
||||
resolved: PathGet,
|
||||
dirflags: wasi::__wasi_lookupflags_t,
|
||||
) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
use yanix::file::{fstatat, AtFlag};
|
||||
let atflags = match dirflags {
|
||||
0 => AtFlag::empty(),
|
||||
_ => AtFlag::SYMLINK_NOFOLLOW,
|
||||
};
|
||||
unsafe { fstatat(resolved.dirfd().as_raw_fd(), resolved.path(), atflags) }
|
||||
.map_err(Into::into)
|
||||
.and_then(host_impl::filestat_from_nix)
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_set_times(
|
||||
resolved: PathGet,
|
||||
dirflags: wasi::__wasi_lookupflags_t,
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
st_mtim: wasi::__wasi_timestamp_t,
|
||||
fst_flags: wasi::__wasi_fstflags_t,
|
||||
) -> WasiResult<()> {
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
use yanix::filetime::*;
|
||||
|
||||
let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0;
|
||||
let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0;
|
||||
let set_mtim = fst_flags & wasi::__WASI_FSTFLAGS_MTIM != 0;
|
||||
let set_mtim_now = fst_flags & wasi::__WASI_FSTFLAGS_MTIM_NOW != 0;
|
||||
|
||||
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||
return Err(WasiError::EINVAL);
|
||||
}
|
||||
|
||||
let symlink_nofollow = wasi::__WASI_LOOKUPFLAGS_SYMLINK_FOLLOW != dirflags;
|
||||
let atim = if set_atim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
|
||||
FileTime::FileTime(filetime::FileTime::from_system_time(time))
|
||||
} else if set_atim_now {
|
||||
FileTime::Now
|
||||
} else {
|
||||
FileTime::Omit
|
||||
};
|
||||
let mtim = if set_mtim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_mtim);
|
||||
FileTime::FileTime(filetime::FileTime::from_system_time(time))
|
||||
} else if set_mtim_now {
|
||||
FileTime::Now
|
||||
} else {
|
||||
FileTime::Omit
|
||||
};
|
||||
|
||||
utimensat(
|
||||
&resolved.dirfd().as_os_handle(),
|
||||
resolved.path(),
|
||||
atim,
|
||||
mtim,
|
||||
symlink_nofollow,
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_remove_directory(resolved: PathGet) -> WasiResult<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
|
||||
unsafe {
|
||||
unlinkat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::REMOVEDIR,
|
||||
)
|
||||
}
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_readdir<'a>(
|
||||
os_handle: &'a mut OsHandle,
|
||||
cookie: wasi::__wasi_dircookie_t,
|
||||
) -> WasiResult<impl Iterator<Item = WasiResult<Dirent>> + 'a> {
|
||||
use yanix::dir::{DirIter, Entry, EntryExt, SeekLoc};
|
||||
|
||||
// Get an instance of `Dir`; this is host-specific due to intricasies
|
||||
// of managing a dir stream between Linux and BSD *nixes
|
||||
let mut dir = fd_readdir_impl::get_dir_from_os_handle(os_handle)?;
|
||||
|
||||
// Seek if needed. Unless cookie is wasi::__WASI_DIRCOOKIE_START,
|
||||
// new items may not be returned to the caller.
|
||||
if cookie == wasi::__WASI_DIRCOOKIE_START {
|
||||
log::trace!(" | fd_readdir: doing rewinddir");
|
||||
dir.rewind();
|
||||
} else {
|
||||
log::trace!(" | fd_readdir: doing seekdir to {}", cookie);
|
||||
let loc = unsafe { SeekLoc::from_raw(cookie as i64)? };
|
||||
dir.seek(loc);
|
||||
}
|
||||
|
||||
Ok(DirIter::new(dir).map(|entry| {
|
||||
let entry: Entry = entry?;
|
||||
Ok(Dirent {
|
||||
name: entry
|
||||
// TODO can we reuse path_from_host for CStr?
|
||||
.file_name()
|
||||
.to_str()?
|
||||
.to_owned(),
|
||||
ino: entry.ino(),
|
||||
ftype: entry.file_type().into(),
|
||||
cookie: entry.seek_loc()?.to_raw().try_into()?,
|
||||
})
|
||||
}))
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use crate::sys::host_impl;
|
||||
use crate::wasi::{self, WasiResult};
|
||||
use std::fs::File;
|
||||
use yanix::file::OFlag;
|
||||
|
||||
pub(crate) fn path_open_rights(
|
||||
rights_base: wasi::__wasi_rights_t,
|
||||
rights_inheriting: wasi::__wasi_rights_t,
|
||||
oflags: wasi::__wasi_oflags_t,
|
||||
fs_flags: wasi::__wasi_fdflags_t,
|
||||
) -> (wasi::__wasi_rights_t, wasi::__wasi_rights_t) {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = wasi::__WASI_RIGHTS_PATH_OPEN;
|
||||
let mut needed_inheriting = rights_base | rights_inheriting;
|
||||
|
||||
// convert open flags
|
||||
let oflags = host_impl::nix_from_oflags(oflags);
|
||||
if oflags.contains(OFlag::CREAT) {
|
||||
needed_base |= wasi::__WASI_RIGHTS_PATH_CREATE_FILE;
|
||||
}
|
||||
if oflags.contains(OFlag::TRUNC) {
|
||||
needed_base |= wasi::__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
let fdflags = host_impl::nix_from_fdflags(fs_flags);
|
||||
if fdflags.contains(OFlag::DSYNC) {
|
||||
needed_inheriting |= wasi::__WASI_RIGHTS_FD_DATASYNC;
|
||||
}
|
||||
if fdflags.intersects(host_impl::O_RSYNC | OFlag::SYNC) {
|
||||
needed_inheriting |= wasi::__WASI_RIGHTS_FD_SYNC;
|
||||
}
|
||||
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> WasiResult<File> {
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
use yanix::file::{openat, Mode};
|
||||
|
||||
log::debug!("path_get openat path = {:?}", path);
|
||||
|
||||
unsafe {
|
||||
openat(
|
||||
dirfd.as_raw_fd(),
|
||||
path,
|
||||
OFlag::RDONLY | OFlag::DIRECTORY | OFlag::NOFOLLOW,
|
||||
Mode::empty(),
|
||||
)
|
||||
}
|
||||
.map(|new_fd| unsafe { File::from_raw_fd(new_fd) })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &File, path: &str) -> WasiResult<String> {
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
use yanix::file::readlinkat;
|
||||
|
||||
log::debug!("path_get readlinkat path = {:?}", path);
|
||||
|
||||
unsafe { readlinkat(dirfd.as_raw_fd(), path) }
|
||||
.map_err(Into::into)
|
||||
.and_then(host_impl::path_from_host)
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use crate::hostcalls_impl::{ClockEventData, FdEventData};
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use std::io;
|
||||
use yanix::clock::{clock_getres, clock_gettime, ClockId};
|
||||
|
||||
fn wasi_clock_id_to_unix(clock_id: wasi::__wasi_clockid_t) -> WasiResult<ClockId> {
|
||||
// convert the supported clocks to libc types, or return EINVAL
|
||||
match clock_id {
|
||||
wasi::__WASI_CLOCKID_REALTIME => Ok(ClockId::Realtime),
|
||||
wasi::__WASI_CLOCKID_MONOTONIC => Ok(ClockId::Monotonic),
|
||||
wasi::__WASI_CLOCKID_PROCESS_CPUTIME_ID => Ok(ClockId::ProcessCPUTime),
|
||||
wasi::__WASI_CLOCKID_THREAD_CPUTIME_ID => Ok(ClockId::ThreadCPUTime),
|
||||
_ => Err(WasiError::EINVAL),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn clock_res_get(
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
) -> WasiResult<wasi::__wasi_timestamp_t> {
|
||||
let clock_id = wasi_clock_id_to_unix(clock_id)?;
|
||||
let timespec = clock_getres(clock_id)?;
|
||||
|
||||
// convert to nanoseconds, returning EOVERFLOW in case of overflow;
|
||||
// this is freelancing a bit from the spec but seems like it'll
|
||||
// be an unusual situation to hit
|
||||
(timespec.tv_sec as wasi::__wasi_timestamp_t)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as wasi::__wasi_timestamp_t))
|
||||
.map_or(Err(WasiError::EOVERFLOW), |resolution| {
|
||||
// a supported clock can never return zero; this case will probably never get hit, but
|
||||
// make sure we follow the spec
|
||||
if resolution == 0 {
|
||||
Err(WasiError::EINVAL)
|
||||
} else {
|
||||
Ok(resolution)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn clock_time_get(
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
) -> WasiResult<wasi::__wasi_timestamp_t> {
|
||||
let clock_id = wasi_clock_id_to_unix(clock_id)?;
|
||||
let timespec = clock_gettime(clock_id)?;
|
||||
|
||||
// convert to nanoseconds, returning EOVERFLOW in case of overflow; this is freelancing a bit
|
||||
// from the spec but seems like it'll be an unusual situation to hit
|
||||
(timespec.tv_sec as wasi::__wasi_timestamp_t)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as wasi::__wasi_timestamp_t))
|
||||
.map_or(Err(WasiError::EOVERFLOW), Ok)
|
||||
}
|
||||
|
||||
pub(crate) fn poll_oneoff(
|
||||
timeout: Option<ClockEventData>,
|
||||
fd_events: Vec<FdEventData>,
|
||||
events: &mut Vec<wasi::__wasi_event_t>,
|
||||
) -> WasiResult<()> {
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
use yanix::poll::{poll, PollFd, PollFlags};
|
||||
|
||||
if fd_events.is_empty() && timeout.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut poll_fds: Vec<_> = fd_events
|
||||
.iter()
|
||||
.map(|event| {
|
||||
let mut flags = PollFlags::empty();
|
||||
match event.r#type {
|
||||
wasi::__WASI_EVENTTYPE_FD_READ => flags.insert(PollFlags::POLLIN),
|
||||
wasi::__WASI_EVENTTYPE_FD_WRITE => flags.insert(PollFlags::POLLOUT),
|
||||
// An event on a file descriptor can currently only be of type FD_READ or FD_WRITE
|
||||
// Nothing else has been defined in the specification, and these are also the only two
|
||||
// events we filtered before. If we get something else here, the code has a serious bug.
|
||||
_ => unreachable!(),
|
||||
};
|
||||
unsafe { PollFd::new(event.descriptor.as_raw_fd(), flags) }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let poll_timeout = timeout.map_or(-1, |timeout| {
|
||||
let delay = timeout.delay / 1_000_000; // poll syscall requires delay to expressed in milliseconds
|
||||
delay.try_into().unwrap_or(libc::c_int::max_value())
|
||||
});
|
||||
log::debug!("poll_oneoff poll_timeout = {:?}", poll_timeout);
|
||||
|
||||
let ready = loop {
|
||||
match poll(&mut poll_fds, poll_timeout) {
|
||||
Err(_) => {
|
||||
let last_err = io::Error::last_os_error();
|
||||
if last_err.raw_os_error().unwrap() == libc::EINTR {
|
||||
continue;
|
||||
}
|
||||
return Err(last_err.into());
|
||||
}
|
||||
Ok(ready) => break ready,
|
||||
}
|
||||
};
|
||||
|
||||
Ok(if ready == 0 {
|
||||
poll_oneoff_handle_timeout_event(timeout.expect("timeout should not be None"), events)
|
||||
} else {
|
||||
let ready_events = fd_events.into_iter().zip(poll_fds.into_iter()).take(ready);
|
||||
poll_oneoff_handle_fd_event(ready_events, events)?
|
||||
})
|
||||
}
|
||||
|
||||
fn poll_oneoff_handle_timeout_event(
|
||||
timeout: ClockEventData,
|
||||
events: &mut Vec<wasi::__wasi_event_t>,
|
||||
) {
|
||||
events.push(wasi::__wasi_event_t {
|
||||
userdata: timeout.userdata,
|
||||
error: wasi::__WASI_ERRNO_SUCCESS,
|
||||
r#type: wasi::__WASI_EVENTTYPE_CLOCK,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
flags: 0,
|
||||
nbytes: 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
fn poll_oneoff_handle_fd_event<'a>(
|
||||
ready_events: impl Iterator<Item = (FdEventData<'a>, yanix::poll::PollFd)>,
|
||||
events: &mut Vec<wasi::__wasi_event_t>,
|
||||
) -> WasiResult<()> {
|
||||
use crate::entry::Descriptor;
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
use yanix::{file::fionread, poll::PollFlags};
|
||||
|
||||
fn query_nbytes(fd: &Descriptor) -> WasiResult<u64> {
|
||||
// fionread may overflow for large files, so use another way for regular files.
|
||||
if let Descriptor::OsHandle(os_handle) = fd {
|
||||
let meta = os_handle.metadata()?;
|
||||
if meta.file_type().is_file() {
|
||||
use yanix::file::tell;
|
||||
let len = meta.len();
|
||||
let host_offset = unsafe { tell(os_handle.as_raw_fd())? };
|
||||
return Ok(len - host_offset);
|
||||
}
|
||||
}
|
||||
unsafe { Ok(fionread(fd.as_raw_fd())?.into()) }
|
||||
}
|
||||
|
||||
for (fd_event, poll_fd) in ready_events {
|
||||
log::debug!("poll_oneoff_handle_fd_event fd_event = {:?}", fd_event);
|
||||
log::debug!("poll_oneoff_handle_fd_event poll_fd = {:?}", poll_fd);
|
||||
|
||||
let revents = match poll_fd.revents() {
|
||||
Some(revents) => revents,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
log::debug!("poll_oneoff_handle_fd_event revents = {:?}", revents);
|
||||
|
||||
let nbytes = if fd_event.r#type == wasi::__WASI_EVENTTYPE_FD_READ {
|
||||
query_nbytes(fd_event.descriptor)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let output_event = if revents.contains(PollFlags::POLLNVAL) {
|
||||
wasi::__wasi_event_t {
|
||||
userdata: fd_event.userdata,
|
||||
error: wasi::__WASI_ERRNO_BADF,
|
||||
r#type: fd_event.r#type,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: 0,
|
||||
flags: wasi::__WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLERR) {
|
||||
wasi::__wasi_event_t {
|
||||
userdata: fd_event.userdata,
|
||||
error: wasi::__WASI_ERRNO_IO,
|
||||
r#type: fd_event.r#type,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: 0,
|
||||
flags: wasi::__WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLHUP) {
|
||||
wasi::__wasi_event_t {
|
||||
userdata: fd_event.userdata,
|
||||
error: wasi::__WASI_ERRNO_SUCCESS,
|
||||
r#type: fd_event.r#type,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: 0,
|
||||
flags: wasi::__WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLIN) | revents.contains(PollFlags::POLLOUT) {
|
||||
wasi::__wasi_event_t {
|
||||
userdata: fd_event.userdata,
|
||||
error: wasi::__WASI_ERRNO_SUCCESS,
|
||||
r#type: fd_event.r#type,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: nbytes.try_into()?,
|
||||
flags: 0,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
events.push(output_event);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
//! Unix-specific hostcalls that implement
|
||||
//! [WASI](https://github.com/WebAssembly/WASI).
|
||||
mod fs;
|
||||
pub(crate) mod fs_helpers;
|
||||
mod misc;
|
||||
|
||||
pub(crate) use self::fs::*;
|
||||
pub(crate) use self::misc::*;
|
||||
19
crates/wasi-common/src/sys/unix/linux/fd.rs
Normal file
19
crates/wasi-common/src/sys/unix/linux/fd.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::wasi::Result;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
pub(crate) fn get_dir_from_os_handle(os_handle: &mut OsHandle) -> Result<Box<Dir>> {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// After a successful call to fdopendir(), fd is used internally by the implementation,
|
||||
// and should not otherwise be used by the application.
|
||||
// `opendir(3p)` also says that it's undefined behavior to
|
||||
// modify the state of the fd in a different way than by accessing DIR*.
|
||||
//
|
||||
// Still, rewinddir will be needed because the two file descriptors
|
||||
// share progress. But we can safely execute closedir now.
|
||||
let fd = os_handle.try_clone()?;
|
||||
// TODO This doesn't look very clean. Can we do something about it?
|
||||
// Boxing is needed here in order to satisfy `yanix`'s trait requirement for the `DirIter`
|
||||
// where `T: Deref<Target = Dir>`.
|
||||
Ok(Box::new(Dir::from(fd)?))
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
use crate::wasi::{self, WasiResult};
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
|
||||
|
||||
pub(crate) fn stdev_from_nix(dev: libc::dev_t) -> WasiResult<wasi::__wasi_device_t> {
|
||||
Ok(wasi::__wasi_device_t::from(dev))
|
||||
}
|
||||
|
||||
pub(crate) fn stino_from_nix(ino: libc::ino_t) -> WasiResult<wasi::__wasi_inode_t> {
|
||||
Ok(wasi::__wasi_device_t::from(ino))
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
use crate::entry::Descriptor;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::wasi::WasiResult;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
unsafe {
|
||||
unlinkat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::empty(),
|
||||
)
|
||||
}
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()> {
|
||||
use yanix::file::symlinkat;
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
log::debug!("path_symlink resolved = {:?}", resolved);
|
||||
|
||||
unsafe { symlinkat(old_path, resolved.dirfd().as_raw_fd(), resolved.path()) }
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiResult<()> {
|
||||
use yanix::file::renameat;
|
||||
match (resolved_old.dirfd(), resolved_new.dirfd()) {
|
||||
(Descriptor::OsHandle(resolved_old_file), Descriptor::OsHandle(resolved_new_file)) => {
|
||||
unsafe {
|
||||
renameat(
|
||||
resolved_old_file.as_raw_fd(),
|
||||
resolved_old.path(),
|
||||
resolved_new_file.as_raw_fd(),
|
||||
resolved_new.path(),
|
||||
)
|
||||
}
|
||||
.map_err(Into::into)
|
||||
}
|
||||
_ => {
|
||||
unimplemented!("path_link with one or more virtual files");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod fd_readdir_impl {
|
||||
use crate::sys::entry_impl::OsHandle;
|
||||
use crate::wasi::WasiResult;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
pub(crate) fn get_dir_from_os_handle(os_handle: &mut OsHandle) -> WasiResult<Box<Dir>> {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// After a successful call to fdopendir(), fd is used internally by the implementation,
|
||||
// and should not otherwise be used by the application.
|
||||
// `opendir(3p)` also says that it's undefined behavior to
|
||||
// modify the state of the fd in a different way than by accessing DIR*.
|
||||
//
|
||||
// Still, rewinddir will be needed because the two file descriptors
|
||||
// share progress. But we can safely execute closedir now.
|
||||
let fd = os_handle.try_clone()?;
|
||||
// TODO This doesn't look very clean. Can we do something about it?
|
||||
// Boxing is needed here in order to satisfy `yanix`'s trait requirement for the `DirIter`
|
||||
// where `T: Deref<Target = Dir>`.
|
||||
Ok(Box::new(Dir::from(fd)?))
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod oshandle;
|
||||
pub(crate) mod path;
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
|
||||
|
||||
46
crates/wasi-common/src/sys/unix/linux/path.rs
Normal file
46
crates/wasi-common/src/sys/unix/linux/path.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use crate::entry::Descriptor;
|
||||
use crate::path::PathGet;
|
||||
use crate::wasi::Result;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
unsafe {
|
||||
unlinkat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::empty(),
|
||||
)?
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use yanix::file::symlinkat;
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
log::debug!("path_symlink resolved = {:?}", resolved);
|
||||
|
||||
unsafe { symlinkat(old_path, resolved.dirfd().as_raw_fd(), resolved.path())? };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use yanix::file::renameat;
|
||||
match (resolved_old.dirfd(), resolved_new.dirfd()) {
|
||||
(Descriptor::OsHandle(resolved_old_file), Descriptor::OsHandle(resolved_new_file)) => {
|
||||
unsafe {
|
||||
renameat(
|
||||
resolved_old_file.as_raw_fd(),
|
||||
resolved_old.path(),
|
||||
resolved_new_file.as_raw_fd(),
|
||||
resolved_new.path(),
|
||||
)?
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
_ => {
|
||||
unimplemented!("path_link with one or more virtual files");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
pub(crate) mod entry_impl;
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
pub(crate) mod clock;
|
||||
pub(crate) mod entry;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod path;
|
||||
pub(crate) mod poll;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
mod linux;
|
||||
use self::linux as sys_impl;
|
||||
use linux as sys_impl;
|
||||
} else if #[cfg(target_os = "emscripten")] {
|
||||
mod emscripten;
|
||||
use self::emscripten as sys_impl;
|
||||
use emscripten as sys_impl;
|
||||
} else if #[cfg(any(target_os = "macos",
|
||||
target_os = "netbsd",
|
||||
target_os = "freebsd",
|
||||
@@ -16,18 +18,258 @@ cfg_if::cfg_if! {
|
||||
target_os = "ios",
|
||||
target_os = "dragonfly"))] {
|
||||
mod bsd;
|
||||
use self::bsd as sys_impl;
|
||||
use bsd as sys_impl;
|
||||
}
|
||||
}
|
||||
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Result;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use sys_impl::O_RSYNC;
|
||||
use yanix::clock::ClockId;
|
||||
use yanix::file::{AtFlag, OFlag};
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
pub(crate) fn dev_null() -> io::Result<File> {
|
||||
OpenOptions::new().read(true).write(true).open("/dev/null")
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
||||
File::open(path)
|
||||
}
|
||||
|
||||
impl From<types::Clockid> for ClockId {
|
||||
fn from(clock_id: types::Clockid) -> Self {
|
||||
use types::Clockid::*;
|
||||
match clock_id {
|
||||
Realtime => Self::Realtime,
|
||||
Monotonic => Self::Monotonic,
|
||||
ProcessCputimeId => Self::ProcessCPUTime,
|
||||
ThreadCputimeId => Self::ThreadCPUTime,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Errno {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code {
|
||||
libc::EPERM => Self::Perm,
|
||||
libc::ENOENT => Self::Noent,
|
||||
libc::ESRCH => Self::Srch,
|
||||
libc::EINTR => Self::Intr,
|
||||
libc::EIO => Self::Io,
|
||||
libc::ENXIO => Self::Nxio,
|
||||
libc::E2BIG => Self::TooBig,
|
||||
libc::ENOEXEC => Self::Noexec,
|
||||
libc::EBADF => Self::Badf,
|
||||
libc::ECHILD => Self::Child,
|
||||
libc::EAGAIN => Self::Again,
|
||||
libc::ENOMEM => Self::Nomem,
|
||||
libc::EACCES => Self::Acces,
|
||||
libc::EFAULT => Self::Fault,
|
||||
libc::EBUSY => Self::Busy,
|
||||
libc::EEXIST => Self::Exist,
|
||||
libc::EXDEV => Self::Xdev,
|
||||
libc::ENODEV => Self::Nodev,
|
||||
libc::ENOTDIR => Self::Notdir,
|
||||
libc::EISDIR => Self::Isdir,
|
||||
libc::EINVAL => Self::Inval,
|
||||
libc::ENFILE => Self::Nfile,
|
||||
libc::EMFILE => Self::Mfile,
|
||||
libc::ENOTTY => Self::Notty,
|
||||
libc::ETXTBSY => Self::Txtbsy,
|
||||
libc::EFBIG => Self::Fbig,
|
||||
libc::ENOSPC => Self::Nospc,
|
||||
libc::ESPIPE => Self::Spipe,
|
||||
libc::EROFS => Self::Rofs,
|
||||
libc::EMLINK => Self::Mlink,
|
||||
libc::EPIPE => Self::Pipe,
|
||||
libc::EDOM => Self::Dom,
|
||||
libc::ERANGE => Self::Range,
|
||||
libc::EDEADLK => Self::Deadlk,
|
||||
libc::ENAMETOOLONG => Self::Nametoolong,
|
||||
libc::ENOLCK => Self::Nolck,
|
||||
libc::ENOSYS => Self::Nosys,
|
||||
libc::ENOTEMPTY => Self::Notempty,
|
||||
libc::ELOOP => Self::Loop,
|
||||
libc::ENOMSG => Self::Nomsg,
|
||||
libc::EIDRM => Self::Idrm,
|
||||
libc::ENOLINK => Self::Nolink,
|
||||
libc::EPROTO => Self::Proto,
|
||||
libc::EMULTIHOP => Self::Multihop,
|
||||
libc::EBADMSG => Self::Badmsg,
|
||||
libc::EOVERFLOW => Self::Overflow,
|
||||
libc::EILSEQ => Self::Ilseq,
|
||||
libc::ENOTSOCK => Self::Notsock,
|
||||
libc::EDESTADDRREQ => Self::Destaddrreq,
|
||||
libc::EMSGSIZE => Self::Msgsize,
|
||||
libc::EPROTOTYPE => Self::Prototype,
|
||||
libc::ENOPROTOOPT => Self::Noprotoopt,
|
||||
libc::EPROTONOSUPPORT => Self::Protonosupport,
|
||||
libc::EAFNOSUPPORT => Self::Afnosupport,
|
||||
libc::EADDRINUSE => Self::Addrinuse,
|
||||
libc::EADDRNOTAVAIL => Self::Addrnotavail,
|
||||
libc::ENETDOWN => Self::Netdown,
|
||||
libc::ENETUNREACH => Self::Netunreach,
|
||||
libc::ENETRESET => Self::Netreset,
|
||||
libc::ECONNABORTED => Self::Connaborted,
|
||||
libc::ECONNRESET => Self::Connreset,
|
||||
libc::ENOBUFS => Self::Nobufs,
|
||||
libc::EISCONN => Self::Isconn,
|
||||
libc::ENOTCONN => Self::Notconn,
|
||||
libc::ETIMEDOUT => Self::Timedout,
|
||||
libc::ECONNREFUSED => Self::Connrefused,
|
||||
libc::EHOSTUNREACH => Self::Hostunreach,
|
||||
libc::EALREADY => Self::Already,
|
||||
libc::EINPROGRESS => Self::Inprogress,
|
||||
libc::ESTALE => Self::Stale,
|
||||
libc::EDQUOT => Self::Dquot,
|
||||
libc::ECANCELED => Self::Canceled,
|
||||
libc::EOWNERDEAD => Self::Ownerdead,
|
||||
libc::ENOTRECOVERABLE => Self::Notrecoverable,
|
||||
libc::ENOTSUP => Self::Notsup,
|
||||
x => {
|
||||
log::debug!("Unknown errno value: {}", x);
|
||||
Self::Io
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::Io
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Fdflags> for OFlag {
|
||||
fn from(fdflags: types::Fdflags) -> Self {
|
||||
let mut nix_flags = Self::empty();
|
||||
if fdflags.contains(&types::Fdflags::APPEND) {
|
||||
nix_flags.insert(Self::APPEND);
|
||||
}
|
||||
if fdflags.contains(&types::Fdflags::DSYNC) {
|
||||
nix_flags.insert(Self::DSYNC);
|
||||
}
|
||||
if fdflags.contains(&types::Fdflags::NONBLOCK) {
|
||||
nix_flags.insert(Self::NONBLOCK);
|
||||
}
|
||||
if fdflags.contains(&types::Fdflags::RSYNC) {
|
||||
nix_flags.insert(O_RSYNC);
|
||||
}
|
||||
if fdflags.contains(&types::Fdflags::SYNC) {
|
||||
nix_flags.insert(Self::SYNC);
|
||||
}
|
||||
nix_flags
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OFlag> for types::Fdflags {
|
||||
fn from(oflags: OFlag) -> Self {
|
||||
let mut fdflags = Self::empty();
|
||||
if oflags.contains(OFlag::APPEND) {
|
||||
fdflags |= Self::APPEND;
|
||||
}
|
||||
if oflags.contains(OFlag::DSYNC) {
|
||||
fdflags |= Self::DSYNC;
|
||||
}
|
||||
if oflags.contains(OFlag::NONBLOCK) {
|
||||
fdflags |= Self::NONBLOCK;
|
||||
}
|
||||
if oflags.contains(O_RSYNC) {
|
||||
fdflags |= Self::RSYNC;
|
||||
}
|
||||
if oflags.contains(OFlag::SYNC) {
|
||||
fdflags |= Self::SYNC;
|
||||
}
|
||||
fdflags
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Oflags> for OFlag {
|
||||
fn from(oflags: types::Oflags) -> Self {
|
||||
let mut nix_flags = Self::empty();
|
||||
if oflags.contains(&types::Oflags::CREAT) {
|
||||
nix_flags.insert(Self::CREAT);
|
||||
}
|
||||
if oflags.contains(&types::Oflags::DIRECTORY) {
|
||||
nix_flags.insert(Self::DIRECTORY);
|
||||
}
|
||||
if oflags.contains(&types::Oflags::EXCL) {
|
||||
nix_flags.insert(Self::EXCL);
|
||||
}
|
||||
if oflags.contains(&types::Oflags::TRUNC) {
|
||||
nix_flags.insert(Self::TRUNC);
|
||||
}
|
||||
nix_flags
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<libc::stat> for types::Filestat {
|
||||
type Error = Errno;
|
||||
|
||||
fn try_from(filestat: libc::stat) -> Result<Self> {
|
||||
fn filestat_to_timestamp(secs: u64, nsecs: u64) -> Result<types::Timestamp> {
|
||||
secs.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_nsec| sec_nsec.checked_add(nsecs))
|
||||
.ok_or(Errno::Overflow)
|
||||
}
|
||||
|
||||
let filetype = yanix::file::FileType::from_stat_st_mode(filestat.st_mode);
|
||||
let dev = filestat.st_dev.try_into()?;
|
||||
let ino = filestat.st_ino.try_into()?;
|
||||
let atim = filestat_to_timestamp(
|
||||
filestat.st_atime.try_into()?,
|
||||
filestat.st_atime_nsec.try_into()?,
|
||||
)?;
|
||||
let ctim = filestat_to_timestamp(
|
||||
filestat.st_ctime.try_into()?,
|
||||
filestat.st_ctime_nsec.try_into()?,
|
||||
)?;
|
||||
let mtim = filestat_to_timestamp(
|
||||
filestat.st_mtime.try_into()?,
|
||||
filestat.st_mtime_nsec.try_into()?,
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
dev,
|
||||
ino,
|
||||
nlink: filestat.st_nlink.into(),
|
||||
size: filestat.st_size as types::Filesize,
|
||||
atim,
|
||||
ctim,
|
||||
mtim,
|
||||
filetype: filetype.into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<yanix::file::FileType> for types::Filetype {
|
||||
fn from(ft: yanix::file::FileType) -> Self {
|
||||
use yanix::file::FileType::*;
|
||||
match ft {
|
||||
RegularFile => Self::RegularFile,
|
||||
Symlink => Self::SymbolicLink,
|
||||
Directory => Self::Directory,
|
||||
BlockDevice => Self::BlockDevice,
|
||||
CharacterDevice => Self::CharacterDevice,
|
||||
/* Unknown | Socket | Fifo */
|
||||
_ => Self::Unknown,
|
||||
// TODO how to discriminate between STREAM and DGRAM?
|
||||
// Perhaps, we should create a more general WASI filetype
|
||||
// such as __WASI_FILETYPE_SOCKET, and then it would be
|
||||
// up to the client to check whether it's actually
|
||||
// STREAM or DGRAM?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Lookupflags> for AtFlag {
|
||||
fn from(flags: types::Lookupflags) -> Self {
|
||||
match flags {
|
||||
types::Lookupflags::SYMLINK_FOLLOW => Self::empty(),
|
||||
_ => Self::SYMLINK_NOFOLLOW,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
298
crates/wasi-common/src/sys/unix/path.rs
Normal file
298
crates/wasi-common/src/sys/unix/path.rs
Normal file
@@ -0,0 +1,298 @@
|
||||
use crate::entry::Descriptor;
|
||||
use crate::path::PathGet;
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::sys::unix::sys_impl;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::File;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, OsStrExt};
|
||||
use std::str;
|
||||
use yanix::file::OFlag;
|
||||
|
||||
pub(crate) use sys_impl::path::*;
|
||||
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_ERRNO_ILSEQ` error is returned.
|
||||
pub(crate) fn from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
let s = str::from_utf8(s.as_ref().as_bytes())?;
|
||||
Ok(s.to_owned())
|
||||
}
|
||||
|
||||
pub(crate) fn open_rights(
|
||||
rights_base: types::Rights,
|
||||
rights_inheriting: types::Rights,
|
||||
oflags: types::Oflags,
|
||||
fs_flags: types::Fdflags,
|
||||
) -> (types::Rights, types::Rights) {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = types::Rights::PATH_OPEN;
|
||||
let mut needed_inheriting = rights_base | rights_inheriting;
|
||||
|
||||
// convert open flags
|
||||
let oflags: OFlag = oflags.into();
|
||||
if oflags.contains(OFlag::CREAT) {
|
||||
needed_base |= types::Rights::PATH_CREATE_FILE;
|
||||
}
|
||||
if oflags.contains(OFlag::TRUNC) {
|
||||
needed_base |= types::Rights::PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
let fdflags: OFlag = fs_flags.into();
|
||||
if fdflags.contains(OFlag::DSYNC) {
|
||||
needed_inheriting |= types::Rights::FD_DATASYNC;
|
||||
}
|
||||
if fdflags.intersects(sys_impl::O_RSYNC | OFlag::SYNC) {
|
||||
needed_inheriting |= types::Rights::FD_SYNC;
|
||||
}
|
||||
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
use yanix::file::{openat, Mode};
|
||||
|
||||
log::debug!("path_get openat path = {:?}", path);
|
||||
|
||||
let raw_fd = unsafe {
|
||||
openat(
|
||||
dirfd.as_raw_fd(),
|
||||
path,
|
||||
OFlag::RDONLY | OFlag::DIRECTORY | OFlag::NOFOLLOW,
|
||||
Mode::empty(),
|
||||
)?
|
||||
};
|
||||
let file = unsafe { File::from_raw_fd(raw_fd) };
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &File, path: &str) -> Result<String> {
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
use yanix::file::readlinkat;
|
||||
|
||||
log::debug!("path_get readlinkat path = {:?}", path);
|
||||
|
||||
let path = unsafe { readlinkat(dirfd.as_raw_fd(), path)? };
|
||||
let path = from_host(path)?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub(crate) fn create_directory(base: &File, path: &str) -> Result<()> {
|
||||
use yanix::file::{mkdirat, Mode};
|
||||
unsafe { mkdirat(base.as_raw_fd(), path, Mode::from_bits_truncate(0o777))? };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn link(
|
||||
resolved_old: PathGet,
|
||||
resolved_new: PathGet,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<()> {
|
||||
use yanix::file::{linkat, AtFlag};
|
||||
let flags = if follow_symlinks {
|
||||
AtFlag::SYMLINK_FOLLOW
|
||||
} else {
|
||||
AtFlag::empty()
|
||||
};
|
||||
unsafe {
|
||||
linkat(
|
||||
resolved_old.dirfd().as_raw_fd(),
|
||||
resolved_old.path(),
|
||||
resolved_new.dirfd().as_raw_fd(),
|
||||
resolved_new.path(),
|
||||
flags,
|
||||
)?
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn open(
|
||||
resolved: PathGet,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fs_flags: types::Fdflags,
|
||||
) -> Result<Descriptor> {
|
||||
use yanix::file::{fstatat, openat, AtFlag, FileType, Mode, OFlag};
|
||||
|
||||
let mut nix_all_oflags = if read && write {
|
||||
OFlag::RDWR
|
||||
} else if write {
|
||||
OFlag::WRONLY
|
||||
} else {
|
||||
OFlag::RDONLY
|
||||
};
|
||||
|
||||
// on non-Capsicum systems, we always want nofollow
|
||||
nix_all_oflags.insert(OFlag::NOFOLLOW);
|
||||
|
||||
// convert open flags
|
||||
nix_all_oflags.insert(oflags.into());
|
||||
|
||||
// convert file descriptor flags
|
||||
nix_all_oflags.insert(fs_flags.into());
|
||||
|
||||
// Call openat. Use mode 0o666 so that we follow whatever the user's
|
||||
// umask is, but don't set the executable flag, because it isn't yet
|
||||
// meaningful for WASI programs to create executable files.
|
||||
|
||||
log::debug!("path_open resolved = {:?}", resolved);
|
||||
log::debug!("path_open oflags = {:?}", nix_all_oflags);
|
||||
|
||||
let fd_no = unsafe {
|
||||
openat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
nix_all_oflags,
|
||||
Mode::from_bits_truncate(0o666),
|
||||
)
|
||||
};
|
||||
let new_fd = match fd_no {
|
||||
Ok(fd) => fd,
|
||||
Err(e) => {
|
||||
match e.raw_os_error().unwrap() {
|
||||
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
|
||||
libc::ENXIO => {
|
||||
match unsafe {
|
||||
fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Socket {
|
||||
return Err(Errno::Notsup);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("path_open fstatat error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Linux returns ENOTDIR instead of ELOOP when using O_NOFOLLOW|O_DIRECTORY
|
||||
// on a symlink.
|
||||
libc::ENOTDIR
|
||||
if !(nix_all_oflags & (OFlag::NOFOLLOW | OFlag::DIRECTORY)).is_empty() =>
|
||||
{
|
||||
match unsafe {
|
||||
fstatat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Symlink {
|
||||
return Err(Errno::Loop);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("path_open fstatat error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on
|
||||
// a symlink.
|
||||
libc::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
|
||||
return Err(Errno::Loop);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
|
||||
log::debug!("path_open (host) new_fd = {:?}", new_fd);
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
Ok(OsHandle::from(unsafe { File::from_raw_fd(new_fd) }).into())
|
||||
}
|
||||
|
||||
pub(crate) fn readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
|
||||
use std::cmp::min;
|
||||
use yanix::file::readlinkat;
|
||||
let read_link = unsafe { readlinkat(resolved.dirfd().as_raw_fd(), resolved.path())? };
|
||||
let read_link = from_host(read_link)?;
|
||||
let copy_len = min(read_link.len(), buf.len());
|
||||
if copy_len > 0 {
|
||||
buf[..copy_len].copy_from_slice(&read_link.as_bytes()[..copy_len]);
|
||||
}
|
||||
Ok(copy_len)
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_get(
|
||||
resolved: PathGet,
|
||||
dirflags: types::Lookupflags,
|
||||
) -> Result<types::Filestat> {
|
||||
use yanix::file::fstatat;
|
||||
let atflags = dirflags.into();
|
||||
let filestat = unsafe { fstatat(resolved.dirfd().as_raw_fd(), resolved.path(), atflags)? };
|
||||
let filestat = filestat.try_into()?;
|
||||
Ok(filestat)
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_set_times(
|
||||
resolved: PathGet,
|
||||
dirflags: types::Lookupflags,
|
||||
st_atim: types::Timestamp,
|
||||
st_mtim: types::Timestamp,
|
||||
fst_flags: types::Fstflags,
|
||||
) -> Result<()> {
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
use yanix::filetime::*;
|
||||
|
||||
let set_atim = fst_flags.contains(&types::Fstflags::ATIM);
|
||||
let set_atim_now = fst_flags.contains(&types::Fstflags::ATIM_NOW);
|
||||
let set_mtim = fst_flags.contains(&types::Fstflags::MTIM);
|
||||
let set_mtim_now = fst_flags.contains(&types::Fstflags::MTIM_NOW);
|
||||
|
||||
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||
return Err(Errno::Inval);
|
||||
}
|
||||
|
||||
let symlink_nofollow = types::Lookupflags::SYMLINK_FOLLOW != dirflags;
|
||||
let atim = if set_atim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
|
||||
FileTime::FileTime(filetime::FileTime::from_system_time(time))
|
||||
} else if set_atim_now {
|
||||
FileTime::Now
|
||||
} else {
|
||||
FileTime::Omit
|
||||
};
|
||||
let mtim = if set_mtim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_mtim);
|
||||
FileTime::FileTime(filetime::FileTime::from_system_time(time))
|
||||
} else if set_mtim_now {
|
||||
FileTime::Now
|
||||
} else {
|
||||
FileTime::Omit
|
||||
};
|
||||
|
||||
utimensat(
|
||||
&resolved.dirfd().as_os_handle(),
|
||||
resolved.path(),
|
||||
atim,
|
||||
mtim,
|
||||
symlink_nofollow,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn remove_directory(resolved: PathGet) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
|
||||
unsafe {
|
||||
unlinkat(
|
||||
resolved.dirfd().as_raw_fd(),
|
||||
resolved.path(),
|
||||
AtFlag::REMOVEDIR,
|
||||
)?
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
159
crates/wasi-common/src/sys/unix/poll.rs
Normal file
159
crates/wasi-common/src/sys/unix/poll.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
use crate::poll::{ClockEventData, FdEventData};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::io;
|
||||
|
||||
pub(crate) fn oneoff(
|
||||
timeout: Option<ClockEventData>,
|
||||
fd_events: Vec<FdEventData>,
|
||||
events: &mut Vec<types::Event>,
|
||||
) -> Result<()> {
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
use yanix::poll::{poll, PollFd, PollFlags};
|
||||
|
||||
if fd_events.is_empty() && timeout.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut poll_fds: Vec<_> = fd_events
|
||||
.iter()
|
||||
.map(|event| {
|
||||
let mut flags = PollFlags::empty();
|
||||
match event.r#type {
|
||||
types::Eventtype::FdRead => flags.insert(PollFlags::POLLIN),
|
||||
types::Eventtype::FdWrite => flags.insert(PollFlags::POLLOUT),
|
||||
// An event on a file descriptor can currently only be of type FD_READ or FD_WRITE
|
||||
// Nothing else has been defined in the specification, and these are also the only two
|
||||
// events we filtered before. If we get something else here, the code has a serious bug.
|
||||
_ => unreachable!(),
|
||||
};
|
||||
unsafe { PollFd::new(event.descriptor.as_raw_fd(), flags) }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let poll_timeout = timeout.map_or(-1, |timeout| {
|
||||
let delay = timeout.delay / 1_000_000; // poll syscall requires delay to expressed in milliseconds
|
||||
delay.try_into().unwrap_or(libc::c_int::max_value())
|
||||
});
|
||||
log::debug!("poll_oneoff poll_timeout = {:?}", poll_timeout);
|
||||
|
||||
let ready = loop {
|
||||
match poll(&mut poll_fds, poll_timeout) {
|
||||
Err(_) => {
|
||||
let last_err = io::Error::last_os_error();
|
||||
if last_err.raw_os_error().unwrap() == libc::EINTR {
|
||||
continue;
|
||||
}
|
||||
return Err(last_err.into());
|
||||
}
|
||||
Ok(ready) => break ready,
|
||||
}
|
||||
};
|
||||
|
||||
Ok(if ready == 0 {
|
||||
handle_timeout_event(timeout.expect("timeout should not be None"), events)
|
||||
} else {
|
||||
let ready_events = fd_events.into_iter().zip(poll_fds.into_iter()).take(ready);
|
||||
handle_fd_event(ready_events, events)?
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_timeout_event(timeout: ClockEventData, events: &mut Vec<types::Event>) {
|
||||
events.push(types::Event {
|
||||
userdata: timeout.userdata,
|
||||
error: Errno::Success,
|
||||
type_: types::Eventtype::Clock,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
flags: types::Eventrwflags::empty(),
|
||||
nbytes: 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
fn handle_fd_event<'a>(
|
||||
ready_events: impl Iterator<Item = (FdEventData<'a>, yanix::poll::PollFd)>,
|
||||
events: &mut Vec<types::Event>,
|
||||
) -> Result<()> {
|
||||
use crate::entry::Descriptor;
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
use yanix::{file::fionread, poll::PollFlags};
|
||||
|
||||
fn query_nbytes(fd: &Descriptor) -> Result<u64> {
|
||||
// fionread may overflow for large files, so use another way for regular files.
|
||||
if let Descriptor::OsHandle(os_handle) = fd {
|
||||
let meta = os_handle.metadata()?;
|
||||
if meta.file_type().is_file() {
|
||||
use yanix::file::tell;
|
||||
let len = meta.len();
|
||||
let host_offset = unsafe { tell(os_handle.as_raw_fd())? };
|
||||
return Ok(len - host_offset);
|
||||
}
|
||||
}
|
||||
unsafe { Ok(fionread(fd.as_raw_fd())?.into()) }
|
||||
}
|
||||
|
||||
for (fd_event, poll_fd) in ready_events {
|
||||
log::debug!("poll_oneoff_handle_fd_event fd_event = {:?}", fd_event);
|
||||
log::debug!("poll_oneoff_handle_fd_event poll_fd = {:?}", poll_fd);
|
||||
|
||||
let revents = match poll_fd.revents() {
|
||||
Some(revents) => revents,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
log::debug!("poll_oneoff_handle_fd_event revents = {:?}", revents);
|
||||
|
||||
let nbytes = if fd_event.r#type == types::Eventtype::FdRead {
|
||||
query_nbytes(&fd_event.descriptor)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let output_event = if revents.contains(PollFlags::POLLNVAL) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Badf,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLERR) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Io,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLHUP) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Success,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
|
||||
},
|
||||
}
|
||||
} else if revents.contains(PollFlags::POLLIN) | revents.contains(PollFlags::POLLOUT) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Success,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: nbytes.try_into()?,
|
||||
flags: types::Eventrwflags::empty(),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
events.push(output_event);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
104
crates/wasi-common/src/sys/windows/clock.rs
Normal file
104
crates/wasi-common/src/sys/windows/clock.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use cpu_time::{ProcessTime, ThreadTime};
|
||||
use lazy_static::lazy_static;
|
||||
use std::convert::TryInto;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
lazy_static! {
|
||||
static ref START_MONOTONIC: Instant = Instant::now();
|
||||
static ref PERF_COUNTER_RES: u64 = get_perf_counter_resolution_ns();
|
||||
}
|
||||
|
||||
// Timer resolution on Windows is really hard. We may consider exposing the resolution of the respective
|
||||
// timers as an associated function in the future.
|
||||
pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
let ts = match clock_id {
|
||||
// This is the best that we can do with std::time::SystemTime.
|
||||
// Rust uses GetSystemTimeAsFileTime, which is said to have the resolution of
|
||||
// 10ms or 55ms, [1] but MSDN doesn't confirm this in any way.
|
||||
// Even the MSDN article on high resolution timestamps doesn't even mention the precision
|
||||
// for this method. [3]
|
||||
//
|
||||
// The timer resolution can be queried using one of the functions: [2, 5]
|
||||
// * NtQueryTimerResolution, which is undocumented and thus not exposed by the winapi crate
|
||||
// * timeGetDevCaps, which returns the upper and lower bound for the precision, in ms.
|
||||
// While the upper bound seems like something we could use, it's typically too high to be meaningful.
|
||||
// For instance, the intervals return by the syscall are:
|
||||
// * [1, 65536] on Wine
|
||||
// * [1, 1000000] on Windows 10, which is up to (sic) 1000 seconds.
|
||||
//
|
||||
// It's possible to manually set the timer resolution, but this sounds like something which should
|
||||
// only be done temporarily. [5]
|
||||
//
|
||||
// Alternatively, we could possibly use GetSystemTimePreciseAsFileTime in clock_time_get, but
|
||||
// this syscall is only available starting from Windows 8.
|
||||
// (we could possibly emulate it on earlier versions of Windows, see [4])
|
||||
// The MSDN are not clear on the resolution of GetSystemTimePreciseAsFileTime either, but a
|
||||
// Microsoft devblog entry [1] suggests that it kind of combines GetSystemTimeAsFileTime with
|
||||
// QueryPeformanceCounter, which probably means that those two should have the same resolution.
|
||||
//
|
||||
// See also this discussion about the use of GetSystemTimePreciseAsFileTime in Python stdlib,
|
||||
// which in particular contains some resolution benchmarks.
|
||||
//
|
||||
// [1] https://devblogs.microsoft.com/oldnewthing/20170921-00/?p=97057
|
||||
// [2] http://www.windowstimestamp.com/description
|
||||
// [3] https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps?redirectedfrom=MSDN
|
||||
// [4] https://www.codeproject.com/Tips/1011902/High-Resolution-Time-For-Windows
|
||||
// [5] https://stackoverflow.com/questions/7685762/windows-7-timing-functions-how-to-use-getsystemtimeadjustment-correctly
|
||||
// [6] https://bugs.python.org/issue19007
|
||||
types::Clockid::Realtime => 55_000_000,
|
||||
// std::time::Instant uses QueryPerformanceCounter & QueryPerformanceFrequency internally
|
||||
types::Clockid::Monotonic => *PERF_COUNTER_RES,
|
||||
// The best we can do is to hardcode the value from the docs.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes
|
||||
types::Clockid::ProcessCputimeId => 100,
|
||||
// The best we can do is to hardcode the value from the docs.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
|
||||
types::Clockid::ThreadCputimeId => 100,
|
||||
};
|
||||
Ok(ts)
|
||||
}
|
||||
|
||||
pub(crate) fn time_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
let duration = match clock_id {
|
||||
types::Clockid::Realtime => get_monotonic_time(),
|
||||
types::Clockid::Monotonic => get_realtime_time()?,
|
||||
types::Clockid::ProcessCputimeId => get_proc_cputime()?,
|
||||
types::Clockid::ThreadCputimeId => get_thread_cputime()?,
|
||||
};
|
||||
let duration = duration.as_nanos().try_into()?;
|
||||
Ok(duration)
|
||||
}
|
||||
|
||||
fn get_monotonic_time() -> Duration {
|
||||
// We're circumventing the fact that we can't get a Duration from an Instant
|
||||
// The epoch of __WASI_CLOCKID_MONOTONIC is undefined, so we fix a time point once
|
||||
// and count relative to this time point.
|
||||
//
|
||||
// The alternative would be to copy over the implementation of std::time::Instant
|
||||
// to our source tree and add a conversion to std::time::Duration
|
||||
START_MONOTONIC.elapsed()
|
||||
}
|
||||
|
||||
fn get_realtime_time() -> Result<Duration> {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Errno::Fault)
|
||||
}
|
||||
|
||||
fn get_proc_cputime() -> Result<Duration> {
|
||||
Ok(ProcessTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
fn get_thread_cputime() -> Result<Duration> {
|
||||
Ok(ThreadTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
fn get_perf_counter_resolution_ns() -> u64 {
|
||||
use winx::time::perf_counter_frequency;
|
||||
const NANOS_PER_SEC: u64 = 1_000_000_000;
|
||||
// This should always succeed starting from Windows XP, so it's fine to panic in case of an error.
|
||||
let freq = perf_counter_frequency().expect("QueryPerformanceFrequency returned an error");
|
||||
let epsilon = NANOS_PER_SEC / freq;
|
||||
epsilon
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::entry::{Descriptor, OsHandleRef};
|
||||
use crate::wasi;
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
@@ -63,23 +63,19 @@ pub(crate) fn descriptor_as_oshandle<'lifetime>(
|
||||
/// This function is unsafe because it operates on a raw file descriptor.
|
||||
pub(crate) unsafe fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
||||
handle: &Handle,
|
||||
) -> io::Result<(
|
||||
wasi::__wasi_filetype_t,
|
||||
wasi::__wasi_rights_t,
|
||||
wasi::__wasi_rights_t,
|
||||
)> {
|
||||
) -> io::Result<(types::Filetype, types::Rights, types::Rights)> {
|
||||
use winx::file::{query_access_information, AccessMode};
|
||||
|
||||
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(handle)?;
|
||||
|
||||
match file_type {
|
||||
wasi::__WASI_FILETYPE_DIRECTORY | wasi::__WASI_FILETYPE_REGULAR_FILE => {
|
||||
types::Filetype::Directory | types::Filetype::RegularFile => {
|
||||
let mode = query_access_information(handle.as_raw_handle())?;
|
||||
if mode.contains(AccessMode::FILE_GENERIC_READ) {
|
||||
rights_base |= wasi::__WASI_RIGHTS_FD_READ;
|
||||
rights_base |= types::Rights::FD_READ;
|
||||
}
|
||||
if mode.contains(AccessMode::FILE_GENERIC_WRITE) {
|
||||
rights_base |= wasi::__WASI_RIGHTS_FD_WRITE;
|
||||
rights_base |= types::Rights::FD_WRITE;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@@ -96,20 +92,16 @@ pub(crate) unsafe fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
||||
/// This function is unsafe because it operates on a raw file descriptor.
|
||||
pub(crate) unsafe fn determine_type_rights<Handle: AsRawHandle>(
|
||||
handle: &Handle,
|
||||
) -> io::Result<(
|
||||
wasi::__wasi_filetype_t,
|
||||
wasi::__wasi_rights_t,
|
||||
wasi::__wasi_rights_t,
|
||||
)> {
|
||||
) -> io::Result<(types::Filetype, types::Rights, types::Rights)> {
|
||||
let (file_type, rights_base, rights_inheriting) = {
|
||||
let file_type = winx::file::get_file_type(handle.as_raw_handle())?;
|
||||
if file_type.is_char() {
|
||||
// character file: LPT device or console
|
||||
// TODO: rule out LPT device
|
||||
(
|
||||
wasi::__WASI_FILETYPE_CHARACTER_DEVICE,
|
||||
wasi::RIGHTS_TTY_BASE,
|
||||
wasi::RIGHTS_TTY_BASE,
|
||||
types::Filetype::CharacterDevice,
|
||||
types::Rights::tty_base(),
|
||||
types::Rights::tty_base(),
|
||||
)
|
||||
} else if file_type.is_disk() {
|
||||
// disk file: file, dir or disk device
|
||||
@@ -117,15 +109,15 @@ pub(crate) unsafe fn determine_type_rights<Handle: AsRawHandle>(
|
||||
let meta = file.metadata()?;
|
||||
if meta.is_dir() {
|
||||
(
|
||||
wasi::__WASI_FILETYPE_DIRECTORY,
|
||||
wasi::RIGHTS_DIRECTORY_BASE,
|
||||
wasi::RIGHTS_DIRECTORY_INHERITING,
|
||||
types::Filetype::Directory,
|
||||
types::Rights::directory_base(),
|
||||
types::Rights::directory_inheriting(),
|
||||
)
|
||||
} else if meta.is_file() {
|
||||
(
|
||||
wasi::__WASI_FILETYPE_REGULAR_FILE,
|
||||
wasi::RIGHTS_REGULAR_FILE_BASE,
|
||||
wasi::RIGHTS_REGULAR_FILE_INHERITING,
|
||||
types::Filetype::RegularFile,
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
)
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
@@ -134,9 +126,9 @@ pub(crate) unsafe fn determine_type_rights<Handle: AsRawHandle>(
|
||||
// pipe object: socket, named pipe or anonymous pipe
|
||||
// TODO: what about pipes, etc?
|
||||
(
|
||||
wasi::__WASI_FILETYPE_SOCKET_STREAM,
|
||||
wasi::RIGHTS_SOCKET_BASE,
|
||||
wasi::RIGHTS_SOCKET_INHERITING,
|
||||
types::Filetype::SocketStream,
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_inheriting(),
|
||||
)
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
188
crates/wasi-common/src/sys/windows/fd.rs
Normal file
188
crates/wasi-common/src/sys/windows/fd.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
use super::file_serial_no;
|
||||
use crate::path;
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::wasi::{types, Result};
|
||||
use log::trace;
|
||||
use std::convert::TryInto;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::Path;
|
||||
use winx::file::{AccessMode, FileModeInformation, Flags};
|
||||
|
||||
pub(crate) fn fdstat_get(fd: &File) -> Result<types::Fdflags> {
|
||||
let mut fdflags = types::Fdflags::empty();
|
||||
let handle = fd.as_raw_handle();
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
let mode = winx::file::query_mode_information(handle)?;
|
||||
|
||||
// Append without write implies append-only (__WASI_FDFLAGS_APPEND)
|
||||
if access_mode.contains(AccessMode::FILE_APPEND_DATA)
|
||||
&& !access_mode.contains(AccessMode::FILE_WRITE_DATA)
|
||||
{
|
||||
fdflags |= types::Fdflags::APPEND;
|
||||
}
|
||||
|
||||
if mode.contains(FileModeInformation::FILE_WRITE_THROUGH) {
|
||||
// Only report __WASI_FDFLAGS_SYNC
|
||||
// This is technically the only one of the O_?SYNC flags Windows supports.
|
||||
fdflags |= types::Fdflags::SYNC;
|
||||
}
|
||||
|
||||
// Files do not support the `__WASI_FDFLAGS_NONBLOCK` flag
|
||||
|
||||
Ok(fdflags)
|
||||
}
|
||||
|
||||
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::Fdflags) -> Result<Option<OsHandle>> {
|
||||
let handle = fd.as_raw_handle();
|
||||
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
|
||||
let new_access_mode = file_access_mode_from_fdflags(
|
||||
fdflags,
|
||||
access_mode.contains(AccessMode::FILE_READ_DATA),
|
||||
access_mode.contains(AccessMode::FILE_WRITE_DATA)
|
||||
| access_mode.contains(AccessMode::FILE_APPEND_DATA),
|
||||
);
|
||||
|
||||
unsafe {
|
||||
Ok(Some(OsHandle::from(File::from_raw_handle(
|
||||
winx::file::reopen_file(handle, new_access_mode, fdflags.into())?,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn advise(
|
||||
_file: &File,
|
||||
_advice: types::Advice,
|
||||
_offset: types::Filesize,
|
||||
_len: types::Filesize,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn file_access_mode_from_fdflags(fdflags: types::Fdflags, read: bool, write: bool) -> AccessMode {
|
||||
let mut access_mode = AccessMode::READ_CONTROL;
|
||||
|
||||
// Note that `GENERIC_READ` and `GENERIC_WRITE` cannot be used to properly support append-only mode
|
||||
// The file-specific flags `FILE_GENERIC_READ` and `FILE_GENERIC_WRITE` are used here instead
|
||||
// These flags have the same semantic meaning for file objects, but allow removal of specific permissions (see below)
|
||||
if read {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_READ);
|
||||
}
|
||||
|
||||
if write {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// For append, grant the handle FILE_APPEND_DATA access but *not* FILE_WRITE_DATA.
|
||||
// This makes the handle "append only".
|
||||
// Changes to the file pointer will be ignored (like POSIX's O_APPEND behavior).
|
||||
if fdflags.contains(&types::Fdflags::APPEND) {
|
||||
access_mode.insert(AccessMode::FILE_APPEND_DATA);
|
||||
access_mode.remove(AccessMode::FILE_WRITE_DATA);
|
||||
}
|
||||
|
||||
access_mode
|
||||
}
|
||||
|
||||
// On Windows there is apparently no support for seeking the directory stream in the OS.
|
||||
// cf. https://github.com/WebAssembly/WASI/issues/61
|
||||
//
|
||||
// The implementation here may perform in O(n^2) if the host buffer is O(1)
|
||||
// and the number of directory entries is O(n).
|
||||
// TODO: Add a heuristic optimization to achieve O(n) time in the most common case
|
||||
// where fd_readdir is resumed where it previously finished
|
||||
//
|
||||
// Correctness of this approach relies upon one assumption: that the order of entries
|
||||
// returned by `FindNextFileW` is stable, i.e. doesn't change if the directory
|
||||
// contents stay the same. This invariant is crucial to be able to implement
|
||||
// any kind of seeking whatsoever without having to read the whole directory at once
|
||||
// and then return the data from cache. (which leaks memory)
|
||||
//
|
||||
// The MSDN documentation explicitly says that the order in which the search returns the files
|
||||
// is not guaranteed, and is dependent on the file system.
|
||||
// cf. https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextfilew
|
||||
//
|
||||
// This stackoverflow post suggests that `FindNextFileW` is indeed stable and that
|
||||
// the order of directory entries depends **only** on the filesystem used, but the
|
||||
// MSDN documentation is not clear about this.
|
||||
// cf. https://stackoverflow.com/questions/47380739/is-findfirstfile-and-findnextfile-order-random-even-for-dvd
|
||||
//
|
||||
// Implementation details:
|
||||
// Cookies for the directory entries start from 1. (0 is reserved by wasi::__WASI_DIRCOOKIE_START)
|
||||
// . gets cookie = 1
|
||||
// .. gets cookie = 2
|
||||
// other entries, in order they were returned by FindNextFileW get subsequent integers as their cookies
|
||||
pub(crate) fn readdir(
|
||||
fd: &File,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<impl Iterator<Item = Result<(types::Dirent, String)>>> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let cookie = cookie.try_into()?;
|
||||
let path = get_file_path(fd)?;
|
||||
// std::fs::ReadDir doesn't return . and .., so we need to emulate it
|
||||
let path = Path::new(&path);
|
||||
// The directory /.. is the same as / on Unix (at least on ext4), so emulate this behavior too
|
||||
let parent = path.parent().unwrap_or(path);
|
||||
let dot = dirent_from_path(path, ".", 1)?;
|
||||
let dotdot = dirent_from_path(parent, "..", 2)?;
|
||||
|
||||
trace!(" | fd_readdir impl: executing std::fs::ReadDir");
|
||||
let iter = path.read_dir()?.zip(3..).map(|(dir, no)| {
|
||||
let dir: std::fs::DirEntry = dir?;
|
||||
let ftype = dir.file_type()?;
|
||||
let name = path::from_host(dir.file_name())?;
|
||||
let d_ino = File::open(dir.path()).and_then(|f| file_serial_no(&f))?;
|
||||
let dirent = types::Dirent {
|
||||
d_namlen: name.len().try_into()?,
|
||||
d_type: ftype.into(),
|
||||
d_ino,
|
||||
d_next: no,
|
||||
};
|
||||
|
||||
Ok((dirent, name))
|
||||
});
|
||||
|
||||
// into_iter for arrays is broken and returns references instead of values,
|
||||
// so we need to use vec![...] and do heap allocation
|
||||
// See https://github.com/rust-lang/rust/issues/25725
|
||||
let iter = vec![dot, dotdot].into_iter().map(Ok).chain(iter);
|
||||
|
||||
// Emulate seekdir(). This may give O(n^2) complexity if used with a
|
||||
// small host_buf, but this is difficult to implement efficiently.
|
||||
//
|
||||
// See https://github.com/WebAssembly/WASI/issues/61 for more details.
|
||||
Ok(iter.skip(cookie))
|
||||
}
|
||||
|
||||
fn dirent_from_path<P: AsRef<Path>>(
|
||||
path: P,
|
||||
name: &str,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<(types::Dirent, String)> {
|
||||
let path = path.as_ref();
|
||||
trace!("dirent_from_path: opening {}", path.to_string_lossy());
|
||||
|
||||
// To open a directory on Windows, FILE_FLAG_BACKUP_SEMANTICS flag must be used
|
||||
let file = OpenOptions::new()
|
||||
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
||||
.read(true)
|
||||
.open(path)?;
|
||||
let ty = file.metadata()?.file_type();
|
||||
let name = name.to_owned();
|
||||
let dirent = types::Dirent {
|
||||
d_namlen: name.len().try_into()?,
|
||||
d_next: cookie,
|
||||
d_type: ty.into(),
|
||||
d_ino: file_serial_no(&file)?,
|
||||
};
|
||||
Ok((dirent, name))
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_get(file: &std::fs::File) -> Result<types::Filestat> {
|
||||
let filestat = file.try_into()?;
|
||||
Ok(filestat)
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
//! WASI host types specific to Windows host.
|
||||
use crate::host::FileType;
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{self, File};
|
||||
use std::io;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use winapi::shared::winerror;
|
||||
|
||||
impl From<io::Error> for WasiError {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code as u32 {
|
||||
winerror::ERROR_SUCCESS => Self::ESUCCESS,
|
||||
winerror::ERROR_BAD_ENVIRONMENT => Self::E2BIG,
|
||||
winerror::ERROR_FILE_NOT_FOUND => Self::ENOENT,
|
||||
winerror::ERROR_PATH_NOT_FOUND => Self::ENOENT,
|
||||
winerror::ERROR_TOO_MANY_OPEN_FILES => Self::ENFILE,
|
||||
winerror::ERROR_ACCESS_DENIED => Self::EACCES,
|
||||
winerror::ERROR_SHARING_VIOLATION => Self::EACCES,
|
||||
winerror::ERROR_PRIVILEGE_NOT_HELD => Self::ENOTCAPABLE,
|
||||
winerror::ERROR_INVALID_HANDLE => Self::EBADF,
|
||||
winerror::ERROR_INVALID_NAME => Self::ENOENT,
|
||||
winerror::ERROR_NOT_ENOUGH_MEMORY => Self::ENOMEM,
|
||||
winerror::ERROR_OUTOFMEMORY => Self::ENOMEM,
|
||||
winerror::ERROR_DIR_NOT_EMPTY => Self::ENOTEMPTY,
|
||||
winerror::ERROR_NOT_READY => Self::EBUSY,
|
||||
winerror::ERROR_BUSY => Self::EBUSY,
|
||||
winerror::ERROR_NOT_SUPPORTED => Self::ENOTSUP,
|
||||
winerror::ERROR_FILE_EXISTS => Self::EEXIST,
|
||||
winerror::ERROR_BROKEN_PIPE => Self::EPIPE,
|
||||
winerror::ERROR_BUFFER_OVERFLOW => Self::ENAMETOOLONG,
|
||||
winerror::ERROR_NOT_A_REPARSE_POINT => Self::EINVAL,
|
||||
winerror::ERROR_NEGATIVE_SEEK => Self::EINVAL,
|
||||
winerror::ERROR_DIRECTORY => Self::ENOTDIR,
|
||||
winerror::ERROR_ALREADY_EXISTS => Self::EEXIST,
|
||||
x => {
|
||||
log::debug!("unknown error value: {}", x);
|
||||
Self::EIO
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::EIO
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn filetype_from_std(ftype: &fs::FileType) -> FileType {
|
||||
if ftype.is_file() {
|
||||
FileType::RegularFile
|
||||
} else if ftype.is_dir() {
|
||||
FileType::Directory
|
||||
} else if ftype.is_symlink() {
|
||||
FileType::Symlink
|
||||
} else {
|
||||
FileType::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
fn num_hardlinks(file: &File) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.nNumberOfLinks.into())
|
||||
}
|
||||
|
||||
fn device_id(file: &File) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.dwVolumeSerialNumber.into())
|
||||
}
|
||||
|
||||
pub(crate) fn file_serial_no(file: &File) -> io::Result<u64> {
|
||||
let info = winx::file::get_fileinfo(file)?;
|
||||
let high = info.nFileIndexHigh;
|
||||
let low = info.nFileIndexLow;
|
||||
let no = (u64::from(high) << 32) | u64::from(low);
|
||||
Ok(no)
|
||||
}
|
||||
|
||||
fn change_time(file: &File) -> io::Result<i64> {
|
||||
winx::file::change_time(file)
|
||||
}
|
||||
|
||||
fn systemtime_to_timestamp(st: SystemTime) -> WasiResult<u64> {
|
||||
st.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| WasiError::EINVAL)? // date earlier than UNIX_EPOCH
|
||||
.as_nanos()
|
||||
.try_into()
|
||||
.map_err(Into::into) // u128 doesn't fit into u64
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_from_win(file: &File) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
let metadata = file.metadata()?;
|
||||
Ok(wasi::__wasi_filestat_t {
|
||||
dev: device_id(file)?,
|
||||
ino: file_serial_no(file)?,
|
||||
nlink: num_hardlinks(file)?.try_into()?, // u64 doesn't fit into u32
|
||||
size: metadata.len(),
|
||||
atim: systemtime_to_timestamp(metadata.accessed()?)?,
|
||||
ctim: change_time(file)?.try_into()?, // i64 doesn't fit into u64
|
||||
mtim: systemtime_to_timestamp(metadata.modified()?)?,
|
||||
filetype: filetype_from_std(&metadata.file_type()).to_wasi(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_ERRNO_ILSEQ` error is returned.
|
||||
pub(crate) fn path_from_host<S: AsRef<OsStr>>(s: S) -> WasiResult<String> {
|
||||
let vec: Vec<u16> = s.as_ref().encode_wide().collect();
|
||||
String::from_utf16(&vec).map_err(|_| WasiError::EILSEQ)
|
||||
}
|
||||
@@ -1,599 +0,0 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused)]
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::entry::{Descriptor, Entry};
|
||||
use crate::host::{Dirent, FileType};
|
||||
use crate::hostcalls_impl::{fd_filestat_set_times_impl, PathGet};
|
||||
use crate::sys::entry_impl::{determine_type_rights, OsHandle};
|
||||
use crate::sys::host_impl::{self, path_from_host};
|
||||
use crate::sys::hostcalls_impl::fs_helpers::PathGetExt;
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use log::{debug, trace};
|
||||
use std::convert::TryInto;
|
||||
use std::fs::{File, Metadata, OpenOptions};
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::os::windows::fs::{FileExt, OpenOptionsExt};
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::{Path, PathBuf};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::{AccessMode, CreationDisposition, FileModeInformation, Flags};
|
||||
|
||||
fn read_at(mut file: &File, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
||||
// get current cursor position
|
||||
let cur_pos = file.seek(SeekFrom::Current(0))?;
|
||||
// perform a seek read by a specified offset
|
||||
let nread = file.seek_read(buf, offset)?;
|
||||
// rewind the cursor back to the original position
|
||||
file.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nread)
|
||||
}
|
||||
|
||||
fn write_at(mut file: &File, buf: &[u8], offset: u64) -> io::Result<usize> {
|
||||
// get current cursor position
|
||||
let cur_pos = file.seek(SeekFrom::Current(0))?;
|
||||
// perform a seek write by a specified offset
|
||||
let nwritten = file.seek_write(buf, offset)?;
|
||||
// rewind the cursor back to the original position
|
||||
file.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
|
||||
// TODO refactor common code with unix
|
||||
pub(crate) fn fd_pread(
|
||||
file: &File,
|
||||
buf: &mut [u8],
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<usize> {
|
||||
read_at(file, buf, offset).map_err(Into::into)
|
||||
}
|
||||
|
||||
// TODO refactor common code with unix
|
||||
pub(crate) fn fd_pwrite(
|
||||
file: &File,
|
||||
buf: &[u8],
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<usize> {
|
||||
write_at(file, buf, offset).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_get(fd: &File) -> WasiResult<wasi::__wasi_fdflags_t> {
|
||||
let mut fdflags = 0;
|
||||
|
||||
let handle = unsafe { fd.as_raw_handle() };
|
||||
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
let mode = winx::file::query_mode_information(handle)?;
|
||||
|
||||
// Append without write implies append-only (__WASI_FDFLAGS_APPEND)
|
||||
if access_mode.contains(AccessMode::FILE_APPEND_DATA)
|
||||
&& !access_mode.contains(AccessMode::FILE_WRITE_DATA)
|
||||
{
|
||||
fdflags |= wasi::__WASI_FDFLAGS_APPEND;
|
||||
}
|
||||
|
||||
if mode.contains(FileModeInformation::FILE_WRITE_THROUGH) {
|
||||
// Only report __WASI_FDFLAGS_SYNC
|
||||
// This is technically the only one of the O_?SYNC flags Windows supports.
|
||||
fdflags |= wasi::__WASI_FDFLAGS_SYNC;
|
||||
}
|
||||
|
||||
// Files do not support the `__WASI_FDFLAGS_NONBLOCK` flag
|
||||
|
||||
Ok(fdflags)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd: &File,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> WasiResult<Option<OsHandle>> {
|
||||
let handle = unsafe { fd.as_raw_handle() };
|
||||
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
|
||||
let new_access_mode = file_access_mode_from_fdflags(
|
||||
fdflags,
|
||||
access_mode.contains(AccessMode::FILE_READ_DATA),
|
||||
access_mode.contains(AccessMode::FILE_WRITE_DATA)
|
||||
| access_mode.contains(AccessMode::FILE_APPEND_DATA),
|
||||
);
|
||||
|
||||
unsafe {
|
||||
Ok(Some(OsHandle::from(File::from_raw_handle(
|
||||
winx::file::reopen_file(handle, new_access_mode, file_flags_from_fdflags(fdflags))?,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
_file: &File,
|
||||
advice: wasi::__wasi_advice_t,
|
||||
_offset: wasi::__wasi_filesize_t,
|
||||
_len: wasi::__wasi_filesize_t,
|
||||
) -> WasiResult<()> {
|
||||
match advice {
|
||||
wasi::__WASI_ADVICE_DONTNEED
|
||||
| wasi::__WASI_ADVICE_SEQUENTIAL
|
||||
| wasi::__WASI_ADVICE_WILLNEED
|
||||
| wasi::__WASI_ADVICE_NOREUSE
|
||||
| wasi::__WASI_ADVICE_RANDOM
|
||||
| wasi::__WASI_ADVICE_NORMAL => {}
|
||||
_ => return Err(WasiError::EINVAL),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn path_create_directory(file: &File, path: &str) -> WasiResult<()> {
|
||||
let path = concatenate(file, path)?;
|
||||
std::fs::create_dir(&path).map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn path_link(
|
||||
resolved_old: PathGet,
|
||||
resolved_new: PathGet,
|
||||
follow_symlinks: bool,
|
||||
) -> WasiResult<()> {
|
||||
unimplemented!("path_link")
|
||||
}
|
||||
|
||||
pub(crate) fn path_open(
|
||||
resolved: PathGet,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: wasi::__wasi_oflags_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> WasiResult<Descriptor> {
|
||||
use winx::file::{AccessMode, CreationDisposition, Flags};
|
||||
|
||||
let is_trunc = oflags & wasi::__WASI_OFLAGS_TRUNC != 0;
|
||||
|
||||
if is_trunc {
|
||||
// Windows does not support append mode when opening for truncation
|
||||
// This is because truncation requires `GENERIC_WRITE` access, which will override the removal
|
||||
// of the `FILE_WRITE_DATA` permission.
|
||||
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
|
||||
return Err(WasiError::ENOTSUP);
|
||||
}
|
||||
}
|
||||
|
||||
// convert open flags
|
||||
// note: the calls to `write(true)` are to bypass an internal OpenOption check
|
||||
// the write flag will ultimately be ignored when `access_mode` is calculated below.
|
||||
let mut opts = OpenOptions::new();
|
||||
match creation_disposition_from_oflags(oflags) {
|
||||
CreationDisposition::CREATE_ALWAYS => {
|
||||
opts.create(true).write(true);
|
||||
}
|
||||
CreationDisposition::CREATE_NEW => {
|
||||
opts.create_new(true).write(true);
|
||||
}
|
||||
CreationDisposition::TRUNCATE_EXISTING => {
|
||||
opts.truncate(true).write(true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
|
||||
match path.symlink_metadata().map(|metadata| metadata.file_type()) {
|
||||
Ok(file_type) => {
|
||||
// check if we are trying to open a symlink
|
||||
if file_type.is_symlink() {
|
||||
return Err(WasiError::ELOOP);
|
||||
}
|
||||
// check if we are trying to open a file as a dir
|
||||
if file_type.is_file() && oflags & wasi::__WASI_OFLAGS_DIRECTORY != 0 {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
}
|
||||
Err(err) => match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_open at symlink_metadata error code={:?}", code);
|
||||
|
||||
if code as u32 != winerror::ERROR_FILE_NOT_FOUND {
|
||||
return Err(err.into());
|
||||
}
|
||||
// file not found, let it proceed to actually
|
||||
// trying to open it
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
return Err(WasiError::EIO);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
let mut access_mode = file_access_mode_from_fdflags(fdflags, read, write);
|
||||
|
||||
// Truncation requires the special `GENERIC_WRITE` bit set (this is why it doesn't work with append-only mode)
|
||||
if is_trunc {
|
||||
access_mode |= AccessMode::GENERIC_WRITE;
|
||||
}
|
||||
|
||||
opts.access_mode(access_mode.bits())
|
||||
.custom_flags(file_flags_from_fdflags(fdflags).bits())
|
||||
.open(&path)
|
||||
.map(|f| OsHandle::from(f).into())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn creation_disposition_from_oflags(oflags: wasi::__wasi_oflags_t) -> CreationDisposition {
|
||||
if oflags & wasi::__WASI_OFLAGS_CREAT != 0 {
|
||||
if oflags & wasi::__WASI_OFLAGS_EXCL != 0 {
|
||||
CreationDisposition::CREATE_NEW
|
||||
} else {
|
||||
CreationDisposition::CREATE_ALWAYS
|
||||
}
|
||||
} else if oflags & wasi::__WASI_OFLAGS_TRUNC != 0 {
|
||||
CreationDisposition::TRUNCATE_EXISTING
|
||||
} else {
|
||||
CreationDisposition::OPEN_EXISTING
|
||||
}
|
||||
}
|
||||
|
||||
fn file_access_mode_from_fdflags(
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
read: bool,
|
||||
write: bool,
|
||||
) -> AccessMode {
|
||||
let mut access_mode = AccessMode::READ_CONTROL;
|
||||
|
||||
// Note that `GENERIC_READ` and `GENERIC_WRITE` cannot be used to properly support append-only mode
|
||||
// The file-specific flags `FILE_GENERIC_READ` and `FILE_GENERIC_WRITE` are used here instead
|
||||
// These flags have the same semantic meaning for file objects, but allow removal of specific permissions (see below)
|
||||
if read {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_READ);
|
||||
}
|
||||
|
||||
if write {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// For append, grant the handle FILE_APPEND_DATA access but *not* FILE_WRITE_DATA.
|
||||
// This makes the handle "append only".
|
||||
// Changes to the file pointer will be ignored (like POSIX's O_APPEND behavior).
|
||||
if fdflags & wasi::__WASI_FDFLAGS_APPEND != 0 {
|
||||
access_mode.insert(AccessMode::FILE_APPEND_DATA);
|
||||
access_mode.remove(AccessMode::FILE_WRITE_DATA);
|
||||
}
|
||||
|
||||
access_mode
|
||||
}
|
||||
|
||||
fn file_flags_from_fdflags(fdflags: wasi::__wasi_fdflags_t) -> Flags {
|
||||
// Enable backup semantics so directories can be opened as files
|
||||
let mut flags = Flags::FILE_FLAG_BACKUP_SEMANTICS;
|
||||
|
||||
// Note: __WASI_FDFLAGS_NONBLOCK is purposely being ignored for files
|
||||
// While Windows does inherently support a non-blocking mode on files, the WASI API will
|
||||
// treat I/O operations on files as synchronous. WASI might have an async-io API in the future.
|
||||
|
||||
// Technically, Windows only supports __WASI_FDFLAGS_SYNC, but treat all the flags as the same.
|
||||
if fdflags & wasi::__WASI_FDFLAGS_DSYNC != 0
|
||||
|| fdflags & wasi::__WASI_FDFLAGS_RSYNC != 0
|
||||
|| fdflags & wasi::__WASI_FDFLAGS_SYNC != 0
|
||||
{
|
||||
flags.insert(Flags::FILE_FLAG_WRITE_THROUGH);
|
||||
}
|
||||
|
||||
flags
|
||||
}
|
||||
|
||||
fn dirent_from_path<P: AsRef<Path>>(
|
||||
path: P,
|
||||
name: &str,
|
||||
cookie: wasi::__wasi_dircookie_t,
|
||||
) -> WasiResult<Dirent> {
|
||||
let path = path.as_ref();
|
||||
trace!("dirent_from_path: opening {}", path.to_string_lossy());
|
||||
|
||||
// To open a directory on Windows, FILE_FLAG_BACKUP_SEMANTICS flag must be used
|
||||
let file = OpenOptions::new()
|
||||
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
||||
.read(true)
|
||||
.open(path)?;
|
||||
let ty = file.metadata()?.file_type();
|
||||
Ok(Dirent {
|
||||
ftype: host_impl::filetype_from_std(&ty),
|
||||
name: name.to_owned(),
|
||||
cookie,
|
||||
ino: host_impl::file_serial_no(&file)?,
|
||||
})
|
||||
}
|
||||
|
||||
// On Windows there is apparently no support for seeking the directory stream in the OS.
|
||||
// cf. https://github.com/WebAssembly/WASI/issues/61
|
||||
//
|
||||
// The implementation here may perform in O(n^2) if the host buffer is O(1)
|
||||
// and the number of directory entries is O(n).
|
||||
// TODO: Add a heuristic optimization to achieve O(n) time in the most common case
|
||||
// where fd_readdir is resumed where it previously finished
|
||||
//
|
||||
// Correctness of this approach relies upon one assumption: that the order of entries
|
||||
// returned by `FindNextFileW` is stable, i.e. doesn't change if the directory
|
||||
// contents stay the same. This invariant is crucial to be able to implement
|
||||
// any kind of seeking whatsoever without having to read the whole directory at once
|
||||
// and then return the data from cache. (which leaks memory)
|
||||
//
|
||||
// The MSDN documentation explicitly says that the order in which the search returns the files
|
||||
// is not guaranteed, and is dependent on the file system.
|
||||
// cf. https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextfilew
|
||||
//
|
||||
// This stackoverflow post suggests that `FindNextFileW` is indeed stable and that
|
||||
// the order of directory entries depends **only** on the filesystem used, but the
|
||||
// MSDN documentation is not clear about this.
|
||||
// cf. https://stackoverflow.com/questions/47380739/is-findfirstfile-and-findnextfile-order-random-even-for-dvd
|
||||
//
|
||||
// Implementation details:
|
||||
// Cookies for the directory entries start from 1. (0 is reserved by wasi::__WASI_DIRCOOKIE_START)
|
||||
// . gets cookie = 1
|
||||
// .. gets cookie = 2
|
||||
// other entries, in order they were returned by FindNextFileW get subsequent integers as their cookies
|
||||
pub(crate) fn fd_readdir(
|
||||
fd: &File,
|
||||
cookie: wasi::__wasi_dircookie_t,
|
||||
) -> WasiResult<impl Iterator<Item = WasiResult<Dirent>>> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let cookie = cookie.try_into()?;
|
||||
let path = get_file_path(fd)?;
|
||||
// std::fs::ReadDir doesn't return . and .., so we need to emulate it
|
||||
let path = Path::new(&path);
|
||||
// The directory /.. is the same as / on Unix (at least on ext4), so emulate this behavior too
|
||||
let parent = path.parent().unwrap_or(path);
|
||||
let dot = dirent_from_path(path, ".", 1)?;
|
||||
let dotdot = dirent_from_path(parent, "..", 2)?;
|
||||
|
||||
trace!(" | fd_readdir impl: executing std::fs::ReadDir");
|
||||
let iter = path.read_dir()?.zip(3..).map(|(dir, no)| {
|
||||
let dir: std::fs::DirEntry = dir?;
|
||||
|
||||
Ok(Dirent {
|
||||
name: path_from_host(dir.file_name())?,
|
||||
ftype: host_impl::filetype_from_std(&dir.file_type()?),
|
||||
ino: File::open(dir.path()).and_then(|f| host_impl::file_serial_no(&f))?,
|
||||
cookie: no,
|
||||
})
|
||||
});
|
||||
|
||||
// into_iter for arrays is broken and returns references instead of values,
|
||||
// so we need to use vec![...] and do heap allocation
|
||||
// See https://github.com/rust-lang/rust/issues/25725
|
||||
let iter = vec![dot, dotdot].into_iter().map(Ok).chain(iter);
|
||||
|
||||
// Emulate seekdir(). This may give O(n^2) complexity if used with a
|
||||
// small host_buf, but this is difficult to implement efficiently.
|
||||
//
|
||||
// See https://github.com/WebAssembly/WASI/issues/61 for more details.
|
||||
Ok(iter.skip(cookie))
|
||||
}
|
||||
|
||||
pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> WasiResult<usize> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
let target_path = path.read_link()?;
|
||||
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(&resolved.dirfd().as_os_handle())?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| WasiError::ENOTCAPABLE)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(WasiError::EILSEQ))?;
|
||||
|
||||
if buf.len() > 0 {
|
||||
let mut chars = target_path.chars();
|
||||
let mut nread = 0usize;
|
||||
|
||||
for i in 0..buf.len() {
|
||||
match chars.next() {
|
||||
Some(ch) => {
|
||||
buf[i] = ch as u8;
|
||||
nread += 1;
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(nread)
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_trailing_slashes_and_concatenate(resolved: &PathGet) -> WasiResult<Option<PathBuf>> {
|
||||
if resolved.path().ends_with('/') {
|
||||
let suffix = resolved.path().trim_end_matches('/');
|
||||
concatenate(&resolved.dirfd().as_os_handle(), Path::new(suffix)).map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> WasiResult<()> {
|
||||
use std::fs;
|
||||
|
||||
let old_path = resolved_old.concatenate()?;
|
||||
let new_path = resolved_new.concatenate()?;
|
||||
|
||||
// First sanity check: check we're not trying to rename dir to file or vice versa.
|
||||
// NB on Windows, the former is actually permitted [std::fs::rename].
|
||||
//
|
||||
// [std::fs::rename]: https://doc.rust-lang.org/std/fs/fn.rename.html
|
||||
if old_path.is_dir() && new_path.is_file() {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
// Second sanity check: check we're not trying to rename a file into a path
|
||||
// ending in a trailing slash.
|
||||
if old_path.is_file() && resolved_new.path().ends_with('/') {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
|
||||
// TODO handle symlinks
|
||||
let err = match fs::rename(&old_path, &new_path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_rename at rename error code={:?}", code);
|
||||
match code as u32 {
|
||||
winerror::ERROR_ACCESS_DENIED => {
|
||||
// So most likely dealing with new_path == dir.
|
||||
// Eliminate case old_path == file first.
|
||||
if old_path.is_file() {
|
||||
return Err(WasiError::EISDIR);
|
||||
} else {
|
||||
// Ok, let's try removing an empty dir at new_path if it exists
|
||||
// and is a nonempty dir.
|
||||
fs::remove_dir(&new_path)?;
|
||||
fs::rename(old_path, new_path)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
winerror::ERROR_INVALID_NAME => {
|
||||
// If source contains trailing slashes, check if we are dealing with
|
||||
// a file instead of a dir, and if so, throw ENOTDIR.
|
||||
if let Some(path) = strip_trailing_slashes_and_concatenate(&resolved_old)? {
|
||||
if path.is_file() {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(WasiError::EIO)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(file: &std::fs::File) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
host_impl::filestat_from_win(file)
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_get(
|
||||
resolved: PathGet,
|
||||
dirflags: wasi::__wasi_lookupflags_t,
|
||||
) -> WasiResult<wasi::__wasi_filestat_t> {
|
||||
let path = resolved.concatenate()?;
|
||||
let file = File::open(path)?;
|
||||
host_impl::filestat_from_win(&file)
|
||||
}
|
||||
|
||||
pub(crate) fn path_filestat_set_times(
|
||||
resolved: PathGet,
|
||||
dirflags: wasi::__wasi_lookupflags_t,
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
mut st_mtim: wasi::__wasi_timestamp_t,
|
||||
fst_flags: wasi::__wasi_fstflags_t,
|
||||
) -> WasiResult<()> {
|
||||
use winx::file::AccessMode;
|
||||
let path = resolved.concatenate()?;
|
||||
let file = OpenOptions::new()
|
||||
.access_mode(AccessMode::FILE_WRITE_ATTRIBUTES.bits())
|
||||
.open(path)?;
|
||||
let modifiable_fd = Descriptor::OsHandle(OsHandle::from(file));
|
||||
fd_filestat_set_times_impl(&modifiable_fd, st_atim, st_mtim, fst_flags)
|
||||
}
|
||||
|
||||
pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> WasiResult<()> {
|
||||
use std::os::windows::fs::{symlink_dir, symlink_file};
|
||||
|
||||
let old_path = concatenate(&resolved.dirfd().as_os_handle(), Path::new(old_path))?;
|
||||
let new_path = resolved.concatenate()?;
|
||||
|
||||
// try creating a file symlink
|
||||
let err = match symlink_file(&old_path, &new_path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_symlink at symlink_file error code={:?}", code);
|
||||
match code as u32 {
|
||||
winerror::ERROR_NOT_A_REPARSE_POINT => {
|
||||
// try creating a dir symlink instead
|
||||
return symlink_dir(old_path, new_path).map_err(Into::into);
|
||||
}
|
||||
winerror::ERROR_ACCESS_DENIED => {
|
||||
// does the target exist?
|
||||
if new_path.exists() {
|
||||
return Err(WasiError::EEXIST);
|
||||
}
|
||||
}
|
||||
winerror::ERROR_INVALID_NAME => {
|
||||
// does the target without trailing slashes exist?
|
||||
if let Some(path) = strip_trailing_slashes_and_concatenate(&resolved)? {
|
||||
if path.exists() {
|
||||
return Err(WasiError::EEXIST);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(WasiError::EIO)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_unlink_file(resolved: PathGet) -> WasiResult<()> {
|
||||
use std::fs;
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
let file_type = path
|
||||
.symlink_metadata()
|
||||
.map(|metadata| metadata.file_type())?;
|
||||
|
||||
// check if we're unlinking a symlink
|
||||
// NB this will get cleaned up a lot when [std::os::windows::fs::FileTypeExt]
|
||||
// stabilises
|
||||
//
|
||||
// [std::os::windows::fs::FileTypeExt]: https://doc.rust-lang.org/std/os/windows/fs/trait.FileTypeExt.html
|
||||
if file_type.is_symlink() {
|
||||
let err = match fs::remove_file(&path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_unlink_file at symlink_file error code={:?}", code);
|
||||
if code as u32 == winerror::ERROR_ACCESS_DENIED {
|
||||
// try unlinking a dir symlink instead
|
||||
return fs::remove_dir(path).map_err(Into::into);
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(WasiError::EIO)
|
||||
}
|
||||
}
|
||||
} else if file_type.is_dir() {
|
||||
Err(WasiError::EISDIR)
|
||||
} else if file_type.is_file() {
|
||||
fs::remove_file(path).map_err(Into::into)
|
||||
} else {
|
||||
Err(WasiError::EINVAL)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_remove_directory(resolved: PathGet) -> WasiResult<()> {
|
||||
let path = resolved.concatenate()?;
|
||||
std::fs::remove_dir(&path).map_err(Into::into)
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use crate::entry::Descriptor;
|
||||
use crate::hostcalls_impl::PathGet;
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::File;
|
||||
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
||||
use std::path::{Path, PathBuf};
|
||||
use winapi::shared::winerror;
|
||||
|
||||
pub(crate) trait PathGetExt {
|
||||
fn concatenate(&self) -> WasiResult<PathBuf>;
|
||||
}
|
||||
|
||||
impl PathGetExt for PathGet {
|
||||
fn concatenate(&self) -> WasiResult<PathBuf> {
|
||||
match self.dirfd() {
|
||||
Descriptor::OsHandle(file) => concatenate(file, Path::new(self.path())),
|
||||
Descriptor::VirtualFile(_virt) => {
|
||||
panic!("concatenate on a virtual base");
|
||||
}
|
||||
Descriptor::Stdin | Descriptor::Stdout | Descriptor::Stderr => {
|
||||
unreachable!("streams do not have paths and should not be accessible via PathGet");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn path_open_rights(
|
||||
rights_base: wasi::__wasi_rights_t,
|
||||
rights_inheriting: wasi::__wasi_rights_t,
|
||||
oflags: wasi::__wasi_oflags_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> (wasi::__wasi_rights_t, wasi::__wasi_rights_t) {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = wasi::__WASI_RIGHTS_PATH_OPEN;
|
||||
let mut needed_inheriting = rights_base | rights_inheriting;
|
||||
|
||||
// convert open flags
|
||||
if oflags & wasi::__WASI_OFLAGS_CREAT != 0 {
|
||||
needed_base |= wasi::__WASI_RIGHTS_PATH_CREATE_FILE;
|
||||
} else if oflags & wasi::__WASI_OFLAGS_TRUNC != 0 {
|
||||
needed_base |= wasi::__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
if fdflags & wasi::__WASI_FDFLAGS_DSYNC != 0
|
||||
|| fdflags & wasi::__WASI_FDFLAGS_RSYNC != 0
|
||||
|| fdflags & wasi::__WASI_FDFLAGS_SYNC != 0
|
||||
{
|
||||
needed_inheriting |= wasi::__WASI_RIGHTS_FD_DATASYNC;
|
||||
needed_inheriting |= wasi::__WASI_RIGHTS_FD_SYNC;
|
||||
}
|
||||
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> WasiResult<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winx::file::Flags;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(path))?;
|
||||
let err = match OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
||||
.open(&path)
|
||||
{
|
||||
Ok(file) => return Ok(file),
|
||||
Err(e) => e,
|
||||
};
|
||||
if let Some(code) = err.raw_os_error() {
|
||||
log::debug!("openat error={:?}", code);
|
||||
if code as u32 == winerror::ERROR_INVALID_NAME {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &File, s_path: &str) -> WasiResult<String> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(s_path))?;
|
||||
let err = match path.read_link() {
|
||||
Ok(target_path) => {
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(dirfd)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| WasiError::ENOTCAPABLE)?;
|
||||
let target_path = target_path.to_str().ok_or(WasiError::EILSEQ)?;
|
||||
return Ok(target_path.to_owned());
|
||||
}
|
||||
Err(e) => e,
|
||||
};
|
||||
if let Some(code) = err.raw_os_error() {
|
||||
log::debug!("readlinkat error={:?}", code);
|
||||
if code as u32 == winerror::ERROR_INVALID_NAME {
|
||||
if s_path.ends_with('/') {
|
||||
// strip "/" and check if exists
|
||||
let path = concatenate(dirfd, Path::new(s_path.trim_end_matches('/')))?;
|
||||
if path.exists() && !path.is_dir() {
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
pub(crate) fn strip_extended_prefix<P: AsRef<OsStr>>(path: P) -> OsString {
|
||||
let path: Vec<u16> = path.as_ref().encode_wide().collect();
|
||||
if &[92, 92, 63, 92] == &path[0..4] {
|
||||
OsString::from_wide(&path[4..])
|
||||
} else {
|
||||
OsString::from_wide(&path)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn concatenate<P: AsRef<Path>>(file: &File, path: P) -> WasiResult<PathBuf> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
// WASI is not able to deal with absolute paths
|
||||
// so error out if absolute
|
||||
if path.as_ref().is_absolute() {
|
||||
return Err(WasiError::ENOTCAPABLE);
|
||||
}
|
||||
|
||||
let dir_path = get_file_path(file)?;
|
||||
// concatenate paths
|
||||
let mut out_path = PathBuf::from(dir_path);
|
||||
out_path.push(path.as_ref());
|
||||
// strip extended prefix; otherwise we will error out on any relative
|
||||
// components with `out_path`
|
||||
let out_path = PathBuf::from(strip_extended_prefix(out_path));
|
||||
|
||||
log::debug!("out_path={:?}", out_path);
|
||||
|
||||
Ok(out_path)
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
//! Windows-specific hostcalls that implement
|
||||
//! [WASI](https://github.com/WebAssembly/WASI).
|
||||
mod fs;
|
||||
pub(crate) mod fs_helpers;
|
||||
mod misc;
|
||||
|
||||
pub(crate) use self::fs::*;
|
||||
pub(crate) use self::misc::*;
|
||||
@@ -1,16 +1,23 @@
|
||||
pub(crate) mod entry_impl;
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
pub(crate) mod clock;
|
||||
pub(crate) mod entry;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod path;
|
||||
pub(crate) mod poll;
|
||||
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Result;
|
||||
use std::path::Path;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{io, string};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::{CreationDisposition, Flags};
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
pub(crate) fn dev_null() -> io::Result<File> {
|
||||
OpenOptions::new().read(true).write(true).open("NUL")
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
|
||||
@@ -25,3 +32,132 @@ pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
|
||||
.open(path)
|
||||
}
|
||||
|
||||
pub(crate) fn file_serial_no(file: &File) -> io::Result<u64> {
|
||||
let info = winx::file::get_fileinfo(file)?;
|
||||
let high = info.nFileIndexHigh;
|
||||
let low = info.nFileIndexLow;
|
||||
let no = (u64::from(high) << 32) | u64::from(low);
|
||||
Ok(no)
|
||||
}
|
||||
|
||||
impl From<io::Error> for Errno {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code as u32 {
|
||||
winerror::ERROR_SUCCESS => Self::Success,
|
||||
winerror::ERROR_BAD_ENVIRONMENT => Self::TooBig,
|
||||
winerror::ERROR_FILE_NOT_FOUND => Self::Noent,
|
||||
winerror::ERROR_PATH_NOT_FOUND => Self::Noent,
|
||||
winerror::ERROR_TOO_MANY_OPEN_FILES => Self::Nfile,
|
||||
winerror::ERROR_ACCESS_DENIED => Self::Acces,
|
||||
winerror::ERROR_SHARING_VIOLATION => Self::Acces,
|
||||
winerror::ERROR_PRIVILEGE_NOT_HELD => Self::Notcapable,
|
||||
winerror::ERROR_INVALID_HANDLE => Self::Badf,
|
||||
winerror::ERROR_INVALID_NAME => Self::Noent,
|
||||
winerror::ERROR_NOT_ENOUGH_MEMORY => Self::Nomem,
|
||||
winerror::ERROR_OUTOFMEMORY => Self::Nomem,
|
||||
winerror::ERROR_DIR_NOT_EMPTY => Self::Notempty,
|
||||
winerror::ERROR_NOT_READY => Self::Busy,
|
||||
winerror::ERROR_BUSY => Self::Busy,
|
||||
winerror::ERROR_NOT_SUPPORTED => Self::Notsup,
|
||||
winerror::ERROR_FILE_EXISTS => Self::Exist,
|
||||
winerror::ERROR_BROKEN_PIPE => Self::Pipe,
|
||||
winerror::ERROR_BUFFER_OVERFLOW => Self::Nametoolong,
|
||||
winerror::ERROR_NOT_A_REPARSE_POINT => Self::Inval,
|
||||
winerror::ERROR_NEGATIVE_SEEK => Self::Inval,
|
||||
winerror::ERROR_DIRECTORY => Self::Notdir,
|
||||
winerror::ERROR_ALREADY_EXISTS => Self::Exist,
|
||||
x => {
|
||||
log::debug!("unknown error value: {}", x);
|
||||
Self::Io
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::Io
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<string::FromUtf16Error> for Errno {
|
||||
fn from(_err: string::FromUtf16Error) -> Self {
|
||||
Self::Ilseq
|
||||
}
|
||||
}
|
||||
|
||||
fn num_hardlinks(file: &File) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.nNumberOfLinks.into())
|
||||
}
|
||||
|
||||
fn device_id(file: &File) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.dwVolumeSerialNumber.into())
|
||||
}
|
||||
|
||||
fn change_time(file: &File) -> io::Result<i64> {
|
||||
winx::file::change_time(file)
|
||||
}
|
||||
|
||||
fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
|
||||
st.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Errno::Inval)? // date earlier than UNIX_EPOCH
|
||||
.as_nanos()
|
||||
.try_into()
|
||||
.map_err(Into::into) // u128 doesn't fit into u64
|
||||
}
|
||||
|
||||
impl TryFrom<&File> for types::Filestat {
|
||||
type Error = Errno;
|
||||
|
||||
fn try_from(file: &File) -> Result<Self> {
|
||||
let metadata = file.metadata()?;
|
||||
Ok(types::Filestat {
|
||||
dev: device_id(file)?,
|
||||
ino: file_serial_no(file)?,
|
||||
nlink: num_hardlinks(file)?.try_into()?, // u64 doesn't fit into u32
|
||||
size: metadata.len(),
|
||||
atim: systemtime_to_timestamp(metadata.accessed()?)?,
|
||||
ctim: change_time(file)?.try_into()?, // i64 doesn't fit into u64
|
||||
mtim: systemtime_to_timestamp(metadata.modified()?)?,
|
||||
filetype: metadata.file_type().into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Oflags> for CreationDisposition {
|
||||
fn from(oflags: types::Oflags) -> Self {
|
||||
if oflags.contains(&types::Oflags::CREAT) {
|
||||
if oflags.contains(&types::Oflags::EXCL) {
|
||||
CreationDisposition::CREATE_NEW
|
||||
} else {
|
||||
CreationDisposition::CREATE_ALWAYS
|
||||
}
|
||||
} else if oflags.contains(&types::Oflags::TRUNC) {
|
||||
CreationDisposition::TRUNCATE_EXISTING
|
||||
} else {
|
||||
CreationDisposition::OPEN_EXISTING
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Fdflags> for Flags {
|
||||
fn from(fdflags: types::Fdflags) -> Self {
|
||||
// Enable backup semantics so directories can be opened as files
|
||||
let mut flags = Flags::FILE_FLAG_BACKUP_SEMANTICS;
|
||||
|
||||
// Note: __WASI_FDFLAGS_NONBLOCK is purposely being ignored for files
|
||||
// While Windows does inherently support a non-blocking mode on files, the WASI API will
|
||||
// treat I/O operations on files as synchronous. WASI might have an async-io API in the future.
|
||||
|
||||
// Technically, Windows only supports __WASI_FDFLAGS_SYNC, but treat all the flags as the same.
|
||||
if fdflags.contains(&types::Fdflags::DSYNC)
|
||||
|| fdflags.contains(&types::Fdflags::RSYNC)
|
||||
|| fdflags.contains(&types::Fdflags::SYNC)
|
||||
{
|
||||
flags.insert(Flags::FILE_FLAG_WRITE_THROUGH);
|
||||
}
|
||||
|
||||
flags
|
||||
}
|
||||
}
|
||||
|
||||
506
crates/wasi-common/src/sys/windows/path.rs
Normal file
506
crates/wasi-common/src/sys/windows/path.rs
Normal file
@@ -0,0 +1,506 @@
|
||||
use crate::entry::Descriptor;
|
||||
use crate::fd;
|
||||
use crate::path::PathGet;
|
||||
use crate::sys::entry::OsHandle;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::AccessMode;
|
||||
|
||||
/// Creates owned WASI path from OS string.
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_ERRNO_ILSEQ` error is returned.
|
||||
pub(crate) fn from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
let vec: Vec<u16> = s.as_ref().encode_wide().collect();
|
||||
let s = String::from_utf16(&vec)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
pub(crate) trait PathGetExt {
|
||||
fn concatenate(&self) -> Result<PathBuf>;
|
||||
}
|
||||
|
||||
impl PathGetExt for PathGet {
|
||||
fn concatenate(&self) -> Result<PathBuf> {
|
||||
match self.dirfd() {
|
||||
Descriptor::OsHandle(file) => concatenate(file, Path::new(self.path())),
|
||||
Descriptor::VirtualFile(_virt) => {
|
||||
panic!("concatenate on a virtual base");
|
||||
}
|
||||
Descriptor::Stdin | Descriptor::Stdout | Descriptor::Stderr => {
|
||||
unreachable!("streams do not have paths and should not be accessible via PathGet");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn open_rights(
|
||||
rights_base: types::Rights,
|
||||
rights_inheriting: types::Rights,
|
||||
oflags: types::Oflags,
|
||||
fdflags: types::Fdflags,
|
||||
) -> (types::Rights, types::Rights) {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = types::Rights::PATH_OPEN;
|
||||
let mut needed_inheriting = rights_base | rights_inheriting;
|
||||
|
||||
// convert open flags
|
||||
if oflags.contains(&types::Oflags::CREAT) {
|
||||
needed_base |= types::Rights::PATH_CREATE_FILE;
|
||||
} else if oflags.contains(&types::Oflags::TRUNC) {
|
||||
needed_base |= types::Rights::PATH_FILESTAT_SET_SIZE;
|
||||
}
|
||||
|
||||
// convert file descriptor flags
|
||||
if fdflags.contains(&types::Fdflags::DSYNC)
|
||||
|| fdflags.contains(&types::Fdflags::RSYNC)
|
||||
|| fdflags.contains(&types::Fdflags::SYNC)
|
||||
{
|
||||
needed_inheriting |= types::Rights::FD_DATASYNC;
|
||||
needed_inheriting |= types::Rights::FD_SYNC;
|
||||
}
|
||||
|
||||
(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winx::file::Flags;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(path))?;
|
||||
let err = match OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(Flags::FILE_FLAG_BACKUP_SEMANTICS.bits())
|
||||
.open(&path)
|
||||
{
|
||||
Ok(file) => return Ok(file),
|
||||
Err(e) => e,
|
||||
};
|
||||
if let Some(code) = err.raw_os_error() {
|
||||
log::debug!("openat error={:?}", code);
|
||||
if code as u32 == winerror::ERROR_INVALID_NAME {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &File, s_path: &str) -> Result<String> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(s_path))?;
|
||||
let err = match path.read_link() {
|
||||
Ok(target_path) => {
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(dirfd)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| Errno::Notcapable)?;
|
||||
let target_path = target_path.to_str().ok_or(Errno::Ilseq)?;
|
||||
return Ok(target_path.to_owned());
|
||||
}
|
||||
Err(e) => e,
|
||||
};
|
||||
if let Some(code) = err.raw_os_error() {
|
||||
log::debug!("readlinkat error={:?}", code);
|
||||
if code as u32 == winerror::ERROR_INVALID_NAME {
|
||||
if s_path.ends_with('/') {
|
||||
// strip "/" and check if exists
|
||||
let path = concatenate(dirfd, Path::new(s_path.trim_end_matches('/')))?;
|
||||
if path.exists() && !path.is_dir() {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
fn strip_extended_prefix<P: AsRef<OsStr>>(path: P) -> OsString {
|
||||
let path: Vec<u16> = path.as_ref().encode_wide().collect();
|
||||
if &[92, 92, 63, 92] == &path[0..4] {
|
||||
OsString::from_wide(&path[4..])
|
||||
} else {
|
||||
OsString::from_wide(&path)
|
||||
}
|
||||
}
|
||||
|
||||
fn concatenate<P: AsRef<Path>>(file: &File, path: P) -> Result<PathBuf> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
// WASI is not able to deal with absolute paths
|
||||
// so error out if absolute
|
||||
if path.as_ref().is_absolute() {
|
||||
return Err(Errno::Notcapable);
|
||||
}
|
||||
|
||||
let dir_path = get_file_path(file)?;
|
||||
// concatenate paths
|
||||
let mut out_path = PathBuf::from(dir_path);
|
||||
out_path.push(path.as_ref());
|
||||
// strip extended prefix; otherwise we will error out on any relative
|
||||
// components with `out_path`
|
||||
let out_path = PathBuf::from(strip_extended_prefix(out_path));
|
||||
|
||||
log::debug!("out_path={:?}", out_path);
|
||||
|
||||
Ok(out_path)
|
||||
}
|
||||
|
||||
pub(crate) fn create_directory(file: &File, path: &str) -> Result<()> {
|
||||
let path = concatenate(file, path)?;
|
||||
std::fs::create_dir(&path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn link(
|
||||
_resolved_old: PathGet,
|
||||
_resolved_new: PathGet,
|
||||
_follow_symlinks: bool,
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_link")
|
||||
}
|
||||
|
||||
pub(crate) fn open(
|
||||
resolved: PathGet,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fdflags: types::Fdflags,
|
||||
) -> Result<Descriptor> {
|
||||
use winx::file::{AccessMode, CreationDisposition, Flags};
|
||||
|
||||
let is_trunc = oflags.contains(&types::Oflags::TRUNC);
|
||||
|
||||
if is_trunc {
|
||||
// Windows does not support append mode when opening for truncation
|
||||
// This is because truncation requires `GENERIC_WRITE` access, which will override the removal
|
||||
// of the `FILE_WRITE_DATA` permission.
|
||||
if fdflags.contains(&types::Fdflags::APPEND) {
|
||||
return Err(Errno::Notsup);
|
||||
}
|
||||
}
|
||||
|
||||
// convert open flags
|
||||
// note: the calls to `write(true)` are to bypass an internal OpenOption check
|
||||
// the write flag will ultimately be ignored when `access_mode` is calculated below.
|
||||
let mut opts = OpenOptions::new();
|
||||
match oflags.into() {
|
||||
CreationDisposition::CREATE_ALWAYS => {
|
||||
opts.create(true).write(true);
|
||||
}
|
||||
CreationDisposition::CREATE_NEW => {
|
||||
opts.create_new(true).write(true);
|
||||
}
|
||||
CreationDisposition::TRUNCATE_EXISTING => {
|
||||
opts.truncate(true).write(true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
|
||||
match path.symlink_metadata().map(|metadata| metadata.file_type()) {
|
||||
Ok(file_type) => {
|
||||
// check if we are trying to open a symlink
|
||||
if file_type.is_symlink() {
|
||||
return Err(Errno::Loop);
|
||||
}
|
||||
// check if we are trying to open a file as a dir
|
||||
if file_type.is_file() && oflags.contains(&types::Oflags::DIRECTORY) {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
}
|
||||
Err(err) => match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_open at symlink_metadata error code={:?}", code);
|
||||
|
||||
if code as u32 != winerror::ERROR_FILE_NOT_FOUND {
|
||||
return Err(err.into());
|
||||
}
|
||||
// file not found, let it proceed to actually
|
||||
// trying to open it
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
return Err(Errno::Io);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
let mut access_mode = file_access_mode_from_fdflags(fdflags, read, write);
|
||||
|
||||
// Truncation requires the special `GENERIC_WRITE` bit set (this is why it doesn't work with append-only mode)
|
||||
if is_trunc {
|
||||
access_mode |= AccessMode::GENERIC_WRITE;
|
||||
}
|
||||
|
||||
let flags: Flags = fdflags.into();
|
||||
opts.access_mode(access_mode.bits())
|
||||
.custom_flags(flags.bits())
|
||||
.open(&path)
|
||||
.map(|f| OsHandle::from(f).into())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn file_access_mode_from_fdflags(fdflags: types::Fdflags, read: bool, write: bool) -> AccessMode {
|
||||
let mut access_mode = AccessMode::READ_CONTROL;
|
||||
|
||||
// Note that `GENERIC_READ` and `GENERIC_WRITE` cannot be used to properly support append-only mode
|
||||
// The file-specific flags `FILE_GENERIC_READ` and `FILE_GENERIC_WRITE` are used here instead
|
||||
// These flags have the same semantic meaning for file objects, but allow removal of specific permissions (see below)
|
||||
if read {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_READ);
|
||||
}
|
||||
|
||||
if write {
|
||||
access_mode.insert(AccessMode::FILE_GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// For append, grant the handle FILE_APPEND_DATA access but *not* FILE_WRITE_DATA.
|
||||
// This makes the handle "append only".
|
||||
// Changes to the file pointer will be ignored (like POSIX's O_APPEND behavior).
|
||||
if fdflags.contains(&types::Fdflags::APPEND) {
|
||||
access_mode.insert(AccessMode::FILE_APPEND_DATA);
|
||||
access_mode.remove(AccessMode::FILE_WRITE_DATA);
|
||||
}
|
||||
|
||||
access_mode
|
||||
}
|
||||
|
||||
pub(crate) fn readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
let target_path = path.read_link()?;
|
||||
|
||||
// since on Windows we are effectively emulating 'at' syscalls
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(&resolved.dirfd().as_os_handle())?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| Errno::Notcapable)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(Errno::Ilseq))?;
|
||||
|
||||
if buf.len() > 0 {
|
||||
let mut chars = target_path.chars();
|
||||
let mut nread = 0usize;
|
||||
|
||||
for i in 0..buf.len() {
|
||||
match chars.next() {
|
||||
Some(ch) => {
|
||||
buf[i] = ch as u8;
|
||||
nread += 1;
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(nread)
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn strip_trailing_slashes_and_concatenate(resolved: &PathGet) -> Result<Option<PathBuf>> {
|
||||
if resolved.path().ends_with('/') {
|
||||
let suffix = resolved.path().trim_end_matches('/');
|
||||
concatenate(&resolved.dirfd().as_os_handle(), Path::new(suffix)).map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
|
||||
use std::fs;
|
||||
|
||||
let old_path = resolved_old.concatenate()?;
|
||||
let new_path = resolved_new.concatenate()?;
|
||||
|
||||
// First sanity check: check we're not trying to rename dir to file or vice versa.
|
||||
// NB on Windows, the former is actually permitted [std::fs::rename].
|
||||
//
|
||||
// [std::fs::rename]: https://doc.rust-lang.org/std/fs/fn.rename.html
|
||||
if old_path.is_dir() && new_path.is_file() {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
// Second sanity check: check we're not trying to rename a file into a path
|
||||
// ending in a trailing slash.
|
||||
if old_path.is_file() && resolved_new.path().ends_with('/') {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
|
||||
// TODO handle symlinks
|
||||
let err = match fs::rename(&old_path, &new_path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_rename at rename error code={:?}", code);
|
||||
match code as u32 {
|
||||
winerror::ERROR_ACCESS_DENIED => {
|
||||
// So most likely dealing with new_path == dir.
|
||||
// Eliminate case old_path == file first.
|
||||
if old_path.is_file() {
|
||||
return Err(Errno::Isdir);
|
||||
} else {
|
||||
// Ok, let's try removing an empty dir at new_path if it exists
|
||||
// and is a nonempty dir.
|
||||
fs::remove_dir(&new_path)?;
|
||||
fs::rename(old_path, new_path)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
winerror::ERROR_INVALID_NAME => {
|
||||
// If source contains trailing slashes, check if we are dealing with
|
||||
// a file instead of a dir, and if so, throw ENOTDIR.
|
||||
if let Some(path) = strip_trailing_slashes_and_concatenate(&resolved_old)? {
|
||||
if path.is_file() {
|
||||
return Err(Errno::Notdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_get(
|
||||
resolved: PathGet,
|
||||
_dirflags: types::Lookupflags,
|
||||
) -> Result<types::Filestat> {
|
||||
let path = resolved.concatenate()?;
|
||||
let file = File::open(path)?;
|
||||
let filestat = (&file).try_into()?;
|
||||
Ok(filestat)
|
||||
}
|
||||
|
||||
pub(crate) fn filestat_set_times(
|
||||
resolved: PathGet,
|
||||
_dirflags: types::Lookupflags,
|
||||
st_atim: types::Timestamp,
|
||||
st_mtim: types::Timestamp,
|
||||
fst_flags: types::Fstflags,
|
||||
) -> Result<()> {
|
||||
use winx::file::AccessMode;
|
||||
let path = resolved.concatenate()?;
|
||||
let file = OpenOptions::new()
|
||||
.access_mode(AccessMode::FILE_WRITE_ATTRIBUTES.bits())
|
||||
.open(path)?;
|
||||
let modifiable_fd = Descriptor::OsHandle(OsHandle::from(file));
|
||||
fd::filestat_set_times_impl(&modifiable_fd, st_atim, st_mtim, fst_flags)
|
||||
}
|
||||
|
||||
pub(crate) fn symlink(old_path: &str, resolved: PathGet) -> Result<()> {
|
||||
use std::os::windows::fs::{symlink_dir, symlink_file};
|
||||
|
||||
let old_path = concatenate(&resolved.dirfd().as_os_handle(), Path::new(old_path))?;
|
||||
let new_path = resolved.concatenate()?;
|
||||
|
||||
// try creating a file symlink
|
||||
let err = match symlink_file(&old_path, &new_path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_symlink at symlink_file error code={:?}", code);
|
||||
match code as u32 {
|
||||
winerror::ERROR_NOT_A_REPARSE_POINT => {
|
||||
// try creating a dir symlink instead
|
||||
return symlink_dir(old_path, new_path).map_err(Into::into);
|
||||
}
|
||||
winerror::ERROR_ACCESS_DENIED => {
|
||||
// does the target exist?
|
||||
if new_path.exists() {
|
||||
return Err(Errno::Exist);
|
||||
}
|
||||
}
|
||||
winerror::ERROR_INVALID_NAME => {
|
||||
// does the target without trailing slashes exist?
|
||||
if let Some(path) = strip_trailing_slashes_and_concatenate(&resolved)? {
|
||||
if path.exists() {
|
||||
return Err(Errno::Exist);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unlink_file(resolved: PathGet) -> Result<()> {
|
||||
use std::fs;
|
||||
|
||||
let path = resolved.concatenate()?;
|
||||
let file_type = path
|
||||
.symlink_metadata()
|
||||
.map(|metadata| metadata.file_type())?;
|
||||
|
||||
// check if we're unlinking a symlink
|
||||
// NB this will get cleaned up a lot when [std::os::windows::fs::FileTypeExt]
|
||||
// stabilises
|
||||
//
|
||||
// [std::os::windows::fs::FileTypeExt]: https://doc.rust-lang.org/std/os/windows/fs/trait.FileTypeExt.html
|
||||
if file_type.is_symlink() {
|
||||
let err = match fs::remove_file(&path) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => e,
|
||||
};
|
||||
match err.raw_os_error() {
|
||||
Some(code) => {
|
||||
log::debug!("path_unlink_file at symlink_file error code={:?}", code);
|
||||
if code as u32 == winerror::ERROR_ACCESS_DENIED {
|
||||
// try unlinking a dir symlink instead
|
||||
return fs::remove_dir(path).map_err(Into::into);
|
||||
}
|
||||
|
||||
Err(err.into())
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
}
|
||||
}
|
||||
} else if file_type.is_dir() {
|
||||
Err(Errno::Isdir)
|
||||
} else if file_type.is_file() {
|
||||
fs::remove_file(path).map_err(Into::into)
|
||||
} else {
|
||||
Err(Errno::Inval)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove_directory(resolved: PathGet) -> Result<()> {
|
||||
let path = resolved.concatenate()?;
|
||||
std::fs::remove_dir(&path).map_err(Into::into)
|
||||
}
|
||||
@@ -1,22 +1,14 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
#![allow(unused)]
|
||||
use crate::entry::Descriptor;
|
||||
use crate::hostcalls_impl::{ClockEventData, FdEventData};
|
||||
use crate::memory::*;
|
||||
use crate::sys::host_impl;
|
||||
use crate::wasi::{self, WasiError, WasiResult};
|
||||
use crate::wasi32;
|
||||
use cpu_time::{ProcessTime, ThreadTime};
|
||||
use crate::poll::{ClockEventData, FdEventData};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use log::{debug, error, trace, warn};
|
||||
use std::convert::TryInto;
|
||||
use std::io;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
|
||||
use std::sync::Mutex;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
use std::time::Duration;
|
||||
|
||||
struct StdinPoll {
|
||||
request_tx: Sender<()>,
|
||||
@@ -27,7 +19,7 @@ enum PollState {
|
||||
Ready,
|
||||
NotReady, // it's not ready, but we didn't wait
|
||||
TimedOut, // it's not ready and a timeout has occurred
|
||||
Error(WasiError),
|
||||
Error(Errno),
|
||||
}
|
||||
|
||||
enum WaitMode {
|
||||
@@ -83,7 +75,7 @@ impl StdinPoll {
|
||||
// Linux returns `POLLIN` in both cases, and we imitate this behavior.
|
||||
let resp = match std::io::stdin().lock().fill_buf() {
|
||||
Ok(_) => PollState::Ready,
|
||||
Err(e) => PollState::Error(WasiError::from(e)),
|
||||
Err(e) => PollState::Error(Errno::from(e)),
|
||||
};
|
||||
|
||||
// Notify the requestor about data in stdin. They may have already timed out,
|
||||
@@ -94,8 +86,6 @@ impl StdinPoll {
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref START_MONOTONIC: Instant = Instant::now();
|
||||
static ref PERF_COUNTER_RES: u64 = get_perf_counter_resolution_ns();
|
||||
static ref STDIN_POLL: Mutex<StdinPoll> = {
|
||||
let (request_tx, request_rx) = mpsc::channel();
|
||||
let (notify_tx, notify_rx) = mpsc::channel();
|
||||
@@ -107,92 +97,30 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
// Timer resolution on Windows is really hard. We may consider exposing the resolution of the respective
|
||||
// timers as an associated function in the future.
|
||||
pub(crate) fn clock_res_get(
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
) -> WasiResult<wasi::__wasi_timestamp_t> {
|
||||
Ok(match clock_id {
|
||||
// This is the best that we can do with std::time::SystemTime.
|
||||
// Rust uses GetSystemTimeAsFileTime, which is said to have the resolution of
|
||||
// 10ms or 55ms, [1] but MSDN doesn't confirm this in any way.
|
||||
// Even the MSDN article on high resolution timestamps doesn't even mention the precision
|
||||
// for this method. [3]
|
||||
//
|
||||
// The timer resolution can be queried using one of the functions: [2, 5]
|
||||
// * NtQueryTimerResolution, which is undocumented and thus not exposed by the winapi crate
|
||||
// * timeGetDevCaps, which returns the upper and lower bound for the precision, in ms.
|
||||
// While the upper bound seems like something we could use, it's typically too high to be meaningful.
|
||||
// For instance, the intervals return by the syscall are:
|
||||
// * [1, 65536] on Wine
|
||||
// * [1, 1000000] on Windows 10, which is up to (sic) 1000 seconds.
|
||||
//
|
||||
// It's possible to manually set the timer resolution, but this sounds like something which should
|
||||
// only be done temporarily. [5]
|
||||
//
|
||||
// Alternatively, we could possibly use GetSystemTimePreciseAsFileTime in clock_time_get, but
|
||||
// this syscall is only available starting from Windows 8.
|
||||
// (we could possibly emulate it on earlier versions of Windows, see [4])
|
||||
// The MSDN are not clear on the resolution of GetSystemTimePreciseAsFileTime either, but a
|
||||
// Microsoft devblog entry [1] suggests that it kind of combines GetSystemTimeAsFileTime with
|
||||
// QueryPeformanceCounter, which probably means that those two should have the same resolution.
|
||||
//
|
||||
// See also this discussion about the use of GetSystemTimePreciseAsFileTime in Python stdlib,
|
||||
// which in particular contains some resolution benchmarks.
|
||||
//
|
||||
// [1] https://devblogs.microsoft.com/oldnewthing/20170921-00/?p=97057
|
||||
// [2] http://www.windowstimestamp.com/description
|
||||
// [3] https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps?redirectedfrom=MSDN
|
||||
// [4] https://www.codeproject.com/Tips/1011902/High-Resolution-Time-For-Windows
|
||||
// [5] https://stackoverflow.com/questions/7685762/windows-7-timing-functions-how-to-use-getsystemtimeadjustment-correctly
|
||||
// [6] https://bugs.python.org/issue19007
|
||||
wasi::__WASI_CLOCKID_REALTIME => 55_000_000,
|
||||
// std::time::Instant uses QueryPerformanceCounter & QueryPerformanceFrequency internally
|
||||
wasi::__WASI_CLOCKID_MONOTONIC => *PERF_COUNTER_RES,
|
||||
// The best we can do is to hardcode the value from the docs.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes
|
||||
wasi::__WASI_CLOCKID_PROCESS_CPUTIME_ID => 100,
|
||||
// The best we can do is to hardcode the value from the docs.
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getthreadtimes
|
||||
wasi::__WASI_CLOCKID_THREAD_CPUTIME_ID => 100,
|
||||
_ => return Err(WasiError::EINVAL),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn clock_time_get(
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
) -> WasiResult<wasi::__wasi_timestamp_t> {
|
||||
let duration = match clock_id {
|
||||
wasi::__WASI_CLOCKID_REALTIME => get_monotonic_time(),
|
||||
wasi::__WASI_CLOCKID_MONOTONIC => get_realtime_time()?,
|
||||
wasi::__WASI_CLOCKID_PROCESS_CPUTIME_ID => get_proc_cputime()?,
|
||||
wasi::__WASI_CLOCKID_THREAD_CPUTIME_ID => get_thread_cputime()?,
|
||||
_ => return Err(WasiError::EINVAL),
|
||||
};
|
||||
duration.as_nanos().try_into().map_err(Into::into)
|
||||
}
|
||||
|
||||
fn make_rw_event(event: &FdEventData, nbytes: WasiResult<u64>) -> wasi::__wasi_event_t {
|
||||
fn make_rw_event(event: &FdEventData, nbytes: Result<u64>) -> types::Event {
|
||||
let (nbytes, error) = match nbytes {
|
||||
Ok(nbytes) => (nbytes, WasiError::ESUCCESS),
|
||||
Ok(nbytes) => (nbytes, Errno::Success),
|
||||
Err(e) => (u64::default(), e),
|
||||
};
|
||||
wasi::__wasi_event_t {
|
||||
types::Event {
|
||||
userdata: event.userdata,
|
||||
r#type: event.r#type,
|
||||
error: error.as_raw_errno(),
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t { nbytes, flags: 0 },
|
||||
type_: event.r#type,
|
||||
error,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes,
|
||||
flags: types::Eventrwflags::empty(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn make_timeout_event(timeout: &ClockEventData) -> wasi::__wasi_event_t {
|
||||
wasi::__wasi_event_t {
|
||||
fn make_timeout_event(timeout: &ClockEventData) -> types::Event {
|
||||
types::Event {
|
||||
userdata: timeout.userdata,
|
||||
r#type: wasi::__WASI_EVENTTYPE_CLOCK,
|
||||
error: wasi::__WASI_ERRNO_SUCCESS,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
type_: types::Eventtype::Clock,
|
||||
error: Errno::Success,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
flags: 0,
|
||||
flags: types::Eventrwflags::empty(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -200,21 +128,22 @@ fn make_timeout_event(timeout: &ClockEventData) -> wasi::__wasi_event_t {
|
||||
fn handle_timeout(
|
||||
timeout_event: ClockEventData,
|
||||
timeout: Duration,
|
||||
events: &mut Vec<wasi::__wasi_event_t>,
|
||||
events: &mut Vec<types::Event>,
|
||||
) {
|
||||
thread::sleep(timeout);
|
||||
handle_timeout_event(timeout_event, events);
|
||||
}
|
||||
|
||||
fn handle_timeout_event(timeout_event: ClockEventData, events: &mut Vec<wasi::__wasi_event_t>) {
|
||||
fn handle_timeout_event(timeout_event: ClockEventData, events: &mut Vec<types::Event>) {
|
||||
let new_event = make_timeout_event(&timeout_event);
|
||||
events.push(new_event);
|
||||
}
|
||||
|
||||
fn handle_rw_event(event: FdEventData, out_events: &mut Vec<wasi::__wasi_event_t>) {
|
||||
let size = match event.descriptor {
|
||||
fn handle_rw_event(event: FdEventData, out_events: &mut Vec<types::Event>) {
|
||||
let descriptor: &Descriptor = &event.descriptor;
|
||||
let size = match descriptor {
|
||||
Descriptor::OsHandle(os_handle) => {
|
||||
if event.r#type == wasi::__WASI_EVENTTYPE_FD_READ {
|
||||
if event.r#type == types::Eventtype::FdRead {
|
||||
os_handle.metadata().map(|m| m.len()).map_err(Into::into)
|
||||
} else {
|
||||
// The spec is unclear what nbytes should actually be for __WASI_EVENTTYPE_FD_WRITE and
|
||||
@@ -237,23 +166,16 @@ fn handle_rw_event(event: FdEventData, out_events: &mut Vec<wasi::__wasi_event_t
|
||||
out_events.push(new_event);
|
||||
}
|
||||
|
||||
fn handle_error_event(
|
||||
event: FdEventData,
|
||||
error: WasiError,
|
||||
out_events: &mut Vec<wasi::__wasi_event_t>,
|
||||
) {
|
||||
fn handle_error_event(event: FdEventData, error: Errno, out_events: &mut Vec<types::Event>) {
|
||||
let new_event = make_rw_event(&event, Err(error));
|
||||
out_events.push(new_event);
|
||||
}
|
||||
|
||||
pub(crate) fn poll_oneoff(
|
||||
pub(crate) fn oneoff(
|
||||
timeout: Option<ClockEventData>,
|
||||
fd_events: Vec<FdEventData>,
|
||||
events: &mut Vec<wasi::__wasi_event_t>,
|
||||
) -> WasiResult<()> {
|
||||
use std::fs::Metadata;
|
||||
use std::thread;
|
||||
|
||||
events: &mut Vec<types::Event>,
|
||||
) -> Result<()> {
|
||||
let timeout = timeout
|
||||
.map(|event| {
|
||||
event
|
||||
@@ -279,8 +201,9 @@ pub(crate) fn poll_oneoff(
|
||||
let mut pipe_events = vec![];
|
||||
|
||||
for event in fd_events {
|
||||
match event.descriptor {
|
||||
Descriptor::Stdin if event.r#type == wasi::__WASI_EVENTTYPE_FD_READ => {
|
||||
let descriptor: &Descriptor = &event.descriptor;
|
||||
match descriptor {
|
||||
Descriptor::Stdin if event.r#type == types::Eventtype::FdRead => {
|
||||
stdin_events.push(event)
|
||||
}
|
||||
// stdout/stderr are always considered ready to write because there seems to
|
||||
@@ -295,7 +218,7 @@ pub(crate) fn poll_oneoff(
|
||||
let ftype = unsafe { winx::file::get_file_type(os_handle.as_raw_handle()) }?;
|
||||
if ftype.is_unknown() || ftype.is_char() {
|
||||
debug!("poll_oneoff: unsupported file type: {:?}", ftype);
|
||||
handle_error_event(event, WasiError::ENOTSUP, events);
|
||||
handle_error_event(event, Errno::Notsup, events);
|
||||
} else if ftype.is_disk() {
|
||||
immediate_events.push(event);
|
||||
} else if ftype.is_pipe() {
|
||||
@@ -314,12 +237,11 @@ pub(crate) fn poll_oneoff(
|
||||
// Process all the events that do not require waiting.
|
||||
if immediate {
|
||||
trace!(" | have immediate events, will return immediately");
|
||||
for mut event in immediate_events {
|
||||
for event in immediate_events {
|
||||
handle_rw_event(event, events);
|
||||
}
|
||||
}
|
||||
if !stdin_events.is_empty() {
|
||||
// During the firt request to poll stdin, we spin up a separate thread to
|
||||
// waiting for data to arrive on stdin. This thread will not terminate.
|
||||
//
|
||||
// We'd like to do the following:
|
||||
@@ -345,7 +267,7 @@ pub(crate) fn poll_oneoff(
|
||||
} else {
|
||||
trace!(" | passively waiting on stdin");
|
||||
match timeout {
|
||||
Some((event, dur)) => WaitMode::Timeout(dur),
|
||||
Some((_event, dur)) => WaitMode::Timeout(dur),
|
||||
None => WaitMode::Infinite,
|
||||
}
|
||||
};
|
||||
@@ -371,43 +293,10 @@ pub(crate) fn poll_oneoff(
|
||||
}
|
||||
None => {
|
||||
error!("Polling only pipes with no timeout not supported on Windows.");
|
||||
return Err(WasiError::ENOTSUP);
|
||||
return Err(Errno::Notsup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_monotonic_time() -> Duration {
|
||||
// We're circumventing the fact that we can't get a Duration from an Instant
|
||||
// The epoch of __WASI_CLOCKID_MONOTONIC is undefined, so we fix a time point once
|
||||
// and count relative to this time point.
|
||||
//
|
||||
// The alternative would be to copy over the implementation of std::time::Instant
|
||||
// to our source tree and add a conversion to std::time::Duration
|
||||
START_MONOTONIC.elapsed()
|
||||
}
|
||||
|
||||
fn get_realtime_time() -> WasiResult<Duration> {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| WasiError::EFAULT)
|
||||
}
|
||||
|
||||
fn get_proc_cputime() -> WasiResult<Duration> {
|
||||
Ok(ProcessTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
fn get_thread_cputime() -> WasiResult<Duration> {
|
||||
Ok(ThreadTime::try_now()?.as_duration())
|
||||
}
|
||||
|
||||
fn get_perf_counter_resolution_ns() -> u64 {
|
||||
use winx::time::perf_counter_frequency;
|
||||
const NANOS_PER_SEC: u64 = 1_000_000_000;
|
||||
// This should always succeed starting from Windows XP, so it's fine to panic in case of an error.
|
||||
let freq = perf_counter_frequency().expect("QueryPerformanceFrequency returned an error");
|
||||
let epsilon = NANOS_PER_SEC / freq;
|
||||
epsilon
|
||||
}
|
||||
Reference in New Issue
Block a user