Create helper Result<T> type

This commit is contained in:
Jakub Konka
2019-07-18 19:32:07 +02:00
committed by Dan Gohman
parent 08aa61f066
commit 310ecb5b5b
19 changed files with 173 additions and 255 deletions

View File

@@ -1,7 +1,6 @@
use super::host_impl;
use crate::fdentry::Descriptor;
use crate::host;
use crate::{host, Result};
use std::fs::File;
use std::io;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle};
@@ -19,14 +18,11 @@ impl AsRawHandle for Descriptor {
pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<
(
host::__wasi_filetype_t,
host::__wasi_rights_t,
host::__wasi_rights_t,
),
host::__wasi_errno_t,
> {
) -> Result<(
host::__wasi_filetype_t,
host::__wasi_rights_t,
host::__wasi_rights_t,
)> {
use winx::file::{get_file_access_rights, AccessRight};
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(handle)?;
@@ -54,14 +50,11 @@ pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
pub(crate) fn determine_type_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<
(
host::__wasi_filetype_t,
host::__wasi_rights_t,
host::__wasi_rights_t,
),
host::__wasi_errno_t,
> {
) -> Result<(
host::__wasi_filetype_t,
host::__wasi_rights_t,
host::__wasi_rights_t,
)> {
let (file_type, rights_base, rights_inheriting) = {
let file_type =
winx::file::get_file_type(handle.as_raw_handle()).map_err(host_impl::errno_from_win)?;

View File

@@ -2,7 +2,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]
use crate::host;
use crate::{host, Result};
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
@@ -108,7 +108,7 @@ pub fn win_from_oflags(
///
/// NB WASI spec requires OS string to be valid UTF-8. Otherwise,
/// `__WASI_EILSEQ` error is returned.
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String, host::__wasi_errno_t> {
pub fn path_from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
let vec: Vec<u16> = s.as_ref().encode_wide().collect();
String::from_utf16(&vec).map_err(|_| host::__WASI_EILSEQ)
}

View File

@@ -3,10 +3,10 @@
use super::fs_helpers::*;
use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry;
use crate::host;
use crate::sys::errno_from_host;
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl;
use crate::{host, Result};
use std::fs::File;
use std::io::{self, Seek, SeekFrom};
use std::os::windows::fs::FileExt;
@@ -36,16 +36,12 @@ pub(crate) fn fd_pread(
file: &File,
buf: &mut [u8],
offset: host::__wasi_filesize_t,
) -> Result<usize, host::__wasi_errno_t> {
) -> Result<usize> {
read_at(file, buf, offset)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
}
pub(crate) fn fd_pwrite(
file: &File,
buf: &[u8],
offset: host::__wasi_filesize_t,
) -> Result<usize, host::__wasi_errno_t> {
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))
}
@@ -54,7 +50,7 @@ pub(crate) fn fd_renumber(
wasi_ctx: &mut WasiCtx,
from: host::__wasi_fd_t,
to: host::__wasi_fd_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("fd_renumber")
}
@@ -62,17 +58,15 @@ pub(crate) fn fd_seek(
fd_entry: &FdEntry,
offset: host::__wasi_filedelta_t,
whence: host::__wasi_whence_t,
) -> Result<u64, host::__wasi_errno_t> {
) -> Result<u64> {
unimplemented!("fd_seek")
}
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
unimplemented!("fd_tell")
}
pub(crate) fn fd_fdstat_get(
fd_entry: &FdEntry,
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
use winx::file::AccessRight;
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle();
@@ -85,7 +79,7 @@ pub(crate) fn fd_fdstat_get(
pub(crate) fn fd_fdstat_set_flags(
fd_entry: &FdEntry,
fdflags: host::__wasi_fdflags_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("fd_fdstat_set_flags")
}
@@ -94,7 +88,7 @@ pub(crate) fn fd_advise(
advice: host::__wasi_advice_t,
offset: host::__wasi_filesize_t,
len: host::__wasi_filesize_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("fd_advise")
}
@@ -102,7 +96,7 @@ pub(crate) fn path_create_directory(
ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &str,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_create_directory")
}
@@ -114,7 +108,7 @@ pub(crate) fn path_link(
new_path: &str,
source_rights: host::__wasi_rights_t,
target_rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_link")
}
@@ -129,7 +123,7 @@ pub(crate) fn path_open(
mut needed_base: host::__wasi_rights_t,
mut needed_inheriting: host::__wasi_rights_t,
fs_flags: host::__wasi_fdflags_t,
) -> Result<FdEntry, host::__wasi_errno_t> {
) -> Result<FdEntry> {
use winx::file::{AccessRight, CreationDisposition, FlagsAndAttributes, ShareMode};
let mut win_rights = AccessRight::READ_CONTROL;
@@ -200,7 +194,7 @@ pub(crate) fn fd_readdir(
fd_entry: &FdEntry,
host_buf: &mut [u8],
cookie: host::__wasi_dircookie_t,
) -> Result<usize, host::__wasi_errno_t> {
) -> Result<usize> {
unimplemented!("fd_readdir")
}
@@ -210,7 +204,7 @@ pub(crate) fn path_readlink(
path: &str,
rights: host::__wasi_rights_t,
buf: &mut [u8],
) -> Result<usize, host::__wasi_errno_t> {
) -> Result<usize> {
unimplemented!("path_readlink")
}
@@ -222,13 +216,11 @@ pub(crate) fn path_rename(
new_dirfd: host::__wasi_fd_t,
new_path: &str,
new_rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_rename")
}
pub(crate) fn fd_filestat_get(
fd_entry: &FdEntry,
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
pub(crate) fn fd_filestat_get(fd_entry: &FdEntry) -> Result<host::__wasi_filestat_t> {
unimplemented!("fd_filestat_get")
}
@@ -237,14 +229,14 @@ pub(crate) fn fd_filestat_set_times(
st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("fd_filestat_set_times")
}
pub(crate) fn fd_filestat_set_size(
fd_entry: &FdEntry,
st_size: host::__wasi_filesize_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("fd_filestat_set_size")
}
@@ -253,7 +245,7 @@ pub(crate) fn path_filestat_get(
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t,
path: &str,
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
) -> Result<host::__wasi_filestat_t> {
unimplemented!("path_filestat_get")
}
@@ -266,7 +258,7 @@ pub(crate) fn path_filestat_set_times(
st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t,
fst_flags: host::__wasi_fstflags_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_filestat_set_times")
}
@@ -276,7 +268,7 @@ pub(crate) fn path_symlink(
rights: host::__wasi_rights_t,
old_path: &str,
new_path: &str,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_symlink")
}
@@ -285,7 +277,7 @@ pub(crate) fn path_unlink_file(
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_unlink_file")
}
@@ -294,6 +286,6 @@ pub(crate) fn path_remove_directory(
dirfd: host::__wasi_fd_t,
path: &str,
rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
) -> Result<()> {
unimplemented!("path_remove_directory")
}

View File

@@ -3,9 +3,9 @@
use crate::ctx::WasiCtx;
use crate::fdentry::Descriptor;
use crate::host;
use crate::sys::errno_from_host;
use crate::sys::host_impl;
use crate::{host, Result};
use std::fs::File;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
use std::path::{Component, Path};
@@ -19,7 +19,7 @@ pub(crate) fn path_get(
needed_base: host::__wasi_rights_t,
needed_inheriting: host::__wasi_rights_t,
needs_final_component: bool,
) -> Result<(File, String), host::__wasi_errno_t> {
) -> Result<(File, String)> {
if path.contains("\0") {
// if contains NUL, return EILSEQ
return Err(host::__WASI_EILSEQ);

View File

@@ -3,25 +3,21 @@
#![allow(unused)]
use crate::memory::*;
use crate::sys::host_impl;
use crate::{host, wasm32};
use crate::{host, wasm32, Result};
use wasi_common_cbindgen::wasi_common_cbindgen;
pub(crate) fn clock_res_get(
clock_id: host::__wasi_clockid_t,
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
unimplemented!("clock_res_get")
}
pub(crate) fn clock_time_get(
clock_id: host::__wasi_clockid_t,
) -> Result<host::__wasi_timestamp_t, host::__wasi_errno_t> {
pub(crate) fn clock_time_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
unimplemented!("clock_time_get")
}
pub(crate) fn poll_oneoff(
input: Vec<Result<host::__wasi_subscription_t, host::__wasi_errno_t>>,
input: Vec<Result<host::__wasi_subscription_t>>,
output_slice: &mut [wasm32::__wasi_event_t],
) -> Result<wasm32::size_t, host::__wasi_errno_t> {
) -> Result<wasm32::size_t> {
unimplemented!("poll_oneoff")
}

View File

@@ -2,16 +2,16 @@ pub(crate) mod fdentry_impl;
pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl;
use crate::host;
use crate::sys::errno_from_host;
use crate::{host, Result};
use std::fs::File;
use std::path::Path;
pub(crate) fn dev_null() -> Result<File, host::__wasi_errno_t> {
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))
}
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File, host::__wasi_errno_t> {
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt;
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;