diff --git a/crates/wasi-common/src/error.rs b/crates/wasi-common/src/error.rs index 302d5d76bd..bd48fd86b0 100644 --- a/crates/wasi-common/src/error.rs +++ b/crates/wasi-common/src/error.rs @@ -104,14 +104,11 @@ pub enum Error { #[cfg(unix)] #[error("Yanix error: {0}")] Yanix(#[from] yanix::YanixError), - #[cfg(windows)] - #[error("Winx error: {0}")] - Winx(#[from] winx::winerror::WinError), } impl From for Error { fn from(_: TryFromIntError) -> Self { - Self::Wasi(WasiError::EOVERFLOW) + Self::EOVERFLOW } } @@ -123,41 +120,49 @@ impl From for Error { impl From for Error { fn from(_: str::Utf8Error) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl From for Error { fn from(_: ffi::NulError) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl From<&ffi::NulError> for Error { fn from(_: &ffi::NulError) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl Error { - pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t { + pub(crate) fn as_wasi_error(&self) -> WasiError { match self { - Self::Wasi(no) => no.as_raw_errno(), - Self::Io(e) => errno_from_ioerror(e.to_owned()), + Self::Wasi(err) => *err, + Self::Io(err) => { + let err = match err.raw_os_error() { + Some(code) => Self::from_raw_os_error(code), + None => { + log::debug!("Inconvertible OS error: {}", err); + Self::EIO + } + }; + err.as_wasi_error() + } #[cfg(unix)] Self::Yanix(err) => { use yanix::YanixError::*; - let err = match err { - Errno(errno) => crate::sys::host_impl::errno_from_nix(*errno), + let err: Self = match err { + Errno(errno) => (*errno).into(), NulError(err) => err.into(), TryFromIntError(err) => (*err).into(), }; - err.as_wasi_errno() + err.as_wasi_error() } - #[cfg(windows)] - Self::Winx(err) => crate::sys::host_impl::errno_from_win(*err), } } + pub const ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS); pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG); pub const EACCES: Self = Error::Wasi(WasiError::EACCES); @@ -237,12 +242,6 @@ impl Error { pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE); } -fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t { - match e.raw_os_error() { - Some(code) => crate::sys::errno_from_host(code), - None => { - log::debug!("Inconvertible OS error: {}", e); - wasi::__WASI_ERRNO_IO - } - } +pub(crate) trait FromRawOsError { + fn from_raw_os_error(code: i32) -> Self; } diff --git a/crates/wasi-common/src/hostcalls/fs.rs b/crates/wasi-common/src/hostcalls/fs.rs index 6746ba79d4..acbd85f006 100644 --- a/crates/wasi-common/src/hostcalls/fs.rs +++ b/crates/wasi-common/src/hostcalls/fs.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] use crate::ctx::WasiCtx; -use crate::{hostcalls_impl, wasi, wasi32}; +use crate::{wasi, wasi32}; hostcalls! { pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t; diff --git a/crates/wasi-common/src/hostcalls/misc.rs b/crates/wasi-common/src/hostcalls/misc.rs index 883b5a54ac..7546987570 100644 --- a/crates/wasi-common/src/hostcalls/misc.rs +++ b/crates/wasi-common/src/hostcalls/misc.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] use crate::ctx::WasiCtx; -use crate::{hostcalls_impl, wasi, wasi32}; +use crate::{wasi, wasi32}; use log::trace; use wasi_common_cbindgen::wasi_common_cbindgen; diff --git a/crates/wasi-common/src/hostcalls_impl/fs_helpers.rs b/crates/wasi-common/src/hostcalls_impl/fs_helpers.rs index 5fd939b167..23a06c23fc 100644 --- a/crates/wasi-common/src/hostcalls_impl/fs_helpers.rs +++ b/crates/wasi-common/src/hostcalls_impl/fs_helpers.rs @@ -1,7 +1,7 @@ #![allow(non_camel_case_types)] use crate::sys::host_impl; use crate::sys::hostcalls_impl::fs_helpers::*; -use crate::{fdentry::FdEntry, wasi, Error, Result}; +use crate::{error::WasiError, fdentry::FdEntry, wasi, Error, Result}; use std::fs::File; use std::path::{Component, Path}; @@ -117,10 +117,10 @@ pub(crate) fn path_get( dir_stack.push(new_dir); } Err(e) => { - match e.as_wasi_errno() { - wasi::__WASI_ERRNO_LOOP - | wasi::__WASI_ERRNO_MLINK - | wasi::__WASI_ERRNO_NOTDIR => + match e.as_wasi_error() { + WasiError::ELOOP + | WasiError::EMLINK + | WasiError::ENOTDIR => // Check to see if it was a symlink. Linux indicates // this with ENOTDIR because of the O_DIRECTORY flag. { @@ -179,12 +179,12 @@ pub(crate) fn path_get( continue; } Err(e) => { - if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL - && e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT + if e.as_wasi_error() != WasiError::EINVAL + && e.as_wasi_error() != WasiError::ENOENT // this handles the cases when trying to link to // a destination that already exists, and the target // path contains a slash - && e.as_wasi_errno() != wasi::__WASI_ERRNO_NOTDIR + && e.as_wasi_error() != WasiError::ENOTDIR { return Err(e); } diff --git a/crates/wasi-common/src/hostcalls_impl/misc.rs b/crates/wasi-common/src/hostcalls_impl/misc.rs index 7e266033f8..893d3dae0b 100644 --- a/crates/wasi-common/src/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/hostcalls_impl/misc.rs @@ -261,7 +261,7 @@ pub(crate) fn poll_oneoff( let event = wasi::__wasi_event_t { userdata: subscription.userdata, r#type, - error: err.as_wasi_errno(), + error: err.as_wasi_error().as_raw_errno(), u: wasi::__wasi_event_u_t { fd_readwrite: wasi::__wasi_event_fd_readwrite_t { nbytes: 0, diff --git a/crates/wasi-common/src/macros.rs b/crates/wasi-common/src/macros.rs index b3da97c215..14cbaba8a6 100644 --- a/crates/wasi-common/src/macros.rs +++ b/crates/wasi-common/src/macros.rs @@ -2,12 +2,12 @@ macro_rules! hostcalls { ($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($( #[wasi_common_cbindgen::wasi_common_cbindgen] pub unsafe fn $name($($arg: $ty,)*) -> $ret { - let ret = match hostcalls_impl::$name($($arg,)*) { - Ok(()) => wasi::__WASI_ERRNO_SUCCESS, - Err(e) => e.as_wasi_errno(), - }; - - ret + let ret = crate::hostcalls_impl::$name($($arg,)*) + .err() + .unwrap_or(crate::Error::ESUCCESS) + .as_wasi_error(); + log::trace!(" | errno={}", ret); + ret.as_raw_errno() } )*) } @@ -18,12 +18,12 @@ macro_rules! hostcalls_old { ($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($( #[wasi_common_cbindgen::wasi_common_cbindgen_old] pub unsafe fn $name($($arg: $ty,)*) -> $ret { - let ret = match hostcalls_impl::$name($($arg,)*) { - Ok(()) => wasi::__WASI_ERRNO_SUCCESS, - Err(e) => e.as_wasi_errno(), - }; - - ret + let ret = crate::old::snapshot_0::hostcalls_impl::$name($($arg,)*) + .err() + .unwrap_or(crate::old::snapshot_0::Error::ESUCCESS) + .as_wasi_error(); + log::trace!(" | errno={}", ret); + ret.as_raw_errno() } )*) } diff --git a/crates/wasi-common/src/old/snapshot_0/error.rs b/crates/wasi-common/src/old/snapshot_0/error.rs index 534e8a3697..554d01f4e3 100644 --- a/crates/wasi-common/src/old/snapshot_0/error.rs +++ b/crates/wasi-common/src/old/snapshot_0/error.rs @@ -104,14 +104,11 @@ pub enum Error { #[cfg(unix)] #[error("Yanix error: {0}")] Yanix(#[from] yanix::YanixError), - #[cfg(windows)] - #[error("Winx error: {0}")] - Winx(#[from] winx::winerror::WinError), } impl From for Error { fn from(_: TryFromIntError) -> Self { - Self::Wasi(WasiError::EOVERFLOW) + Self::EOVERFLOW } } @@ -123,41 +120,49 @@ impl From for Error { impl From for Error { fn from(_: str::Utf8Error) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl From for Error { fn from(_: ffi::NulError) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl From<&ffi::NulError> for Error { fn from(_: &ffi::NulError) -> Self { - Self::Wasi(WasiError::EILSEQ) + Self::EILSEQ } } impl Error { - pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t { + pub(crate) fn as_wasi_error(&self) -> WasiError { match self { - Self::Wasi(no) => no.as_raw_errno(), - Self::Io(e) => errno_from_ioerror(e.to_owned()), + Self::Wasi(err) => *err, + Self::Io(err) => { + let err = match err.raw_os_error() { + Some(code) => Self::from_raw_os_error(code), + None => { + log::debug!("Inconvertible OS error: {}", err); + Self::EIO + } + }; + err.as_wasi_error() + } #[cfg(unix)] Self::Yanix(err) => { use yanix::YanixError::*; - let err = match err { - Errno(errno) => crate::old::snapshot_0::sys::host_impl::errno_from_nix(*errno), + let err: Self = match err { + Errno(errno) => (*errno).into(), NulError(err) => err.into(), TryFromIntError(err) => (*err).into(), }; - err.as_wasi_errno() + err.as_wasi_error() } - #[cfg(windows)] - Self::Winx(err) => crate::old::snapshot_0::sys::host_impl::errno_from_win(*err), } } + pub const ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS); pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG); pub const EACCES: Self = Error::Wasi(WasiError::EACCES); @@ -237,12 +242,6 @@ impl Error { pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE); } -fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t { - match e.raw_os_error() { - Some(code) => crate::old::snapshot_0::sys::errno_from_host(code), - None => { - log::debug!("Inconvertible OS error: {}", e); - wasi::__WASI_ERRNO_IO - } - } +pub(crate) trait FromRawOsError { + fn from_raw_os_error(code: i32) -> Self; } diff --git a/crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs b/crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs index 4836da4e76..7c7158b3a0 100644 --- a/crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs +++ b/crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] use crate::old::snapshot_0::ctx::WasiCtx; -use crate::old::snapshot_0::{hostcalls_impl, wasi, wasi32}; +use crate::old::snapshot_0::{wasi, wasi32}; hostcalls_old! { pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t; diff --git a/crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs b/crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs index 42712cb4d2..98812e2e04 100644 --- a/crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs +++ b/crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] use crate::old::snapshot_0::ctx::WasiCtx; -use crate::old::snapshot_0::{hostcalls_impl, wasi, wasi32}; +use crate::old::snapshot_0::{wasi, wasi32}; use log::trace; use wasi_common_cbindgen::wasi_common_cbindgen_old; diff --git a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs_helpers.rs b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs_helpers.rs index 3a26bfc54b..ff41bbc3a9 100644 --- a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs_helpers.rs +++ b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/fs_helpers.rs @@ -1,7 +1,7 @@ #![allow(non_camel_case_types)] use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::*; -use crate::old::snapshot_0::{fdentry::FdEntry, wasi, Error, Result}; +use crate::old::snapshot_0::{error::WasiError, fdentry::FdEntry, wasi, Error, Result}; use std::fs::File; use std::path::{Component, Path}; @@ -117,10 +117,10 @@ pub(crate) fn path_get( dir_stack.push(new_dir); } Err(e) => { - match e.as_wasi_errno() { - wasi::__WASI_ERRNO_LOOP - | wasi::__WASI_ERRNO_MLINK - | wasi::__WASI_ERRNO_NOTDIR => + match e.as_wasi_error() { + WasiError::ELOOP + | WasiError::EMLINK + | WasiError::ENOTDIR => // Check to see if it was a symlink. Linux indicates // this with ENOTDIR because of the O_DIRECTORY flag. { @@ -179,12 +179,12 @@ pub(crate) fn path_get( continue; } Err(e) => { - if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL - && e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT + if e.as_wasi_error() != WasiError::EINVAL + && e.as_wasi_error() != WasiError::ENOENT // this handles the cases when trying to link to // a destination that already exists, and the target // path contains a slash - && e.as_wasi_errno() != wasi::__WASI_ERRNO_NOTDIR + && e.as_wasi_error() != WasiError::ENOTDIR { return Err(e); } diff --git a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs index f06e052348..32cd6468e9 100644 --- a/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/old/snapshot_0/hostcalls_impl/misc.rs @@ -258,7 +258,7 @@ pub(crate) fn poll_oneoff( let event = wasi::__wasi_event_t { userdata: subscription.userdata, r#type, - error: err.as_wasi_errno(), + error: err.as_wasi_error().as_raw_errno(), u: wasi::__wasi_event_u_t { fd_readwrite: wasi::__wasi_event_fd_readwrite_t { nbytes: 0, diff --git a/crates/wasi-common/src/old/snapshot_0/sys/mod.rs b/crates/wasi-common/src/old/snapshot_0/sys/mod.rs index 2e62d6f1c4..6d8b3fd82f 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/mod.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/mod.rs @@ -1,21 +1,12 @@ -use crate::old::snapshot_0::wasi; use cfg_if::cfg_if; cfg_if! { if #[cfg(unix)] { mod unix; pub(crate) use self::unix::*; - - pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { - host_impl::errno_from_nix(yanix::Errno::from_i32(err)).as_wasi_errno() - } } else if #[cfg(windows)] { mod windows; pub(crate) use self::windows::*; - - pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { - host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32)) - } } else { compile_error!("wasi-common doesn't compile for this platform yet"); } diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs index 7db8808ea7..5afab3305b 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/bsd/hostcalls_impl.rs @@ -1,5 +1,4 @@ use crate::old::snapshot_0::hostcalls_impl::PathGet; -use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::{Error, Result}; use std::os::unix::prelude::AsRawFd; @@ -80,7 +79,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { Err(Error::ENOTDIR) } } - x => Err(host_impl::errno_from_nix(x)), + x => Err(x.into()), } } else { Err(err.into()) @@ -132,7 +131,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul Err(Error::ENOENT) } } - x => Err(host_impl::errno_from_nix(x)), + x => Err(x.into()), } } else { Err(err.into()) diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs index ac74fd3e83..1233478ab5 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/host_impl.rs @@ -3,7 +3,9 @@ #![allow(non_snake_case)] #![allow(dead_code)] use crate::old::snapshot_0::host::FileType; -use crate::old::snapshot_0::{helpers, sys::unix::sys_impl, wasi, Error, Result}; +use crate::old::snapshot_0::{ + error::FromRawOsError, helpers, sys::unix::sys_impl, wasi, Error, Result, +}; use std::ffi::OsStr; use std::os::unix::prelude::OsStrExt; use yanix::{ @@ -13,82 +15,90 @@ use yanix::{ pub(crate) use sys_impl::host_impl::*; -pub(crate) fn errno_from_nix(errno: Errno) -> Error { - match errno { - Errno::EPERM => Error::EPERM, - Errno::ENOENT => Error::ENOENT, - Errno::ESRCH => Error::ESRCH, - Errno::EINTR => Error::EINTR, - Errno::EIO => Error::EIO, - Errno::ENXIO => Error::ENXIO, - Errno::E2BIG => Error::E2BIG, - Errno::ENOEXEC => Error::ENOEXEC, - Errno::EBADF => Error::EBADF, - Errno::ECHILD => Error::ECHILD, - Errno::EAGAIN => Error::EAGAIN, - Errno::ENOMEM => Error::ENOMEM, - Errno::EACCES => Error::EACCES, - Errno::EFAULT => Error::EFAULT, - Errno::EBUSY => Error::EBUSY, - Errno::EEXIST => Error::EEXIST, - Errno::EXDEV => Error::EXDEV, - Errno::ENODEV => Error::ENODEV, - Errno::ENOTDIR => Error::ENOTDIR, - Errno::EISDIR => Error::EISDIR, - Errno::EINVAL => Error::EINVAL, - Errno::ENFILE => Error::ENFILE, - Errno::EMFILE => Error::EMFILE, - Errno::ENOTTY => Error::ENOTTY, - Errno::ETXTBSY => Error::ETXTBSY, - Errno::EFBIG => Error::EFBIG, - Errno::ENOSPC => Error::ENOSPC, - Errno::ESPIPE => Error::ESPIPE, - Errno::EROFS => Error::EROFS, - Errno::EMLINK => Error::EMLINK, - Errno::EPIPE => Error::EPIPE, - Errno::EDOM => Error::EDOM, - Errno::ERANGE => Error::ERANGE, - Errno::EDEADLK => Error::EDEADLK, - Errno::ENAMETOOLONG => Error::ENAMETOOLONG, - Errno::ENOLCK => Error::ENOLCK, - Errno::ENOSYS => Error::ENOSYS, - Errno::ENOTEMPTY => Error::ENOTEMPTY, - Errno::ELOOP => Error::ELOOP, - Errno::ENOMSG => Error::ENOMSG, - Errno::EIDRM => Error::EIDRM, - Errno::ENOLINK => Error::ENOLINK, - Errno::EPROTO => Error::EPROTO, - Errno::EMULTIHOP => Error::EMULTIHOP, - Errno::EBADMSG => Error::EBADMSG, - Errno::EOVERFLOW => Error::EOVERFLOW, - Errno::EILSEQ => Error::EILSEQ, - Errno::ENOTSOCK => Error::ENOTSOCK, - Errno::EDESTADDRREQ => Error::EDESTADDRREQ, - Errno::EMSGSIZE => Error::EMSGSIZE, - Errno::EPROTOTYPE => Error::EPROTOTYPE, - Errno::ENOPROTOOPT => Error::ENOPROTOOPT, - Errno::EPROTONOSUPPORT => Error::EPROTONOSUPPORT, - Errno::EAFNOSUPPORT => Error::EAFNOSUPPORT, - Errno::EADDRINUSE => Error::EADDRINUSE, - Errno::EADDRNOTAVAIL => Error::EADDRNOTAVAIL, - Errno::ENETDOWN => Error::ENETDOWN, - Errno::ENETUNREACH => Error::ENETUNREACH, - Errno::ENETRESET => Error::ENETRESET, - Errno::ECONNABORTED => Error::ECONNABORTED, - Errno::ECONNRESET => Error::ECONNRESET, - Errno::ENOBUFS => Error::ENOBUFS, - Errno::EISCONN => Error::EISCONN, - Errno::ENOTCONN => Error::ENOTCONN, - Errno::ETIMEDOUT => Error::ETIMEDOUT, - Errno::ECONNREFUSED => Error::ECONNREFUSED, - Errno::EHOSTUNREACH => Error::EHOSTUNREACH, - Errno::EALREADY => Error::EALREADY, - Errno::EINPROGRESS => Error::EINPROGRESS, - Errno::ESTALE => Error::ESTALE, - Errno::EDQUOT => Error::EDQUOT, - Errno::ECANCELED => Error::ECANCELED, - Errno::EOWNERDEAD => Error::EOWNERDEAD, - Errno::ENOTRECOVERABLE => Error::ENOTRECOVERABLE, +impl FromRawOsError for Error { + fn from_raw_os_error(code: i32) -> Self { + Self::from(Errno::from_i32(code)) + } +} + +impl From for Error { + fn from(errno: Errno) -> Self { + match errno { + Errno::EPERM => Self::EPERM, + Errno::ENOENT => Self::ENOENT, + Errno::ESRCH => Self::ESRCH, + Errno::EINTR => Self::EINTR, + Errno::EIO => Self::EIO, + Errno::ENXIO => Self::ENXIO, + Errno::E2BIG => Self::E2BIG, + Errno::ENOEXEC => Self::ENOEXEC, + Errno::EBADF => Self::EBADF, + Errno::ECHILD => Self::ECHILD, + Errno::EAGAIN => Self::EAGAIN, + Errno::ENOMEM => Self::ENOMEM, + Errno::EACCES => Self::EACCES, + Errno::EFAULT => Self::EFAULT, + Errno::EBUSY => Self::EBUSY, + Errno::EEXIST => Self::EEXIST, + Errno::EXDEV => Self::EXDEV, + Errno::ENODEV => Self::ENODEV, + Errno::ENOTDIR => Self::ENOTDIR, + Errno::EISDIR => Self::EISDIR, + Errno::EINVAL => Self::EINVAL, + Errno::ENFILE => Self::ENFILE, + Errno::EMFILE => Self::EMFILE, + Errno::ENOTTY => Self::ENOTTY, + Errno::ETXTBSY => Self::ETXTBSY, + Errno::EFBIG => Self::EFBIG, + Errno::ENOSPC => Self::ENOSPC, + Errno::ESPIPE => Self::ESPIPE, + Errno::EROFS => Self::EROFS, + Errno::EMLINK => Self::EMLINK, + Errno::EPIPE => Self::EPIPE, + Errno::EDOM => Self::EDOM, + Errno::ERANGE => Self::ERANGE, + Errno::EDEADLK => Self::EDEADLK, + Errno::ENAMETOOLONG => Self::ENAMETOOLONG, + Errno::ENOLCK => Self::ENOLCK, + Errno::ENOSYS => Self::ENOSYS, + Errno::ENOTEMPTY => Self::ENOTEMPTY, + Errno::ELOOP => Self::ELOOP, + Errno::ENOMSG => Self::ENOMSG, + Errno::EIDRM => Self::EIDRM, + Errno::ENOLINK => Self::ENOLINK, + Errno::EPROTO => Self::EPROTO, + Errno::EMULTIHOP => Self::EMULTIHOP, + Errno::EBADMSG => Self::EBADMSG, + Errno::EOVERFLOW => Self::EOVERFLOW, + Errno::EILSEQ => Self::EILSEQ, + Errno::ENOTSOCK => Self::ENOTSOCK, + Errno::EDESTADDRREQ => Self::EDESTADDRREQ, + Errno::EMSGSIZE => Self::EMSGSIZE, + Errno::EPROTOTYPE => Self::EPROTOTYPE, + Errno::ENOPROTOOPT => Self::ENOPROTOOPT, + Errno::EPROTONOSUPPORT => Self::EPROTONOSUPPORT, + Errno::EAFNOSUPPORT => Self::EAFNOSUPPORT, + Errno::EADDRINUSE => Self::EADDRINUSE, + Errno::EADDRNOTAVAIL => Self::EADDRNOTAVAIL, + Errno::ENETDOWN => Self::ENETDOWN, + Errno::ENETUNREACH => Self::ENETUNREACH, + Errno::ENETRESET => Self::ENETRESET, + Errno::ECONNABORTED => Self::ECONNABORTED, + Errno::ECONNRESET => Self::ECONNRESET, + Errno::ENOBUFS => Self::ENOBUFS, + Errno::EISCONN => Self::EISCONN, + Errno::ENOTCONN => Self::ENOTCONN, + Errno::ETIMEDOUT => Self::ETIMEDOUT, + Errno::ECONNREFUSED => Self::ECONNREFUSED, + Errno::EHOSTUNREACH => Self::EHOSTUNREACH, + Errno::EALREADY => Self::EALREADY, + Errno::EINPROGRESS => Self::EINPROGRESS, + Errno::ESTALE => Self::ESTALE, + Errno::EDQUOT => Self::EDQUOT, + Errno::ECANCELED => Self::ECANCELED, + Errno::EOWNERDEAD => Self::EOWNERDEAD, + Errno::ENOTRECOVERABLE => Self::ENOTRECOVERABLE, + } } } diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs index a26f24e505..f67320d88c 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/fs.rs @@ -170,7 +170,7 @@ pub(crate) fn path_open( Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => { return Err(Error::ELOOP); } - errno => return Err(host_impl::errno_from_nix(errno)), + errno => return Err(errno.into()), } } else { return Err(e.into()); diff --git a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs index 4be75c61d4..42f67c1178 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/unix/hostcalls_impl/misc.rs @@ -1,7 +1,6 @@ #![allow(non_camel_case_types)] #![allow(unused_unsafe)] use crate::old::snapshot_0::hostcalls_impl::{ClockEventData, FdEventData}; -use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::{wasi, Error, Result}; use yanix::clock::{clock_getres, clock_gettime, ClockId}; @@ -92,7 +91,7 @@ pub(crate) fn poll_oneoff( if Errno::last() == Errno::EINTR { continue; } - return Err(host_impl::errno_from_nix(Errno::last())); + return Err(Errno::last().into()); } Ok(ready) => break ready, } diff --git a/crates/wasi-common/src/old/snapshot_0/sys/windows/host_impl.rs b/crates/wasi-common/src/old/snapshot_0/sys/windows/host_impl.rs index e5b80e2193..5fcebb052e 100644 --- a/crates/wasi-common/src/old/snapshot_0/sys/windows/host_impl.rs +++ b/crates/wasi-common/src/old/snapshot_0/sys/windows/host_impl.rs @@ -3,7 +3,7 @@ #![allow(non_snake_case)] #![allow(unused)] use crate::old::snapshot_0::host::FileType; -use crate::old::snapshot_0::{wasi, Error, Result}; +use crate::old::snapshot_0::{error::FromRawOsError, wasi, Error, Result}; use std::convert::TryInto; use std::ffi::OsStr; use std::fs::OpenOptions; @@ -13,35 +13,44 @@ use std::os::windows::ffi::OsStrExt; use std::os::windows::fs::OpenOptionsExt; use std::time::{SystemTime, UNIX_EPOCH}; use winx::file::{AccessMode, Attributes, CreationDisposition, Flags}; +use winx::winerror::WinError; -pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> wasi::__wasi_errno_t { - // TODO: implement error mapping between Windows and WASI - use winx::winerror::WinError::*; - match error { - ERROR_SUCCESS => wasi::__WASI_ERRNO_SUCCESS, - ERROR_BAD_ENVIRONMENT => wasi::__WASI_ERRNO_2BIG, - ERROR_FILE_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, - ERROR_PATH_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, - ERROR_TOO_MANY_OPEN_FILES => wasi::__WASI_ERRNO_NFILE, - ERROR_ACCESS_DENIED => wasi::__WASI_ERRNO_ACCES, - ERROR_SHARING_VIOLATION => wasi::__WASI_ERRNO_ACCES, - ERROR_PRIVILEGE_NOT_HELD => wasi::__WASI_ERRNO_NOTCAPABLE, // TODO is this the correct mapping? - ERROR_INVALID_HANDLE => wasi::__WASI_ERRNO_BADF, - ERROR_INVALID_NAME => wasi::__WASI_ERRNO_NOENT, - ERROR_NOT_ENOUGH_MEMORY => wasi::__WASI_ERRNO_NOMEM, - ERROR_OUTOFMEMORY => wasi::__WASI_ERRNO_NOMEM, - ERROR_DIR_NOT_EMPTY => wasi::__WASI_ERRNO_NOTEMPTY, - ERROR_NOT_READY => wasi::__WASI_ERRNO_BUSY, - ERROR_BUSY => wasi::__WASI_ERRNO_BUSY, - ERROR_NOT_SUPPORTED => wasi::__WASI_ERRNO_NOTSUP, - ERROR_FILE_EXISTS => wasi::__WASI_ERRNO_EXIST, - ERROR_BROKEN_PIPE => wasi::__WASI_ERRNO_PIPE, - ERROR_BUFFER_OVERFLOW => wasi::__WASI_ERRNO_NAMETOOLONG, - ERROR_NOT_A_REPARSE_POINT => wasi::__WASI_ERRNO_INVAL, - ERROR_NEGATIVE_SEEK => wasi::__WASI_ERRNO_INVAL, - ERROR_DIRECTORY => wasi::__WASI_ERRNO_NOTDIR, - ERROR_ALREADY_EXISTS => wasi::__WASI_ERRNO_EXIST, - _ => wasi::__WASI_ERRNO_NOTSUP, +impl FromRawOsError for Error { + fn from_raw_os_error(code: i32) -> Self { + Self::from(WinError::from_u32(code as u32)) + } +} + +impl From for Error { + fn from(err: WinError) -> Self { + // TODO: implement error mapping between Windows and WASI + use winx::winerror::WinError::*; + match err { + ERROR_SUCCESS => Self::ESUCCESS, + ERROR_BAD_ENVIRONMENT => Self::E2BIG, + ERROR_FILE_NOT_FOUND => Self::ENOENT, + ERROR_PATH_NOT_FOUND => Self::ENOENT, + ERROR_TOO_MANY_OPEN_FILES => Self::ENFILE, + ERROR_ACCESS_DENIED => Self::EACCES, + ERROR_SHARING_VIOLATION => Self::EACCES, + ERROR_PRIVILEGE_NOT_HELD => Self::ENOTCAPABLE, // TODO is this the correct mapping? + ERROR_INVALID_HANDLE => Self::EBADF, + ERROR_INVALID_NAME => Self::ENOENT, + ERROR_NOT_ENOUGH_MEMORY => Self::ENOMEM, + ERROR_OUTOFMEMORY => Self::ENOMEM, + ERROR_DIR_NOT_EMPTY => Self::ENOTEMPTY, + ERROR_NOT_READY => Self::EBUSY, + ERROR_BUSY => Self::EBUSY, + ERROR_NOT_SUPPORTED => Self::ENOTSUP, + ERROR_FILE_EXISTS => Self::EEXIST, + ERROR_BROKEN_PIPE => Self::EPIPE, + ERROR_BUFFER_OVERFLOW => Self::ENAMETOOLONG, + ERROR_NOT_A_REPARSE_POINT => Self::EINVAL, + ERROR_NEGATIVE_SEEK => Self::EINVAL, + ERROR_DIRECTORY => Self::ENOTDIR, + ERROR_ALREADY_EXISTS => Self::EEXIST, + _ => Self::ENOTSUP, + } } } diff --git a/crates/wasi-common/src/sys/mod.rs b/crates/wasi-common/src/sys/mod.rs index 1149a426a3..849fb7ee16 100644 --- a/crates/wasi-common/src/sys/mod.rs +++ b/crates/wasi-common/src/sys/mod.rs @@ -1,4 +1,3 @@ -use crate::wasi; use cfg_if::cfg_if; cfg_if! { @@ -6,18 +5,10 @@ cfg_if! { mod unix; pub(crate) use unix::*; pub use unix::preopen_dir; - - pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { - host_impl::errno_from_nix(yanix::Errno::from_i32(err)).as_wasi_errno() - } } else if #[cfg(windows)] { mod windows; pub(crate) use windows::*; pub use windows::preopen_dir; - - pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t { - host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32)) - } } else { compile_error!("wasi-common doesn't compile for this platform yet"); } diff --git a/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs b/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs index 015c52da4f..e03cbb8a86 100644 --- a/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs +++ b/crates/wasi-common/src/sys/unix/bsd/hostcalls_impl.rs @@ -1,5 +1,4 @@ use crate::hostcalls_impl::PathGet; -use crate::sys::host_impl; use crate::{Error, Result}; use std::os::unix::prelude::AsRawFd; @@ -80,7 +79,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { Err(Error::ENOTDIR) } } - x => Err(host_impl::errno_from_nix(x)), + x => Err(x.into()), } } else { Err(err.into()) @@ -132,7 +131,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul Err(Error::ENOENT) } } - x => Err(host_impl::errno_from_nix(x)), + x => Err(x.into()), } } else { Err(err.into()) diff --git a/crates/wasi-common/src/sys/unix/host_impl.rs b/crates/wasi-common/src/sys/unix/host_impl.rs index e74c2bcc7f..8aecef3083 100644 --- a/crates/wasi-common/src/sys/unix/host_impl.rs +++ b/crates/wasi-common/src/sys/unix/host_impl.rs @@ -3,7 +3,7 @@ #![allow(non_snake_case)] #![allow(dead_code)] use crate::host::FileType; -use crate::{helpers, sys::unix::sys_impl, wasi, Error, Result}; +use crate::{error::FromRawOsError, helpers, sys::unix::sys_impl, wasi, Error, Result}; use std::ffi::OsStr; use std::os::unix::prelude::OsStrExt; use yanix::{ @@ -13,82 +13,90 @@ use yanix::{ pub(crate) use sys_impl::host_impl::*; -pub(crate) fn errno_from_nix(errno: Errno) -> Error { - match errno { - Errno::EPERM => Error::EPERM, - Errno::ENOENT => Error::ENOENT, - Errno::ESRCH => Error::ESRCH, - Errno::EINTR => Error::EINTR, - Errno::EIO => Error::EIO, - Errno::ENXIO => Error::ENXIO, - Errno::E2BIG => Error::E2BIG, - Errno::ENOEXEC => Error::ENOEXEC, - Errno::EBADF => Error::EBADF, - Errno::ECHILD => Error::ECHILD, - Errno::EAGAIN => Error::EAGAIN, - Errno::ENOMEM => Error::ENOMEM, - Errno::EACCES => Error::EACCES, - Errno::EFAULT => Error::EFAULT, - Errno::EBUSY => Error::EBUSY, - Errno::EEXIST => Error::EEXIST, - Errno::EXDEV => Error::EXDEV, - Errno::ENODEV => Error::ENODEV, - Errno::ENOTDIR => Error::ENOTDIR, - Errno::EISDIR => Error::EISDIR, - Errno::EINVAL => Error::EINVAL, - Errno::ENFILE => Error::ENFILE, - Errno::EMFILE => Error::EMFILE, - Errno::ENOTTY => Error::ENOTTY, - Errno::ETXTBSY => Error::ETXTBSY, - Errno::EFBIG => Error::EFBIG, - Errno::ENOSPC => Error::ENOSPC, - Errno::ESPIPE => Error::ESPIPE, - Errno::EROFS => Error::EROFS, - Errno::EMLINK => Error::EMLINK, - Errno::EPIPE => Error::EPIPE, - Errno::EDOM => Error::EDOM, - Errno::ERANGE => Error::ERANGE, - Errno::EDEADLK => Error::EDEADLK, - Errno::ENAMETOOLONG => Error::ENAMETOOLONG, - Errno::ENOLCK => Error::ENOLCK, - Errno::ENOSYS => Error::ENOSYS, - Errno::ENOTEMPTY => Error::ENOTEMPTY, - Errno::ELOOP => Error::ELOOP, - Errno::ENOMSG => Error::ENOMSG, - Errno::EIDRM => Error::EIDRM, - Errno::ENOLINK => Error::ENOLINK, - Errno::EPROTO => Error::EPROTO, - Errno::EMULTIHOP => Error::EMULTIHOP, - Errno::EBADMSG => Error::EBADMSG, - Errno::EOVERFLOW => Error::EOVERFLOW, - Errno::EILSEQ => Error::EILSEQ, - Errno::ENOTSOCK => Error::ENOTSOCK, - Errno::EDESTADDRREQ => Error::EDESTADDRREQ, - Errno::EMSGSIZE => Error::EMSGSIZE, - Errno::EPROTOTYPE => Error::EPROTOTYPE, - Errno::ENOPROTOOPT => Error::ENOPROTOOPT, - Errno::EPROTONOSUPPORT => Error::EPROTONOSUPPORT, - Errno::EAFNOSUPPORT => Error::EAFNOSUPPORT, - Errno::EADDRINUSE => Error::EADDRINUSE, - Errno::EADDRNOTAVAIL => Error::EADDRNOTAVAIL, - Errno::ENETDOWN => Error::ENETDOWN, - Errno::ENETUNREACH => Error::ENETUNREACH, - Errno::ENETRESET => Error::ENETRESET, - Errno::ECONNABORTED => Error::ECONNABORTED, - Errno::ECONNRESET => Error::ECONNRESET, - Errno::ENOBUFS => Error::ENOBUFS, - Errno::EISCONN => Error::EISCONN, - Errno::ENOTCONN => Error::ENOTCONN, - Errno::ETIMEDOUT => Error::ETIMEDOUT, - Errno::ECONNREFUSED => Error::ECONNREFUSED, - Errno::EHOSTUNREACH => Error::EHOSTUNREACH, - Errno::EALREADY => Error::EALREADY, - Errno::EINPROGRESS => Error::EINPROGRESS, - Errno::ESTALE => Error::ESTALE, - Errno::EDQUOT => Error::EDQUOT, - Errno::ECANCELED => Error::ECANCELED, - Errno::EOWNERDEAD => Error::EOWNERDEAD, - Errno::ENOTRECOVERABLE => Error::ENOTRECOVERABLE, +impl FromRawOsError for Error { + fn from_raw_os_error(code: i32) -> Self { + Self::from(Errno::from_i32(code)) + } +} + +impl From for Error { + fn from(errno: Errno) -> Self { + match errno { + Errno::EPERM => Self::EPERM, + Errno::ENOENT => Self::ENOENT, + Errno::ESRCH => Self::ESRCH, + Errno::EINTR => Self::EINTR, + Errno::EIO => Self::EIO, + Errno::ENXIO => Self::ENXIO, + Errno::E2BIG => Self::E2BIG, + Errno::ENOEXEC => Self::ENOEXEC, + Errno::EBADF => Self::EBADF, + Errno::ECHILD => Self::ECHILD, + Errno::EAGAIN => Self::EAGAIN, + Errno::ENOMEM => Self::ENOMEM, + Errno::EACCES => Self::EACCES, + Errno::EFAULT => Self::EFAULT, + Errno::EBUSY => Self::EBUSY, + Errno::EEXIST => Self::EEXIST, + Errno::EXDEV => Self::EXDEV, + Errno::ENODEV => Self::ENODEV, + Errno::ENOTDIR => Self::ENOTDIR, + Errno::EISDIR => Self::EISDIR, + Errno::EINVAL => Self::EINVAL, + Errno::ENFILE => Self::ENFILE, + Errno::EMFILE => Self::EMFILE, + Errno::ENOTTY => Self::ENOTTY, + Errno::ETXTBSY => Self::ETXTBSY, + Errno::EFBIG => Self::EFBIG, + Errno::ENOSPC => Self::ENOSPC, + Errno::ESPIPE => Self::ESPIPE, + Errno::EROFS => Self::EROFS, + Errno::EMLINK => Self::EMLINK, + Errno::EPIPE => Self::EPIPE, + Errno::EDOM => Self::EDOM, + Errno::ERANGE => Self::ERANGE, + Errno::EDEADLK => Self::EDEADLK, + Errno::ENAMETOOLONG => Self::ENAMETOOLONG, + Errno::ENOLCK => Self::ENOLCK, + Errno::ENOSYS => Self::ENOSYS, + Errno::ENOTEMPTY => Self::ENOTEMPTY, + Errno::ELOOP => Self::ELOOP, + Errno::ENOMSG => Self::ENOMSG, + Errno::EIDRM => Self::EIDRM, + Errno::ENOLINK => Self::ENOLINK, + Errno::EPROTO => Self::EPROTO, + Errno::EMULTIHOP => Self::EMULTIHOP, + Errno::EBADMSG => Self::EBADMSG, + Errno::EOVERFLOW => Self::EOVERFLOW, + Errno::EILSEQ => Self::EILSEQ, + Errno::ENOTSOCK => Self::ENOTSOCK, + Errno::EDESTADDRREQ => Self::EDESTADDRREQ, + Errno::EMSGSIZE => Self::EMSGSIZE, + Errno::EPROTOTYPE => Self::EPROTOTYPE, + Errno::ENOPROTOOPT => Self::ENOPROTOOPT, + Errno::EPROTONOSUPPORT => Self::EPROTONOSUPPORT, + Errno::EAFNOSUPPORT => Self::EAFNOSUPPORT, + Errno::EADDRINUSE => Self::EADDRINUSE, + Errno::EADDRNOTAVAIL => Self::EADDRNOTAVAIL, + Errno::ENETDOWN => Self::ENETDOWN, + Errno::ENETUNREACH => Self::ENETUNREACH, + Errno::ENETRESET => Self::ENETRESET, + Errno::ECONNABORTED => Self::ECONNABORTED, + Errno::ECONNRESET => Self::ECONNRESET, + Errno::ENOBUFS => Self::ENOBUFS, + Errno::EISCONN => Self::EISCONN, + Errno::ENOTCONN => Self::ENOTCONN, + Errno::ETIMEDOUT => Self::ETIMEDOUT, + Errno::ECONNREFUSED => Self::ECONNREFUSED, + Errno::EHOSTUNREACH => Self::EHOSTUNREACH, + Errno::EALREADY => Self::EALREADY, + Errno::EINPROGRESS => Self::EINPROGRESS, + Errno::ESTALE => Self::ESTALE, + Errno::EDQUOT => Self::EDQUOT, + Errno::ECANCELED => Self::ECANCELED, + Errno::EOWNERDEAD => Self::EOWNERDEAD, + Errno::ENOTRECOVERABLE => Self::ENOTRECOVERABLE, + } } } diff --git a/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs b/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs index cf7c143a41..1ac926cb1f 100644 --- a/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs +++ b/crates/wasi-common/src/sys/unix/hostcalls_impl/fs.rs @@ -175,7 +175,7 @@ pub(crate) fn path_open( Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => { return Err(Error::ELOOP); } - errno => return Err(host_impl::errno_from_nix(errno)), + errno => return Err(errno.into()), } } else { return Err(e.into()); diff --git a/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs b/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs index 34135f59de..30adda29ed 100644 --- a/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs +++ b/crates/wasi-common/src/sys/unix/hostcalls_impl/misc.rs @@ -1,7 +1,6 @@ #![allow(non_camel_case_types)] #![allow(unused_unsafe)] use crate::hostcalls_impl::{ClockEventData, FdEventData}; -use crate::sys::host_impl; use crate::{wasi, Error, Result}; use yanix::clock::{clock_getres, clock_gettime, ClockId}; @@ -92,7 +91,7 @@ pub(crate) fn poll_oneoff( if Errno::last() == Errno::EINTR { continue; } - return Err(host_impl::errno_from_nix(Errno::last())); + return Err(Errno::last().into()); } Ok(ready) => break ready, } diff --git a/crates/wasi-common/src/sys/windows/host_impl.rs b/crates/wasi-common/src/sys/windows/host_impl.rs index 71d7ef0332..ba6717e5e6 100644 --- a/crates/wasi-common/src/sys/windows/host_impl.rs +++ b/crates/wasi-common/src/sys/windows/host_impl.rs @@ -1,41 +1,50 @@ //! WASI host types specific to Windows host. use crate::host::FileType; -use crate::{wasi, Error, Result}; +use crate::{error::FromRawOsError, wasi, Error, Result}; 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 winx::winerror::WinError; -pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> wasi::__wasi_errno_t { - // TODO: implement error mapping between Windows and WASI - use winx::winerror::WinError::*; - match error { - ERROR_SUCCESS => wasi::__WASI_ERRNO_SUCCESS, - ERROR_BAD_ENVIRONMENT => wasi::__WASI_ERRNO_2BIG, - ERROR_FILE_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, - ERROR_PATH_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, - ERROR_TOO_MANY_OPEN_FILES => wasi::__WASI_ERRNO_NFILE, - ERROR_ACCESS_DENIED => wasi::__WASI_ERRNO_ACCES, - ERROR_SHARING_VIOLATION => wasi::__WASI_ERRNO_ACCES, - ERROR_PRIVILEGE_NOT_HELD => wasi::__WASI_ERRNO_NOTCAPABLE, // TODO is this the correct mapping? - ERROR_INVALID_HANDLE => wasi::__WASI_ERRNO_BADF, - ERROR_INVALID_NAME => wasi::__WASI_ERRNO_NOENT, - ERROR_NOT_ENOUGH_MEMORY => wasi::__WASI_ERRNO_NOMEM, - ERROR_OUTOFMEMORY => wasi::__WASI_ERRNO_NOMEM, - ERROR_DIR_NOT_EMPTY => wasi::__WASI_ERRNO_NOTEMPTY, - ERROR_NOT_READY => wasi::__WASI_ERRNO_BUSY, - ERROR_BUSY => wasi::__WASI_ERRNO_BUSY, - ERROR_NOT_SUPPORTED => wasi::__WASI_ERRNO_NOTSUP, - ERROR_FILE_EXISTS => wasi::__WASI_ERRNO_EXIST, - ERROR_BROKEN_PIPE => wasi::__WASI_ERRNO_PIPE, - ERROR_BUFFER_OVERFLOW => wasi::__WASI_ERRNO_NAMETOOLONG, - ERROR_NOT_A_REPARSE_POINT => wasi::__WASI_ERRNO_INVAL, - ERROR_NEGATIVE_SEEK => wasi::__WASI_ERRNO_INVAL, - ERROR_DIRECTORY => wasi::__WASI_ERRNO_NOTDIR, - ERROR_ALREADY_EXISTS => wasi::__WASI_ERRNO_EXIST, - _ => wasi::__WASI_ERRNO_NOTSUP, +impl FromRawOsError for Error { + fn from_raw_os_error(code: i32) -> Self { + Self::from(WinError::from_u32(code as u32)) + } +} + +impl From for Error { + fn from(err: WinError) -> Self { + // TODO: implement error mapping between Windows and WASI + use winx::winerror::WinError::*; + match err { + ERROR_SUCCESS => Self::ESUCCESS, + ERROR_BAD_ENVIRONMENT => Self::E2BIG, + ERROR_FILE_NOT_FOUND => Self::ENOENT, + ERROR_PATH_NOT_FOUND => Self::ENOENT, + ERROR_TOO_MANY_OPEN_FILES => Self::ENFILE, + ERROR_ACCESS_DENIED => Self::EACCES, + ERROR_SHARING_VIOLATION => Self::EACCES, + ERROR_PRIVILEGE_NOT_HELD => Self::ENOTCAPABLE, // TODO is this the correct mapping? + ERROR_INVALID_HANDLE => Self::EBADF, + ERROR_INVALID_NAME => Self::ENOENT, + ERROR_NOT_ENOUGH_MEMORY => Self::ENOMEM, + ERROR_OUTOFMEMORY => Self::ENOMEM, + ERROR_DIR_NOT_EMPTY => Self::ENOTEMPTY, + ERROR_NOT_READY => Self::EBUSY, + ERROR_BUSY => Self::EBUSY, + ERROR_NOT_SUPPORTED => Self::ENOTSUP, + ERROR_FILE_EXISTS => Self::EEXIST, + ERROR_BROKEN_PIPE => Self::EPIPE, + ERROR_BUFFER_OVERFLOW => Self::ENAMETOOLONG, + ERROR_NOT_A_REPARSE_POINT => Self::EINVAL, + ERROR_NEGATIVE_SEEK => Self::EINVAL, + ERROR_DIRECTORY => Self::ENOTDIR, + ERROR_ALREADY_EXISTS => Self::EEXIST, + _ => Self::ENOTSUP, + } } }