redefine crate to use Error everywhere except in wasi

This commit is contained in:
Pat Hickey
2020-08-17 18:45:33 -07:00
parent b8409dd9aa
commit e8160c9a6b
30 changed files with 465 additions and 415 deletions

View File

@@ -2,9 +2,9 @@ use crate::entry::{Entry, EntryHandle};
use crate::handle::HandleRights;
use crate::sys::clock;
use crate::wasi::wasi_snapshot_preview1::WasiSnapshotPreview1;
use crate::wasi::{types, AsBytes, Errno, Result};
use crate::WasiCtx;
use crate::wasi::{types, AsBytes};
use crate::{path, poll};
use crate::{Error, Result, WasiCtx};
use log::{debug, error, trace};
use std::convert::TryInto;
use std::io::{self, SeekFrom};
@@ -36,7 +36,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let mut argv_size: types::Size = 0;
for arg in &self.args {
let arg_len = arg.as_bytes_with_nul().len().try_into()?;
argv_size = argv_size.checked_add(arg_len).ok_or(Errno::Overflow)?;
argv_size = argv_size.checked_add(arg_len).ok_or(Error::Overflow)?;
}
Ok((argc, argv_size))
}
@@ -66,7 +66,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let mut environ_size: types::Size = 0;
for environ in &self.env {
let env_len = environ.as_bytes_with_nul().len().try_into()?;
environ_size = environ_size.checked_add(env_len).ok_or(Errno::Overflow)?;
environ_size = environ_size.checked_add(env_len).ok_or(Error::Overflow)?;
}
Ok((environ_count, environ_size))
}
@@ -114,7 +114,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
if let Ok(fe) = self.get_entry(fd) {
// can't close preopened files
if fe.preopen_path.is_some() {
return Err(Errno::Notsup);
return Err(Error::Notsup);
}
}
self.remove_entry(fd)?;
@@ -156,7 +156,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let rights = HandleRights::new(fs_rights_base, fs_rights_inheriting);
let entry = self.get_entry(fd)?;
if !entry.get_rights().contains(&rights) {
return Err(Errno::Notcapable);
return Err(Error::Notcapable);
}
entry.set_rights(rights);
Ok(())
@@ -174,7 +174,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let entry = self.get_entry(fd)?;
// This check will be unnecessary when rust-lang/rust#63326 is fixed
if size > i64::max_value() as u64 {
return Err(Errno::TooBig);
return Err(Error::TooBig);
}
entry.as_handle(&required_rights)?.filestat_set_size(size)
}
@@ -210,7 +210,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
HandleRights::from_base(types::Rights::FD_READ | types::Rights::FD_SEEK);
let entry = self.get_entry(fd)?;
if offset > i64::max_value() as u64 {
return Err(Errno::Io);
return Err(Error::Io);
}
let host_nread = {
@@ -229,9 +229,9 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat> {
// TODO: should we validate any rights here?
let entry = self.get_entry(fd)?;
let po_path = entry.preopen_path.as_ref().ok_or(Errno::Notsup)?;
let po_path = entry.preopen_path.as_ref().ok_or(Error::Notsup)?;
if entry.get_file_type() != types::Filetype::Directory {
return Err(Errno::Notdir);
return Err(Error::Notdir);
}
let path = path::from_host(po_path.as_os_str())?;
@@ -249,16 +249,16 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
) -> Result<()> {
// TODO: should we validate any rights here?
let entry = self.get_entry(fd)?;
let po_path = entry.preopen_path.as_ref().ok_or(Errno::Notsup)?;
let po_path = entry.preopen_path.as_ref().ok_or(Error::Notsup)?;
if entry.get_file_type() != types::Filetype::Directory {
return Err(Errno::Notdir);
return Err(Error::Notdir);
}
let host_path = path::from_host(po_path.as_os_str())?;
let host_path_len = host_path.len().try_into()?;
if host_path_len > path_len {
return Err(Errno::Nametoolong);
return Err(Error::Nametoolong);
}
trace!(" | path='{}'", host_path);
@@ -287,7 +287,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let entry = self.get_entry(fd)?;
if offset > i64::max_value() as u64 {
return Err(Errno::Io);
return Err(Error::Io);
}
let host_nwritten = {
@@ -343,7 +343,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let dirent_len: types::Size = dirent_raw.len().try_into()?;
let name_raw = name.as_bytes();
let name_len = name_raw.len().try_into()?;
let offset = dirent_len.checked_add(name_len).ok_or(Errno::Overflow)?;
let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?;
if (buf_len - bufused) < offset {
break;
} else {
@@ -360,7 +360,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_renumber(&self, from: types::Fd, to: types::Fd) -> Result<()> {
if !self.contains_entry(from) {
return Err(Errno::Badf);
return Err(Error::Badf);
}
// Don't allow renumbering over a pre-opened resource.
@@ -368,12 +368,12 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
// userspace is capable of removing entries from its tables as well.
if let Ok(from_fe) = self.get_entry(from) {
if from_fe.preopen_path.is_some() {
return Err(Errno::Notsup);
return Err(Error::Notsup);
}
}
if let Ok(to_fe) = self.get_entry(to) {
if to_fe.preopen_path.is_some() {
return Err(Errno::Notsup);
return Err(Error::Notsup);
}
}
let fe = self.remove_entry(from)?;
@@ -676,7 +676,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
nsubscriptions: types::Size,
) -> Result<types::Size> {
if u64::from(nsubscriptions) > types::Filesize::max_value() {
return Err(Errno::Inval);
return Err(Error::Inval);
}
let mut subscriptions = Vec::new();
@@ -694,7 +694,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
// As mandated by the WASI spec:
// > If `nsubscriptions` is 0, returns `errno::inval`.
if subscriptions.is_empty() {
return Err(Errno::Inval);
return Err(Error::Inval);
}
for subscription in subscriptions {
@@ -722,7 +722,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
Err(error) => {
events.push(types::Event {
userdata: subscription.userdata,
error,
error: error.into(),
type_: types::Eventtype::FdRead,
fd_readwrite: types::EventFdReadwrite {
nbytes: 0,
@@ -748,7 +748,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
Err(error) => {
events.push(types::Event {
userdata: subscription.userdata,
error,
error: error.into(),
type_: types::Eventtype::FdWrite,
fd_readwrite: types::EventFdReadwrite {
nbytes: 0,
@@ -804,7 +804,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let mut slice = buf.as_array(buf_len).as_slice()?;
getrandom::getrandom(&mut *slice).map_err(|err| {
error!("getrandom failure: {:?}", err);
Errno::Io
Error::Io
})
}