redefine crate to use Error everywhere except in wasi
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::wasi::types::{Subclockflags, SubscriptionClock};
|
||||
use crate::wasi::{Errno, Result};
|
||||
use crate::{Error, Result};
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub(crate) use super::sys_impl::clock::*;
|
||||
@@ -10,7 +10,7 @@ pub(crate) fn to_relative_ns_delay(clock: &SubscriptionClock) -> Result<u128> {
|
||||
}
|
||||
let now: u128 = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|_| Errno::Notcapable)?
|
||||
.map_err(|_| Error::Notcapable)?
|
||||
.as_nanos();
|
||||
let deadline = u128::from(clock.timeout);
|
||||
Ok(deadline.saturating_sub(now))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use filetime::{set_file_handle_times, FileTime};
|
||||
use std::fs::File;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
@@ -17,7 +18,7 @@ pub(crate) fn filestat_set_times(
|
||||
let set_mtim_now = fst_flags.contains(&types::Fstflags::MTIM_NOW);
|
||||
|
||||
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||
return Err(Errno::Inval);
|
||||
return Err(Error::Inval);
|
||||
}
|
||||
let atim = if set_atim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::sys_impl::oshandle::RawOsHandle;
|
||||
use super::{fd, path, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use log::{debug, error};
|
||||
use std::any::Any;
|
||||
use std::io;
|
||||
@@ -102,7 +103,7 @@ impl Handle for OsDir {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to link with handle that's not an OsDir");
|
||||
return Err(Errno::Badf);
|
||||
return Err(Error::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
@@ -121,7 +122,7 @@ impl Handle for OsDir {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to rename with handle that's not an OsDir");
|
||||
return Err(Errno::Badf);
|
||||
return Err(Error::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::sys_impl::oshandle::RawOsHandle;
|
||||
use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
@@ -77,10 +78,10 @@ impl Handle for OsFile {
|
||||
let fd = self.as_file()?;
|
||||
let metadata = fd.metadata()?;
|
||||
let current_size = metadata.len();
|
||||
let wanted_size = offset.checked_add(len).ok_or(Errno::TooBig)?;
|
||||
let wanted_size = offset.checked_add(len).ok_or(Error::TooBig)?;
|
||||
// This check will be unnecessary when rust-lang/rust#63326 is fixed
|
||||
if wanted_size > i64::max_value() as u64 {
|
||||
return Err(Errno::TooBig);
|
||||
return Err(Error::TooBig);
|
||||
}
|
||||
if wanted_size > current_size {
|
||||
fd.set_len(wanted_size)?;
|
||||
|
||||
@@ -3,7 +3,7 @@ use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use crate::wasi::types::{self, Filetype};
|
||||
use crate::wasi::Result;
|
||||
use crate::Result;
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
|
||||
@@ -20,7 +20,8 @@ use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use crate::wasi::types::{self, Filetype};
|
||||
use crate::wasi::{Errno, Result, RightsExt};
|
||||
use crate::wasi::RightsExt;
|
||||
use crate::{Error, Result};
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::convert::TryInto;
|
||||
@@ -226,7 +227,7 @@ impl Handle for NullDevice {
|
||||
let mut total_len = 0u32;
|
||||
for iov in iovs {
|
||||
let len: types::Size = iov.len().try_into()?;
|
||||
total_len = total_len.checked_add(len).ok_or(Errno::Overflow)?;
|
||||
total_len = total_len.checked_add(len).ok_or(Error::Overflow)?;
|
||||
}
|
||||
Ok(total_len as usize)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::sys_impl::oshandle::RawOsHandle;
|
||||
use crate::wasi::Result;
|
||||
use crate::Result;
|
||||
use std::cell::{Cell, RefCell, RefMut};
|
||||
use std::io;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::wasi::{Errno, Result};
|
||||
use crate::{Error, Result};
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
@@ -20,7 +20,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Directory {
|
||||
return Err(Errno::Isdir);
|
||||
return Err(Error::Isdir);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -56,7 +56,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Resu
|
||||
let new_path = new_path.trim_end_matches('/');
|
||||
match unsafe { fstatat(new_dirfd.as_raw_fd(), new_path, AtFlags::SYMLINK_NOFOLLOW) }
|
||||
{
|
||||
Ok(_) => return Err(Errno::Exist),
|
||||
Ok(_) => return Err(Error::Exist),
|
||||
Err(err) => {
|
||||
log::debug!("path_symlink fstatat error: {:?}", err);
|
||||
}
|
||||
@@ -100,9 +100,9 @@ pub(crate) fn rename(
|
||||
Ok(_) => {
|
||||
// check if destination contains a trailing slash
|
||||
if new_path.contains('/') {
|
||||
return Err(Errno::Notdir);
|
||||
return Err(Error::Notdir);
|
||||
} else {
|
||||
return Err(Errno::Noent);
|
||||
return Err(Error::Noent);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use yanix::clock::{clock_getres, clock_gettime, ClockId};
|
||||
|
||||
pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
@@ -11,11 +12,11 @@ pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
(timespec.tv_sec as types::Timestamp)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
|
||||
.map_or(Err(Errno::Overflow), |resolution| {
|
||||
.map_or(Err(Error::Overflow), |resolution| {
|
||||
// a supported clock can never return zero; this case will probably never get hit, but
|
||||
// make sure we follow the spec
|
||||
if resolution == 0 {
|
||||
Err(Errno::Inval)
|
||||
Err(Error::Inval)
|
||||
} else {
|
||||
Ok(resolution)
|
||||
}
|
||||
@@ -31,5 +32,5 @@ pub(crate) fn time_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
|
||||
(timespec.tv_sec as types::Timestamp)
|
||||
.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
|
||||
.map_or(Err(Errno::Overflow), Ok)
|
||||
.map_or(Err(Error::Overflow), Ok)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::wasi::{self, types, Result};
|
||||
use crate::wasi::{self, types};
|
||||
use crate::Result;
|
||||
use std::convert::TryInto;
|
||||
use std::fs::File;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::sys_impl::oshandle::RawOsHandle;
|
||||
use crate::wasi::Result;
|
||||
use crate::Result;
|
||||
use std::cell::Cell;
|
||||
use std::io;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::wasi::Result;
|
||||
use crate::Result;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
|
||||
@@ -28,7 +28,8 @@ cfg_if::cfg_if! {
|
||||
|
||||
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::io;
|
||||
@@ -141,98 +142,6 @@ impl From<types::Clockid> for ClockId {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Errno {
|
||||
fn from(err: io::Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(code) => match code {
|
||||
libc::EPERM => Self::Perm,
|
||||
libc::ENOENT => Self::Noent,
|
||||
libc::ESRCH => Self::Srch,
|
||||
libc::EINTR => Self::Intr,
|
||||
libc::EIO => Self::Io,
|
||||
libc::ENXIO => Self::Nxio,
|
||||
libc::E2BIG => Self::TooBig,
|
||||
libc::ENOEXEC => Self::Noexec,
|
||||
libc::EBADF => Self::Badf,
|
||||
libc::ECHILD => Self::Child,
|
||||
libc::EAGAIN => Self::Again,
|
||||
libc::ENOMEM => Self::Nomem,
|
||||
libc::EACCES => Self::Acces,
|
||||
libc::EFAULT => Self::Fault,
|
||||
libc::EBUSY => Self::Busy,
|
||||
libc::EEXIST => Self::Exist,
|
||||
libc::EXDEV => Self::Xdev,
|
||||
libc::ENODEV => Self::Nodev,
|
||||
libc::ENOTDIR => Self::Notdir,
|
||||
libc::EISDIR => Self::Isdir,
|
||||
libc::EINVAL => Self::Inval,
|
||||
libc::ENFILE => Self::Nfile,
|
||||
libc::EMFILE => Self::Mfile,
|
||||
libc::ENOTTY => Self::Notty,
|
||||
libc::ETXTBSY => Self::Txtbsy,
|
||||
libc::EFBIG => Self::Fbig,
|
||||
libc::ENOSPC => Self::Nospc,
|
||||
libc::ESPIPE => Self::Spipe,
|
||||
libc::EROFS => Self::Rofs,
|
||||
libc::EMLINK => Self::Mlink,
|
||||
libc::EPIPE => Self::Pipe,
|
||||
libc::EDOM => Self::Dom,
|
||||
libc::ERANGE => Self::Range,
|
||||
libc::EDEADLK => Self::Deadlk,
|
||||
libc::ENAMETOOLONG => Self::Nametoolong,
|
||||
libc::ENOLCK => Self::Nolck,
|
||||
libc::ENOSYS => Self::Nosys,
|
||||
libc::ENOTEMPTY => Self::Notempty,
|
||||
libc::ELOOP => Self::Loop,
|
||||
libc::ENOMSG => Self::Nomsg,
|
||||
libc::EIDRM => Self::Idrm,
|
||||
libc::ENOLINK => Self::Nolink,
|
||||
libc::EPROTO => Self::Proto,
|
||||
libc::EMULTIHOP => Self::Multihop,
|
||||
libc::EBADMSG => Self::Badmsg,
|
||||
libc::EOVERFLOW => Self::Overflow,
|
||||
libc::EILSEQ => Self::Ilseq,
|
||||
libc::ENOTSOCK => Self::Notsock,
|
||||
libc::EDESTADDRREQ => Self::Destaddrreq,
|
||||
libc::EMSGSIZE => Self::Msgsize,
|
||||
libc::EPROTOTYPE => Self::Prototype,
|
||||
libc::ENOPROTOOPT => Self::Noprotoopt,
|
||||
libc::EPROTONOSUPPORT => Self::Protonosupport,
|
||||
libc::EAFNOSUPPORT => Self::Afnosupport,
|
||||
libc::EADDRINUSE => Self::Addrinuse,
|
||||
libc::EADDRNOTAVAIL => Self::Addrnotavail,
|
||||
libc::ENETDOWN => Self::Netdown,
|
||||
libc::ENETUNREACH => Self::Netunreach,
|
||||
libc::ENETRESET => Self::Netreset,
|
||||
libc::ECONNABORTED => Self::Connaborted,
|
||||
libc::ECONNRESET => Self::Connreset,
|
||||
libc::ENOBUFS => Self::Nobufs,
|
||||
libc::EISCONN => Self::Isconn,
|
||||
libc::ENOTCONN => Self::Notconn,
|
||||
libc::ETIMEDOUT => Self::Timedout,
|
||||
libc::ECONNREFUSED => Self::Connrefused,
|
||||
libc::EHOSTUNREACH => Self::Hostunreach,
|
||||
libc::EALREADY => Self::Already,
|
||||
libc::EINPROGRESS => Self::Inprogress,
|
||||
libc::ESTALE => Self::Stale,
|
||||
libc::EDQUOT => Self::Dquot,
|
||||
libc::ECANCELED => Self::Canceled,
|
||||
libc::EOWNERDEAD => Self::Ownerdead,
|
||||
libc::ENOTRECOVERABLE => Self::Notrecoverable,
|
||||
libc::ENOTSUP => Self::Notsup,
|
||||
x => {
|
||||
log::debug!("Unknown errno value: {}", x);
|
||||
Self::Io
|
||||
}
|
||||
},
|
||||
None => {
|
||||
log::debug!("Other I/O error: {}", err);
|
||||
Self::Io
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<types::Fdflags> for OFlags {
|
||||
fn from(fdflags: types::Fdflags) -> Self {
|
||||
let mut nix_flags = Self::empty();
|
||||
@@ -297,13 +206,13 @@ impl From<types::Oflags> for OFlags {
|
||||
}
|
||||
|
||||
impl TryFrom<libc::stat> for types::Filestat {
|
||||
type Error = Errno;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(filestat: libc::stat) -> Result<Self> {
|
||||
fn filestat_to_timestamp(secs: u64, nsecs: u64) -> Result<types::Timestamp> {
|
||||
secs.checked_mul(1_000_000_000)
|
||||
.and_then(|sec_nsec| sec_nsec.checked_add(nsecs))
|
||||
.ok_or(Errno::Overflow)
|
||||
.ok_or(Error::Overflow)
|
||||
}
|
||||
|
||||
let filetype = yanix::file::FileType::from_stat_st_mode(filestat.st_mode);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::File;
|
||||
@@ -144,7 +145,7 @@ pub(crate) fn open(
|
||||
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Socket {
|
||||
return Err(Errno::Notsup);
|
||||
return Err(Error::Notsup);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -160,7 +161,7 @@ pub(crate) fn open(
|
||||
match unsafe { fstatat(dirfd.as_raw_fd(), path, AtFlags::SYMLINK_NOFOLLOW) } {
|
||||
Ok(stat) => {
|
||||
if FileType::from_stat_st_mode(stat.st_mode) == FileType::Symlink {
|
||||
return Err(Errno::Loop);
|
||||
return Err(Error::Loop);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -171,7 +172,7 @@ pub(crate) fn open(
|
||||
// FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on
|
||||
// a symlink.
|
||||
libc::EMLINK if !(nix_all_oflags & OFlags::NOFOLLOW).is_empty() => {
|
||||
return Err(Errno::Loop);
|
||||
return Err(Error::Loop);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -235,7 +236,7 @@ pub(crate) fn filestat_set_times_at(
|
||||
let set_mtim_now = fst_flags.contains(&types::Fstflags::MTIM_NOW);
|
||||
|
||||
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||
return Err(Errno::Inval);
|
||||
return Err(Error::Inval);
|
||||
}
|
||||
|
||||
let atim = if set_atim {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::entry::EntryHandle;
|
||||
use crate::poll::{ClockEventData, FdEventData};
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::wasi::types;
|
||||
use crate::{Error, Result};
|
||||
use std::io;
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
use yanix::file::fionread;
|
||||
@@ -64,7 +65,7 @@ pub(crate) fn oneoff(
|
||||
fn handle_timeout_event(timeout: ClockEventData, events: &mut Vec<types::Event>) {
|
||||
events.push(types::Event {
|
||||
userdata: timeout.userdata,
|
||||
error: Errno::Success,
|
||||
error: types::Errno::Success,
|
||||
type_: types::Eventtype::Clock,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
flags: types::Eventrwflags::empty(),
|
||||
@@ -110,7 +111,7 @@ fn handle_fd_event(
|
||||
let output_event = if revents.contains(PollFlags::POLLNVAL) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Badf,
|
||||
error: Error::Badf.into(),
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
@@ -120,7 +121,7 @@ fn handle_fd_event(
|
||||
} else if revents.contains(PollFlags::POLLERR) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Io,
|
||||
error: Error::Io.into(),
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
@@ -130,7 +131,7 @@ fn handle_fd_event(
|
||||
} else if revents.contains(PollFlags::POLLHUP) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Success,
|
||||
error: types::Errno::Success,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: 0,
|
||||
@@ -140,7 +141,7 @@ fn handle_fd_event(
|
||||
} else if revents.contains(PollFlags::POLLIN) | revents.contains(PollFlags::POLLOUT) {
|
||||
types::Event {
|
||||
userdata: fd_event.userdata,
|
||||
error: Errno::Success,
|
||||
error: types::Errno::Success,
|
||||
type_: fd_event.r#type,
|
||||
fd_readwrite: types::EventFdReadwrite {
|
||||
nbytes: nbytes.try_into()?,
|
||||
|
||||
@@ -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