redefine crate to use Error everywhere except in wasi
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use cpu_time::{ProcessTime, ThreadTime};
|
||||
use lazy_static::lazy_static;
|
||||
use std::convert::TryInto;
|
||||
@@ -83,7 +84,7 @@ fn get_monotonic_time() -> Duration {
|
||||
fn get_realtime_time() -> Result<Duration> {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Errno::Fault)
|
||||
.map_err(|_| Error::Fault)
|
||||
}
|
||||
|
||||
fn get_proc_cputime() -> Result<Duration> {
|
||||
|
||||
@@ -4,7 +4,8 @@ use crate::path;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::Result;
|
||||
use log::trace;
|
||||
use std::convert::TryInto;
|
||||
use std::fs::{File, OpenOptions};
|
||||
|
||||
@@ -10,7 +10,8 @@ pub(crate) mod stdio;
|
||||
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result, RightsExt};
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use crate::{Error, Result};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fs::File;
|
||||
use std::mem::ManuallyDrop;
|
||||
@@ -18,7 +19,6 @@ use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::Path;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{io, string};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::{CreationDisposition, Flags};
|
||||
|
||||
impl<T: AsRawHandle> AsFile for T {
|
||||
@@ -106,47 +106,7 @@ pub(crate) fn file_serial_no(file: &File) -> io::Result<u64> {
|
||||
Ok(no)
|
||||
}
|
||||
|
||||
impl From<io::Error> for Errno {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code as u32 {
|
||||
winerror::ERROR_SUCCESS => Self::Success,
|
||||
winerror::ERROR_BAD_ENVIRONMENT => Self::TooBig,
|
||||
winerror::ERROR_FILE_NOT_FOUND => Self::Noent,
|
||||
winerror::ERROR_PATH_NOT_FOUND => Self::Noent,
|
||||
winerror::ERROR_TOO_MANY_OPEN_FILES => Self::Nfile,
|
||||
winerror::ERROR_ACCESS_DENIED => Self::Acces,
|
||||
winerror::ERROR_SHARING_VIOLATION => Self::Acces,
|
||||
winerror::ERROR_PRIVILEGE_NOT_HELD => Self::Notcapable,
|
||||
winerror::ERROR_INVALID_HANDLE => Self::Badf,
|
||||
winerror::ERROR_INVALID_NAME => Self::Noent,
|
||||
winerror::ERROR_NOT_ENOUGH_MEMORY => Self::Nomem,
|
||||
winerror::ERROR_OUTOFMEMORY => Self::Nomem,
|
||||
winerror::ERROR_DIR_NOT_EMPTY => Self::Notempty,
|
||||
winerror::ERROR_NOT_READY => Self::Busy,
|
||||
winerror::ERROR_BUSY => Self::Busy,
|
||||
winerror::ERROR_NOT_SUPPORTED => Self::Notsup,
|
||||
winerror::ERROR_FILE_EXISTS => Self::Exist,
|
||||
winerror::ERROR_BROKEN_PIPE => Self::Pipe,
|
||||
winerror::ERROR_BUFFER_OVERFLOW => Self::Nametoolong,
|
||||
winerror::ERROR_NOT_A_REPARSE_POINT => Self::Inval,
|
||||
winerror::ERROR_NEGATIVE_SEEK => Self::Inval,
|
||||
winerror::ERROR_DIRECTORY => Self::Notdir,
|
||||
winerror::ERROR_ALREADY_EXISTS => Self::Exist,
|
||||
x => {
|
||||
log::debug!("winerror: unknown error value: {}", x);
|
||||
Self::Io
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::Io
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<string::FromUtf16Error> for Errno {
|
||||
impl From<string::FromUtf16Error> for Error {
|
||||
fn from(_err: string::FromUtf16Error) -> Self {
|
||||
Self::Ilseq
|
||||
}
|
||||
@@ -166,14 +126,14 @@ fn change_time(file: &File) -> io::Result<i64> {
|
||||
|
||||
fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
|
||||
st.duration_since(UNIX_EPOCH)
|
||||
.map_err(|_| Errno::Inval)? // date earlier than UNIX_EPOCH
|
||||
.map_err(|_| Error::Inval)? // date earlier than UNIX_EPOCH
|
||||
.as_nanos()
|
||||
.try_into()
|
||||
.map_err(Into::into) // u128 doesn't fit into u64
|
||||
}
|
||||
|
||||
impl TryFrom<&File> for types::Filestat {
|
||||
type Error = Errno;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(file: &File) -> Result<Self> {
|
||||
let metadata = file.metadata()?;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::{fd, AsFile};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::{self, Metadata, OpenOptions};
|
||||
@@ -35,7 +36,7 @@ fn concatenate<P: AsRef<Path>>(file: &OsDir, path: P) -> Result<PathBuf> {
|
||||
// WASI is not able to deal with absolute paths
|
||||
// so error out if absolute
|
||||
if path.as_ref().is_absolute() {
|
||||
return Err(Errno::Notcapable);
|
||||
return Err(Error::Notcapable);
|
||||
}
|
||||
|
||||
let dir_path = get_file_path(&*file.as_file()?)?;
|
||||
@@ -131,8 +132,8 @@ pub(crate) fn readlinkat(dirfd: &OsDir, s_path: &str) -> Result<String> {
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| Errno::Notcapable)?;
|
||||
let target_path = target_path.to_str().ok_or(Errno::Ilseq)?;
|
||||
.map_err(|_| Error::Notcapable)?;
|
||||
let target_path = target_path.to_str().ok_or(Error::Ilseq)?;
|
||||
return Ok(target_path.to_owned());
|
||||
}
|
||||
Err(e) => e,
|
||||
@@ -144,7 +145,7 @@ pub(crate) fn readlinkat(dirfd: &OsDir, s_path: &str) -> Result<String> {
|
||||
// strip "/" and check if exists
|
||||
let path = concatenate(dirfd, Path::new(s_path.trim_end_matches('/')))?;
|
||||
if path.exists() && !path.is_dir() {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,7 +176,7 @@ pub(crate) fn link(
|
||||
// fs::canonicalize under Windows will return:
|
||||
// * ERROR_FILE_NOT_FOUND, if it encounters a dangling symlink
|
||||
// * ERROR_CANT_RESOLVE_FILENAME, if it encounters a symlink loop
|
||||
Some(code) if code as u32 == winerror::ERROR_CANT_RESOLVE_FILENAME => Errno::Loop,
|
||||
Some(code) if code as u32 == winerror::ERROR_CANT_RESOLVE_FILENAME => Error::Loop,
|
||||
_ => e.into(),
|
||||
})?;
|
||||
}
|
||||
@@ -190,7 +191,7 @@ pub(crate) fn link(
|
||||
// implementations of link return `EPERM`, but `ERROR_ACCESS_DENIED` is converted
|
||||
// to `EACCES`. We detect and correct this case here.
|
||||
if fs::metadata(&old_path).map(|m| m.is_dir()).unwrap_or(false) {
|
||||
return Err(Errno::Perm);
|
||||
return Err(Error::Perm);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -214,7 +215,7 @@ pub(crate) fn open(
|
||||
// This is because truncation requires `GENERIC_WRITE` access, which will override the removal
|
||||
// of the `FILE_WRITE_DATA` permission.
|
||||
if fdflags.contains(&types::Fdflags::APPEND) {
|
||||
return Err(Errno::Notsup);
|
||||
return Err(Error::Notsup);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,11 +240,11 @@ pub(crate) fn open(
|
||||
Ok(file_type) => {
|
||||
// check if we are trying to open a symlink
|
||||
if file_type.is_symlink() {
|
||||
return Err(Errno::Loop);
|
||||
return Err(Error::Loop);
|
||||
}
|
||||
// check if we are trying to open a file as a dir
|
||||
if file_type.is_file() && oflags.contains(&types::Oflags::DIRECTORY) {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
}
|
||||
Err(err) => match err.raw_os_error() {
|
||||
@@ -257,14 +258,14 @@ pub(crate) fn open(
|
||||
winerror::ERROR_INVALID_NAME => {
|
||||
// TODO rethink this. For now, migrate how we handled
|
||||
// it in `path::openat` on Windows.
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
_ => return Err(err.into()),
|
||||
};
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
return Err(Errno::Io);
|
||||
return Err(Error::Io);
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -299,8 +300,8 @@ pub(crate) fn readlink(dirfd: &OsDir, path: &str, buf: &mut [u8]) -> Result<usiz
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
.map_err(|_| Errno::Notcapable)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(Errno::Ilseq))?;
|
||||
.map_err(|_| Error::Notcapable)
|
||||
.and_then(|path| path.to_str().map(String::from).ok_or(Error::Ilseq))?;
|
||||
|
||||
if buf.len() > 0 {
|
||||
let mut chars = target_path.chars();
|
||||
@@ -338,12 +339,12 @@ pub(crate) fn rename(
|
||||
//
|
||||
// [std::fs::rename]: https://doc.rust-lang.org/std/fs/fn.rename.html
|
||||
if old_path.is_dir() && new_path.is_file() {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
// Second sanity check: check we're not trying to rename a file into a path
|
||||
// ending in a trailing slash.
|
||||
if old_path.is_file() && new_path_.ends_with('/') {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
|
||||
// TODO handle symlinks
|
||||
@@ -359,7 +360,7 @@ pub(crate) fn rename(
|
||||
// So most likely dealing with new_path == dir.
|
||||
// Eliminate case old_path == file first.
|
||||
if old_path.is_file() {
|
||||
return Err(Errno::Isdir);
|
||||
return Err(Error::Isdir);
|
||||
} else {
|
||||
// Ok, let's try removing an empty dir at new_path if it exists
|
||||
// and is a nonempty dir.
|
||||
@@ -375,7 +376,7 @@ pub(crate) fn rename(
|
||||
strip_trailing_slashes_and_concatenate(old_dirfd, old_path_)?
|
||||
{
|
||||
if path.is_file() {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -386,7 +387,7 @@ pub(crate) fn rename(
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
Err(Error::Io)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -432,7 +433,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path_: &str) -> Res
|
||||
strip_trailing_slashes_and_concatenate(new_dirfd, new_path_)?
|
||||
{
|
||||
if path.exists() {
|
||||
return Err(Errno::Exist);
|
||||
return Err(Error::Exist);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -443,7 +444,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path_: &str) -> Res
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
Err(Error::Io)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -478,15 +479,15 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
}
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", err);
|
||||
Err(Errno::Io)
|
||||
Err(Error::Io)
|
||||
}
|
||||
}
|
||||
} else if file_type.is_dir() {
|
||||
Err(Errno::Isdir)
|
||||
Err(Error::Isdir)
|
||||
} else if file_type.is_file() {
|
||||
fs::remove_file(path).map_err(Into::into)
|
||||
} else {
|
||||
Err(Errno::Inval)
|
||||
Err(Error::Inval)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ use crate::sys::osfile::OsFile;
|
||||
use crate::sys::osother::OsOther;
|
||||
use crate::sys::stdio::{Stderr, Stdin, Stdout};
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use log::{debug, error, trace, warn};
|
||||
use std::convert::TryInto;
|
||||
@@ -23,7 +24,7 @@ enum PollState {
|
||||
Ready,
|
||||
NotReady, // it's not ready, but we didn't wait
|
||||
TimedOut, // it's not ready and a timeout has occurred
|
||||
Error(Errno),
|
||||
Error(Error),
|
||||
}
|
||||
|
||||
enum WaitMode {
|
||||
@@ -79,7 +80,7 @@ impl StdinPoll {
|
||||
// Linux returns `POLLIN` in both cases, and we imitate this behavior.
|
||||
let resp = match std::io::stdin().lock().fill_buf() {
|
||||
Ok(_) => PollState::Ready,
|
||||
Err(e) => PollState::Error(Errno::from(e)),
|
||||
Err(e) => PollState::Error(Error::from(e)),
|
||||
};
|
||||
|
||||
// Notify the requestor about data in stdin. They may have already timed out,
|
||||
@@ -103,7 +104,7 @@ lazy_static! {
|
||||
|
||||
fn make_rw_event(event: &FdEventData, nbytes: Result<u64>) -> types::Event {
|
||||
let (nbytes, error) = match nbytes {
|
||||
Ok(nbytes) => (nbytes, Errno::Success),
|
||||
Ok(nbytes) => (nbytes, Error::Success),
|
||||
Err(e) => (u64::default(), e),
|
||||
};
|
||||
types::Event {
|
||||
@@ -121,7 +122,7 @@ fn make_timeout_event(timeout: &ClockEventData) -> types::Event {
|
||||
types::Event {
|
||||
userdata: timeout.userdata,
|
||||
type_: types::Eventtype::Clock,
|
||||
error: Errno::Success,
|
||||
error: Error::Success,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
flags: types::Eventrwflags::empty(),
|
||||
@@ -173,7 +174,7 @@ fn handle_rw_event(event: FdEventData, out_events: &mut Vec<types::Event>) {
|
||||
out_events.push(new_event);
|
||||
}
|
||||
|
||||
fn handle_error_event(event: FdEventData, error: Errno, out_events: &mut Vec<types::Event>) {
|
||||
fn handle_error_event(event: FdEventData, error: Error, out_events: &mut Vec<types::Event>) {
|
||||
let new_event = make_rw_event(&event, Err(error));
|
||||
out_events.push(new_event);
|
||||
}
|
||||
@@ -238,11 +239,11 @@ pub(crate) fn oneoff(
|
||||
"poll_oneoff: unsupported file type: {}",
|
||||
other.get_file_type()
|
||||
);
|
||||
handle_error_event(event, Errno::Notsup, events);
|
||||
handle_error_event(event, Error::Notsup, events);
|
||||
}
|
||||
} else {
|
||||
log::error!("can poll FdEvent for OS resources only");
|
||||
return Err(Errno::Badf);
|
||||
return Err(Error::Badf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +307,7 @@ pub(crate) fn oneoff(
|
||||
}
|
||||
None => {
|
||||
error!("Polling only pipes with no timeout not supported on Windows.");
|
||||
return Err(Errno::Notsup);
|
||||
return Err(Error::Notsup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user