wasi-common error cleanup: part 1, yanix (#1226)

* Reuse std::io::Error for raw *nix errno

This commit removes custom `yanix::Errno` and instead (as was
previously suggested) reuses `std::io::Error` to generate and wrap
raw *nix errno value.

* Update wasi-common to use new Yanix error type

This commit updates `wasi-common` to use new way of handling raw
OS error in `yanix`; i.e., via re-use of `std::io::Error` instead
of a custom `Errno` enum.

* Fix formatting

* Unwrap if io::Error created from raw OS error

This commit calls `unwrap` on `err` if that one was created via
`io::Error::last_os_error()`. It also refactors error matching
in several syscalls on the BSD platform (mainly).
This commit is contained in:
Jakub Konka
2020-03-05 10:08:28 +01:00
committed by GitHub
parent 19d8ff2bf5
commit 135a48ca7e
25 changed files with 524 additions and 705 deletions

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use std::mem::MaybeUninit;
#[derive(Debug, Copy, Clone)]
@@ -22,7 +22,7 @@ impl ClockId {
pub fn clock_getres(clock_id: ClockId) -> Result<libc::timespec> {
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
Errno::from_success_code(unsafe {
Error::from_success_code(unsafe {
libc::clock_getres(clock_id.as_raw(), timespec.as_mut_ptr())
})?;
Ok(unsafe { timespec.assume_init() })
@@ -30,7 +30,7 @@ pub fn clock_getres(clock_id: ClockId) -> Result<libc::timespec> {
pub fn clock_gettime(clock_id: ClockId) -> Result<libc::timespec> {
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
Errno::from_success_code(unsafe {
Error::from_success_code(unsafe {
libc::clock_gettime(clock_id.as_raw(), timespec.as_mut_ptr())
})?;
Ok(unsafe { timespec.assume_init() })

View File

@@ -1,10 +1,10 @@
use crate::{
file::FileType,
sys::dir::{iter_impl, EntryImpl},
Errno, Result,
Result,
};
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::{ffi::CStr, ops::Deref, ptr};
use std::{ffi::CStr, io, ops::Deref, ptr};
pub use crate::sys::EntryExt;
@@ -25,7 +25,7 @@ impl Dir {
if let Some(d) = ptr::NonNull::new(d) {
Ok(Self(d))
} else {
let e = Errno::last();
let e = io::Error::last_os_error();
libc::close(fd);
Err(e.into())
}

View File

@@ -1,227 +0,0 @@
//! Errno-specific for different Unix platforms
use crate::Result;
use std::{fmt, io};
use thiserror::Error;
#[derive(Debug, Copy, Clone, Error, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum Errno {
EPERM = libc::EPERM,
ENOENT = libc::ENOENT,
ESRCH = libc::ESRCH,
EINTR = libc::EINTR,
EIO = libc::EIO,
ENXIO = libc::ENXIO,
E2BIG = libc::E2BIG,
ENOEXEC = libc::ENOEXEC,
EBADF = libc::EBADF,
ECHILD = libc::ECHILD,
EAGAIN = libc::EAGAIN,
ENOMEM = libc::ENOMEM,
EACCES = libc::EACCES,
EFAULT = libc::EFAULT,
EBUSY = libc::EBUSY,
EEXIST = libc::EEXIST,
EXDEV = libc::EXDEV,
ENODEV = libc::ENODEV,
ENOTDIR = libc::ENOTDIR,
EISDIR = libc::EISDIR,
EINVAL = libc::EINVAL,
ENFILE = libc::ENFILE,
EMFILE = libc::EMFILE,
ENOTTY = libc::ENOTTY,
ETXTBSY = libc::ETXTBSY,
EFBIG = libc::EFBIG,
ENOSPC = libc::ENOSPC,
ESPIPE = libc::ESPIPE,
EROFS = libc::EROFS,
EMLINK = libc::EMLINK,
EPIPE = libc::EPIPE,
EDOM = libc::EDOM,
ERANGE = libc::ERANGE,
EDEADLK = libc::EDEADLK,
ENAMETOOLONG = libc::ENAMETOOLONG,
ENOLCK = libc::ENOLCK,
ENOSYS = libc::ENOSYS,
ENOTEMPTY = libc::ENOTEMPTY,
ELOOP = libc::ELOOP,
ENOMSG = libc::ENOMSG,
EIDRM = libc::EIDRM,
ENOLINK = libc::ENOLINK,
EPROTO = libc::EPROTO,
EMULTIHOP = libc::EMULTIHOP,
EBADMSG = libc::EBADMSG,
EOVERFLOW = libc::EOVERFLOW,
EILSEQ = libc::EILSEQ,
ENOTSOCK = libc::ENOTSOCK,
EDESTADDRREQ = libc::EDESTADDRREQ,
EMSGSIZE = libc::EMSGSIZE,
EPROTOTYPE = libc::EPROTOTYPE,
ENOPROTOOPT = libc::ENOPROTOOPT,
EPROTONOSUPPORT = libc::EPROTONOSUPPORT,
EAFNOSUPPORT = libc::EAFNOSUPPORT,
EADDRINUSE = libc::EADDRINUSE,
EADDRNOTAVAIL = libc::EADDRNOTAVAIL,
ENETDOWN = libc::ENETDOWN,
ENETUNREACH = libc::ENETUNREACH,
ENETRESET = libc::ENETRESET,
ECONNABORTED = libc::ECONNABORTED,
ECONNRESET = libc::ECONNRESET,
ENOBUFS = libc::ENOBUFS,
EISCONN = libc::EISCONN,
ENOTCONN = libc::ENOTCONN,
ETIMEDOUT = libc::ETIMEDOUT,
ECONNREFUSED = libc::ECONNREFUSED,
EHOSTUNREACH = libc::EHOSTUNREACH,
EALREADY = libc::EALREADY,
EINPROGRESS = libc::EINPROGRESS,
ESTALE = libc::ESTALE,
EDQUOT = libc::EDQUOT,
ECANCELED = libc::ECANCELED,
EOWNERDEAD = libc::EOWNERDEAD,
ENOTRECOVERABLE = libc::ENOTRECOVERABLE,
}
impl Errno {
pub fn from_i32(err: i32) -> Self {
match err {
libc::EPERM => Self::EPERM,
libc::ENOENT => Self::ENOENT,
libc::ESRCH => Self::ESRCH,
libc::EINTR => Self::EINTR,
libc::EIO => Self::EIO,
libc::ENXIO => Self::ENXIO,
libc::E2BIG => Self::E2BIG,
libc::ENOEXEC => Self::ENOEXEC,
libc::EBADF => Self::EBADF,
libc::ECHILD => Self::ECHILD,
libc::EAGAIN => Self::EAGAIN,
libc::ENOMEM => Self::ENOMEM,
libc::EACCES => Self::EACCES,
libc::EFAULT => Self::EFAULT,
libc::EBUSY => Self::EBUSY,
libc::EEXIST => Self::EEXIST,
libc::EXDEV => Self::EXDEV,
libc::ENODEV => Self::ENODEV,
libc::ENOTDIR => Self::ENOTDIR,
libc::EISDIR => Self::EISDIR,
libc::EINVAL => Self::EINVAL,
libc::ENFILE => Self::ENFILE,
libc::EMFILE => Self::EMFILE,
libc::ENOTTY => Self::ENOTTY,
libc::ETXTBSY => Self::ETXTBSY,
libc::EFBIG => Self::EFBIG,
libc::ENOSPC => Self::ENOSPC,
libc::ESPIPE => Self::ESPIPE,
libc::EROFS => Self::EROFS,
libc::EMLINK => Self::EMLINK,
libc::EPIPE => Self::EPIPE,
libc::EDOM => Self::EDOM,
libc::ERANGE => Self::ERANGE,
libc::EDEADLK => Self::EDEADLK,
libc::ENAMETOOLONG => Self::ENAMETOOLONG,
libc::ENOLCK => Self::ENOLCK,
libc::ENOSYS => Self::ENOSYS,
libc::ENOTEMPTY => Self::ENOTEMPTY,
libc::ELOOP => Self::ELOOP,
libc::ENOMSG => Self::ENOMSG,
libc::EIDRM => Self::EIDRM,
libc::ENOLINK => Self::ENOLINK,
libc::EPROTO => Self::EPROTO,
libc::EMULTIHOP => Self::EMULTIHOP,
libc::EBADMSG => Self::EBADMSG,
libc::EOVERFLOW => Self::EOVERFLOW,
libc::EILSEQ => Self::EILSEQ,
libc::ENOTSOCK => Self::ENOTSOCK,
libc::EDESTADDRREQ => Self::EDESTADDRREQ,
libc::EMSGSIZE => Self::EMSGSIZE,
libc::EPROTOTYPE => Self::EPROTOTYPE,
libc::ENOPROTOOPT => Self::ENOPROTOOPT,
libc::EPROTONOSUPPORT => Self::EPROTONOSUPPORT,
libc::EAFNOSUPPORT => Self::EAFNOSUPPORT,
libc::EADDRINUSE => Self::EADDRINUSE,
libc::EADDRNOTAVAIL => Self::EADDRNOTAVAIL,
libc::ENETDOWN => Self::ENETDOWN,
libc::ENETUNREACH => Self::ENETUNREACH,
libc::ENETRESET => Self::ENETRESET,
libc::ECONNABORTED => Self::ECONNABORTED,
libc::ECONNRESET => Self::ECONNRESET,
libc::ENOBUFS => Self::ENOBUFS,
libc::EISCONN => Self::EISCONN,
libc::ENOTCONN => Self::ENOTCONN,
libc::ETIMEDOUT => Self::ETIMEDOUT,
libc::ECONNREFUSED => Self::ECONNREFUSED,
libc::EHOSTUNREACH => Self::EHOSTUNREACH,
libc::EALREADY => Self::EALREADY,
libc::EINPROGRESS => Self::EINPROGRESS,
libc::ESTALE => Self::ESTALE,
libc::EDQUOT => Self::EDQUOT,
libc::ECANCELED => Self::ECANCELED,
libc::EOWNERDEAD => Self::EOWNERDEAD,
libc::ENOTRECOVERABLE => Self::ENOTRECOVERABLE,
other => {
log::warn!("Unknown errno: {}", other);
Self::ENOSYS
}
}
}
pub fn last() -> Self {
let errno = io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::ENOSYS);
Self::from_i32(errno)
}
pub fn from_success_code<T: IsZero>(t: T) -> Result<()> {
if t.is_zero() {
Ok(())
} else {
Err(Self::last().into())
}
}
pub fn from_result<T: IsMinusOne>(t: T) -> Result<T> {
if t.is_minus_one() {
Err(Self::last().into())
} else {
Ok(t)
}
}
}
impl fmt::Display for Errno {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Errno code: {}", self)
}
}
#[doc(hidden)]
pub trait IsZero {
fn is_zero(&self) -> bool;
}
macro_rules! impl_is_zero {
($($t:ident)*) => ($(impl IsZero for $t {
fn is_zero(&self) -> bool {
*self == 0
}
})*)
}
impl_is_zero! { i32 i64 isize }
#[doc(hidden)]
pub trait IsMinusOne {
fn is_minus_one(&self) -> bool;
}
macro_rules! impl_is_minus_one {
($($t:ident)*) => ($(impl IsMinusOne for $t {
fn is_minus_one(&self) -> bool {
*self == -1
}
})*)
}
impl_is_minus_one! { i32 i64 isize }

View File

@@ -0,0 +1,61 @@
//! Error type
use crate::Result;
use std::{ffi, io, num};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error {0}")]
Io(#[from] io::Error),
#[error("a nul byte was not found in the expected position")]
Nul(#[from] ffi::NulError),
#[error("integral type conversion failed")]
IntConversion(#[from] num::TryFromIntError),
}
impl Error {
pub fn from_success_code<T: IsZero>(t: T) -> Result<()> {
if t.is_zero() {
Ok(())
} else {
Err(Self::from(io::Error::last_os_error()))
}
}
pub fn from_result<T: IsMinusOne>(t: T) -> Result<T> {
if t.is_minus_one() {
Err(Self::from(io::Error::last_os_error()))
} else {
Ok(t)
}
}
}
#[doc(hidden)]
pub trait IsZero {
fn is_zero(&self) -> bool;
}
macro_rules! impl_is_zero {
($($t:ident)*) => ($(impl IsZero for $t {
fn is_zero(&self) -> bool {
*self == 0
}
})*)
}
impl_is_zero! { i32 i64 isize }
#[doc(hidden)]
pub trait IsMinusOne {
fn is_minus_one(&self) -> bool;
}
macro_rules! impl_is_minus_one {
($($t:ident)*) => ($(impl IsMinusOne for $t {
fn is_minus_one(&self) -> bool {
*self == -1
}
})*)
}
impl_is_minus_one! { i32 i64 isize }

View File

@@ -1,6 +1,6 @@
use crate::{
file::{FdFlag, OFlag},
Errno, Result,
Error, Result,
};
use std::os::unix::prelude::*;
@@ -9,7 +9,7 @@ pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
// the minimum duplicated RawFd number. In our case, I don't
// think we have to worry about this that much, so passing in
// the RawFd descriptor we want duplicated
Errno::from_result(if close_on_exec {
Error::from_result(if close_on_exec {
libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, fd)
} else {
libc::fcntl(fd, libc::F_DUPFD, fd)
@@ -17,17 +17,17 @@ pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
}
pub unsafe fn get_fd_flags(fd: RawFd) -> Result<FdFlag> {
Errno::from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlag::from_bits_truncate)
Error::from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlag::from_bits_truncate)
}
pub unsafe fn set_fd_flags(fd: RawFd, flags: FdFlag) -> Result<()> {
Errno::from_success_code(libc::fcntl(fd, libc::F_SETFD, flags.bits()))
Error::from_success_code(libc::fcntl(fd, libc::F_SETFD, flags.bits()))
}
pub unsafe fn get_status_flags(fd: RawFd) -> Result<OFlag> {
Errno::from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlag::from_bits_truncate)
Error::from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlag::from_bits_truncate)
}
pub unsafe fn set_status_flags(fd: RawFd, flags: OFlag) -> Result<()> {
Errno::from_success_code(libc::fcntl(fd, libc::F_SETFL, flags.bits()))
Error::from_success_code(libc::fcntl(fd, libc::F_SETFL, flags.bits()))
}

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use bitflags::bitflags;
use cfg_if::cfg_if;
use std::{
@@ -137,7 +137,7 @@ pub unsafe fn openat<P: AsRef<OsStr>>(
mode: Mode,
) -> Result<RawFd> {
let path = CString::new(path.as_ref().as_bytes())?;
Errno::from_result(libc::openat(
Error::from_result(libc::openat(
dirfd,
path.as_ptr(),
oflag.bits(),
@@ -148,7 +148,7 @@ pub unsafe fn openat<P: AsRef<OsStr>>(
pub unsafe fn readlinkat<P: AsRef<OsStr>>(dirfd: RawFd, path: P) -> Result<OsString> {
let path = CString::new(path.as_ref().as_bytes())?;
let buffer = &mut [0u8; libc::PATH_MAX as usize + 1];
Errno::from_result(libc::readlinkat(
Error::from_result(libc::readlinkat(
dirfd,
path.as_ptr(),
buffer.as_mut_ptr() as *mut _,
@@ -162,7 +162,7 @@ pub unsafe fn readlinkat<P: AsRef<OsStr>>(dirfd: RawFd, path: P) -> Result<OsStr
pub unsafe fn mkdirat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, mode: Mode) -> Result<()> {
let path = CString::new(path.as_ref().as_bytes())?;
Errno::from_success_code(libc::mkdirat(dirfd, path.as_ptr(), mode.bits()))
Error::from_success_code(libc::mkdirat(dirfd, path.as_ptr(), mode.bits()))
}
pub unsafe fn linkat<P: AsRef<OsStr>>(
@@ -174,7 +174,7 @@ pub unsafe fn linkat<P: AsRef<OsStr>>(
) -> Result<()> {
let old_path = CString::new(old_path.as_ref().as_bytes())?;
let new_path = CString::new(new_path.as_ref().as_bytes())?;
Errno::from_success_code(libc::linkat(
Error::from_success_code(libc::linkat(
old_dirfd,
old_path.as_ptr(),
new_dirfd,
@@ -185,7 +185,7 @@ pub unsafe fn linkat<P: AsRef<OsStr>>(
pub unsafe fn unlinkat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) -> Result<()> {
let path = CString::new(path.as_ref().as_bytes())?;
Errno::from_success_code(libc::unlinkat(dirfd, path.as_ptr(), flags.bits()))
Error::from_success_code(libc::unlinkat(dirfd, path.as_ptr(), flags.bits()))
}
pub unsafe fn renameat<P: AsRef<OsStr>>(
@@ -196,7 +196,7 @@ pub unsafe fn renameat<P: AsRef<OsStr>>(
) -> Result<()> {
let old_path = CString::new(old_path.as_ref().as_bytes())?;
let new_path = CString::new(new_path.as_ref().as_bytes())?;
Errno::from_success_code(libc::renameat(
Error::from_success_code(libc::renameat(
old_dirfd,
old_path.as_ptr(),
new_dirfd,
@@ -207,7 +207,7 @@ pub unsafe fn renameat<P: AsRef<OsStr>>(
pub unsafe fn symlinkat<P: AsRef<OsStr>>(old_path: P, new_dirfd: RawFd, new_path: P) -> Result<()> {
let old_path = CString::new(old_path.as_ref().as_bytes())?;
let new_path = CString::new(new_path.as_ref().as_bytes())?;
Errno::from_success_code(libc::symlinkat(
Error::from_success_code(libc::symlinkat(
old_path.as_ptr(),
new_dirfd,
new_path.as_ptr(),
@@ -218,7 +218,7 @@ pub unsafe fn fstatat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) ->
use std::mem::MaybeUninit;
let path = CString::new(path.as_ref().as_bytes())?;
let mut filestat = MaybeUninit::<libc::stat>::uninit();
Errno::from_result(libc::fstatat(
Error::from_result(libc::fstatat(
dirfd,
path.as_ptr(),
filestat.as_mut_ptr(),
@@ -230,20 +230,20 @@ pub unsafe fn fstatat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) ->
pub unsafe fn fstat(fd: RawFd) -> Result<libc::stat> {
use std::mem::MaybeUninit;
let mut filestat = MaybeUninit::<libc::stat>::uninit();
Errno::from_result(libc::fstat(fd, filestat.as_mut_ptr()))?;
Error::from_result(libc::fstat(fd, filestat.as_mut_ptr()))?;
Ok(filestat.assume_init())
}
/// `fionread()` function, equivalent to `ioctl(fd, FIONREAD, *bytes)`.
pub unsafe fn fionread(fd: RawFd) -> Result<u32> {
let mut nread: libc::c_int = 0;
Errno::from_result(libc::ioctl(fd, libc::FIONREAD, &mut nread as *mut _))?;
Error::from_result(libc::ioctl(fd, libc::FIONREAD, &mut nread as *mut _))?;
Ok(nread.try_into()?)
}
/// This function is unsafe because it operates on a raw file descriptor.
/// It's provided, because std::io::Seek requires a mutable borrow.
pub unsafe fn tell(fd: RawFd) -> Result<u64> {
let offset: i64 = Errno::from_result(libc::lseek(fd, 0, libc::SEEK_CUR))?;
let offset: i64 = Error::from_result(libc::lseek(fd, 0, libc::SEEK_CUR))?;
Ok(offset.try_into()?)
}

View File

@@ -16,25 +16,12 @@ pub mod file;
pub mod poll;
pub mod socket;
mod errno;
mod error;
mod sys;
pub mod fadvise {
pub use super::sys::fadvise::*;
}
pub use errno::Errno;
use std::{ffi, num};
use thiserror::Error;
pub type Result<T> = std::result::Result<T, YanixError>;
#[derive(Debug, Error)]
pub enum YanixError {
#[error("raw os error {0}")]
Errno(#[from] Errno),
#[error("a nul byte was not found in the expected position")]
NulError(#[from] ffi::NulError),
#[error("integral type conversion failed")]
TryFromIntError(#[from] num::TryFromIntError),
}
pub use error::Error;
pub type Result<T> = std::result::Result<T, Error>;

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use bitflags::bitflags;
use std::{convert::TryInto, os::unix::prelude::*};
@@ -36,7 +36,7 @@ impl PollFd {
}
pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<usize> {
Errno::from_result(unsafe {
Error::from_result(unsafe {
libc::poll(
fds.as_mut_ptr() as *mut libc::pollfd,
fds.len() as libc::nfds_t,

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use std::os::unix::prelude::*;
#[derive(Debug, Clone, Copy)]
@@ -15,7 +15,7 @@ pub unsafe fn get_socket_type(fd: RawFd) -> Result<SockType> {
use std::mem::{self, MaybeUninit};
let mut buffer = MaybeUninit::<SockType>::zeroed().assume_init();
let mut out_len = mem::size_of::<SockType>() as libc::socklen_t;
Errno::from_success_code(libc::getsockopt(
Error::from_success_code(libc::getsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_TYPE,

View File

@@ -1,8 +1,8 @@
use crate::{
dir::{Dir, Entry, EntryExt, SeekLoc},
Errno, Result,
Result,
};
use std::ops::Deref;
use std::{io, ops::Deref};
#[derive(Copy, Clone, Debug)]
pub(crate) struct EntryImpl {
@@ -19,16 +19,17 @@ impl Deref for EntryImpl {
}
pub(crate) fn iter_impl(dir: &Dir) -> Option<Result<EntryImpl>> {
let errno = Errno::last();
let errno = io::Error::last_os_error();
let dirent = unsafe { libc::readdir(dir.as_raw().as_ptr()) };
if dirent.is_null() {
if errno != Errno::last() {
let curr_errno = io::Error::last_os_error();
if errno.raw_os_error() != curr_errno.raw_os_error() {
// TODO This should be verified on different BSD-flavours.
//
// According to 4.3BSD/POSIX.1-2001 man pages, there was an error
// if the errno value has changed at some point during the sequence
// of readdir calls.
Some(Err(Errno::last().into()))
Some(Err(curr_errno.into()))
} else {
// Not an error. We've simply reached the end of the stream.
None

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use std::{convert::TryInto, os::unix::prelude::*};
#[cfg(not(any(target_os = "freebsd", target_os = "netbsd")))]
@@ -48,7 +48,7 @@ pub unsafe fn posix_fadvise(
ra_offset: offset,
ra_count: len.try_into()?,
};
Errno::from_success_code(libc::fcntl(fd, libc::F_RDADVISE, &advisory))
Error::from_success_code(libc::fcntl(fd, libc::F_RDADVISE, &advisory))
}
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
@@ -58,7 +58,7 @@ pub unsafe fn posix_fadvise(
len: libc::off_t,
advice: PosixFadviseAdvice,
) -> Result<()> {
Errno::from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
Error::from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
}
// On BSDs without support we leave it as no-op

View File

@@ -1,5 +1,5 @@
use crate::{Errno, Result};
use std::os::unix::prelude::*;
use crate::Result;
use std::{io, os::unix::prelude::*};
pub unsafe fn isatty(fd: RawFd) -> Result<bool> {
let res = libc::isatty(fd);
@@ -8,9 +8,13 @@ pub unsafe fn isatty(fd: RawFd) -> Result<bool> {
Ok(true)
} else {
// ... otherwise 0 is returned, and errno is set to indicate the error.
let errno = Errno::last();
if errno == Errno::ENOTTY {
Ok(false)
let errno = io::Error::last_os_error();
if let Some(raw_errno) = errno.raw_os_error() {
if raw_errno == libc::ENOTTY {
Ok(false)
} else {
Err(errno.into())
}
} else {
Err(errno.into())
}

View File

@@ -1,8 +1,8 @@
use crate::{
dir::{Dir, Entry, EntryExt, SeekLoc},
Errno, Result,
Result,
};
use std::ops::Deref;
use std::{io, ops::Deref};
#[derive(Copy, Clone, Debug)]
pub(crate) struct EntryImpl(libc::dirent64);
@@ -26,16 +26,17 @@ impl EntryExt for Entry {
}
pub(crate) fn iter_impl(dir: &Dir) -> Option<Result<EntryImpl>> {
let errno = Errno::last();
let errno = io::Error::last_os_error();
let dirent = unsafe { libc::readdir64(dir.as_raw().as_ptr()) };
if dirent.is_null() {
if errno != Errno::last() {
let curr_errno = io::Error::last_os_error();
if errno.raw_os_error() != curr_errno.raw_os_error() {
// TODO This should be verified on different BSD-flavours.
//
// According to 4.3BSD/POSIX.1-2001 man pages, there was an error
// if the errno value has changed at some point during the sequence
// of readdir calls.
Some(Err(Errno::last().into()))
Some(Err(curr_errno.into()))
} else {
// Not an error. We've simply reached the end of the stream.
None

View File

@@ -1,4 +1,4 @@
use crate::{Errno, Result};
use crate::{Error, Result};
use std::os::unix::prelude::*;
#[derive(Debug, Copy, Clone)]
@@ -18,5 +18,5 @@ pub unsafe fn posix_fadvise(
len: libc::off_t,
advice: PosixFadviseAdvice,
) -> Result<()> {
Errno::from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
Error::from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
}

View File

@@ -1,5 +1,5 @@
use crate::{Errno, Result};
use std::os::unix::prelude::*;
use crate::Result;
use std::{io, os::unix::prelude::*};
pub unsafe fn isatty(fd: RawFd) -> Result<bool> {
let res = libc::isatty(fd);
@@ -8,14 +8,18 @@ pub unsafe fn isatty(fd: RawFd) -> Result<bool> {
Ok(true)
} else {
// ... otherwise 0 is returned, and errno is set to indicate the error.
let errno = Errno::last();
// While POSIX specifies ENOTTY if the passed
// fd is *not* a tty, on Linux, some implementations
// may return EINVAL instead.
//
// https://linux.die.net/man/3/isatty
if errno == Errno::ENOTTY || errno == Errno::EINVAL {
Ok(false)
let errno = io::Error::last_os_error();
if let Some(raw_errno) = errno.raw_os_error() {
// While POSIX specifies ENOTTY if the passed
// fd is *not* a tty, on Linux, some implementations
// may return EINVAL instead.
//
// https://linux.die.net/man/3/isatty
if raw_errno == libc::ENOTTY || raw_errno == libc::EINVAL {
Ok(false)
} else {
Err(errno.into())
}
} else {
Err(errno.into())
}