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,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())
}