Create helper Result<T> type
This commit is contained in:
42
src/ctx.rs
42
src/ctx.rs
@@ -1,6 +1,6 @@
|
||||
use super::fdentry::FdEntry;
|
||||
use super::host;
|
||||
use super::sys::{dev_null, errno_from_host};
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::sys::{dev_null, errno_from_host};
|
||||
use crate::{host, Result};
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CString;
|
||||
@@ -16,7 +16,7 @@ pub struct WasiCtxBuilder {
|
||||
|
||||
impl WasiCtxBuilder {
|
||||
/// Builder for a new `WasiCtx`.
|
||||
pub fn new() -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn new() -> Result<Self> {
|
||||
let mut builder = Self {
|
||||
fds: HashMap::new(),
|
||||
preopens: HashMap::new(),
|
||||
@@ -31,35 +31,32 @@ impl WasiCtxBuilder {
|
||||
Ok(builder)
|
||||
}
|
||||
|
||||
pub fn args<S: AsRef<str>>(
|
||||
mut self,
|
||||
args: impl Iterator<Item = S>,
|
||||
) -> Result<Self, host::__wasi_errno_t> {
|
||||
let args: Result<Vec<CString>, _> = args
|
||||
pub fn args<S: AsRef<str>>(mut self, args: impl Iterator<Item = S>) -> Result<Self> {
|
||||
let args: Result<Vec<CString>> = args
|
||||
.map(|arg| CString::new(arg.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE))
|
||||
.collect();
|
||||
self.args = args?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn arg(mut self, arg: &str) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn arg(mut self, arg: &str) -> Result<Self> {
|
||||
self.args
|
||||
.push(CString::new(arg).map_err(|_| host::__WASI_ENOTCAPABLE)?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn inherit_stdio(mut self) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn inherit_stdio(mut self) -> Result<Self> {
|
||||
self.fds.insert(0, FdEntry::duplicate_stdin()?);
|
||||
self.fds.insert(1, FdEntry::duplicate_stdout()?);
|
||||
self.fds.insert(2, FdEntry::duplicate_stderr()?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn inherit_env(self) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn inherit_env(self) -> Result<Self> {
|
||||
self.envs(std::env::vars())
|
||||
}
|
||||
|
||||
pub fn env<S: AsRef<str>>(mut self, k: S, v: S) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn env<S: AsRef<str>>(mut self, k: S, v: S) -> Result<Self> {
|
||||
self.env.insert(
|
||||
CString::new(k.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE)?,
|
||||
CString::new(v.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE)?,
|
||||
@@ -70,8 +67,8 @@ impl WasiCtxBuilder {
|
||||
pub fn envs<S: AsRef<str>, T: Borrow<(S, S)>>(
|
||||
mut self,
|
||||
envs: impl Iterator<Item = T>,
|
||||
) -> Result<Self, host::__wasi_errno_t> {
|
||||
let env: Result<HashMap<CString, CString>, _> = envs
|
||||
) -> Result<Self> {
|
||||
let env: Result<HashMap<CString, CString>> = envs
|
||||
.map(|t| {
|
||||
let (k, v) = t.borrow();
|
||||
let k = CString::new(k.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE);
|
||||
@@ -91,7 +88,7 @@ impl WasiCtxBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(mut self) -> Result<WasiCtx, host::__wasi_errno_t> {
|
||||
pub fn build(mut self) -> Result<WasiCtx> {
|
||||
// startup code starts looking at fd 3 for preopens
|
||||
let mut preopen_fd = 3;
|
||||
for (guest_path, dir) in self.preopens {
|
||||
@@ -147,7 +144,7 @@ impl WasiCtx {
|
||||
/// - Environment variables are inherited from the host process.
|
||||
///
|
||||
/// To override these behaviors, use `WasiCtxBuilder`.
|
||||
pub fn new<S: AsRef<str>>(args: impl Iterator<Item = S>) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn new<S: AsRef<str>>(args: impl Iterator<Item = S>) -> Result<Self> {
|
||||
WasiCtxBuilder::new()
|
||||
.and_then(|ctx| ctx.args(args))
|
||||
.and_then(|ctx| ctx.inherit_stdio())
|
||||
@@ -160,7 +157,7 @@ impl WasiCtx {
|
||||
fd: host::__wasi_fd_t,
|
||||
rights_base: host::__wasi_rights_t,
|
||||
rights_inheriting: host::__wasi_rights_t,
|
||||
) -> Result<&FdEntry, host::__wasi_errno_t> {
|
||||
) -> Result<&FdEntry> {
|
||||
if let Some(fe) = self.fds.get(&fd) {
|
||||
Self::validate_rights(fe, rights_base, rights_inheriting).and(Ok(fe))
|
||||
} else {
|
||||
@@ -173,7 +170,7 @@ impl WasiCtx {
|
||||
fd: host::__wasi_fd_t,
|
||||
rights_base: host::__wasi_rights_t,
|
||||
rights_inheriting: host::__wasi_rights_t,
|
||||
) -> Result<&mut FdEntry, host::__wasi_errno_t> {
|
||||
) -> Result<&mut FdEntry> {
|
||||
if let Some(fe) = self.fds.get_mut(&fd) {
|
||||
Self::validate_rights(fe, rights_base, rights_inheriting).and(Ok(fe))
|
||||
} else {
|
||||
@@ -185,7 +182,7 @@ impl WasiCtx {
|
||||
fe: &FdEntry,
|
||||
rights_base: host::__wasi_rights_t,
|
||||
rights_inheriting: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
if !fe.rights_base & rights_base != 0 || !fe.rights_inheriting & rights_inheriting != 0 {
|
||||
Err(host::__WASI_ENOTCAPABLE)
|
||||
} else {
|
||||
@@ -193,10 +190,7 @@ impl WasiCtx {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_fd_entry(
|
||||
&mut self,
|
||||
fe: FdEntry,
|
||||
) -> Result<host::__wasi_fd_t, host::__wasi_errno_t> {
|
||||
pub fn insert_fd_entry(&mut self, fe: FdEntry) -> Result<host::__wasi_fd_t> {
|
||||
// never insert where stdio handles usually are
|
||||
let mut fd = 3;
|
||||
while self.fds.contains_key(&fd) {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use super::host;
|
||||
use crate::sys::{errno_from_host, fdentry_impl};
|
||||
use crate::{host, Result};
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Descriptor {
|
||||
@@ -39,7 +38,7 @@ impl Drop for FdObject {
|
||||
}
|
||||
|
||||
impl FdEntry {
|
||||
pub fn from(file: fs::File) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn from(file: fs::File) -> Result<Self> {
|
||||
fdentry_impl::determine_type_and_access_rights(&file).map(
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
@@ -54,13 +53,13 @@ impl FdEntry {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn duplicate(file: &fs::File) -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn duplicate(file: &fs::File) -> Result<Self> {
|
||||
file.try_clone()
|
||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
.and_then(Self::from)
|
||||
}
|
||||
|
||||
pub fn duplicate_stdin() -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn duplicate_stdin() -> Result<Self> {
|
||||
fdentry_impl::determine_type_and_access_rights(&io::stdin()).map(
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
@@ -75,7 +74,7 @@ impl FdEntry {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn duplicate_stdout() -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn duplicate_stdout() -> Result<Self> {
|
||||
fdentry_impl::determine_type_and_access_rights(&io::stdout()).map(
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
@@ -90,7 +89,7 @@ impl FdEntry {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn duplicate_stderr() -> Result<Self, host::__wasi_errno_t> {
|
||||
pub fn duplicate_stderr() -> Result<Self> {
|
||||
fdentry_impl::determine_type_and_access_rights(&io::stderr()).map(
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! still largely reflects that.
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::Result;
|
||||
use std::{io, slice, str};
|
||||
|
||||
pub type void = ::std::os::raw::c_void;
|
||||
@@ -499,7 +499,7 @@ pub unsafe fn iovec_to_host_mut<'a>(iovec: &'a mut __wasi_iovec_t) -> io::IoSlic
|
||||
///
|
||||
/// NB WASI spec requires bytes to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_slice<'a>(s: &'a [u8]) -> Result<&'a str, __wasi_errno_t> {
|
||||
pub fn path_from_slice<'a>(s: &'a [u8]) -> Result<&'a str> {
|
||||
str::from_utf8(s).map_err(|_| __WASI_EILSEQ)
|
||||
}
|
||||
|
||||
@@ -507,7 +507,7 @@ pub fn path_from_slice<'a>(s: &'a [u8]) -> Result<&'a str, __wasi_errno_t> {
|
||||
///
|
||||
/// NB WASI spec requires bytes to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_vec<S: Into<Vec<u8>>>(s: S) -> Result<String, __wasi_errno_t> {
|
||||
pub fn path_from_vec<S: Into<Vec<u8>>>(s: S) -> Result<String> {
|
||||
String::from_utf8(s.into()).map_err(|_| __WASI_EILSEQ)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,10 @@ pub use self::fs::*;
|
||||
pub use self::misc::*;
|
||||
pub use self::sock::*;
|
||||
|
||||
fn return_enc_errno(errno: super::host::__wasi_errno_t) -> super::wasm32::__wasi_errno_t {
|
||||
let errno = super::memory::enc_errno(errno);
|
||||
log::trace!(" -> errno={}", super::wasm32::strerror(errno));
|
||||
use crate::{host, memory, wasm32};
|
||||
|
||||
fn return_enc_errno(errno: host::__wasi_errno_t) -> wasm32::__wasi_errno_t {
|
||||
let errno = memory::enc_errno(errno);
|
||||
log::trace!(" -> errno={}", wasm32::strerror(errno));
|
||||
errno
|
||||
}
|
||||
|
||||
@@ -31,3 +31,5 @@ pub mod wasm32;
|
||||
|
||||
pub use ctx::{WasiCtx, WasiCtxBuilder};
|
||||
pub use sys::preopen_dir;
|
||||
|
||||
pub(crate) type Result<T> = std::result::Result<T, self::host::__wasi_errno_t>;
|
||||
|
||||
112
src/memory.rs
112
src/memory.rs
@@ -1,22 +1,11 @@
|
||||
//! Functions to go back and forth between WASI types in host and wasm32 representations.
|
||||
#![allow(unused)]
|
||||
use crate::{host, wasm32};
|
||||
use crate::{host, wasm32, Result};
|
||||
use std::convert::TryFrom;
|
||||
use std::mem::{align_of, size_of};
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::{ptr, slice};
|
||||
|
||||
macro_rules! bail_errno {
|
||||
( $errno:ident ) => {
|
||||
return Err(host::$errno);
|
||||
};
|
||||
}
|
||||
|
||||
fn dec_ptr(
|
||||
memory: &[u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: usize,
|
||||
) -> Result<*const u8, host::__wasi_errno_t> {
|
||||
fn dec_ptr(memory: &[u8], ptr: wasm32::uintptr_t, len: usize) -> Result<*const u8> {
|
||||
// check for overflow
|
||||
let checked_len = (ptr as usize).checked_add(len).ok_or(host::__WASI_EFAULT)?;
|
||||
|
||||
@@ -27,11 +16,7 @@ fn dec_ptr(
|
||||
.map(|mem| mem.as_ptr())
|
||||
}
|
||||
|
||||
fn dec_ptr_mut(
|
||||
memory: &mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: usize,
|
||||
) -> Result<*mut u8, host::__wasi_errno_t> {
|
||||
fn dec_ptr_mut(memory: &mut [u8], ptr: wasm32::uintptr_t, len: usize) -> Result<*mut u8> {
|
||||
// check for overflow
|
||||
let checked_len = (ptr as usize).checked_add(len).ok_or(host::__WASI_EFAULT)?;
|
||||
|
||||
@@ -42,13 +27,10 @@ fn dec_ptr_mut(
|
||||
.map(|mem| mem.as_mut_ptr())
|
||||
}
|
||||
|
||||
fn dec_ptr_to<'memory, T>(
|
||||
memory: &'memory [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<&'memory T, host::__wasi_errno_t> {
|
||||
fn dec_ptr_to<'memory, T>(memory: &'memory [u8], ptr: wasm32::uintptr_t) -> Result<&'memory T> {
|
||||
// check that the ptr is aligned
|
||||
if ptr as usize % align_of::<T>() != 0 {
|
||||
bail_errno!(__WASI_EINVAL);
|
||||
return Err(host::__WASI_EINVAL);
|
||||
}
|
||||
|
||||
dec_ptr(memory, ptr, size_of::<T>()).map(|p| unsafe { &*(p as *const T) })
|
||||
@@ -57,31 +39,24 @@ fn dec_ptr_to<'memory, T>(
|
||||
fn dec_ptr_to_mut<'memory, T>(
|
||||
memory: &'memory mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<&'memory mut T, host::__wasi_errno_t> {
|
||||
) -> Result<&'memory mut T> {
|
||||
// check that the ptr is aligned
|
||||
if ptr as usize % align_of::<T>() != 0 {
|
||||
bail_errno!(__WASI_EINVAL);
|
||||
return Err(host::__WASI_EINVAL);
|
||||
}
|
||||
|
||||
dec_ptr_mut(memory, ptr, size_of::<T>()).map(|p| unsafe { &mut *(p as *mut T) })
|
||||
}
|
||||
|
||||
pub fn dec_pointee<T>(memory: &[u8], ptr: wasm32::uintptr_t) -> Result<T, host::__wasi_errno_t> {
|
||||
pub fn dec_pointee<T>(memory: &[u8], ptr: wasm32::uintptr_t) -> Result<T> {
|
||||
dec_ptr_to::<T>(memory, ptr).map(|p| unsafe { ptr::read(p) })
|
||||
}
|
||||
|
||||
pub fn enc_pointee<T>(
|
||||
memory: &mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
t: T,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
pub fn enc_pointee<T>(memory: &mut [u8], ptr: wasm32::uintptr_t, t: T) -> Result<()> {
|
||||
dec_ptr_to_mut::<T>(memory, ptr).map(|p| unsafe { ptr::write(p, t) })
|
||||
}
|
||||
|
||||
fn check_slice_of<T>(
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<(usize, usize), host::__wasi_errno_t> {
|
||||
fn check_slice_of<T>(ptr: wasm32::uintptr_t, len: wasm32::size_t) -> Result<(usize, usize)> {
|
||||
// check alignment, and that length doesn't overflow
|
||||
if ptr as usize % align_of::<T>() != 0 {
|
||||
return Err(host::__WASI_EINVAL);
|
||||
@@ -100,7 +75,7 @@ pub fn dec_slice_of<'memory, T>(
|
||||
memory: &'memory [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<&'memory [T], host::__wasi_errno_t> {
|
||||
) -> Result<&'memory [T]> {
|
||||
let (len, len_bytes) = check_slice_of::<T>(ptr, len)?;
|
||||
let ptr = dec_ptr(memory, ptr, len_bytes)? as *const T;
|
||||
Ok(unsafe { slice::from_raw_parts(ptr, len) })
|
||||
@@ -110,17 +85,13 @@ pub fn dec_slice_of_mut<'memory, T>(
|
||||
memory: &'memory mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<&'memory mut [T], host::__wasi_errno_t> {
|
||||
) -> Result<&'memory mut [T]> {
|
||||
let (len, len_bytes) = check_slice_of::<T>(ptr, len)?;
|
||||
let ptr = dec_ptr_mut(memory, ptr, len_bytes)? as *mut T;
|
||||
Ok(unsafe { slice::from_raw_parts_mut(ptr, len) })
|
||||
}
|
||||
|
||||
pub fn enc_slice_of<T>(
|
||||
memory: &mut [u8],
|
||||
slice: &[T],
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
pub fn enc_slice_of<T>(memory: &mut [u8], slice: &[T], ptr: wasm32::uintptr_t) -> Result<()> {
|
||||
// check alignment
|
||||
if ptr as usize % align_of::<T>() != 0 {
|
||||
return Err(host::__WASI_EINVAL);
|
||||
@@ -147,10 +118,7 @@ macro_rules! dec_enc_scalar {
|
||||
host::$ty::from_le(x)
|
||||
}
|
||||
|
||||
pub fn $dec_byref(
|
||||
memory: &mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
) -> Result<host::$ty, host::__wasi_errno_t> {
|
||||
pub fn $dec_byref(memory: &mut [u8], ptr: wasm32::uintptr_t) -> Result<host::$ty> {
|
||||
dec_pointee::<wasm32::$ty>(memory, ptr).map($dec)
|
||||
}
|
||||
|
||||
@@ -158,11 +126,7 @@ macro_rules! dec_enc_scalar {
|
||||
x.to_le()
|
||||
}
|
||||
|
||||
pub fn $enc_byref(
|
||||
memory: &mut [u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
x: host::$ty,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
pub fn $enc_byref(memory: &mut [u8], ptr: wasm32::uintptr_t, x: host::$ty) -> Result<()> {
|
||||
enc_pointee::<wasm32::$ty>(memory, ptr, $enc(x))
|
||||
}
|
||||
};
|
||||
@@ -171,7 +135,7 @@ macro_rules! dec_enc_scalar {
|
||||
pub fn dec_ciovec(
|
||||
memory: &[u8],
|
||||
ciovec: &wasm32::__wasi_ciovec_t,
|
||||
) -> Result<host::__wasi_ciovec_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_ciovec_t> {
|
||||
let len = dec_usize(ciovec.buf_len);
|
||||
Ok(host::__wasi_ciovec_t {
|
||||
buf: dec_ptr(memory, ciovec.buf, len)? as *const host::void,
|
||||
@@ -183,15 +147,12 @@ pub fn dec_ciovec_slice(
|
||||
memory: &[u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<Vec<host::__wasi_ciovec_t>, host::__wasi_errno_t> {
|
||||
) -> Result<Vec<host::__wasi_ciovec_t>> {
|
||||
let slice = dec_slice_of::<wasm32::__wasi_ciovec_t>(memory, ptr, len)?;
|
||||
slice.iter().map(|iov| dec_ciovec(memory, iov)).collect()
|
||||
}
|
||||
|
||||
pub fn dec_iovec(
|
||||
memory: &[u8],
|
||||
iovec: &wasm32::__wasi_iovec_t,
|
||||
) -> Result<host::__wasi_iovec_t, host::__wasi_errno_t> {
|
||||
pub fn dec_iovec(memory: &[u8], iovec: &wasm32::__wasi_iovec_t) -> Result<host::__wasi_iovec_t> {
|
||||
let len = dec_usize(iovec.buf_len);
|
||||
Ok(host::__wasi_iovec_t {
|
||||
buf: dec_ptr(memory, iovec.buf, len)? as *mut host::void,
|
||||
@@ -203,7 +164,7 @@ pub fn dec_iovec_slice(
|
||||
memory: &[u8],
|
||||
ptr: wasm32::uintptr_t,
|
||||
len: wasm32::size_t,
|
||||
) -> Result<Vec<host::__wasi_iovec_t>, host::__wasi_errno_t> {
|
||||
) -> Result<Vec<host::__wasi_iovec_t>> {
|
||||
let slice = dec_slice_of::<wasm32::__wasi_iovec_t>(memory, ptr, len)?;
|
||||
slice.iter().map(|iov| dec_iovec(memory, iov)).collect()
|
||||
}
|
||||
@@ -215,6 +176,7 @@ dec_enc_scalar!(
|
||||
enc_clockid,
|
||||
enc_clockid_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_errno_t,
|
||||
dec_errno,
|
||||
@@ -222,6 +184,7 @@ dec_enc_scalar!(
|
||||
enc_errno,
|
||||
enc_errno_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_exitcode_t,
|
||||
dec_exitcode,
|
||||
@@ -229,7 +192,9 @@ dec_enc_scalar!(
|
||||
enc_exitcode,
|
||||
enc_exitcode_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(__wasi_fd_t, dec_fd, dec_fd_byref, enc_fd, enc_fd_byref);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_fdflags_t,
|
||||
dec_fdflags,
|
||||
@@ -237,6 +202,7 @@ dec_enc_scalar!(
|
||||
enc_fdflags,
|
||||
enc_fdflags_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_device_t,
|
||||
dec_device,
|
||||
@@ -244,6 +210,7 @@ dec_enc_scalar!(
|
||||
enc_device,
|
||||
enc_device_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_inode_t,
|
||||
dec_inode,
|
||||
@@ -251,6 +218,7 @@ dec_enc_scalar!(
|
||||
enc_inode,
|
||||
enc_inode_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_linkcount_t,
|
||||
dec_linkcount,
|
||||
@@ -275,7 +243,7 @@ pub fn dec_filestat(filestat: wasm32::__wasi_filestat_t) -> host::__wasi_filesta
|
||||
pub fn dec_filestat_byref(
|
||||
memory: &mut [u8],
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_filestat_t> {
|
||||
dec_pointee::<wasm32::__wasi_filestat_t>(memory, filestat_ptr).map(dec_filestat)
|
||||
}
|
||||
|
||||
@@ -296,7 +264,7 @@ pub fn enc_filestat_byref(
|
||||
memory: &mut [u8],
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
host_filestat: host::__wasi_filestat_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
let filestat = enc_filestat(host_filestat);
|
||||
enc_pointee::<wasm32::__wasi_filestat_t>(memory, filestat_ptr, filestat)
|
||||
}
|
||||
@@ -313,7 +281,7 @@ pub fn dec_fdstat(fdstat: wasm32::__wasi_fdstat_t) -> host::__wasi_fdstat_t {
|
||||
pub fn dec_fdstat_byref(
|
||||
memory: &mut [u8],
|
||||
fdstat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<host::__wasi_fdstat_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_fdstat_t> {
|
||||
dec_pointee::<wasm32::__wasi_fdstat_t>(memory, fdstat_ptr).map(dec_fdstat)
|
||||
}
|
||||
|
||||
@@ -331,7 +299,7 @@ pub fn enc_fdstat_byref(
|
||||
memory: &mut [u8],
|
||||
fdstat_ptr: wasm32::uintptr_t,
|
||||
host_fdstat: host::__wasi_fdstat_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
let fdstat = enc_fdstat(host_fdstat);
|
||||
enc_pointee::<wasm32::__wasi_fdstat_t>(memory, fdstat_ptr, fdstat)
|
||||
}
|
||||
@@ -343,6 +311,7 @@ dec_enc_scalar!(
|
||||
enc_filedelta,
|
||||
enc_filedelta_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_filesize_t,
|
||||
dec_filesize,
|
||||
@@ -375,9 +344,7 @@ dec_enc_scalar!(
|
||||
enc_oflags_byref
|
||||
);
|
||||
|
||||
pub fn dec_prestat(
|
||||
prestat: wasm32::__wasi_prestat_t,
|
||||
) -> Result<host::__wasi_prestat_t, host::__wasi_errno_t> {
|
||||
pub fn dec_prestat(prestat: wasm32::__wasi_prestat_t) -> Result<host::__wasi_prestat_t> {
|
||||
match prestat.pr_type {
|
||||
wasm32::__WASI_PREOPENTYPE_DIR => {
|
||||
let u = host::__wasi_prestat_t___wasi_prestat_u {
|
||||
@@ -397,13 +364,11 @@ pub fn dec_prestat(
|
||||
pub fn dec_prestat_byref(
|
||||
memory: &mut [u8],
|
||||
prestat_ptr: wasm32::uintptr_t,
|
||||
) -> Result<host::__wasi_prestat_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_prestat_t> {
|
||||
dec_pointee::<wasm32::__wasi_prestat_t>(memory, prestat_ptr).and_then(dec_prestat)
|
||||
}
|
||||
|
||||
pub fn enc_prestat(
|
||||
prestat: host::__wasi_prestat_t,
|
||||
) -> Result<wasm32::__wasi_prestat_t, host::__wasi_errno_t> {
|
||||
pub fn enc_prestat(prestat: host::__wasi_prestat_t) -> Result<wasm32::__wasi_prestat_t> {
|
||||
match prestat.pr_type {
|
||||
host::__WASI_PREOPENTYPE_DIR => {
|
||||
let u = wasm32::__wasi_prestat_t___wasi_prestat_u {
|
||||
@@ -424,7 +389,7 @@ pub fn enc_prestat_byref(
|
||||
memory: &mut [u8],
|
||||
prestat_ptr: wasm32::uintptr_t,
|
||||
host_prestat: host::__wasi_prestat_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
let prestat = enc_prestat(host_prestat)?;
|
||||
enc_pointee::<wasm32::__wasi_prestat_t>(memory, prestat_ptr, prestat)
|
||||
}
|
||||
@@ -436,6 +401,7 @@ dec_enc_scalar!(
|
||||
enc_rights,
|
||||
enc_rights_byref
|
||||
);
|
||||
|
||||
dec_enc_scalar!(
|
||||
__wasi_timestamp_t,
|
||||
dec_timestamp,
|
||||
@@ -464,7 +430,7 @@ pub fn enc_usize_byref(
|
||||
memory: &mut [u8],
|
||||
usize_ptr: wasm32::uintptr_t,
|
||||
host_usize: usize,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
enc_pointee::<wasm32::size_t>(memory, usize_ptr, enc_usize(host_usize))
|
||||
}
|
||||
|
||||
@@ -510,7 +476,7 @@ dec_enc_scalar!(
|
||||
|
||||
pub fn dec_subscription(
|
||||
subscription: &wasm32::__wasi_subscription_t,
|
||||
) -> Result<host::__wasi_subscription_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_subscription_t> {
|
||||
let userdata = dec_userdata(subscription.userdata);
|
||||
let type_ = dec_eventtype(subscription.type_);
|
||||
let u_orig = subscription.u;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::host;
|
||||
use crate::host;
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
|
||||
use crate::{host, Result};
|
||||
use std::io;
|
||||
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
|
||||
|
||||
@@ -18,14 +17,11 @@ impl AsRawFd for Descriptor {
|
||||
|
||||
pub(crate) fn determine_type_and_access_rights<Fd: AsRawFd>(
|
||||
fd: &Fd,
|
||||
) -> Result<
|
||||
(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
),
|
||||
host::__wasi_errno_t,
|
||||
> {
|
||||
) -> Result<(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
)> {
|
||||
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(fd)?;
|
||||
|
||||
use nix::fcntl::{fcntl, OFlag, F_GETFL};
|
||||
@@ -46,14 +42,11 @@ pub(crate) fn determine_type_and_access_rights<Fd: AsRawFd>(
|
||||
|
||||
pub(crate) fn determine_type_rights<Fd: AsRawFd>(
|
||||
fd: &Fd,
|
||||
) -> Result<
|
||||
(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
),
|
||||
host::__wasi_errno_t,
|
||||
> {
|
||||
) -> Result<(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
)> {
|
||||
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 =
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(dead_code)]
|
||||
use crate::{host, memory, wasm32};
|
||||
use crate::{host, memory, wasm32, Result};
|
||||
use std::ffi::OsStr;
|
||||
use std::os::unix::prelude::OsStrExt;
|
||||
|
||||
@@ -196,9 +196,7 @@ pub fn nix_from_filetype(sflags: host::__wasi_filetype_t) -> nix::sys::stat::SFl
|
||||
nix_sflags
|
||||
}
|
||||
|
||||
pub fn filestat_from_nix(
|
||||
filestat: nix::sys::stat::FileStat,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
pub fn filestat_from_nix(filestat: nix::sys::stat::FileStat) -> Result<host::__wasi_filestat_t> {
|
||||
use std::convert::TryFrom;
|
||||
|
||||
let filetype = nix::sys::stat::SFlag::from_bits_truncate(filestat.st_mode);
|
||||
@@ -220,9 +218,7 @@ pub fn filestat_from_nix(
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn dirent_from_host(
|
||||
host_entry: &nix::libc::dirent,
|
||||
) -> Result<wasm32::__wasi_dirent_t, host::__wasi_errno_t> {
|
||||
pub fn dirent_from_host(host_entry: &nix::libc::dirent) -> Result<wasm32::__wasi_dirent_t> {
|
||||
let mut entry = unsafe { std::mem::zeroed::<wasm32::__wasi_dirent_t>() };
|
||||
let d_namlen = unsafe { std::ffi::CStr::from_ptr(host_entry.d_name.as_ptr()) }
|
||||
.to_bytes()
|
||||
@@ -238,9 +234,7 @@ pub fn dirent_from_host(
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn dirent_from_host(
|
||||
host_entry: &nix::libc::dirent,
|
||||
) -> Result<wasm32::__wasi_dirent_t, host::__wasi_errno_t> {
|
||||
pub fn dirent_from_host(host_entry: &nix::libc::dirent) -> Result<wasm32::__wasi_dirent_t> {
|
||||
let mut entry = unsafe { std::mem::zeroed::<wasm32::__wasi_dirent_t>() };
|
||||
entry.d_ino = memory::enc_inode(host_entry.d_ino);
|
||||
entry.d_next = memory::enc_dircookie(host_entry.d_seekoff);
|
||||
@@ -253,6 +247,6 @@ pub fn dirent_from_host(
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String, host::__wasi_errno_t> {
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
host::path_from_slice(s.as_ref().as_bytes()).map(String::from)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::fdentry::FdEntry;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, wasm32};
|
||||
use crate::{host, wasm32, Result};
|
||||
use nix::libc::{self, c_long, c_void, off_t};
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
@@ -17,16 +17,12 @@ pub(crate) fn fd_pread(
|
||||
file: &File,
|
||||
buf: &mut [u8],
|
||||
offset: host::__wasi_filesize_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
file.read_at(buf, offset)
|
||||
.map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_pwrite(
|
||||
file: &File,
|
||||
buf: &[u8],
|
||||
offset: host::__wasi_filesize_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
||||
file.write_at(buf, offset)
|
||||
.map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
@@ -35,7 +31,7 @@ pub(crate) fn fd_renumber(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
from: host::__wasi_fd_t,
|
||||
to: host::__wasi_fd_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
let fe_from = match wasi_ctx.fds.get(&from) {
|
||||
Some(fe_from) => fe_from,
|
||||
None => return Err(host::__WASI_EBADF),
|
||||
@@ -68,7 +64,7 @@ pub(crate) fn fd_seek(
|
||||
fd_entry: &FdEntry,
|
||||
offset: host::__wasi_filedelta_t,
|
||||
whence: host::__wasi_whence_t,
|
||||
) -> Result<u64, host::__wasi_errno_t> {
|
||||
) -> Result<u64> {
|
||||
use nix::unistd::{lseek, Whence};
|
||||
let nwhence = match whence {
|
||||
host::__WASI_WHENCE_CUR => Whence::SeekCur,
|
||||
@@ -85,7 +81,7 @@ pub(crate) fn fd_seek(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
|
||||
use nix::unistd::{lseek, Whence};
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
@@ -95,9 +91,7 @@ pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
||||
use nix::fcntl::{fcntl, OFlag, F_GETFL};
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
@@ -110,7 +104,7 @@ pub(crate) fn fd_fdstat_get(
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd_entry: &FdEntry,
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
let nix_flags = host_impl::nix_from_fdflags(fdflags);
|
||||
match nix::fcntl::fcntl(rawfd, nix::fcntl::F_SETFL(nix_flags)) {
|
||||
@@ -124,7 +118,7 @@ pub(crate) fn fd_advise(
|
||||
advice: host::__wasi_advice_t,
|
||||
offset: host::__wasi_filesize_t,
|
||||
len: host::__wasi_filesize_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let host_advice = match advice {
|
||||
@@ -164,7 +158,7 @@ pub(crate) fn path_create_directory(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::libc::mkdirat;
|
||||
|
||||
let (dir, path) = match path_get(
|
||||
@@ -196,7 +190,7 @@ pub(crate) fn path_link(
|
||||
new_path: &str,
|
||||
source_rights: host::__wasi_rights_t,
|
||||
target_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::libc::linkat;
|
||||
let (old_dir, old_path) = match path_get(ctx, old_dirfd, 0, old_path, source_rights, 0, false) {
|
||||
Ok((dir, path)) => (dir, path),
|
||||
@@ -238,7 +232,7 @@ pub(crate) fn path_open(
|
||||
mut needed_base: host::__wasi_rights_t,
|
||||
mut needed_inheriting: host::__wasi_rights_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> Result<FdEntry, host::__wasi_errno_t> {
|
||||
) -> Result<FdEntry> {
|
||||
use nix::errno::Errno;
|
||||
use nix::fcntl::{openat, AtFlags, OFlag};
|
||||
use nix::sys::stat::{fstatat, Mode, SFlag};
|
||||
@@ -354,7 +348,7 @@ pub(crate) fn fd_readdir(
|
||||
fd_entry: &FdEntry,
|
||||
host_buf: &mut [u8],
|
||||
cookie: host::__wasi_dircookie_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
use libc::{dirent, fdopendir, memcpy, readdir_r, seekdir};
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
@@ -415,7 +409,7 @@ pub(crate) fn path_readlink(
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
buf: &mut [u8],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
use nix::errno::Errno;
|
||||
|
||||
let (dir, path) = match path_get(wasi_ctx, dirfd, 0, path, rights, 0, false) {
|
||||
@@ -459,7 +453,7 @@ pub(crate) fn path_rename(
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
new_path: &str,
|
||||
new_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::libc::renameat;
|
||||
|
||||
let (old_dir, old_path) = match path_get(wasi_ctx, old_dirfd, 0, old_path, old_rights, 0, false)
|
||||
@@ -490,9 +484,7 @@ pub(crate) fn path_rename(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result<host::__wasi_filestat_t> {
|
||||
use nix::sys::stat::fstat;
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
@@ -507,7 +499,7 @@ pub(crate) fn fd_filestat_set_times(
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
fst_flags: host::__wasi_fstflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::sys::time::{TimeSpec, TimeValLike};
|
||||
|
||||
if fst_flags & host::__WASI_FILESTAT_SET_MTIM_NOW != 0 {
|
||||
@@ -553,7 +545,7 @@ pub(crate) fn fd_filestat_set_times(
|
||||
pub(crate) fn fd_filestat_set_size(
|
||||
fd_entry: &FdEntry,
|
||||
st_size: host::__wasi_filesize_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::unistd::ftruncate;
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
@@ -565,7 +557,7 @@ pub(crate) fn path_filestat_get(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_filestat_t> {
|
||||
use nix::fcntl::AtFlags;
|
||||
use nix::sys::stat::fstatat;
|
||||
|
||||
@@ -601,7 +593,7 @@ pub(crate) fn path_filestat_set_times(
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
fst_flags: host::__wasi_fstflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::sys::time::{TimeSpec, TimeValLike};
|
||||
|
||||
let (dir, path) = match path_get(wasi_ctx, dirfd, dirflags, &path, rights, 0, false) {
|
||||
@@ -661,7 +653,7 @@ pub(crate) fn path_symlink(
|
||||
rights: host::__wasi_rights_t,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::libc::symlinkat;
|
||||
|
||||
let (dir, new_path) = match path_get(wasi_ctx, dirfd, 0, new_path, rights, 0, false) {
|
||||
@@ -690,7 +682,7 @@ pub(crate) fn path_unlink_file(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::unlinkat;
|
||||
|
||||
@@ -741,7 +733,7 @@ pub(crate) fn path_remove_directory(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
use nix::errno;
|
||||
use nix::libc::{unlinkat, AT_REMOVEDIR};
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use nix::libc::{self, c_long};
|
||||
use std::fs::File;
|
||||
use std::path::{Component, Path};
|
||||
@@ -21,7 +21,7 @@ pub(crate) fn path_get(
|
||||
needed_base: host::__wasi_rights_t,
|
||||
needed_inheriting: host::__wasi_rights_t,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, String), host::__wasi_errno_t> {
|
||||
) -> Result<(File, String)> {
|
||||
const MAX_SYMLINK_EXPANSIONS: usize = 128;
|
||||
|
||||
if path.contains("\0") {
|
||||
@@ -186,7 +186,7 @@ pub(crate) fn path_get(
|
||||
}
|
||||
}
|
||||
|
||||
fn openat(dirfd: &File, path: &str) -> Result<File, host::__wasi_errno_t> {
|
||||
fn openat(dirfd: &File, path: &str) -> Result<File> {
|
||||
use nix::fcntl::{self, OFlag};
|
||||
use nix::sys::stat::Mode;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
@@ -201,7 +201,7 @@ fn openat(dirfd: &File, path: &str) -> Result<File, host::__wasi_errno_t> {
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
fn readlinkat(dirfd: &File, path: &str) -> Result<String, host::__wasi_errno_t> {
|
||||
fn readlinkat(dirfd: &File, path: &str) -> Result<String> {
|
||||
use nix::fcntl;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
|
||||
@@ -2,16 +2,13 @@
|
||||
#![allow(unused_unsafe)]
|
||||
use crate::memory::*;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, wasm32};
|
||||
|
||||
use crate::{host, wasm32, Result};
|
||||
use nix::convert_ioctl_res;
|
||||
use nix::libc::{self, c_int};
|
||||
use std::cmp;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub(crate) fn clock_res_get(
|
||||
clock_id: host::__wasi_clockid_t,
|
||||
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
// convert the supported clocks to the libc types, or return EINVAL
|
||||
let clock_id = match clock_id {
|
||||
host::__WASI_CLOCK_REALTIME => libc::CLOCK_REALTIME,
|
||||
@@ -45,9 +42,7 @@ pub(crate) fn clock_res_get(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn clock_time_get(
|
||||
clock_id: host::__wasi_clockid_t,
|
||||
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
// convert the supported clocks to the libc types, or return EINVAL
|
||||
let clock_id = match clock_id {
|
||||
host::__WASI_CLOCK_REALTIME => libc::CLOCK_REALTIME,
|
||||
@@ -73,9 +68,9 @@ pub(crate) fn clock_time_get(
|
||||
}
|
||||
|
||||
pub(crate) fn poll_oneoff(
|
||||
input: Vec<Result<host::__wasi_subscription_t, host::__wasi_errno_t>>,
|
||||
input: Vec<Result<host::__wasi_subscription_t>>,
|
||||
output_slice: &mut [wasm32::__wasi_event_t],
|
||||
) -> Result<wasm32::size_t, host::__wasi_errno_t> {
|
||||
) -> Result<wasm32::size_t> {
|
||||
let timeout = input
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
@@ -152,7 +147,7 @@ nix::ioctl_read_bad!(fionread, nix::libc::FIONREAD, c_int);
|
||||
|
||||
fn wasi_clock_to_relative_ns_delay(
|
||||
wasi_clock: host::__wasi_subscription_t___wasi_subscription_u___wasi_subscription_u_clock_t,
|
||||
) -> Result<u128, host::__wasi_errno_t> {
|
||||
) -> Result<u128> {
|
||||
if wasi_clock.flags != wasm32::__WASI_SUBSCRIPTION_CLOCK_ABSTIME {
|
||||
return Ok(wasi_clock.timeout as u128);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,16 @@ pub(crate) mod fdentry_impl;
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File, host::__wasi_errno_t> {
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
File::open("/dev/null")
|
||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File, host::__wasi_errno_t> {
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
File::open(path).map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use super::host_impl;
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle};
|
||||
@@ -19,14 +18,11 @@ impl AsRawHandle for Descriptor {
|
||||
|
||||
pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
||||
handle: &Handle,
|
||||
) -> Result<
|
||||
(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
),
|
||||
host::__wasi_errno_t,
|
||||
> {
|
||||
) -> Result<(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
)> {
|
||||
use winx::file::{get_file_access_rights, AccessRight};
|
||||
|
||||
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(handle)?;
|
||||
@@ -54,14 +50,11 @@ pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
||||
|
||||
pub(crate) fn determine_type_rights<Handle: AsRawHandle>(
|
||||
handle: &Handle,
|
||||
) -> Result<
|
||||
(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
),
|
||||
host::__wasi_errno_t,
|
||||
> {
|
||||
) -> Result<(
|
||||
host::__wasi_filetype_t,
|
||||
host::__wasi_rights_t,
|
||||
host::__wasi_rights_t,
|
||||
)> {
|
||||
let (file_type, rights_base, rights_inheriting) = {
|
||||
let file_type =
|
||||
winx::file::get_file_type(handle.as_raw_handle()).map_err(host_impl::errno_from_win)?;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(unused)]
|
||||
use crate::host;
|
||||
use crate::{host, Result};
|
||||
use std::ffi::OsStr;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
|
||||
@@ -108,7 +108,7 @@ pub fn win_from_oflags(
|
||||
///
|
||||
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
|
||||
/// `__WASI_EILSEQ` error is returned.
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String, host::__wasi_errno_t> {
|
||||
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
let vec: Vec<u16> = s.as_ref().encode_wide().collect();
|
||||
String::from_utf16(&vec).map_err(|_| host::__WASI_EILSEQ)
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::os::windows::fs::FileExt;
|
||||
@@ -36,16 +36,12 @@ pub(crate) fn fd_pread(
|
||||
file: &File,
|
||||
buf: &mut [u8],
|
||||
offset: host::__wasi_filesize_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
read_at(file, buf, offset)
|
||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_pwrite(
|
||||
file: &File,
|
||||
buf: &[u8],
|
||||
offset: host::__wasi_filesize_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
||||
write_at(file, buf, offset)
|
||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
@@ -54,7 +50,7 @@ pub(crate) fn fd_renumber(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
from: host::__wasi_fd_t,
|
||||
to: host::__wasi_fd_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("fd_renumber")
|
||||
}
|
||||
|
||||
@@ -62,17 +58,15 @@ pub(crate) fn fd_seek(
|
||||
fd_entry: &FdEntry,
|
||||
offset: host::__wasi_filedelta_t,
|
||||
whence: host::__wasi_whence_t,
|
||||
) -> Result<u64, host::__wasi_errno_t> {
|
||||
) -> Result<u64> {
|
||||
unimplemented!("fd_seek")
|
||||
}
|
||||
|
||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
|
||||
unimplemented!("fd_tell")
|
||||
}
|
||||
|
||||
pub(crate) fn fd_fdstat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
||||
use winx::file::AccessRight;
|
||||
|
||||
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle();
|
||||
@@ -85,7 +79,7 @@ pub(crate) fn fd_fdstat_get(
|
||||
pub(crate) fn fd_fdstat_set_flags(
|
||||
fd_entry: &FdEntry,
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("fd_fdstat_set_flags")
|
||||
}
|
||||
|
||||
@@ -94,7 +88,7 @@ pub(crate) fn fd_advise(
|
||||
advice: host::__wasi_advice_t,
|
||||
offset: host::__wasi_filesize_t,
|
||||
len: host::__wasi_filesize_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("fd_advise")
|
||||
}
|
||||
|
||||
@@ -102,7 +96,7 @@ pub(crate) fn path_create_directory(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_create_directory")
|
||||
}
|
||||
|
||||
@@ -114,7 +108,7 @@ pub(crate) fn path_link(
|
||||
new_path: &str,
|
||||
source_rights: host::__wasi_rights_t,
|
||||
target_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_link")
|
||||
}
|
||||
|
||||
@@ -129,7 +123,7 @@ pub(crate) fn path_open(
|
||||
mut needed_base: host::__wasi_rights_t,
|
||||
mut needed_inheriting: host::__wasi_rights_t,
|
||||
fs_flags: host::__wasi_fdflags_t,
|
||||
) -> Result<FdEntry, host::__wasi_errno_t> {
|
||||
) -> Result<FdEntry> {
|
||||
use winx::file::{AccessRight, CreationDisposition, FlagsAndAttributes, ShareMode};
|
||||
|
||||
let mut win_rights = AccessRight::READ_CONTROL;
|
||||
@@ -200,7 +194,7 @@ pub(crate) fn fd_readdir(
|
||||
fd_entry: &FdEntry,
|
||||
host_buf: &mut [u8],
|
||||
cookie: host::__wasi_dircookie_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
unimplemented!("fd_readdir")
|
||||
}
|
||||
|
||||
@@ -210,7 +204,7 @@ pub(crate) fn path_readlink(
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
buf: &mut [u8],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
) -> Result<usize> {
|
||||
unimplemented!("path_readlink")
|
||||
}
|
||||
|
||||
@@ -222,13 +216,11 @@ pub(crate) fn path_rename(
|
||||
new_dirfd: host::__wasi_fd_t,
|
||||
new_path: &str,
|
||||
new_rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_rename")
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result<host::__wasi_filestat_t> {
|
||||
unimplemented!("fd_filestat_get")
|
||||
}
|
||||
|
||||
@@ -237,14 +229,14 @@ pub(crate) fn fd_filestat_set_times(
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
fst_flags: host::__wasi_fstflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("fd_filestat_set_times")
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_set_size(
|
||||
fd_entry: &FdEntry,
|
||||
st_size: host::__wasi_filesize_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("fd_filestat_set_size")
|
||||
}
|
||||
|
||||
@@ -253,7 +245,7 @@ pub(crate) fn path_filestat_get(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
dirflags: host::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
) -> Result<host::__wasi_filestat_t> {
|
||||
unimplemented!("path_filestat_get")
|
||||
}
|
||||
|
||||
@@ -266,7 +258,7 @@ pub(crate) fn path_filestat_set_times(
|
||||
st_atim: host::__wasi_timestamp_t,
|
||||
mut st_mtim: host::__wasi_timestamp_t,
|
||||
fst_flags: host::__wasi_fstflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_filestat_set_times")
|
||||
}
|
||||
|
||||
@@ -276,7 +268,7 @@ pub(crate) fn path_symlink(
|
||||
rights: host::__wasi_rights_t,
|
||||
old_path: &str,
|
||||
new_path: &str,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_symlink")
|
||||
}
|
||||
|
||||
@@ -285,7 +277,7 @@ pub(crate) fn path_unlink_file(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_unlink_file")
|
||||
}
|
||||
|
||||
@@ -294,6 +286,6 @@ pub(crate) fn path_remove_directory(
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &str,
|
||||
rights: host::__wasi_rights_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
) -> Result<()> {
|
||||
unimplemented!("path_remove_directory")
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::Descriptor;
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::{Component, Path};
|
||||
@@ -19,7 +19,7 @@ pub(crate) fn path_get(
|
||||
needed_base: host::__wasi_rights_t,
|
||||
needed_inheriting: host::__wasi_rights_t,
|
||||
needs_final_component: bool,
|
||||
) -> Result<(File, String), host::__wasi_errno_t> {
|
||||
) -> Result<(File, String)> {
|
||||
if path.contains("\0") {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(host::__WASI_EILSEQ);
|
||||
|
||||
@@ -3,25 +3,21 @@
|
||||
#![allow(unused)]
|
||||
use crate::memory::*;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, wasm32};
|
||||
use crate::{host, wasm32, Result};
|
||||
|
||||
use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
pub(crate) fn clock_res_get(
|
||||
clock_id: host::__wasi_clockid_t,
|
||||
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
unimplemented!("clock_res_get")
|
||||
}
|
||||
|
||||
pub(crate) fn clock_time_get(
|
||||
clock_id: host::__wasi_clockid_t,
|
||||
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
|
||||
pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
|
||||
unimplemented!("clock_time_get")
|
||||
}
|
||||
|
||||
pub(crate) fn poll_oneoff(
|
||||
input: Vec<Result<host::__wasi_subscription_t, host::__wasi_errno_t>>,
|
||||
input: Vec<Result<host::__wasi_subscription_t>>,
|
||||
output_slice: &mut [wasm32::__wasi_event_t],
|
||||
) -> Result<wasm32::size_t, host::__wasi_errno_t> {
|
||||
) -> Result<wasm32::size_t> {
|
||||
unimplemented!("poll_oneoff")
|
||||
}
|
||||
|
||||
@@ -2,16 +2,16 @@ pub(crate) mod fdentry_impl;
|
||||
pub(crate) mod host_impl;
|
||||
pub(crate) mod hostcalls_impl;
|
||||
|
||||
use crate::host;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
pub(crate) fn dev_null() -> Result<File, host::__wasi_errno_t> {
|
||||
pub(crate) fn dev_null() -> Result<File> {
|
||||
File::open("NUL").map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File, host::__wasi_errno_t> {
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
|
||||
|
||||
Reference in New Issue
Block a user