diff --git a/src/ctx.rs b/src/ctx.rs index d5aa0a1..c31b055 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -1,5 +1,5 @@ use crate::fdentry::FdEntry; -use crate::sys::{dev_null, errno_from_host}; +use crate::sys::{dev_null, errno_from_ioerror}; use crate::{host, Result}; use std::borrow::Borrow; use std::collections::HashMap; @@ -92,11 +92,7 @@ impl WasiCtxBuilder { // startup code starts looking at fd 3 for preopens let mut preopen_fd = 3; for (guest_path, dir) in self.preopens { - if !dir - .metadata() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))? - .is_dir() - { + if !dir.metadata().map_err(errno_from_ioerror)?.is_dir() { return Err(host::__WASI_EBADF); } diff --git a/src/fdentry.rs b/src/fdentry.rs index 976d8a6..6ca142c 100644 --- a/src/fdentry.rs +++ b/src/fdentry.rs @@ -1,4 +1,4 @@ -use crate::sys::{errno_from_host, fdentry_impl}; +use crate::sys::{errno_from_ioerror, fdentry_impl}; use crate::{host, Result}; use std::mem::ManuallyDrop; @@ -92,7 +92,7 @@ impl FdEntry { 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)) + .map_err(errno_from_ioerror) .and_then(Self::from) } diff --git a/src/hostcalls_impl/fs.rs b/src/hostcalls_impl/fs.rs index 332386a..ecbfdc5 100644 --- a/src/hostcalls_impl/fs.rs +++ b/src/hostcalls_impl/fs.rs @@ -5,7 +5,7 @@ use crate::fdentry::{Descriptor, FdEntry}; use crate::memory::*; use crate::sys::fdentry_impl::determine_type_rights; use crate::sys::hostcalls_impl::fs_helpers::path_open_rights; -use crate::sys::{errno_from_host, host_impl, hostcalls_impl}; +use crate::sys::{errno_from_ioerror, host_impl, hostcalls_impl}; use crate::{host, wasm32, Result}; use log::trace; use std::io::{self, Read, Seek, SeekFrom, Write}; @@ -35,8 +35,7 @@ pub(crate) fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> Result .get_fd_entry(fd, host::__WASI_RIGHT_FD_DATASYNC, 0) .and_then(|fe| fe.fd_object.descriptor.as_file())?; - fd.sync_data() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + fd.sync_data().map_err(errno_from_ioerror) } pub(crate) fn fd_pread( @@ -161,8 +160,7 @@ pub(crate) fn fd_read( _ => return Err(host::__WASI_EBADF), }; - let host_nread = maybe_host_nread - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?; + let host_nread = maybe_host_nread.map_err(errno_from_ioerror)?; trace!(" | *nread={:?}", host_nread); @@ -245,10 +243,7 @@ pub(crate) fn fd_seek( host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64), _ => return Err(host::__WASI_EINVAL), }; - let host_newoffset = fd.seek(pos).map_err(|err| { - log::debug!("fd_seek error={:?}", err); - err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host) - })?; + let host_newoffset = fd.seek(pos).map_err(errno_from_ioerror)?; trace!(" | *newoffset={:?}", host_newoffset); @@ -268,9 +263,7 @@ pub(crate) fn fd_tell( .get_fd_entry(fd, host::__WASI_RIGHT_FD_TELL, 0) .and_then(|fe| fe.fd_object.descriptor.as_file())?; - let host_offset = fd - .seek(SeekFrom::Current(0)) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?; + let host_offset = fd.seek(SeekFrom::Current(0)).map_err(errno_from_ioerror)?; trace!(" | *newoffset={:?}", host_offset); @@ -352,8 +345,7 @@ pub(crate) fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> Result<()> let fd = wasi_ctx .get_fd_entry(fd, host::__WASI_RIGHT_FD_SYNC, 0) .and_then(|fe| fe.fd_object.descriptor.as_file())?; - fd.sync_all() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + fd.sync_all().map_err(errno_from_ioerror) } pub(crate) fn fd_write( @@ -382,26 +374,20 @@ pub(crate) fn fd_write( // perform unbuffered writes let host_nwritten = match &mut *fe.fd_object.descriptor { - Descriptor::File(f) => f - .write_vectored(&iovs) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?, + Descriptor::File(f) => f.write_vectored(&iovs).map_err(errno_from_ioerror)?, Descriptor::Stdin => return Err(host::__WASI_EBADF), Descriptor::Stdout => { // lock for the duration of the scope let stdout = io::stdout(); let mut stdout = stdout.lock(); - let nwritten = stdout - .write_vectored(&iovs) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?; - stdout - .flush() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?; + let nwritten = stdout.write_vectored(&iovs).map_err(errno_from_ioerror)?; + stdout.flush().map_err(errno_from_ioerror)?; nwritten } Descriptor::Stderr => io::stderr() .lock() .write_vectored(&iovs) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?, + .map_err(errno_from_ioerror)?, }; trace!(" | *nwritten={:?}", host_nwritten); @@ -450,9 +436,7 @@ pub(crate) fn fd_allocate( .get_fd_entry(fd, host::__WASI_RIGHT_FD_ALLOCATE, 0) .and_then(|fe| fe.fd_object.descriptor.as_file())?; - let metadata = fd - .metadata() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?; + let metadata = fd.metadata().map_err(errno_from_ioerror)?; let current_size = metadata.len(); let wanted_size = offset.checked_add(len).ok_or(host::__WASI_E2BIG)?; @@ -461,8 +445,7 @@ pub(crate) fn fd_allocate( } if wanted_size > current_size { - fd.set_len(wanted_size) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + fd.set_len(wanted_size).map_err(errno_from_ioerror) } else { Ok(()) } diff --git a/src/sys/unix/fdentry_impl.rs b/src/sys/unix/fdentry_impl.rs index 9be3954..de9494e 100644 --- a/src/sys/unix/fdentry_impl.rs +++ b/src/sys/unix/fdentry_impl.rs @@ -1,5 +1,5 @@ use crate::fdentry::Descriptor; -use crate::sys::errno_from_host; +use crate::sys::{errno_from_host, errno_from_ioerror}; use crate::{host, Result}; use std::io; use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd}; @@ -53,7 +53,7 @@ pub(crate) fn determine_type_rights( std::mem::ManuallyDrop::new(unsafe { std::fs::File::from_raw_fd(fd.as_raw_fd()) }); let ft = file .metadata() - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))? + .map_err(errno_from_ioerror)? .file_type(); if ft.is_block_device() { ( diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 7c33262..c0e1365 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -3,8 +3,8 @@ use super::fs_helpers::*; use crate::helpers::systemtime_to_timestamp; use crate::hostcalls_impl::PathGet; +use crate::sys::errno_from_ioerror; use crate::sys::host_impl; -use crate::sys::{errno_from_host, errno_from_ioerror}; use crate::{host, wasm32, Result}; use nix::libc::{self, c_long, c_void, off_t}; use std::convert::TryInto; @@ -18,13 +18,11 @@ pub(crate) fn fd_pread( buf: &mut [u8], offset: host::__wasi_filesize_t, ) -> Result { - file.read_at(buf, offset) - .map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + file.read_at(buf, offset).map_err(errno_from_ioerror) } 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)) + file.write_at(buf, offset).map_err(errno_from_ioerror) } pub(crate) fn fd_fdstat_get(fd: &File) -> Result { diff --git a/src/sys/unix/mod.rs b/src/sys/unix/mod.rs index 0e39f63..ca5bffb 100644 --- a/src/sys/unix/mod.rs +++ b/src/sys/unix/mod.rs @@ -2,16 +2,15 @@ pub(crate) mod fdentry_impl; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; -use crate::sys::errno_from_host; -use crate::{host, Result}; +use crate::sys::errno_from_ioerror; +use crate::Result; use std::fs::File; use std::path::Path; 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)) + File::open("/dev/null").map_err(errno_from_ioerror) } 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)) + File::open(path).map_err(errno_from_ioerror) } diff --git a/src/sys/windows/hostcalls_impl/fs.rs b/src/sys/windows/hostcalls_impl/fs.rs index 1d354eb..a375611 100644 --- a/src/sys/windows/hostcalls_impl/fs.rs +++ b/src/sys/windows/hostcalls_impl/fs.rs @@ -42,12 +42,12 @@ pub(crate) fn fd_pread( offset: host::__wasi_filesize_t, ) -> Result { read_at(file, buf, offset) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + .map_err(errno_from_ioerror) } 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)) + .map_err(errno_from_ioerror) } pub(crate) fn fd_fdstat_get(fd: &File) -> Result { diff --git a/src/sys/windows/mod.rs b/src/sys/windows/mod.rs index 02e562c..fe030eb 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -2,13 +2,13 @@ pub(crate) mod fdentry_impl; pub(crate) mod host_impl; pub(crate) mod hostcalls_impl; -use crate::sys::errno_from_host; -use crate::{host, Result}; +use crate::sys::errno_from_ioerror; +use crate::Result; use std::fs::File; use std::path::Path; pub(crate) fn dev_null() -> Result { - File::open("NUL").map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + File::open("NUL").map_err(errno_from_ioerror) } pub fn preopen_dir>(path: P) -> Result { @@ -25,5 +25,5 @@ pub fn preopen_dir>(path: P) -> Result { .read(true) .attributes(FILE_FLAG_BACKUP_SEMANTICS) .open(path) - .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + .map_err(errno_from_ioerror) }