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 committed Jul 26, 2019
1 parent d591585 commit 32899ae
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 61 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
29 changes: 9 additions & 20 deletions src/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::ctx::WasiCtx;
use crate::fdentry::{Descriptor, FdEntry};
use crate::memory::*;
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 @@ -32,8 +32,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 @@ -158,8 +157,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 @@ -242,9 +240,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| 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 @@ -264,9 +260,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 @@ -348,8 +342,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 @@ -383,8 +376,7 @@ pub(crate) fn fd_write(
Descriptor::Stderr => io::stderr().lock().write_vectored(&iovs),
};

let host_nwritten = maybe_host_nwritten
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
let host_nwritten = maybe_host_nwritten.map_err(errno_from_ioerror)?;

trace!(" | *nwritten={:?}", host_nwritten);

Expand Down Expand Up @@ -432,9 +424,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 @@ -443,8 +433,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
7 changes: 2 additions & 5 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 @@ -51,10 +51,7 @@ pub(crate) fn determine_type_rights<Fd: AsRawFd>(
// we just make a `File` here for convenience; we don't want it to close when it drops
let file =
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))?
.file_type();
let ft = file.metadata().map_err(errno_from_ioerror)?.file_type();
if ft.is_block_device() {
(
host::__WASI_FILETYPE_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 @@ -6,7 +6,7 @@ use crate::fdentry::FdEntry;
use crate::helpers::systemtime_to_timestamp;
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl;
use crate::sys::{errno_from_host, errno_from_ioerror};
use crate::sys::{errno_from_ioerror};
use crate::{host, wasm32, Result};
use nix::libc::{self, c_long, c_void, off_t};
use std::convert::TryInto;
Expand All @@ -20,13 +20,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
7 changes: 2 additions & 5 deletions src/sys/unix/hostcalls_impl/fs_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]

use crate::sys::errno_from_host;
use crate::sys::errno_from_ioerror;
use crate::sys::host_impl;
use crate::{host, Result};
use nix::libc::{self, c_long};
Expand All @@ -24,10 +24,7 @@ pub(crate) fn path_get(
return Err(host::__WASI_EILSEQ);
}

let dirfd = dirfd.try_clone().map_err(|err| {
err.raw_os_error()
.map_or(host::__WASI_EBADF, errno_from_host)
})?;
let dirfd = dirfd.try_clone().map_err(errno_from_ioerror)?;

// Stack of directory file descriptors. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a file descriptor to be pushed, while handling
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)
}
8 changes: 3 additions & 5 deletions src/sys/windows/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use super::fs_helpers::*;
use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry;
use crate::helpers::systemtime_to_timestamp;
use crate::sys::{errno_from_ioerror, errno_from_host};
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl;
use crate::sys::{errno_from_host, errno_from_ioerror};
use crate::{host, Result};
use std::convert::TryInto;
use std::fs::{File, Metadata};
Expand Down Expand Up @@ -39,13 +39,11 @@ pub(crate) fn fd_pread(
buf: &mut [u8],
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))
read_at(file, buf, offset).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))
write_at(file, buf, offset).map_err(errno_from_ioerror)
}

pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
Expand Down
7 changes: 2 additions & 5 deletions src/sys/windows/hostcalls_impl/fs_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]

use crate::sys::errno_from_host;
use crate::sys::errno_from_ioerror;
use crate::sys::host_impl;
use crate::{host, Result};
use std::fs::File;
Expand All @@ -20,10 +20,7 @@ pub(crate) fn path_get(
return Err(host::__WASI_EILSEQ);
}

let dirfd = dirfd.try_clone().map_err(|err| {
err.raw_os_error()
.map_or(host::__WASI_EBADF, errno_from_host)
})?;
let dirfd = dirfd.try_clone().map_err(errno_from_ioerror)?;

// Stack of directory handles. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a handle to be pushed, while handling
Expand Down
6 changes: 3 additions & 3 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::sys::errno_from_ioerror;
use crate::{host, 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 32899ae

Please sign in to comment.