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

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marmistrz committed Jul 26, 2019
1 parent 7cef07b commit d591585
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
14 changes: 12 additions & 2 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ cfg_if! {
mod unix;
pub use self::unix::*;

pub fn errno_from_host(err: i32) -> host::__wasi_errno_t {
pub(crate) fn errno_from_host(err: i32) -> host::__wasi_errno_t {
host_impl::errno_from_nix(nix::errno::from_i32(err))
}
} else if #[cfg(windows)] {
mod windows;
pub use self::windows::*;

pub fn errno_from_host(err: i32) -> host::__wasi_errno_t {
pub(crate) fn errno_from_host(err: i32) -> host::__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");
}
}

pub(crate) fn errno_from_ioerror(e: std::io::Error) -> host::__wasi_errno_t {
match e.raw_os_error() {
Some(code) => errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
host::__WASI_EIO
}
}
}
14 changes: 4 additions & 10 deletions src/sys/unix/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use super::fs_helpers::*;
use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry;
use crate::helpers::systemtime_to_timestamp;
use crate::sys::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, wasm32, Result};
use nix::libc::{self, c_long, c_void, off_t};
use std::convert::TryInto;
use std::ffi::CString;
use std::fs::{File, Metadata};
use std::io;
use std::os::unix::fs::FileExt;
use std::os::unix::prelude::{AsRawFd, FromRawFd};

Expand Down Expand Up @@ -362,12 +361,7 @@ pub(crate) fn path_rename(
pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_filestat_t> {
use std::os::unix::fs::MetadataExt;

fn convert_err(e: io::Error) -> host::__wasi_errno_t {
log::debug!("fd_filestat_get: os error: {}", e);
e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)
}

let metadata = file.metadata().map_err(convert_err)?;
let metadata = file.metadata().map_err(errno_from_ioerror)?;
Ok(host::__wasi_filestat_t {
st_dev: metadata.dev(),
st_ino: metadata.ino(),
Expand All @@ -378,15 +372,15 @@ pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_
st_size: metadata.len(),
st_atim: metadata
.accessed()
.map_err(convert_err)
.map_err(errno_from_ioerror)
.and_then(systemtime_to_timestamp)?,
st_ctim: metadata
.ctime()
.try_into()
.map_err(|_| host::__WASI_EOVERFLOW)?, // i64 doesn't fit into u64
st_mtim: metadata
.modified()
.map_err(convert_err)
.map_err(errno_from_ioerror)
.and_then(systemtime_to_timestamp)?,
st_filetype: filetype(&metadata),
})
Expand Down
23 changes: 9 additions & 14 deletions src/sys/windows/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::fs_helpers::*;
use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry;
use crate::helpers::systemtime_to_timestamp;
use crate::sys::errno_from_host;
use crate::sys::{errno_from_ioerror, errno_from_host};
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl;
use crate::{host, Result};
Expand Down Expand Up @@ -196,33 +196,28 @@ pub(crate) fn change_time(file: &File, _metadata: &Metadata) -> io::Result<i64>
}

pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_filestat_t> {
fn convert_err(e: io::Error) -> host::__wasi_errno_t {
log::debug!("fd_filestat_get: os error: {}", e);
e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)
}

let metadata = file.metadata().map_err(convert_err)?;
let metadata = file.metadata().map_err(errno_from_ioerror)?;
Ok(host::__wasi_filestat_t {
st_dev: device_id(file, &metadata).map_err(convert_err)?,
st_ino: file_serial_no(file, &metadata).map_err(convert_err)?,
st_dev: device_id(file, &metadata).map_err(errno_from_ioerror)?,
st_ino: file_serial_no(file, &metadata).map_err(errno_from_ioerror)?,
st_nlink: num_hardlinks(file, &metadata)
.map_err(convert_err)?
.map_err(errno_from_ioerror)?
.try_into()
.map_err(|_| host::__WASI_EOVERFLOW)?, // u64 doesn't fit into u32
st_size: metadata.len(),
st_atim: metadata
.accessed()
.map_err(convert_err)
.map_err(errno_from_ioerror)
.and_then(systemtime_to_timestamp)?,
st_ctim: change_time(file, &metadata)
.map_err(convert_err)?
.map_err(errno_from_ioerror)?
.try_into()
.map_err(|_| host::__WASI_EOVERFLOW)?, // i64 doesn't fit into u64
st_mtim: metadata
.modified()
.map_err(convert_err)
.map_err(errno_from_ioerror)
.and_then(systemtime_to_timestamp)?,
st_filetype: filetype(&metadata).map_err(convert_err)?,
st_filetype: filetype(&metadata).map_err(errno_from_ioerror)?,
})
}

Expand Down

0 comments on commit d591585

Please sign in to comment.