[wasi-common] Log string representation of WASI errno at the trace level (#760)

* Log str repr of WASI errno at trace level

This commit refactors `Error` enum, and adds logging of the WASI
errno string representation at the trace level. Now, when tracing
WASI syscalls, we will be greeted with a nicely formatted errno
value after each syscall:

```
path_open(...)
     | *fd=5
     | errno=ESUCCESS
```

This commit gets rid of `errno_from_nix`, `errno_from_win` and
`errno_from_host` helper fns in favour of direct `From` implementations
for the relevant types such as `yanix::Errno` and `winx::winerror::WinError`.
`errno_from_host` is replaced by a trait `FromRawOsError`.

* Back port changes to snapshot0

* Fix indentation in logs
This commit is contained in:
Jakub Konka
2020-01-16 21:52:04 +01:00
committed by GitHub
parent b2bfb98f1f
commit e474a9e822
23 changed files with 334 additions and 322 deletions

View File

@@ -104,14 +104,11 @@ pub enum Error {
#[cfg(unix)] #[cfg(unix)]
#[error("Yanix error: {0}")] #[error("Yanix error: {0}")]
Yanix(#[from] yanix::YanixError), Yanix(#[from] yanix::YanixError),
#[cfg(windows)]
#[error("Winx error: {0}")]
Winx(#[from] winx::winerror::WinError),
} }
impl From<TryFromIntError> for Error { impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self { fn from(_: TryFromIntError) -> Self {
Self::Wasi(WasiError::EOVERFLOW) Self::EOVERFLOW
} }
} }
@@ -123,41 +120,49 @@ impl From<Infallible> for Error {
impl From<str::Utf8Error> for Error { impl From<str::Utf8Error> for Error {
fn from(_: str::Utf8Error) -> Self { fn from(_: str::Utf8Error) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl From<ffi::NulError> for Error { impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self { fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl From<&ffi::NulError> for Error { impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self { fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl Error { impl Error {
pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t { pub(crate) fn as_wasi_error(&self) -> WasiError {
match self { match self {
Self::Wasi(no) => no.as_raw_errno(), Self::Wasi(err) => *err,
Self::Io(e) => errno_from_ioerror(e.to_owned()), 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)] #[cfg(unix)]
Self::Yanix(err) => { Self::Yanix(err) => {
use yanix::YanixError::*; use yanix::YanixError::*;
let err = match err { let err: Self = match err {
Errno(errno) => crate::sys::host_impl::errno_from_nix(*errno), Errno(errno) => (*errno).into(),
NulError(err) => err.into(), NulError(err) => err.into(),
TryFromIntError(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 ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS);
pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG); pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG);
pub const EACCES: Self = Error::Wasi(WasiError::EACCES); pub const EACCES: Self = Error::Wasi(WasiError::EACCES);
@@ -237,12 +242,6 @@ impl Error {
pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE); pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE);
} }
fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t { pub(crate) trait FromRawOsError {
match e.raw_os_error() { fn from_raw_os_error(code: i32) -> Self;
Some(code) => crate::sys::errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
wasi::__WASI_ERRNO_IO
}
}
} }

View File

@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::ctx::WasiCtx; use crate::ctx::WasiCtx;
use crate::{hostcalls_impl, wasi, wasi32}; use crate::{wasi, wasi32};
hostcalls! { hostcalls! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t; pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;

View File

@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::ctx::WasiCtx; use crate::ctx::WasiCtx;
use crate::{hostcalls_impl, wasi, wasi32}; use crate::{wasi, wasi32};
use log::trace; use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen; use wasi_common_cbindgen::wasi_common_cbindgen;

View File

@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::sys::hostcalls_impl::fs_helpers::*; 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::fs::File;
use std::path::{Component, Path}; use std::path::{Component, Path};
@@ -117,10 +117,10 @@ pub(crate) fn path_get(
dir_stack.push(new_dir); dir_stack.push(new_dir);
} }
Err(e) => { Err(e) => {
match e.as_wasi_errno() { match e.as_wasi_error() {
wasi::__WASI_ERRNO_LOOP WasiError::ELOOP
| wasi::__WASI_ERRNO_MLINK | WasiError::EMLINK
| wasi::__WASI_ERRNO_NOTDIR => | WasiError::ENOTDIR =>
// Check to see if it was a symlink. Linux indicates // Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag. // this with ENOTDIR because of the O_DIRECTORY flag.
{ {
@@ -179,12 +179,12 @@ pub(crate) fn path_get(
continue; continue;
} }
Err(e) => { Err(e) => {
if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL if e.as_wasi_error() != WasiError::EINVAL
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT && e.as_wasi_error() != WasiError::ENOENT
// this handles the cases when trying to link to // this handles the cases when trying to link to
// a destination that already exists, and the target // a destination that already exists, and the target
// path contains a slash // path contains a slash
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOTDIR && e.as_wasi_error() != WasiError::ENOTDIR
{ {
return Err(e); return Err(e);
} }

View File

@@ -261,7 +261,7 @@ pub(crate) fn poll_oneoff(
let event = wasi::__wasi_event_t { let event = wasi::__wasi_event_t {
userdata: subscription.userdata, userdata: subscription.userdata,
r#type, r#type,
error: err.as_wasi_errno(), error: err.as_wasi_error().as_raw_errno(),
u: wasi::__wasi_event_u_t { u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t { fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0, nbytes: 0,

View File

@@ -2,12 +2,12 @@ macro_rules! hostcalls {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($( ($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen] #[wasi_common_cbindgen::wasi_common_cbindgen]
pub unsafe fn $name($($arg: $ty,)*) -> $ret { pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = match hostcalls_impl::$name($($arg,)*) { let ret = crate::hostcalls_impl::$name($($arg,)*)
Ok(()) => wasi::__WASI_ERRNO_SUCCESS, .err()
Err(e) => e.as_wasi_errno(), .unwrap_or(crate::Error::ESUCCESS)
}; .as_wasi_error();
log::trace!(" | errno={}", ret);
ret ret.as_raw_errno()
} }
)*) )*)
} }
@@ -18,12 +18,12 @@ macro_rules! hostcalls_old {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($( ($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen_old] #[wasi_common_cbindgen::wasi_common_cbindgen_old]
pub unsafe fn $name($($arg: $ty,)*) -> $ret { pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = match hostcalls_impl::$name($($arg,)*) { let ret = crate::old::snapshot_0::hostcalls_impl::$name($($arg,)*)
Ok(()) => wasi::__WASI_ERRNO_SUCCESS, .err()
Err(e) => e.as_wasi_errno(), .unwrap_or(crate::old::snapshot_0::Error::ESUCCESS)
}; .as_wasi_error();
log::trace!(" | errno={}", ret);
ret ret.as_raw_errno()
} }
)*) )*)
} }

View File

@@ -104,14 +104,11 @@ pub enum Error {
#[cfg(unix)] #[cfg(unix)]
#[error("Yanix error: {0}")] #[error("Yanix error: {0}")]
Yanix(#[from] yanix::YanixError), Yanix(#[from] yanix::YanixError),
#[cfg(windows)]
#[error("Winx error: {0}")]
Winx(#[from] winx::winerror::WinError),
} }
impl From<TryFromIntError> for Error { impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self { fn from(_: TryFromIntError) -> Self {
Self::Wasi(WasiError::EOVERFLOW) Self::EOVERFLOW
} }
} }
@@ -123,41 +120,49 @@ impl From<Infallible> for Error {
impl From<str::Utf8Error> for Error { impl From<str::Utf8Error> for Error {
fn from(_: str::Utf8Error) -> Self { fn from(_: str::Utf8Error) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl From<ffi::NulError> for Error { impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self { fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl From<&ffi::NulError> for Error { impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self { fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ) Self::EILSEQ
} }
} }
impl Error { impl Error {
pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t { pub(crate) fn as_wasi_error(&self) -> WasiError {
match self { match self {
Self::Wasi(no) => no.as_raw_errno(), Self::Wasi(err) => *err,
Self::Io(e) => errno_from_ioerror(e.to_owned()), 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)] #[cfg(unix)]
Self::Yanix(err) => { Self::Yanix(err) => {
use yanix::YanixError::*; use yanix::YanixError::*;
let err = match err { let err: Self = match err {
Errno(errno) => crate::old::snapshot_0::sys::host_impl::errno_from_nix(*errno), Errno(errno) => (*errno).into(),
NulError(err) => err.into(), NulError(err) => err.into(),
TryFromIntError(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 ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS);
pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG); pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG);
pub const EACCES: Self = Error::Wasi(WasiError::EACCES); pub const EACCES: Self = Error::Wasi(WasiError::EACCES);
@@ -237,12 +242,6 @@ impl Error {
pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE); pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE);
} }
fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t { pub(crate) trait FromRawOsError {
match e.raw_os_error() { fn from_raw_os_error(code: i32) -> Self;
Some(code) => crate::old::snapshot_0::sys::errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
wasi::__WASI_ERRNO_IO
}
}
} }

View File

@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx; 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! { hostcalls_old! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t; pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;

View File

@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx; 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 log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen_old; use wasi_common_cbindgen::wasi_common_cbindgen_old;

View File

@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::old::snapshot_0::sys::host_impl; use crate::old::snapshot_0::sys::host_impl;
use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::*; 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::fs::File;
use std::path::{Component, Path}; use std::path::{Component, Path};
@@ -117,10 +117,10 @@ pub(crate) fn path_get(
dir_stack.push(new_dir); dir_stack.push(new_dir);
} }
Err(e) => { Err(e) => {
match e.as_wasi_errno() { match e.as_wasi_error() {
wasi::__WASI_ERRNO_LOOP WasiError::ELOOP
| wasi::__WASI_ERRNO_MLINK | WasiError::EMLINK
| wasi::__WASI_ERRNO_NOTDIR => | WasiError::ENOTDIR =>
// Check to see if it was a symlink. Linux indicates // Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag. // this with ENOTDIR because of the O_DIRECTORY flag.
{ {
@@ -179,12 +179,12 @@ pub(crate) fn path_get(
continue; continue;
} }
Err(e) => { Err(e) => {
if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL if e.as_wasi_error() != WasiError::EINVAL
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT && e.as_wasi_error() != WasiError::ENOENT
// this handles the cases when trying to link to // this handles the cases when trying to link to
// a destination that already exists, and the target // a destination that already exists, and the target
// path contains a slash // path contains a slash
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOTDIR && e.as_wasi_error() != WasiError::ENOTDIR
{ {
return Err(e); return Err(e);
} }

View File

@@ -258,7 +258,7 @@ pub(crate) fn poll_oneoff(
let event = wasi::__wasi_event_t { let event = wasi::__wasi_event_t {
userdata: subscription.userdata, userdata: subscription.userdata,
r#type, r#type,
error: err.as_wasi_errno(), error: err.as_wasi_error().as_raw_errno(),
u: wasi::__wasi_event_u_t { u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t { fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0, nbytes: 0,

View File

@@ -1,21 +1,12 @@
use crate::old::snapshot_0::wasi;
use cfg_if::cfg_if; use cfg_if::cfg_if;
cfg_if! { cfg_if! {
if #[cfg(unix)] { if #[cfg(unix)] {
mod unix; mod unix;
pub(crate) use self::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)] { } else if #[cfg(windows)] {
mod windows; mod windows;
pub(crate) use self::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 { } else {
compile_error!("wasi-common doesn't compile for this platform yet"); compile_error!("wasi-common doesn't compile for this platform yet");
} }

View File

@@ -1,5 +1,4 @@
use crate::old::snapshot_0::hostcalls_impl::PathGet; use crate::old::snapshot_0::hostcalls_impl::PathGet;
use crate::old::snapshot_0::sys::host_impl;
use crate::old::snapshot_0::{Error, Result}; use crate::old::snapshot_0::{Error, Result};
use std::os::unix::prelude::AsRawFd; use std::os::unix::prelude::AsRawFd;
@@ -80,7 +79,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
Err(Error::ENOTDIR) Err(Error::ENOTDIR)
} }
} }
x => Err(host_impl::errno_from_nix(x)), x => Err(x.into()),
} }
} else { } else {
Err(err.into()) Err(err.into())
@@ -132,7 +131,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul
Err(Error::ENOENT) Err(Error::ENOENT)
} }
} }
x => Err(host_impl::errno_from_nix(x)), x => Err(x.into()),
} }
} else { } else {
Err(err.into()) Err(err.into())

View File

@@ -3,7 +3,9 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use crate::old::snapshot_0::host::FileType; use crate::old::snapshot_0::host::FileType;
use crate::old::snapshot_0::{helpers, 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::ffi::OsStr;
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use yanix::{ use yanix::{
@@ -13,82 +15,90 @@ use yanix::{
pub(crate) use sys_impl::host_impl::*; pub(crate) use sys_impl::host_impl::*;
pub(crate) fn errno_from_nix(errno: Errno) -> Error { impl FromRawOsError for Error {
fn from_raw_os_error(code: i32) -> Self {
Self::from(Errno::from_i32(code))
}
}
impl From<Errno> for Error {
fn from(errno: Errno) -> Self {
match errno { match errno {
Errno::EPERM => Error::EPERM, Errno::EPERM => Self::EPERM,
Errno::ENOENT => Error::ENOENT, Errno::ENOENT => Self::ENOENT,
Errno::ESRCH => Error::ESRCH, Errno::ESRCH => Self::ESRCH,
Errno::EINTR => Error::EINTR, Errno::EINTR => Self::EINTR,
Errno::EIO => Error::EIO, Errno::EIO => Self::EIO,
Errno::ENXIO => Error::ENXIO, Errno::ENXIO => Self::ENXIO,
Errno::E2BIG => Error::E2BIG, Errno::E2BIG => Self::E2BIG,
Errno::ENOEXEC => Error::ENOEXEC, Errno::ENOEXEC => Self::ENOEXEC,
Errno::EBADF => Error::EBADF, Errno::EBADF => Self::EBADF,
Errno::ECHILD => Error::ECHILD, Errno::ECHILD => Self::ECHILD,
Errno::EAGAIN => Error::EAGAIN, Errno::EAGAIN => Self::EAGAIN,
Errno::ENOMEM => Error::ENOMEM, Errno::ENOMEM => Self::ENOMEM,
Errno::EACCES => Error::EACCES, Errno::EACCES => Self::EACCES,
Errno::EFAULT => Error::EFAULT, Errno::EFAULT => Self::EFAULT,
Errno::EBUSY => Error::EBUSY, Errno::EBUSY => Self::EBUSY,
Errno::EEXIST => Error::EEXIST, Errno::EEXIST => Self::EEXIST,
Errno::EXDEV => Error::EXDEV, Errno::EXDEV => Self::EXDEV,
Errno::ENODEV => Error::ENODEV, Errno::ENODEV => Self::ENODEV,
Errno::ENOTDIR => Error::ENOTDIR, Errno::ENOTDIR => Self::ENOTDIR,
Errno::EISDIR => Error::EISDIR, Errno::EISDIR => Self::EISDIR,
Errno::EINVAL => Error::EINVAL, Errno::EINVAL => Self::EINVAL,
Errno::ENFILE => Error::ENFILE, Errno::ENFILE => Self::ENFILE,
Errno::EMFILE => Error::EMFILE, Errno::EMFILE => Self::EMFILE,
Errno::ENOTTY => Error::ENOTTY, Errno::ENOTTY => Self::ENOTTY,
Errno::ETXTBSY => Error::ETXTBSY, Errno::ETXTBSY => Self::ETXTBSY,
Errno::EFBIG => Error::EFBIG, Errno::EFBIG => Self::EFBIG,
Errno::ENOSPC => Error::ENOSPC, Errno::ENOSPC => Self::ENOSPC,
Errno::ESPIPE => Error::ESPIPE, Errno::ESPIPE => Self::ESPIPE,
Errno::EROFS => Error::EROFS, Errno::EROFS => Self::EROFS,
Errno::EMLINK => Error::EMLINK, Errno::EMLINK => Self::EMLINK,
Errno::EPIPE => Error::EPIPE, Errno::EPIPE => Self::EPIPE,
Errno::EDOM => Error::EDOM, Errno::EDOM => Self::EDOM,
Errno::ERANGE => Error::ERANGE, Errno::ERANGE => Self::ERANGE,
Errno::EDEADLK => Error::EDEADLK, Errno::EDEADLK => Self::EDEADLK,
Errno::ENAMETOOLONG => Error::ENAMETOOLONG, Errno::ENAMETOOLONG => Self::ENAMETOOLONG,
Errno::ENOLCK => Error::ENOLCK, Errno::ENOLCK => Self::ENOLCK,
Errno::ENOSYS => Error::ENOSYS, Errno::ENOSYS => Self::ENOSYS,
Errno::ENOTEMPTY => Error::ENOTEMPTY, Errno::ENOTEMPTY => Self::ENOTEMPTY,
Errno::ELOOP => Error::ELOOP, Errno::ELOOP => Self::ELOOP,
Errno::ENOMSG => Error::ENOMSG, Errno::ENOMSG => Self::ENOMSG,
Errno::EIDRM => Error::EIDRM, Errno::EIDRM => Self::EIDRM,
Errno::ENOLINK => Error::ENOLINK, Errno::ENOLINK => Self::ENOLINK,
Errno::EPROTO => Error::EPROTO, Errno::EPROTO => Self::EPROTO,
Errno::EMULTIHOP => Error::EMULTIHOP, Errno::EMULTIHOP => Self::EMULTIHOP,
Errno::EBADMSG => Error::EBADMSG, Errno::EBADMSG => Self::EBADMSG,
Errno::EOVERFLOW => Error::EOVERFLOW, Errno::EOVERFLOW => Self::EOVERFLOW,
Errno::EILSEQ => Error::EILSEQ, Errno::EILSEQ => Self::EILSEQ,
Errno::ENOTSOCK => Error::ENOTSOCK, Errno::ENOTSOCK => Self::ENOTSOCK,
Errno::EDESTADDRREQ => Error::EDESTADDRREQ, Errno::EDESTADDRREQ => Self::EDESTADDRREQ,
Errno::EMSGSIZE => Error::EMSGSIZE, Errno::EMSGSIZE => Self::EMSGSIZE,
Errno::EPROTOTYPE => Error::EPROTOTYPE, Errno::EPROTOTYPE => Self::EPROTOTYPE,
Errno::ENOPROTOOPT => Error::ENOPROTOOPT, Errno::ENOPROTOOPT => Self::ENOPROTOOPT,
Errno::EPROTONOSUPPORT => Error::EPROTONOSUPPORT, Errno::EPROTONOSUPPORT => Self::EPROTONOSUPPORT,
Errno::EAFNOSUPPORT => Error::EAFNOSUPPORT, Errno::EAFNOSUPPORT => Self::EAFNOSUPPORT,
Errno::EADDRINUSE => Error::EADDRINUSE, Errno::EADDRINUSE => Self::EADDRINUSE,
Errno::EADDRNOTAVAIL => Error::EADDRNOTAVAIL, Errno::EADDRNOTAVAIL => Self::EADDRNOTAVAIL,
Errno::ENETDOWN => Error::ENETDOWN, Errno::ENETDOWN => Self::ENETDOWN,
Errno::ENETUNREACH => Error::ENETUNREACH, Errno::ENETUNREACH => Self::ENETUNREACH,
Errno::ENETRESET => Error::ENETRESET, Errno::ENETRESET => Self::ENETRESET,
Errno::ECONNABORTED => Error::ECONNABORTED, Errno::ECONNABORTED => Self::ECONNABORTED,
Errno::ECONNRESET => Error::ECONNRESET, Errno::ECONNRESET => Self::ECONNRESET,
Errno::ENOBUFS => Error::ENOBUFS, Errno::ENOBUFS => Self::ENOBUFS,
Errno::EISCONN => Error::EISCONN, Errno::EISCONN => Self::EISCONN,
Errno::ENOTCONN => Error::ENOTCONN, Errno::ENOTCONN => Self::ENOTCONN,
Errno::ETIMEDOUT => Error::ETIMEDOUT, Errno::ETIMEDOUT => Self::ETIMEDOUT,
Errno::ECONNREFUSED => Error::ECONNREFUSED, Errno::ECONNREFUSED => Self::ECONNREFUSED,
Errno::EHOSTUNREACH => Error::EHOSTUNREACH, Errno::EHOSTUNREACH => Self::EHOSTUNREACH,
Errno::EALREADY => Error::EALREADY, Errno::EALREADY => Self::EALREADY,
Errno::EINPROGRESS => Error::EINPROGRESS, Errno::EINPROGRESS => Self::EINPROGRESS,
Errno::ESTALE => Error::ESTALE, Errno::ESTALE => Self::ESTALE,
Errno::EDQUOT => Error::EDQUOT, Errno::EDQUOT => Self::EDQUOT,
Errno::ECANCELED => Error::ECANCELED, Errno::ECANCELED => Self::ECANCELED,
Errno::EOWNERDEAD => Error::EOWNERDEAD, Errno::EOWNERDEAD => Self::EOWNERDEAD,
Errno::ENOTRECOVERABLE => Error::ENOTRECOVERABLE, Errno::ENOTRECOVERABLE => Self::ENOTRECOVERABLE,
}
} }
} }

View File

@@ -170,7 +170,7 @@ pub(crate) fn path_open(
Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => { Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
return Err(Error::ELOOP); return Err(Error::ELOOP);
} }
errno => return Err(host_impl::errno_from_nix(errno)), errno => return Err(errno.into()),
} }
} else { } else {
return Err(e.into()); return Err(e.into());

View File

@@ -1,7 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use crate::old::snapshot_0::hostcalls_impl::{ClockEventData, FdEventData}; 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 crate::old::snapshot_0::{wasi, Error, Result};
use yanix::clock::{clock_getres, clock_gettime, ClockId}; use yanix::clock::{clock_getres, clock_gettime, ClockId};
@@ -92,7 +91,7 @@ pub(crate) fn poll_oneoff(
if Errno::last() == Errno::EINTR { if Errno::last() == Errno::EINTR {
continue; continue;
} }
return Err(host_impl::errno_from_nix(Errno::last())); return Err(Errno::last().into());
} }
Ok(ready) => break ready, Ok(ready) => break ready,
} }

View File

@@ -3,7 +3,7 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(unused)] #![allow(unused)]
use crate::old::snapshot_0::host::FileType; 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::convert::TryInto;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::OpenOptions; use std::fs::OpenOptions;
@@ -13,35 +13,44 @@ use std::os::windows::ffi::OsStrExt;
use std::os::windows::fs::OpenOptionsExt; use std::os::windows::fs::OpenOptionsExt;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use winx::file::{AccessMode, Attributes, CreationDisposition, Flags}; 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 { impl FromRawOsError for Error {
fn from_raw_os_error(code: i32) -> Self {
Self::from(WinError::from_u32(code as u32))
}
}
impl From<WinError> for Error {
fn from(err: WinError) -> Self {
// TODO: implement error mapping between Windows and WASI // TODO: implement error mapping between Windows and WASI
use winx::winerror::WinError::*; use winx::winerror::WinError::*;
match error { match err {
ERROR_SUCCESS => wasi::__WASI_ERRNO_SUCCESS, ERROR_SUCCESS => Self::ESUCCESS,
ERROR_BAD_ENVIRONMENT => wasi::__WASI_ERRNO_2BIG, ERROR_BAD_ENVIRONMENT => Self::E2BIG,
ERROR_FILE_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, ERROR_FILE_NOT_FOUND => Self::ENOENT,
ERROR_PATH_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, ERROR_PATH_NOT_FOUND => Self::ENOENT,
ERROR_TOO_MANY_OPEN_FILES => wasi::__WASI_ERRNO_NFILE, ERROR_TOO_MANY_OPEN_FILES => Self::ENFILE,
ERROR_ACCESS_DENIED => wasi::__WASI_ERRNO_ACCES, ERROR_ACCESS_DENIED => Self::EACCES,
ERROR_SHARING_VIOLATION => wasi::__WASI_ERRNO_ACCES, ERROR_SHARING_VIOLATION => Self::EACCES,
ERROR_PRIVILEGE_NOT_HELD => wasi::__WASI_ERRNO_NOTCAPABLE, // TODO is this the correct mapping? ERROR_PRIVILEGE_NOT_HELD => Self::ENOTCAPABLE, // TODO is this the correct mapping?
ERROR_INVALID_HANDLE => wasi::__WASI_ERRNO_BADF, ERROR_INVALID_HANDLE => Self::EBADF,
ERROR_INVALID_NAME => wasi::__WASI_ERRNO_NOENT, ERROR_INVALID_NAME => Self::ENOENT,
ERROR_NOT_ENOUGH_MEMORY => wasi::__WASI_ERRNO_NOMEM, ERROR_NOT_ENOUGH_MEMORY => Self::ENOMEM,
ERROR_OUTOFMEMORY => wasi::__WASI_ERRNO_NOMEM, ERROR_OUTOFMEMORY => Self::ENOMEM,
ERROR_DIR_NOT_EMPTY => wasi::__WASI_ERRNO_NOTEMPTY, ERROR_DIR_NOT_EMPTY => Self::ENOTEMPTY,
ERROR_NOT_READY => wasi::__WASI_ERRNO_BUSY, ERROR_NOT_READY => Self::EBUSY,
ERROR_BUSY => wasi::__WASI_ERRNO_BUSY, ERROR_BUSY => Self::EBUSY,
ERROR_NOT_SUPPORTED => wasi::__WASI_ERRNO_NOTSUP, ERROR_NOT_SUPPORTED => Self::ENOTSUP,
ERROR_FILE_EXISTS => wasi::__WASI_ERRNO_EXIST, ERROR_FILE_EXISTS => Self::EEXIST,
ERROR_BROKEN_PIPE => wasi::__WASI_ERRNO_PIPE, ERROR_BROKEN_PIPE => Self::EPIPE,
ERROR_BUFFER_OVERFLOW => wasi::__WASI_ERRNO_NAMETOOLONG, ERROR_BUFFER_OVERFLOW => Self::ENAMETOOLONG,
ERROR_NOT_A_REPARSE_POINT => wasi::__WASI_ERRNO_INVAL, ERROR_NOT_A_REPARSE_POINT => Self::EINVAL,
ERROR_NEGATIVE_SEEK => wasi::__WASI_ERRNO_INVAL, ERROR_NEGATIVE_SEEK => Self::EINVAL,
ERROR_DIRECTORY => wasi::__WASI_ERRNO_NOTDIR, ERROR_DIRECTORY => Self::ENOTDIR,
ERROR_ALREADY_EXISTS => wasi::__WASI_ERRNO_EXIST, ERROR_ALREADY_EXISTS => Self::EEXIST,
_ => wasi::__WASI_ERRNO_NOTSUP, _ => Self::ENOTSUP,
}
} }
} }

View File

@@ -1,4 +1,3 @@
use crate::wasi;
use cfg_if::cfg_if; use cfg_if::cfg_if;
cfg_if! { cfg_if! {
@@ -6,18 +5,10 @@ cfg_if! {
mod unix; mod unix;
pub(crate) use unix::*; pub(crate) use unix::*;
pub use unix::preopen_dir; 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)] { } else if #[cfg(windows)] {
mod windows; mod windows;
pub(crate) use windows::*; pub(crate) use windows::*;
pub use windows::preopen_dir; 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 { } else {
compile_error!("wasi-common doesn't compile for this platform yet"); compile_error!("wasi-common doesn't compile for this platform yet");
} }

View File

@@ -1,5 +1,4 @@
use crate::hostcalls_impl::PathGet; use crate::hostcalls_impl::PathGet;
use crate::sys::host_impl;
use crate::{Error, Result}; use crate::{Error, Result};
use std::os::unix::prelude::AsRawFd; use std::os::unix::prelude::AsRawFd;
@@ -80,7 +79,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
Err(Error::ENOTDIR) Err(Error::ENOTDIR)
} }
} }
x => Err(host_impl::errno_from_nix(x)), x => Err(x.into()),
} }
} else { } else {
Err(err.into()) Err(err.into())
@@ -132,7 +131,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul
Err(Error::ENOENT) Err(Error::ENOENT)
} }
} }
x => Err(host_impl::errno_from_nix(x)), x => Err(x.into()),
} }
} else { } else {
Err(err.into()) Err(err.into())

View File

@@ -3,7 +3,7 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(dead_code)] #![allow(dead_code)]
use crate::host::FileType; use crate::host::FileType;
use crate::{helpers, 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::ffi::OsStr;
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use yanix::{ use yanix::{
@@ -13,82 +13,90 @@ use yanix::{
pub(crate) use sys_impl::host_impl::*; pub(crate) use sys_impl::host_impl::*;
pub(crate) fn errno_from_nix(errno: Errno) -> Error { impl FromRawOsError for Error {
fn from_raw_os_error(code: i32) -> Self {
Self::from(Errno::from_i32(code))
}
}
impl From<Errno> for Error {
fn from(errno: Errno) -> Self {
match errno { match errno {
Errno::EPERM => Error::EPERM, Errno::EPERM => Self::EPERM,
Errno::ENOENT => Error::ENOENT, Errno::ENOENT => Self::ENOENT,
Errno::ESRCH => Error::ESRCH, Errno::ESRCH => Self::ESRCH,
Errno::EINTR => Error::EINTR, Errno::EINTR => Self::EINTR,
Errno::EIO => Error::EIO, Errno::EIO => Self::EIO,
Errno::ENXIO => Error::ENXIO, Errno::ENXIO => Self::ENXIO,
Errno::E2BIG => Error::E2BIG, Errno::E2BIG => Self::E2BIG,
Errno::ENOEXEC => Error::ENOEXEC, Errno::ENOEXEC => Self::ENOEXEC,
Errno::EBADF => Error::EBADF, Errno::EBADF => Self::EBADF,
Errno::ECHILD => Error::ECHILD, Errno::ECHILD => Self::ECHILD,
Errno::EAGAIN => Error::EAGAIN, Errno::EAGAIN => Self::EAGAIN,
Errno::ENOMEM => Error::ENOMEM, Errno::ENOMEM => Self::ENOMEM,
Errno::EACCES => Error::EACCES, Errno::EACCES => Self::EACCES,
Errno::EFAULT => Error::EFAULT, Errno::EFAULT => Self::EFAULT,
Errno::EBUSY => Error::EBUSY, Errno::EBUSY => Self::EBUSY,
Errno::EEXIST => Error::EEXIST, Errno::EEXIST => Self::EEXIST,
Errno::EXDEV => Error::EXDEV, Errno::EXDEV => Self::EXDEV,
Errno::ENODEV => Error::ENODEV, Errno::ENODEV => Self::ENODEV,
Errno::ENOTDIR => Error::ENOTDIR, Errno::ENOTDIR => Self::ENOTDIR,
Errno::EISDIR => Error::EISDIR, Errno::EISDIR => Self::EISDIR,
Errno::EINVAL => Error::EINVAL, Errno::EINVAL => Self::EINVAL,
Errno::ENFILE => Error::ENFILE, Errno::ENFILE => Self::ENFILE,
Errno::EMFILE => Error::EMFILE, Errno::EMFILE => Self::EMFILE,
Errno::ENOTTY => Error::ENOTTY, Errno::ENOTTY => Self::ENOTTY,
Errno::ETXTBSY => Error::ETXTBSY, Errno::ETXTBSY => Self::ETXTBSY,
Errno::EFBIG => Error::EFBIG, Errno::EFBIG => Self::EFBIG,
Errno::ENOSPC => Error::ENOSPC, Errno::ENOSPC => Self::ENOSPC,
Errno::ESPIPE => Error::ESPIPE, Errno::ESPIPE => Self::ESPIPE,
Errno::EROFS => Error::EROFS, Errno::EROFS => Self::EROFS,
Errno::EMLINK => Error::EMLINK, Errno::EMLINK => Self::EMLINK,
Errno::EPIPE => Error::EPIPE, Errno::EPIPE => Self::EPIPE,
Errno::EDOM => Error::EDOM, Errno::EDOM => Self::EDOM,
Errno::ERANGE => Error::ERANGE, Errno::ERANGE => Self::ERANGE,
Errno::EDEADLK => Error::EDEADLK, Errno::EDEADLK => Self::EDEADLK,
Errno::ENAMETOOLONG => Error::ENAMETOOLONG, Errno::ENAMETOOLONG => Self::ENAMETOOLONG,
Errno::ENOLCK => Error::ENOLCK, Errno::ENOLCK => Self::ENOLCK,
Errno::ENOSYS => Error::ENOSYS, Errno::ENOSYS => Self::ENOSYS,
Errno::ENOTEMPTY => Error::ENOTEMPTY, Errno::ENOTEMPTY => Self::ENOTEMPTY,
Errno::ELOOP => Error::ELOOP, Errno::ELOOP => Self::ELOOP,
Errno::ENOMSG => Error::ENOMSG, Errno::ENOMSG => Self::ENOMSG,
Errno::EIDRM => Error::EIDRM, Errno::EIDRM => Self::EIDRM,
Errno::ENOLINK => Error::ENOLINK, Errno::ENOLINK => Self::ENOLINK,
Errno::EPROTO => Error::EPROTO, Errno::EPROTO => Self::EPROTO,
Errno::EMULTIHOP => Error::EMULTIHOP, Errno::EMULTIHOP => Self::EMULTIHOP,
Errno::EBADMSG => Error::EBADMSG, Errno::EBADMSG => Self::EBADMSG,
Errno::EOVERFLOW => Error::EOVERFLOW, Errno::EOVERFLOW => Self::EOVERFLOW,
Errno::EILSEQ => Error::EILSEQ, Errno::EILSEQ => Self::EILSEQ,
Errno::ENOTSOCK => Error::ENOTSOCK, Errno::ENOTSOCK => Self::ENOTSOCK,
Errno::EDESTADDRREQ => Error::EDESTADDRREQ, Errno::EDESTADDRREQ => Self::EDESTADDRREQ,
Errno::EMSGSIZE => Error::EMSGSIZE, Errno::EMSGSIZE => Self::EMSGSIZE,
Errno::EPROTOTYPE => Error::EPROTOTYPE, Errno::EPROTOTYPE => Self::EPROTOTYPE,
Errno::ENOPROTOOPT => Error::ENOPROTOOPT, Errno::ENOPROTOOPT => Self::ENOPROTOOPT,
Errno::EPROTONOSUPPORT => Error::EPROTONOSUPPORT, Errno::EPROTONOSUPPORT => Self::EPROTONOSUPPORT,
Errno::EAFNOSUPPORT => Error::EAFNOSUPPORT, Errno::EAFNOSUPPORT => Self::EAFNOSUPPORT,
Errno::EADDRINUSE => Error::EADDRINUSE, Errno::EADDRINUSE => Self::EADDRINUSE,
Errno::EADDRNOTAVAIL => Error::EADDRNOTAVAIL, Errno::EADDRNOTAVAIL => Self::EADDRNOTAVAIL,
Errno::ENETDOWN => Error::ENETDOWN, Errno::ENETDOWN => Self::ENETDOWN,
Errno::ENETUNREACH => Error::ENETUNREACH, Errno::ENETUNREACH => Self::ENETUNREACH,
Errno::ENETRESET => Error::ENETRESET, Errno::ENETRESET => Self::ENETRESET,
Errno::ECONNABORTED => Error::ECONNABORTED, Errno::ECONNABORTED => Self::ECONNABORTED,
Errno::ECONNRESET => Error::ECONNRESET, Errno::ECONNRESET => Self::ECONNRESET,
Errno::ENOBUFS => Error::ENOBUFS, Errno::ENOBUFS => Self::ENOBUFS,
Errno::EISCONN => Error::EISCONN, Errno::EISCONN => Self::EISCONN,
Errno::ENOTCONN => Error::ENOTCONN, Errno::ENOTCONN => Self::ENOTCONN,
Errno::ETIMEDOUT => Error::ETIMEDOUT, Errno::ETIMEDOUT => Self::ETIMEDOUT,
Errno::ECONNREFUSED => Error::ECONNREFUSED, Errno::ECONNREFUSED => Self::ECONNREFUSED,
Errno::EHOSTUNREACH => Error::EHOSTUNREACH, Errno::EHOSTUNREACH => Self::EHOSTUNREACH,
Errno::EALREADY => Error::EALREADY, Errno::EALREADY => Self::EALREADY,
Errno::EINPROGRESS => Error::EINPROGRESS, Errno::EINPROGRESS => Self::EINPROGRESS,
Errno::ESTALE => Error::ESTALE, Errno::ESTALE => Self::ESTALE,
Errno::EDQUOT => Error::EDQUOT, Errno::EDQUOT => Self::EDQUOT,
Errno::ECANCELED => Error::ECANCELED, Errno::ECANCELED => Self::ECANCELED,
Errno::EOWNERDEAD => Error::EOWNERDEAD, Errno::EOWNERDEAD => Self::EOWNERDEAD,
Errno::ENOTRECOVERABLE => Error::ENOTRECOVERABLE, Errno::ENOTRECOVERABLE => Self::ENOTRECOVERABLE,
}
} }
} }

View File

@@ -175,7 +175,7 @@ pub(crate) fn path_open(
Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => { Errno::EMLINK if !(nix_all_oflags & OFlag::NOFOLLOW).is_empty() => {
return Err(Error::ELOOP); return Err(Error::ELOOP);
} }
errno => return Err(host_impl::errno_from_nix(errno)), errno => return Err(errno.into()),
} }
} else { } else {
return Err(e.into()); return Err(e.into());

View File

@@ -1,7 +1,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(unused_unsafe)] #![allow(unused_unsafe)]
use crate::hostcalls_impl::{ClockEventData, FdEventData}; use crate::hostcalls_impl::{ClockEventData, FdEventData};
use crate::sys::host_impl;
use crate::{wasi, Error, Result}; use crate::{wasi, Error, Result};
use yanix::clock::{clock_getres, clock_gettime, ClockId}; use yanix::clock::{clock_getres, clock_gettime, ClockId};
@@ -92,7 +91,7 @@ pub(crate) fn poll_oneoff(
if Errno::last() == Errno::EINTR { if Errno::last() == Errno::EINTR {
continue; continue;
} }
return Err(host_impl::errno_from_nix(Errno::last())); return Err(Errno::last().into());
} }
Ok(ready) => break ready, Ok(ready) => break ready,
} }

View File

@@ -1,41 +1,50 @@
//! WASI host types specific to Windows host. //! WASI host types specific to Windows host.
use crate::host::FileType; use crate::host::FileType;
use crate::{wasi, Error, Result}; use crate::{error::FromRawOsError, wasi, Error, Result};
use std::convert::TryInto; use std::convert::TryInto;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io; use std::io;
use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStrExt;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use winx::winerror::WinError;
pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> wasi::__wasi_errno_t { impl FromRawOsError for Error {
fn from_raw_os_error(code: i32) -> Self {
Self::from(WinError::from_u32(code as u32))
}
}
impl From<WinError> for Error {
fn from(err: WinError) -> Self {
// TODO: implement error mapping between Windows and WASI // TODO: implement error mapping between Windows and WASI
use winx::winerror::WinError::*; use winx::winerror::WinError::*;
match error { match err {
ERROR_SUCCESS => wasi::__WASI_ERRNO_SUCCESS, ERROR_SUCCESS => Self::ESUCCESS,
ERROR_BAD_ENVIRONMENT => wasi::__WASI_ERRNO_2BIG, ERROR_BAD_ENVIRONMENT => Self::E2BIG,
ERROR_FILE_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, ERROR_FILE_NOT_FOUND => Self::ENOENT,
ERROR_PATH_NOT_FOUND => wasi::__WASI_ERRNO_NOENT, ERROR_PATH_NOT_FOUND => Self::ENOENT,
ERROR_TOO_MANY_OPEN_FILES => wasi::__WASI_ERRNO_NFILE, ERROR_TOO_MANY_OPEN_FILES => Self::ENFILE,
ERROR_ACCESS_DENIED => wasi::__WASI_ERRNO_ACCES, ERROR_ACCESS_DENIED => Self::EACCES,
ERROR_SHARING_VIOLATION => wasi::__WASI_ERRNO_ACCES, ERROR_SHARING_VIOLATION => Self::EACCES,
ERROR_PRIVILEGE_NOT_HELD => wasi::__WASI_ERRNO_NOTCAPABLE, // TODO is this the correct mapping? ERROR_PRIVILEGE_NOT_HELD => Self::ENOTCAPABLE, // TODO is this the correct mapping?
ERROR_INVALID_HANDLE => wasi::__WASI_ERRNO_BADF, ERROR_INVALID_HANDLE => Self::EBADF,
ERROR_INVALID_NAME => wasi::__WASI_ERRNO_NOENT, ERROR_INVALID_NAME => Self::ENOENT,
ERROR_NOT_ENOUGH_MEMORY => wasi::__WASI_ERRNO_NOMEM, ERROR_NOT_ENOUGH_MEMORY => Self::ENOMEM,
ERROR_OUTOFMEMORY => wasi::__WASI_ERRNO_NOMEM, ERROR_OUTOFMEMORY => Self::ENOMEM,
ERROR_DIR_NOT_EMPTY => wasi::__WASI_ERRNO_NOTEMPTY, ERROR_DIR_NOT_EMPTY => Self::ENOTEMPTY,
ERROR_NOT_READY => wasi::__WASI_ERRNO_BUSY, ERROR_NOT_READY => Self::EBUSY,
ERROR_BUSY => wasi::__WASI_ERRNO_BUSY, ERROR_BUSY => Self::EBUSY,
ERROR_NOT_SUPPORTED => wasi::__WASI_ERRNO_NOTSUP, ERROR_NOT_SUPPORTED => Self::ENOTSUP,
ERROR_FILE_EXISTS => wasi::__WASI_ERRNO_EXIST, ERROR_FILE_EXISTS => Self::EEXIST,
ERROR_BROKEN_PIPE => wasi::__WASI_ERRNO_PIPE, ERROR_BROKEN_PIPE => Self::EPIPE,
ERROR_BUFFER_OVERFLOW => wasi::__WASI_ERRNO_NAMETOOLONG, ERROR_BUFFER_OVERFLOW => Self::ENAMETOOLONG,
ERROR_NOT_A_REPARSE_POINT => wasi::__WASI_ERRNO_INVAL, ERROR_NOT_A_REPARSE_POINT => Self::EINVAL,
ERROR_NEGATIVE_SEEK => wasi::__WASI_ERRNO_INVAL, ERROR_NEGATIVE_SEEK => Self::EINVAL,
ERROR_DIRECTORY => wasi::__WASI_ERRNO_NOTDIR, ERROR_DIRECTORY => Self::ENOTDIR,
ERROR_ALREADY_EXISTS => wasi::__WASI_ERRNO_EXIST, ERROR_ALREADY_EXISTS => Self::EEXIST,
_ => wasi::__WASI_ERRNO_NOTSUP, _ => Self::ENOTSUP,
}
} }
} }