Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
Reuse errno_from_ioerror to simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz authored and kubkon committed Aug 8, 2019
1 parent e18175c commit 92c2b56
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 55 deletions.
8 changes: 2 additions & 6 deletions src/ctx.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/fdentry.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl FdEntry {

pub fn duplicate(file: &fs::File) -> Result<Self> {
file.try_clone()
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
.map_err(errno_from_ioerror)
.and_then(Self::from)
}

Expand Down
41 changes: 12 additions & 29 deletions src/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)?;
Expand All @@ -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(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/sys/unix/fdentry_impl.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -53,7 +53,7 @@ pub(crate) fn determine_type_rights<Fd: AsRawFd>(
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() {
(
Expand Down
8 changes: 3 additions & 5 deletions src/sys/unix/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,13 +18,11 @@ pub(crate) fn fd_pread(
buf: &mut [u8],
offset: host::__wasi_filesize_t,
) -> Result<usize> {
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<usize> {
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<host::__wasi_fdflags_t> {
Expand Down
9 changes: 4 additions & 5 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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<P: AsRef<Path>>(path: P) -> Result<File> {
File::open(path).map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
File::open(path).map_err(errno_from_ioerror)
}
4 changes: 2 additions & 2 deletions src/sys/windows/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub(crate) fn fd_pread(
offset: host::__wasi_filesize_t,
) -> Result<usize> {
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<usize> {
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<host::__wasi_fdflags_t> {
Expand Down
8 changes: 4 additions & 4 deletions src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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<P: AsRef<Path>>(path: P) -> Result<File> {
Expand All @@ -25,5 +25,5 @@ pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
.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)
}

0 comments on commit 92c2b56

Please sign in to comment.