diff --git a/src/ctx.rs b/src/ctx.rs index 6c8d9cbbb7..01063857e0 100644 --- a/src/ctx.rs +++ b/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 { + pub fn new() -> Result { let mut builder = Self { fds: HashMap::new(), preopens: HashMap::new(), @@ -31,35 +31,32 @@ impl WasiCtxBuilder { Ok(builder) } - pub fn args>( - mut self, - args: impl Iterator, - ) -> Result { - let args: Result, _> = args + pub fn args>(mut self, args: impl Iterator) -> Result { + let args: Result> = 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 { + pub fn arg(mut self, arg: &str) -> Result { self.args .push(CString::new(arg).map_err(|_| host::__WASI_ENOTCAPABLE)?); Ok(self) } - pub fn inherit_stdio(mut self) -> Result { + pub fn inherit_stdio(mut self) -> Result { 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 { + pub fn inherit_env(self) -> Result { self.envs(std::env::vars()) } - pub fn env>(mut self, k: S, v: S) -> Result { + pub fn env>(mut self, k: S, v: S) -> Result { 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, T: Borrow<(S, S)>>( mut self, envs: impl Iterator, - ) -> Result { - let env: Result, _> = envs + ) -> Result { + let env: Result> = 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 { + pub fn build(mut self) -> Result { // 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>(args: impl Iterator) -> Result { + pub fn new>(args: impl Iterator) -> Result { 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 { + pub fn insert_fd_entry(&mut self, fe: FdEntry) -> Result { // never insert where stdio handles usually are let mut fd = 3; while self.fds.contains_key(&fd) { diff --git a/src/fdentry.rs b/src/fdentry.rs index c3842caf6b..a2baadbceb 100644 --- a/src/fdentry.rs +++ b/src/fdentry.rs @@ -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 { + pub fn from(file: fs::File) -> Result { 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 { + pub fn duplicate(file: &fs::File) -> Result { 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 { + pub fn duplicate_stdin() -> Result { 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 { + pub fn duplicate_stdout() -> Result { 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 { + pub fn duplicate_stderr() -> Result { fdentry_impl::determine_type_and_access_rights(&io::stderr()).map( |(file_type, rights_base, rights_inheriting)| Self { fd_object: FdObject { diff --git a/src/host.rs b/src/host.rs index fac60f1cd4..a45d98555c 100644 --- a/src/host.rs +++ b/src/host.rs @@ -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: S) -> Result { +pub fn path_from_vec>>(s: S) -> Result { String::from_utf8(s.into()).map_err(|_| __WASI_EILSEQ) } diff --git a/src/hostcalls/mod.rs b/src/hostcalls/mod.rs index 925fcc4a2f..f09011901b 100644 --- a/src/hostcalls/mod.rs +++ b/src/hostcalls/mod.rs @@ -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 } diff --git a/src/lib.rs b/src/lib.rs index de7e9298cc..5703d88058 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,3 +31,5 @@ pub mod wasm32; pub use ctx::{WasiCtx, WasiCtxBuilder}; pub use sys::preopen_dir; + +pub(crate) type Result = std::result::Result; diff --git a/src/memory.rs b/src/memory.rs index 22128a4bda..ef504ac2d5 100644 --- a/src/memory.rs +++ b/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::() != 0 { - bail_errno!(__WASI_EINVAL); + return Err(host::__WASI_EINVAL); } dec_ptr(memory, ptr, size_of::()).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::() != 0 { - bail_errno!(__WASI_EINVAL); + return Err(host::__WASI_EINVAL); } dec_ptr_mut(memory, ptr, size_of::()).map(|p| unsafe { &mut *(p as *mut T) }) } -pub fn dec_pointee(memory: &[u8], ptr: wasm32::uintptr_t) -> Result { +pub fn dec_pointee(memory: &[u8], ptr: wasm32::uintptr_t) -> Result { dec_ptr_to::(memory, ptr).map(|p| unsafe { ptr::read(p) }) } -pub fn enc_pointee( - memory: &mut [u8], - ptr: wasm32::uintptr_t, - t: T, -) -> Result<(), host::__wasi_errno_t> { +pub fn enc_pointee(memory: &mut [u8], ptr: wasm32::uintptr_t, t: T) -> Result<()> { dec_ptr_to_mut::(memory, ptr).map(|p| unsafe { ptr::write(p, t) }) } -fn check_slice_of( - ptr: wasm32::uintptr_t, - len: wasm32::size_t, -) -> Result<(usize, usize), host::__wasi_errno_t> { +fn check_slice_of(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::() != 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::(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::(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( - memory: &mut [u8], - slice: &[T], - ptr: wasm32::uintptr_t, -) -> Result<(), host::__wasi_errno_t> { +pub fn enc_slice_of(memory: &mut [u8], slice: &[T], ptr: wasm32::uintptr_t) -> Result<()> { // check alignment if ptr as usize % align_of::() != 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 { + pub fn $dec_byref(memory: &mut [u8], ptr: wasm32::uintptr_t) -> Result { dec_pointee::(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::(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 { +) -> Result { 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, host::__wasi_errno_t> { +) -> Result> { let slice = dec_slice_of::(memory, ptr, len)?; slice.iter().map(|iov| dec_ciovec(memory, iov)).collect() } -pub fn dec_iovec( - memory: &[u8], - iovec: &wasm32::__wasi_iovec_t, -) -> Result { +pub fn dec_iovec(memory: &[u8], iovec: &wasm32::__wasi_iovec_t) -> Result { 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, host::__wasi_errno_t> { +) -> Result> { let slice = dec_slice_of::(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 { +) -> Result { dec_pointee::(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::(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 { +) -> Result { dec_pointee::(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::(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 { +pub fn dec_prestat(prestat: wasm32::__wasi_prestat_t) -> Result { 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 { +) -> Result { dec_pointee::(memory, prestat_ptr).and_then(dec_prestat) } -pub fn enc_prestat( - prestat: host::__wasi_prestat_t, -) -> Result { +pub fn enc_prestat(prestat: host::__wasi_prestat_t) -> Result { 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::(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::(memory, usize_ptr, enc_usize(host_usize)) } @@ -510,7 +476,7 @@ dec_enc_scalar!( pub fn dec_subscription( subscription: &wasm32::__wasi_subscription_t, -) -> Result { +) -> Result { let userdata = dec_userdata(subscription.userdata); let type_ = dec_eventtype(subscription.type_); let u_orig = subscription.u; diff --git a/src/sys/mod.rs b/src/sys/mod.rs index ec7071ff38..dcad0ea7fc 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -1,4 +1,4 @@ -use super::host; +use crate::host; use cfg_if::cfg_if; cfg_if! { diff --git a/src/sys/unix/fdentry_impl.rs b/src/sys/unix/fdentry_impl.rs index 2367ff4f7c..9be3954021 100644 --- a/src/sys/unix/fdentry_impl.rs +++ b/src/sys/unix/fdentry_impl.rs @@ -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: &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( pub(crate) fn determine_type_rights( 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 = diff --git a/src/sys/unix/host_impl.rs b/src/sys/unix/host_impl.rs index 461e433748..fa7601a054 100644 --- a/src/sys/unix/host_impl.rs +++ b/src/sys/unix/host_impl.rs @@ -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 { +pub fn filestat_from_nix(filestat: nix::sys::stat::FileStat) -> Result { 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 { +pub fn dirent_from_host(host_entry: &nix::libc::dirent) -> Result { let mut entry = unsafe { std::mem::zeroed::() }; 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 { +pub fn dirent_from_host(host_entry: &nix::libc::dirent) -> Result { let mut entry = unsafe { std::mem::zeroed::() }; 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: S) -> Result { +pub fn path_from_host>(s: S) -> Result { host::path_from_slice(s.as_ref().as_bytes()).map(String::from) } diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 37e70ee267..6b65d828e5 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -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 { +) -> Result { 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 { +pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result { 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 { +) -> Result { 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 { +pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result { 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 { } } -pub(crate) fn fd_fdstat_get( - fd_entry: &FdEntry, -) -> Result { +pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result { 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 { +) -> Result { 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 { +) -> Result { 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 { +) -> Result { 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 { +pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result { 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 { +) -> Result { 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}; diff --git a/src/sys/unix/hostcalls_impl/fs_helpers.rs b/src/sys/unix/hostcalls_impl/fs_helpers.rs index 0cb7d40e69..539b965807 100644 --- a/src/sys/unix/hostcalls_impl/fs_helpers.rs +++ b/src/sys/unix/hostcalls_impl/fs_helpers.rs @@ -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 { +fn openat(dirfd: &File, path: &str) -> Result { 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 { .map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap())) } -fn readlinkat(dirfd: &File, path: &str) -> Result { +fn readlinkat(dirfd: &File, path: &str) -> Result { use nix::fcntl; use std::os::unix::prelude::AsRawFd; diff --git a/src/sys/unix/hostcalls_impl/misc.rs b/src/sys/unix/hostcalls_impl/misc.rs index 4d3852390c..27edac64d4 100644 --- a/src/sys/unix/hostcalls_impl/misc.rs +++ b/src/sys/unix/hostcalls_impl/misc.rs @@ -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 { +pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result { // 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 { +pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result { // 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>, + input: Vec>, output_slice: &mut [wasm32::__wasi_event_t], -) -> Result { +) -> Result { 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 { +) -> Result { if wasi_clock.flags != wasm32::__WASI_SUBSCRIPTION_CLOCK_ABSTIME { return Ok(wasi_clock.timeout as u128); } diff --git a/src/sys/unix/mod.rs b/src/sys/unix/mod.rs index f634561680..0e39f63cb4 100644 --- a/src/sys/unix/mod.rs +++ b/src/sys/unix/mod.rs @@ -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 { +pub(crate) fn dev_null() -> Result { File::open("/dev/null") .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) } -pub fn preopen_dir>(path: P) -> Result { +pub fn preopen_dir>(path: P) -> Result { File::open(path).map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) } diff --git a/src/sys/windows/fdentry_impl.rs b/src/sys/windows/fdentry_impl.rs index e8d6acbd84..0a0bfdaf0f 100644 --- a/src/sys/windows/fdentry_impl.rs +++ b/src/sys/windows/fdentry_impl.rs @@ -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: &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( pub(crate) fn determine_type_rights( 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)?; diff --git a/src/sys/windows/host_impl.rs b/src/sys/windows/host_impl.rs index 065868f2b7..b591774c62 100644 --- a/src/sys/windows/host_impl.rs +++ b/src/sys/windows/host_impl.rs @@ -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: S) -> Result { +pub fn path_from_host>(s: S) -> Result { let vec: Vec = s.as_ref().encode_wide().collect(); String::from_utf16(&vec).map_err(|_| host::__WASI_EILSEQ) } diff --git a/src/sys/windows/hostcalls_impl/fs.rs b/src/sys/windows/hostcalls_impl/fs.rs index 671b7c9954..bf3b042c90 100644 --- a/src/sys/windows/hostcalls_impl/fs.rs +++ b/src/sys/windows/hostcalls_impl/fs.rs @@ -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 { +) -> Result { 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 { +pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result { 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 { +) -> Result { unimplemented!("fd_seek") } -pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result { +pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result { unimplemented!("fd_tell") } -pub(crate) fn fd_fdstat_get( - fd_entry: &FdEntry, -) -> Result { +pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result { 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 { +) -> Result { 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 { +) -> Result { unimplemented!("fd_readdir") } @@ -210,7 +204,7 @@ pub(crate) fn path_readlink( path: &str, rights: host::__wasi_rights_t, buf: &mut [u8], -) -> Result { +) -> Result { 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 { +pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result { 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 { +) -> Result { 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") } diff --git a/src/sys/windows/hostcalls_impl/fs_helpers.rs b/src/sys/windows/hostcalls_impl/fs_helpers.rs index 53db390705..7e6a409056 100644 --- a/src/sys/windows/hostcalls_impl/fs_helpers.rs +++ b/src/sys/windows/hostcalls_impl/fs_helpers.rs @@ -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); diff --git a/src/sys/windows/hostcalls_impl/misc.rs b/src/sys/windows/hostcalls_impl/misc.rs index 2daac48ab6..f920957753 100644 --- a/src/sys/windows/hostcalls_impl/misc.rs +++ b/src/sys/windows/hostcalls_impl/misc.rs @@ -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 { +pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result { unimplemented!("clock_res_get") } -pub(crate) fn clock_time_get( - clock_id: host::__wasi_clockid_t, -) -> Result { +pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result { unimplemented!("clock_time_get") } pub(crate) fn poll_oneoff( - input: Vec>, + input: Vec>, output_slice: &mut [wasm32::__wasi_event_t], -) -> Result { +) -> Result { unimplemented!("poll_oneoff") } diff --git a/src/sys/windows/mod.rs b/src/sys/windows/mod.rs index 3e8b24f019..02e562c067 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -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 { +pub(crate) fn dev_null() -> Result { File::open("NUL").map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) } -pub fn preopen_dir>(path: P) -> Result { +pub fn preopen_dir>(path: P) -> Result { use std::fs::OpenOptions; use std::os::windows::fs::OpenOptionsExt; use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;