summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/errno.rs89
-rw-r--r--src/fcntl.rs22
-rw-r--r--src/lib.rs18
-rw-r--r--src/mount.rs9
-rw-r--r--src/mqueue.rs33
-rw-r--r--src/poll.rs9
-rw-r--r--src/sched.rs27
-rw-r--r--src/sys/epoll.rs18
-rw-r--r--src/sys/event.rs21
-rw-r--r--src/sys/eventfd.rs8
-rw-r--r--src/sys/ioctl/platform/linux.rs6
-rw-r--r--src/sys/memfd.rs6
-rw-r--r--src/sys/mman.rs41
-rw-r--r--src/sys/ptrace.rs19
-rw-r--r--src/sys/quota.rs7
-rw-r--r--src/sys/select.rs9
-rw-r--r--src/sys/signal.rs43
-rw-r--r--src/sys/signalfd.rs8
-rw-r--r--src/sys/socket/addr.rs4
-rw-r--r--src/sys/socket/mod.rs102
-rw-r--r--src/sys/socket/sockopt.rs9
-rw-r--r--src/sys/stat.rs19
-rw-r--r--src/sys/statfs.rs9
-rw-r--r--src/sys/statvfs.rs9
-rw-r--r--src/sys/termios.rs35
-rw-r--r--src/sys/uio.rs42
-rw-r--r--src/sys/wait.rs14
-rw-r--r--src/unistd.rs129
28 files changed, 259 insertions, 506 deletions
diff --git a/src/errno.rs b/src/errno.rs
index 5ca8ef08..8de55b80 100644
--- a/src/errno.rs
+++ b/src/errno.rs
@@ -1,9 +1,10 @@
use libc::c_int;
+use std::{fmt, io, error, result};
+use Error;
pub use self::consts::*;
pub use self::consts::Errno::*;
-
#[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "freebsd"))]
@@ -52,28 +53,72 @@ pub fn errno() -> i32 {
}
}
-macro_rules! impl_errno {
- ($errno:ty) => {
- impl $errno {
- pub fn last() -> Errno {
- super::last()
- }
+impl Errno {
+ pub fn last() -> Self {
+ last()
+ }
+
+ pub fn desc(self) -> &'static str {
+ desc(self)
+ }
- pub fn desc(self) -> &'static str {
- super::desc(self)
- }
+ pub fn from_i32(err: i32) -> Errno {
+ from_i32(err)
+ }
- pub fn from_i32(err: i32) -> Errno {
- from_i32(err)
- }
+ pub unsafe fn clear() -> () {
+ clear()
+ }
- pub unsafe fn clear() -> () {
- super::clear()
- }
+ /// Returns `Ok(value)` if it does not contain the sentinel value. This
+ /// should not be used when `-1` is not the errno sentinel value.
+ pub fn result<S: ErrnoSentinel + PartialEq<S>>(value: S) -> Result<S> {
+ if value == S::sentinel() {
+ Err(Error::Sys(Self::last()))
+ } else {
+ Ok(value)
}
}
}
+/// The sentinel value indicates that a function failed and more detailed
+/// information about the error can be found in `errno`
+pub trait ErrnoSentinel: Sized {
+ fn sentinel() -> Self;
+}
+
+impl ErrnoSentinel for isize {
+ fn sentinel() -> Self { -1 }
+}
+
+impl ErrnoSentinel for i32 {
+ fn sentinel() -> Self { -1 }
+}
+
+impl ErrnoSentinel for i64 {
+ fn sentinel() -> Self { -1 }
+}
+
+impl error::Error for Errno {
+ fn description(&self) -> &str {
+ self.desc()
+ }
+}
+
+impl fmt::Display for Errno {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{:?}: {}", self, self.desc())
+ }
+}
+
+impl From<Errno> for io::Error {
+ fn from(err: Errno) -> Self {
+ io::Error::from_raw_os_error(err as i32)
+ }
+}
+
+pub type Result<T> = result::Result<T, Error>;
+
fn last() -> Errno {
Errno::from_i32(errno())
}
@@ -618,8 +663,6 @@ mod consts {
EHWPOISON = 133,
}
- impl_errno!(Errno);
-
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;
@@ -880,8 +923,6 @@ mod consts {
EQFULL = 106,
}
- impl_errno!(Errno);
-
pub const ELAST: Errno = Errno::EQFULL;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;
@@ -1108,8 +1149,6 @@ mod consts {
}
- impl_errno!(Errno);
-
pub const ELAST: Errno = Errno::EOWNERDEAD;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;
@@ -1330,8 +1369,6 @@ mod consts {
EASYNC = 99,
}
- impl_errno!(Errno);
-
pub const ELAST: Errno = Errno::EASYNC;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
pub const EDEADLOCK: Errno = Errno::EDEADLK;
@@ -1547,8 +1584,6 @@ mod consts {
ENOTSUP = 91,
}
- impl_errno!(Errno);
-
pub const ELAST: Errno = Errno::ENOTSUP;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
@@ -1758,8 +1793,6 @@ mod consts {
EPROTO = 96,
}
- impl_errno!(Errno);
-
pub const ELAST: Errno = Errno::ENOTSUP;
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
diff --git a/src/fcntl.rs b/src/fcntl.rs
index 7ff27195..197f0059 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -1,5 +1,5 @@
-use {Error, Result, NixPath};
-use errno::Errno;
+use NixPath;
+use errno::{Errno, Result};
use libc::{c_int, c_uint};
use sys::stat::Mode;
use std::os::unix::io::RawFd;
@@ -102,11 +102,7 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
unsafe { ffi::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
}));
- if fd < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(fd)
+ Errno::result(fd)
}
pub enum FcntlArg<'a> {
@@ -157,11 +153,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
}
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
pub enum FlockArg {
@@ -187,11 +179,7 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {
}
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
diff --git a/src/lib.rs b/src/lib.rs
index 0de90494..d430ac75 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,8 +18,9 @@ extern crate libc;
#[cfg(test)]
extern crate nix_test as nixtest;
-// Re-export some libc constants
+// Re-exports
pub use libc::{c_int, c_void};
+pub use errno::{Errno, Result};
pub mod errno;
pub mod features;
@@ -44,12 +45,12 @@ pub mod unistd;
/*
*
- * ===== Result / Error =====
+ * ===== Error =====
*
*/
use libc::c_char;
-use std::{ptr, result};
+use std::ptr;
use std::ffi::{CStr, OsStr};
use std::path::{Path, PathBuf};
use std::os::unix::ffi::OsStrExt;
@@ -58,8 +59,6 @@ use std::fmt;
use std::error;
use libc::PATH_MAX;
-pub type Result<T> = result::Result<T, Error>;
-
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Error {
Sys(errno::Errno),
@@ -224,12 +223,3 @@ impl<'a, NP: ?Sized + NixPath> NixPath for Option<&'a NP> {
}
}
}
-
-#[inline]
-pub fn from_ffi(res: libc::c_int) -> Result<()> {
- if res != 0 {
- return Err(Error::Sys(errno::Errno::last()));
- }
-
- Ok(())
-}
diff --git a/src/mount.rs b/src/mount.rs
index cd0afa0a..c08c3fd8 100644
--- a/src/mount.rs
+++ b/src/mount.rs
@@ -1,5 +1,6 @@
use libc::{c_ulong, c_int};
-use {Result, NixPath, from_ffi};
+use NixPath;
+use errno::{Errno, Result};
bitflags!(
flags MsFlags: c_ulong {
@@ -90,7 +91,7 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P
})
})))));
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
@@ -98,7 +99,7 @@ pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
unsafe { ffi::umount(cstr.as_ptr()) }
}));
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
@@ -106,5 +107,5 @@ pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
unsafe { ffi::umount2(cstr.as_ptr(), flags.bits) }
}));
- from_ffi(res)
+ Errno::result(res).map(drop)
}
diff --git a/src/mqueue.rs b/src/mqueue.rs
index e24b6800..1625d4ef 100644
--- a/src/mqueue.rs
+++ b/src/mqueue.rs
@@ -2,8 +2,7 @@
//!
//! [Further reading and details on the C API](http://man7.org/linux/man-pages/man7/mq_overview.7.html)
-use {Error, Result, from_ffi};
-use errno::Errno;
+use errno::{Errno, Result};
use libc::{c_int, c_long, c_char, size_t, mode_t, strlen};
use std::ffi::CString;
@@ -80,21 +79,17 @@ impl MqAttr {
pub fn mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: &MqAttr) -> Result<MQd> {
let res = unsafe { ffi::mq_open(name.as_ptr(), oflag.bits(), mode.bits() as mode_t, attr as *const MqAttr) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
pub fn mq_unlink(name: &CString) -> Result<()> {
let res = unsafe { ffi::mq_unlink(name.as_ptr()) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn mq_close(mqdes: MQd) -> Result<()> {
let res = unsafe { ffi::mq_close(mqdes) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
@@ -102,30 +97,20 @@ pub fn mq_receive(mqdes: MQd, message: &mut [u8], msq_prio: u32) -> Result<usize
let len = message.len() as size_t;
let res = unsafe { ffi::mq_receive(mqdes, message.as_mut_ptr() as *mut c_char, len, &msq_prio) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
pub fn mq_send(mqdes: MQd, message: &CString, msq_prio: u32) -> Result<usize> {
let len = unsafe { strlen(message.as_ptr()) as size_t };
let res = unsafe { ffi::mq_send(mqdes, message.as_ptr(), len, msq_prio) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
pub fn mq_getattr(mqd: MQd) -> Result<MqAttr> {
let mut attr = MqAttr::new(0, 0, 0, 0);
let res = unsafe { ffi::mq_getattr(mqd, &mut attr) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(attr)
}
@@ -137,9 +122,7 @@ pub fn mq_getattr(mqd: MQd) -> Result<MqAttr> {
pub fn mq_setattr(mqd: MQd, newattr: &MqAttr) -> Result<MqAttr> {
let mut attr = MqAttr::new(0, 0, 0, 0);
let res = unsafe { ffi::mq_setattr(mqd, newattr as *const MqAttr, &mut attr) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(attr)
}
diff --git a/src/poll.rs b/src/poll.rs
index d43e358b..631a47cc 100644
--- a/src/poll.rs
+++ b/src/poll.rs
@@ -1,6 +1,5 @@
use libc::c_int;
-use {Error, Result};
-use errno::Errno;
+use errno::{Errno, Result};
pub use self::ffi::PollFd;
pub use self::ffi::consts::*;
@@ -72,9 +71,5 @@ pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> {
ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout)
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
diff --git a/src/sched.rs b/src/sched.rs
index 0c8837f5..7d7ec0f0 100644
--- a/src/sched.rs
+++ b/src/sched.rs
@@ -1,8 +1,7 @@
use std::mem;
use std::os::unix::io::RawFd;
use libc::{c_int, c_uint, c_void, c_ulong, pid_t};
-use errno::Errno;
-use {Result, Error};
+use errno::{Errno, Result};
pub type CloneFlags = c_uint;
@@ -172,11 +171,7 @@ pub fn sched_setaffinity(pid: isize, cpuset: &CpuSet) -> Result<()> {
ffi::sched_setaffinity(pid as pid_t, mem::size_of::<CpuSet>() as size_t, mem::transmute(cpuset))
};
- if res != 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(())
- }
+ Errno::result(res).map(drop)
}
pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags) -> Result<pid_t> {
@@ -190,29 +185,17 @@ pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags) -> Result<pid
ffi::clone(mem::transmute(callback), ptr as *mut c_void, flags, &mut cb)
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
pub fn unshare(flags: CloneFlags) -> Result<()> {
let res = unsafe { ffi::unshare(flags) };
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
pub fn setns(fd: RawFd, nstype: CloneFlags) -> Result<()> {
let res = unsafe { ffi::setns(fd, nstype) };
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs
index 22587641..a079649a 100644
--- a/src/sys/epoll.rs
+++ b/src/sys/epoll.rs
@@ -1,5 +1,4 @@
-use {Error, Result, from_ffi};
-use errno::Errno;
+use errno::{Errno, Result};
use libc::c_int;
use std::os::unix::io::RawFd;
@@ -76,17 +75,14 @@ fn test_epoll_event_size() {
pub fn epoll_create() -> Result<RawFd> {
let res = unsafe { ffi::epoll_create(1024) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
#[inline]
pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Result<()> {
let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) };
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
#[inline]
@@ -95,9 +91,5 @@ pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) ->
ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int)
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
diff --git a/src/sys/event.rs b/src/sys/event.rs
index 6498cb97..eb99f7d9 100644
--- a/src/sys/event.rs
+++ b/src/sys/event.rs
@@ -1,8 +1,7 @@
/* TOOD: Implement for other kqueue based systems
*/
-use {Error, Result};
-use errno::Errno;
+use errno::{Errno, Result};
#[cfg(not(target_os = "netbsd"))]
use libc::{timespec, time_t, c_int, c_long, uintptr_t};
#[cfg(target_os = "netbsd")]
@@ -277,11 +276,7 @@ pub const EV_OOBAND: EventFlag = EV_FLAG1;
pub fn kqueue() -> Result<RawFd> {
let res = unsafe { ffi::kqueue() };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
pub fn kevent(kq: RawFd,
@@ -314,11 +309,7 @@ pub fn kevent_ts(kq: RawFd,
if let Some(ref timeout) = timeout_opt {timeout as *const timespec} else {ptr::null()})
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
#[cfg(target_os = "netbsd")]
@@ -337,11 +328,7 @@ pub fn kevent_ts(kq: RawFd,
if let Some(ref timeout) = timeout_opt {timeout as *const timespec} else {ptr::null()})
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
#[cfg(not(target_os = "netbsd"))]
diff --git a/src/sys/eventfd.rs b/src/sys/eventfd.rs
index 80f7f47c..d2cd6b93 100644
--- a/src/sys/eventfd.rs
+++ b/src/sys/eventfd.rs
@@ -1,6 +1,6 @@
use libc;
use std::os::unix::io::RawFd;
-use {Error, Result};
+use errno::{Errno, Result};
bitflags!(
flags EventFdFlag: libc::c_int {
@@ -22,10 +22,6 @@ pub fn eventfd(initval: usize, flags: EventFdFlag) -> Result<RawFd> {
unsafe {
let res = ffi::eventfd(initval as libc::c_uint, flags.bits());
- if res < 0 {
- return Err(Error::last());
- }
-
- Ok(res as RawFd)
+ Errno::result(res).map(|r| r as RawFd)
}
}
diff --git a/src/sys/ioctl/platform/linux.rs b/src/sys/ioctl/platform/linux.rs
index b0fa6bee..60311224 100644
--- a/src/sys/ioctl/platform/linux.rs
+++ b/src/sys/ioctl/platform/linux.rs
@@ -82,11 +82,7 @@ macro_rules! iorw {
macro_rules! convert_ioctl_res {
($w:expr) => (
{
- let res = $w;
- if res < 0 {
- return Err($crate::Error::Sys($crate::errno::Errno::last()))
- }
- Ok(res) // res may contain useful information for user
+ $crate::Errno::result($w)
}
);
}
diff --git a/src/sys/memfd.rs b/src/sys/memfd.rs
index e142218f..c5b7b803 100644
--- a/src/sys/memfd.rs
+++ b/src/sys/memfd.rs
@@ -1,6 +1,6 @@
use libc;
use std::os::unix::io::RawFd;
-use {Error, Result};
+use errno::{Errno, Result};
use std::ffi::CStr;
bitflags!(
@@ -13,6 +13,6 @@ bitflags!(
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
use sys::syscall::{syscall, MEMFD_CREATE};
let res = unsafe { syscall(MEMFD_CREATE, name.as_ptr(), flags.bits()) };
- if res == -1 { Err(Error::last()) }
- else { Ok(res as RawFd) }
+
+ Errno::result(res).map(|r| r as RawFd)
}
diff --git a/src/sys/mman.rs b/src/sys/mman.rs
index f74f0b8e..cb36cf7b 100644
--- a/src/sys/mman.rs
+++ b/src/sys/mman.rs
@@ -1,5 +1,5 @@
-use {Error, Result, NixPath};
-use errno::Errno;
+use {NixPath, Error};
+use errno::{Errno, Result};
use fcntl::OFlag;
use libc::{c_void, size_t, off_t, mode_t};
use sys::stat::Mode;
@@ -191,17 +191,11 @@ mod ffi {
}
pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
- match ffi::mlock(addr, length) {
- 0 => Ok(()),
- _ => Err(Error::Sys(Errno::last()))
- }
+ Errno::result(ffi::mlock(addr, length)).map(drop)
}
pub fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
- match unsafe { ffi::munlock(addr, length) } {
- 0 => Ok(()),
- _ => Err(Error::Sys(Errno::last()))
- }
+ Errno::result(unsafe { ffi::munlock(addr, length) }).map(drop)
}
/// Calls to mmap are inherently unsafe, so they must be made in an unsafe block. Typically
@@ -217,24 +211,15 @@ pub fn mmap(addr: *mut c_void, length: size_t, prot: MmapProt, flags: MmapFlag,
}
pub fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
- match unsafe { ffi::munmap(addr, len) } {
- 0 => Ok(()),
- _ => Err(Error::Sys(Errno::last()))
- }
+ Errno::result(unsafe { ffi::munmap(addr, len) }).map(drop)
}
pub fn madvise(addr: *const c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
- match unsafe { ffi::madvise(addr, length, advise) } {
- 0 => Ok(()),
- _ => Err(Error::Sys(Errno::last()))
- }
+ Errno::result(unsafe { ffi::madvise(addr, length, advise) }).map(drop)
}
pub fn msync(addr: *const c_void, length: size_t, flags: MmapSync) -> Result<()> {
- match unsafe { ffi::msync(addr, length, flags) } {
- 0 => Ok(()),
- _ => Err(Error::Sys(Errno::last()))
- }
+ Errno::result(unsafe { ffi::msync(addr, length, flags) }).map(drop)
}
pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Result<RawFd> {
@@ -244,11 +229,7 @@ pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Resul
}
}));
- if ret < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(ret)
- }
+ Errno::result(ret)
}
pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
@@ -256,9 +237,5 @@ pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
unsafe { ffi::shm_unlink(cstr.as_ptr()) }
}));
- if ret < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(())
- }
+ Errno::result(ret).map(drop)
}
diff --git a/src/sys/ptrace.rs b/src/sys/ptrace.rs
index 7c8c3e8e..9d8e6932 100644
--- a/src/sys/ptrace.rs
+++ b/src/sys/ptrace.rs
@@ -1,5 +1,5 @@
-use {Error, Result};
-use errno::Errno;
+use Error;
+use errno::{Errno, Result};
use libc::{pid_t, c_void, c_long};
#[cfg(all(target_os = "linux",
@@ -86,17 +86,14 @@ fn ptrace_peek(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, da
Errno::clear();
ffi::ptrace(request, pid, addr, data)
};
- if ret == -1 && Errno::last() != Errno::UnknownErrno {
- return Err(Error::Sys(Errno::last()));
+ match Errno::result(ret) {
+ Ok(..) | Err(Error::Sys(Errno::UnknownErrno)) => Ok(ret),
+ err @ Err(..) => err,
}
- Ok::<c_long, Error>(ret)
}
fn ptrace_other(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result<c_long> {
- match unsafe { ffi::ptrace(request, pid, addr, data) } {
- -1 => Err(Error::Sys(Errno::last())),
- _ => Ok(0)
- }
+ Errno::result(unsafe { ffi::ptrace(request, pid, addr, data) }).map(|_| 0)
}
/// Set options, as with ptrace(PTRACE_SETOPTIONS,...).
@@ -104,7 +101,5 @@ pub fn ptrace_setoptions(pid: pid_t, options: ptrace::PtraceOptions) -> Result<(
use self::ptrace::*;
use std::ptr;
- try!(ptrace(PTRACE_SETOPTIONS, pid, ptr::null_mut(), options as *mut c_void));
- Ok(())
+ ptrace(PTRACE_SETOPTIONS, pid, ptr::null_mut(), options as *mut c_void).map(drop)
}
-
diff --git a/src/sys/quota.rs b/src/sys/quota.rs
index 4786f80d..7a7718e4 100644
--- a/src/sys/quota.rs
+++ b/src/sys/quota.rs
@@ -1,5 +1,5 @@
-use {Result, NixPath, from_ffi};
-use errno::Errno;
+use NixPath;
+use errno::{Errno, Result};
use libc::{c_int, c_char};
#[cfg(all(target_os = "linux",
@@ -90,7 +90,8 @@ fn quotactl<P: ?Sized + NixPath>(cmd: quota::QuotaCmd, special: Option<&P>, id:
None => Ok(ffi::quotactl(cmd.as_int(), ptr::null(), id, addr)),
}
);
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
}
diff --git a/src/sys/select.rs b/src/sys/select.rs
index 5cae88fd..db0a55b9 100644
--- a/src/sys/select.rs
+++ b/src/sys/select.rs
@@ -1,8 +1,7 @@
use std::ptr::null_mut;
use std::os::unix::io::RawFd;
use libc::c_int;
-use {Result, Error};
-use errno::Errno;
+use errno::{Errno, Result};
use sys::time::TimeVal;
pub const FD_SETSIZE: RawFd = 1024;
@@ -82,9 +81,5 @@ pub fn select(nfds: c_int,
ffi::select(nfds, readfds, writefds, errorfds, timeout)
};
- if res == -1 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(res)
- }
+ Errno::result(res)
}
diff --git a/src/sys/signal.rs b/src/sys/signal.rs
index e3d5e407..76984f2c 100644
--- a/src/sys/signal.rs
+++ b/src/sys/signal.rs
@@ -2,10 +2,9 @@
// See http://rust-lang.org/COPYRIGHT.
use libc;
-use errno::Errno;
+use errno::{Errno, Result};
use std::mem;
use std::ptr;
-use {Error, Result};
pub use libc::{
SIGHUP,
@@ -324,30 +323,22 @@ impl SigSet {
pub fn add(&mut self, signum: SigNum) -> Result<()> {
let res = unsafe { ffi::sigaddset(&mut self.sigset as *mut sigset_t, signum) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
pub fn remove(&mut self, signum: SigNum) -> Result<()> {
let res = unsafe { ffi::sigdelset(&mut self.sigset as *mut sigset_t, signum) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
pub fn contains(&self, signum: SigNum) -> Result<bool> {
let res = unsafe { ffi::sigismember(&self.sigset as *const sigset_t, signum) };
- match res {
+ match try!(Errno::result(res)) {
1 => Ok(true),
0 => Ok(false),
- _ => Err(Error::Sys(Errno::last()))
+ _ => unreachable!("unexpected value from sigismember"),
}
}
@@ -410,11 +401,7 @@ pub unsafe fn sigaction(signum: SigNum, sigaction: &SigAction) -> Result<SigActi
let res =
ffi::sigaction(signum, &sigaction.sigaction as *const sigaction_t, &mut oldact as *mut sigaction_t);
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(SigAction { sigaction: oldact })
+ Errno::result(res).map(|_| SigAction { sigaction: oldact })
}
/// Manages the signal mask (set of blocked signals) for the calling thread.
@@ -448,31 +435,19 @@ pub fn pthread_sigmask(how: HowFlag,
|os| &mut os.sigset as *mut sigset_t))
};
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
pub fn kill(pid: libc::pid_t, signum: SigNum) -> Result<()> {
let res = unsafe { ffi::kill(pid, signum) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
pub fn raise(signum: SigNum) -> Result<()> {
let res = unsafe { ffi::raise(signum) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
#[cfg(test)]
diff --git a/src/sys/signalfd.rs b/src/sys/signalfd.rs
index 66eaa04e..7d1455e6 100644
--- a/src/sys/signalfd.rs
+++ b/src/sys/signalfd.rs
@@ -16,9 +16,8 @@
//! Please note that signal discarding is not specific to `signalfd`, but also happens with regular
//! signal handlers.
use libc::{c_int, pid_t, uid_t};
-use {Error, Result};
use unistd;
-use errno::Errno;
+use errno::{Errno, Result};
use sys::signal::signal::siginfo as signal_siginfo;
pub use sys::signal::{self, SigSet};
@@ -56,10 +55,7 @@ pub const CREATE_NEW_FD: RawFd = -1;
/// See [the signalfd man page for more information](http://man7.org/linux/man-pages/man2/signalfd.2.html)
pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result<RawFd> {
unsafe {
- match ffi::signalfd(fd as c_int, mask.as_ref(), flags.bits()) {
- -1 => Err(Error::Sys(Errno::last())),
- res => Ok(res as RawFd),
- }
+ Errno::result(ffi::signalfd(fd as c_int, mask.as_ref(), flags.bits()))
}
}
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs
index 943df26e..49a1ecf5 100644
--- a/src/sys/socket/addr.rs
+++ b/src/sys/socket/addr.rs
@@ -1,6 +1,6 @@
-use {Result, Error, NixPath};
use super::{consts, sa_family_t};
-use errno::Errno;
+use {NixPath, Error};
+use errno::{Errno, Result};
use libc;
use std::{fmt, hash, mem, net, ptr};
use std::ffi::OsStr;
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 8a74029f..60b138ed 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -1,8 +1,8 @@
//! Socket interface functions
//!
//! [Further reading](http://man7.org/linux/man-pages/man7/socket.7.html)
-use {Error, Result, from_ffi};
-use errno::Errno;
+use Error;
+use errno::{Errno, Result};
use features;
use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
@@ -293,11 +293,7 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'
};
let ret = unsafe { ffi::sendmsg(fd, &mhdr, flags) };
- if ret < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(ret as usize)
- }
+ Errno::result(ret).map(|r| r as usize)
}
/// Receive message in scatter-gather vectors from a socket, and
@@ -320,18 +316,14 @@ pub fn recvmsg<'a, T>(fd: RawFd, iov: &[IoVec<&mut [u8]>], cmsg_buffer: Option<&
};
let ret = unsafe { ffi::recvmsg(fd, &mut mhdr, flags) };
- if ret < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(unsafe { RecvMsg {
- bytes: ret as usize,
- cmsg_buffer: slice::from_raw_parts(mhdr.msg_control as *const u8,
- mhdr.msg_controllen as usize),
- address: sockaddr_storage_to_addr(&address,
- mhdr.msg_namelen as usize).ok(),
- flags: mhdr.msg_flags,
- } })
- }
+ Ok(unsafe { RecvMsg {
+ bytes: try!(Errno::result(ret)) as usize,
+ cmsg_buffer: slice::from_raw_parts(mhdr.msg_control as *const u8,
+ mhdr.msg_controllen as usize),
+ address: sockaddr_storage_to_addr(&address,
+ mhdr.msg_namelen as usize).ok(),
+ flags: mhdr.msg_flags,
+ } })
}
@@ -347,11 +339,7 @@ pub fn socket(domain: AddressFamily, ty: SockType, flags: SockFlag, protocol: c_
}
// TODO: Check the kernel version
- let res = unsafe { ffi::socket(domain as c_int, ty, protocol) };
-
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ let res = try!(Errno::result(unsafe { ffi::socket(domain as c_int, ty, protocol) }));
if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
@@ -381,10 +369,7 @@ pub fn socketpair(domain: AddressFamily, ty: SockType, protocol: c_int,
let res = unsafe {
ffi::socketpair(domain as c_int, ty, protocol, fds.as_mut_ptr())
};
-
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
if !feat_atomic {
if flags.contains(SOCK_CLOEXEC) {
@@ -405,7 +390,8 @@ pub fn socketpair(domain: AddressFamily, ty: SockType, protocol: c_int,
/// [Further reading](http://man7.org/linux/man-pages/man2/listen.2.html)
pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
let res = unsafe { ffi::listen(sockfd, backlog as c_int) };
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
/// Bind a name to a socket
@@ -417,7 +403,7 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
ffi::bind(fd, ptr, len)
};
- from_ffi(res)
+ Errno::result(res).map(drop)
}
/// Accept a connection on a socket
@@ -426,11 +412,7 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
pub fn accept(sockfd: RawFd) -> Result<RawFd> {
let res = unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
/// Accept a connection on a socket
@@ -442,11 +424,7 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
#[inline]
fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
- let res = unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) };
-
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ let res = try!(Errno::result(unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }));
if flags.contains(SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FD_CLOEXEC)));
@@ -468,7 +446,7 @@ pub fn connect(fd: RawFd, addr: &SockAddr) -> Result<()> {
ffi::connect(fd, ptr, len)
};
- from_ffi(res)
+ Errno::result(res).map(drop)
}
/// Receive data from a connection-oriented socket. Returns the number of
@@ -483,11 +461,7 @@ pub fn recv(sockfd: RawFd, buf: &mut [u8], flags: SockMessageFlags) -> Result<us
buf.len() as size_t,
flags);
- if ret < 0 {
- Err(Error::last())
- } else {
- Ok(ret as usize)
- }
+ Errno::result(ret).map(|r| r as usize)
}
}
@@ -500,17 +474,13 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8]) -> Result<(usize, SockAddr)> {
let addr: sockaddr_storage = mem::zeroed();
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
- let ret = ffi::recvfrom(
+ let ret = try!(Errno::result(ffi::recvfrom(
sockfd,
buf.as_ptr() as *mut c_void,
buf.len() as size_t,
0,
mem::transmute(&addr),
- &mut len as *mut socklen_t);
-
- if ret < 0 {
- return Err(Error::last());
- }
+ &mut len as *mut socklen_t)));
sockaddr_storage_to_addr(&addr, len as usize)
.map(|addr| (ret as usize, addr))
@@ -523,11 +493,7 @@ pub fn sendto(fd: RawFd, buf: &[u8], addr: &SockAddr, flags: SockMessageFlags) -
ffi::sendto(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags, ptr, len)
};
- if ret < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(ret as usize)
- }
+ Errno::result(ret).map(|r| r as usize)
}
/// Send data to a connection-oriented socket. Returns the number of bytes read
@@ -538,11 +504,7 @@ pub fn send(fd: RawFd, buf: &[u8], flags: SockMessageFlags) -> Result<usize> {
ffi::send(fd, buf.as_ptr() as *const c_void, buf.len() as size_t, flags)
};
- if ret < 0 {
- Err(Error::last())
- } else {
- Ok(ret as usize)
- }
+ Errno::result(ret).map(|r| r as usize)
}
#[repr(C)]
@@ -621,9 +583,7 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
let ret = ffi::getpeername(fd, mem::transmute(&addr), &mut len);
- if ret < 0 {
- return Err(Error::last());
- }
+ try!(Errno::result(ret));
sockaddr_storage_to_addr(&addr, len as usize)
}
@@ -639,9 +599,7 @@ pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
let ret = ffi::getsockname(fd, mem::transmute(&addr), &mut len);
- if ret < 0 {
- return Err(Error::last());
- }
+ try!(Errno::result(ret));
sockaddr_storage_to_addr(&addr, len as usize)
}
@@ -696,14 +654,8 @@ pub fn shutdown(df: RawFd, how: Shutdown) -> Result<()> {
Shutdown::Both => consts::SHUT_RDWR,
};
- let ret = shutdown(df, how);
-
- if ret < 0 {
- return Err(Error::last());
- }
+ Errno::result(shutdown(df, how)).map(drop)
}
-
- Ok(())
}
#[test]
diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs
index 4fea8d53..a9d6e67c 100644
--- a/src/sys/socket/sockopt.rs
+++ b/src/sys/socket/sockopt.rs
@@ -1,6 +1,5 @@
-use {Result, Error, from_ffi};
use super::{ffi, consts, GetSockOpt, SetSockOpt};
-use errno::Errno;
+use errno::{Errno, Result};
use sys::time::TimeVal;
use libc::{c_int, uint8_t, c_void, socklen_t};
use std::mem;
@@ -18,7 +17,7 @@ macro_rules! setsockopt_impl {
let res = ffi::setsockopt(fd, $level, $flag,
setter.ffi_ptr(),
setter.ffi_len());
- from_ffi(res)
+ Errno::result(res).map(drop)
}
}
}
@@ -37,9 +36,7 @@ macro_rules! getsockopt_impl {
let res = ffi::getsockopt(fd, $level, $flag,
getter.ffi_ptr(),
getter.ffi_len());
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(getter.unwrap())
}
diff --git a/src/sys/stat.rs b/src/sys/stat.rs
index 2a34d282..391924da 100644
--- a/src/sys/stat.rs
+++ b/src/sys/stat.rs
@@ -1,8 +1,8 @@
pub use libc::dev_t;
pub use libc::stat as FileStat;
-use {Error, Result, NixPath, from_ffi};
-use errno::Errno;
+use NixPath;
+use errno::{Errno, Result};
use libc::mode_t;
use std::mem;
use std::os::unix::io::RawFd;
@@ -56,7 +56,8 @@ pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t)
ffi::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev)
}
}));
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
#[cfg(target_os = "linux")]
@@ -80,9 +81,7 @@ pub fn stat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> {
}
}));
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(dst)
}
@@ -95,9 +94,7 @@ pub fn lstat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> {
}
}));
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(dst)
}
@@ -106,9 +103,7 @@ pub fn fstat(fd: RawFd) -> Result<FileStat> {
let mut dst = unsafe { mem::uninitialized() };
let res = unsafe { ffi::fstat(fd, &mut dst as *mut FileStat) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(dst)
}
diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs
index 63ee1ac4..0552e3d0 100644
--- a/src/sys/statfs.rs
+++ b/src/sys/statfs.rs
@@ -1,5 +1,5 @@
-use {Result, NixPath, from_ffi};
-use errno::Errno;
+use NixPath;
+use errno::{Errno, Result};
use std::os::unix::io::AsRawFd;
pub mod vfs {
@@ -104,13 +104,14 @@ pub fn statfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statfs) -> Result<(
let res = try!(
path.with_nix_path(|path| ffi::statfs(path.as_ptr(), stat))
);
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
}
pub fn fstatfs<T: AsRawFd>(fd: &T, stat: &mut vfs::Statfs) -> Result<()> {
unsafe {
Errno::clear();
- from_ffi(ffi::fstatfs(fd.as_raw_fd(), stat))
+ Errno::result(ffi::fstatfs(fd.as_raw_fd(), stat)).map(drop)
}
}
diff --git a/src/sys/statvfs.rs b/src/sys/statvfs.rs
index 0d996afe..abfc6c9b 100644
--- a/src/sys/statvfs.rs
+++ b/src/sys/statvfs.rs
@@ -2,8 +2,8 @@
//!
//! See the `vfs::Statvfs` struct for some rusty wrappers
-use {Result, NixPath, from_ffi};
-use errno::Errno;
+use NixPath;
+use errno::{Errno, Result};
use std::os::unix::io::AsRawFd;
pub mod vfs {
@@ -147,7 +147,8 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statvfs) -> Result
let res = try!(
path.with_nix_path(|path| ffi::statvfs(path.as_ptr(), stat))
);
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
}
@@ -155,7 +156,7 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statvfs) -> Result
pub fn fstatvfs<T: AsRawFd>(fd: &T, stat: &mut vfs::Statvfs) -> Result<()> {
unsafe {
Errno::clear();
- from_ffi(ffi::fstatvfs(fd.as_raw_fd(), stat))
+ Errno::result(ffi::fstatvfs(fd.as_raw_fd(), stat)).map(drop)
}
}
diff --git a/src/sys/termios.rs b/src/sys/termios.rs
index 4e6b2e51..9c26661f 100644
--- a/src/sys/termios.rs
+++ b/src/sys/termios.rs
@@ -1,5 +1,4 @@
-use {Error, Result, from_ffi};
-use errno::Errno;
+use errno::{Errno, Result};
use libc::c_int;
use std::mem;
use std::os::unix::io::RawFd;
@@ -440,15 +439,15 @@ pub fn cfgetospeed(termios: &Termios) -> speed_t {
}
pub fn cfsetispeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::cfsetispeed(termios, speed)
- })
+ }).map(drop)
}
pub fn cfsetospeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::cfsetospeed(termios, speed)
- })
+ }).map(drop)
}
pub fn tcgetattr(fd: RawFd) -> Result<Termios> {
@@ -458,9 +457,7 @@ pub fn tcgetattr(fd: RawFd) -> Result<Termios> {
ffi::tcgetattr(fd, &mut termios)
};
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok(termios)
}
@@ -468,31 +465,31 @@ pub fn tcgetattr(fd: RawFd) -> Result<Termios> {
pub fn tcsetattr(fd: RawFd,
actions: SetArg,
termios: &Termios) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::tcsetattr(fd, actions as c_int, termios)
- })
+ }).map(drop)
}
pub fn tcdrain(fd: RawFd) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::tcdrain(fd)
- })
+ }).map(drop)
}
pub fn tcflow(fd: RawFd, action: FlowArg) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::tcflow(fd, action as c_int)
- })
+ }).map(drop)
}
pub fn tcflush(fd: RawFd, action: FlushArg) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::tcflush(fd, action as c_int)
- })
+ }).map(drop)
}
pub fn tcsendbreak(fd: RawFd, action: c_int) -> Result<()> {
- from_ffi(unsafe {
+ Errno::result(unsafe {
ffi::tcsendbreak(fd, action)
- })
+ }).map(drop)
}
diff --git a/src/sys/uio.rs b/src/sys/uio.rs
index b43ede3c..7e734f84 100644
--- a/src/sys/uio.rs
+++ b/src/sys/uio.rs
@@ -1,8 +1,7 @@
// Silence invalid warnings due to rust-lang/rust#16719
#![allow(improper_ctypes)]
-use {Result, Error};
-use errno::Errno;
+use errno::{Errno, Result};
use libc::{c_int, c_void, size_t, off_t};
use std::marker::PhantomData;
use std::os::unix::io::RawFd;
@@ -48,20 +47,13 @@ mod ffi {
pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result<usize> {
let res = unsafe { ffi::writev(fd, iov.as_ptr(), iov.len() as c_int) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
let res = unsafe { ffi::readv(fd, iov.as_ptr(), iov.len() as c_int) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
#[cfg(feature = "preadv_pwritev")]
@@ -70,11 +62,8 @@ pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
let res = unsafe {
ffi::pwritev(fd, iov.as_ptr(), iov.len() as c_int, offset)
};
- if res < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(res as usize)
- }
+
+ Errno::result(res).map(|r| r as usize)
}
#[cfg(feature = "preadv_pwritev")]
@@ -83,11 +72,8 @@ pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>],
let res = unsafe {
ffi::preadv(fd, iov.as_ptr(), iov.len() as c_int, offset)
};
- if res < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(res as usize)
- }
+
+ Errno::result(res).map(|r| r as usize)
}
pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
@@ -95,11 +81,8 @@ pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
ffi::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
offset)
};
- if res < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(res as usize)
- }
+
+ Errno::result(res).map(|r| r as usize)
}
pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
@@ -107,11 +90,8 @@ pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
ffi::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
offset)
};
- if res < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(res as usize)
- }
+
+ Errno::result(res).map(|r| r as usize)
}
#[repr(C)]
diff --git a/src/sys/wait.rs b/src/sys/wait.rs
index 530ef7fa..ab91a848 100644
--- a/src/sys/wait.rs
+++ b/src/sys/wait.rs
@@ -1,6 +1,5 @@
use libc::{pid_t, c_int};
-use errno::Errno;
-use {Error, Result};
+use errno::{Errno, Result};
use sys::signal;
@@ -203,13 +202,10 @@ pub fn waitpid(pid: pid_t, options: Option<WaitPidFlag>) -> Result<WaitStatus> {
let res = unsafe { ffi::waitpid(pid as pid_t, &mut status as *mut c_int, option_bits) };
- if res < 0 {
- Err(Error::Sys(Errno::last()))
- } else if res == 0 {
- Ok(StillAlive)
- } else {
- Ok(decode(res, status))
- }
+ Ok(match try!(Errno::result(res)) {
+ 0 => StillAlive,
+ res => decode(res, status),
+ })
}
pub fn wait() -> Result<WaitStatus> {
diff --git a/src/unistd.rs b/src/unistd.rs
index 3d210c44..f2c1fdce 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -1,7 +1,7 @@
//! Standard symbolic constants and types
//!
-use {Error, Result, NixPath, from_ffi};
-use errno::Errno;
+use {NixPath, Error};
+use errno::{Errno, Result};
use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
use libc::{c_char, c_void, c_int, size_t, pid_t, off_t, uid_t, gid_t};
@@ -91,17 +91,12 @@ impl Fork {
}
pub fn fork() -> Result<Fork> {
- use self::Fork::*;
-
let res = unsafe { ffi::fork() };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- } else if res == 0 {
- Ok(Child)
- } else {
- Ok(Parent(res))
- }
+ Errno::result(res).map(|res| match res {
+ 0 => Fork::Child,
+ res => Fork::Parent(res)
+ })
}
#[inline]
@@ -115,32 +110,21 @@ pub fn getppid() -> pid_t {
#[inline]
pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
let res = unsafe { ffi::setpgid(pid, pgid) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
- Ok(())
+ Errno::result(res).map(drop)
}
#[inline]
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
let res = unsafe { ffi::dup(oldfd) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
#[inline]
pub fn dup2(oldfd: RawFd, newfd: RawFd) -> Result<RawFd> {
let res = unsafe { ffi::dup2(oldfd, newfd) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(res)
+ Errno::result(res)
}
pub fn dup3(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> {
@@ -171,11 +155,7 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
unsafe { ffi::chdir(cstr.as_ptr()) }
}));
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(())
+ Errno::result(res).map(drop)
}
fn to_exec_array(args: &[CString]) -> Vec<*const c_char> {
@@ -223,7 +203,7 @@ pub fn execvp(filename: &CString, args: &[CString]) -> Result<()> {
pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
let res = unsafe { ffi::daemon(nochdir as c_int, noclose as c_int) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn sethostname(name: &[u8]) -> Result<()> {
@@ -231,7 +211,7 @@ pub fn sethostname(name: &[u8]) -> Result<()> {
let len = name.len() as size_t;
let res = unsafe { ffi::sethostname(ptr, len) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn gethostname(name: &mut [u8]) -> Result<()> {
@@ -239,32 +219,24 @@ pub fn gethostname(name: &mut [u8]) -> Result<()> {
let len = name.len() as size_t;
let res = unsafe { ffi::gethostname(ptr, len) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn close(fd: RawFd) -> Result<()> {
let res = unsafe { ffi::close(fd) };
- from_ffi(res)
+ Errno::result(res).map(drop)
}
pub fn read(fd: RawFd, buf: &mut [u8]) -> Result<usize> {
let res = unsafe { ffi::read(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
let res = unsafe { ffi::write(fd, buf.as_ptr() as *const c_void, buf.len() as size_t) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- return Ok(res as usize)
+ Errno::result(res).map(|r| r as usize)
}
pub fn pipe() -> Result<(RawFd, RawFd)> {
@@ -273,9 +245,7 @@ pub fn pipe() -> Result<(RawFd, RawFd)> {
let res = ffi::pipe(fds.as_mut_ptr());
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
Ok((fds[0], fds[1]))
}
@@ -287,9 +257,7 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
let res = ffi::pipe(fds.as_mut_ptr());
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
+ try!(Errno::result(res));
try!(pipe2_setflags(fds[0], fds[1], flags));
@@ -323,35 +291,34 @@ fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> {
}
pub fn ftruncate(fd: RawFd, len: off_t) -> Result<()> {
- if unsafe { ffi::ftruncate(fd, len) } < 0 {
- Err(Error::Sys(Errno::last()))
- } else {
- Ok(())
- }
+ Errno::result(unsafe { ffi::ftruncate(fd, len) }).map(drop)
}
pub fn isatty(fd: RawFd) -> Result<bool> {
use libc;
- if unsafe { libc::isatty(fd) } == 1 {
- Ok(true)
- } else {
- match Errno::last() {
- // ENOTTY means `fd` is a valid file descriptor, but not a TTY, so
- // we return `Ok(false)`
- Errno::ENOTTY => Ok(false),
- err => Err(Error::Sys(err))
+ unsafe {
+ // ENOTTY means `fd` is a valid file descriptor, but not a TTY, so
+ // we return `Ok(false)`
+ if libc::isatty(fd) == 1 {
+ Ok(true)
+ } else {
+ match Errno::last() {
+ Errno::ENOTTY => Ok(false),
+ err => Err(Error::Sys(err)),
+ }
}
}
}
pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
- unsafe {
- ffi::unlink(cstr.as_ptr())
- }
+ unsafe {
+ ffi::unlink(cstr.as_ptr())
+ }
}));
- from_ffi(res)
+
+ Errno::result(res).map(drop)
}
#[inline]
@@ -360,33 +327,21 @@ pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
unsafe { ffi::chroot(cstr.as_ptr()) }
}));
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
#[inline]
pub fn fsync(fd: RawFd) -> Result<()> {
let res = unsafe { ffi::fsync(fd) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
#[inline]
pub fn fdatasync(fd: RawFd) -> Result<()> {
let res = unsafe { ffi::fdatasync(fd) };
- if res < 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
// POSIX requires that getuid, geteuid, getgid, getegid are always successful,
@@ -418,8 +373,8 @@ pub fn getegid() -> gid_t {
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
use sys::syscall::{syscall, SYSPIVOTROOT};
- use errno::Errno;
- use {Error, Result, NixPath};
+ use NixPath;
+ use errno::{Errno, Result};
#[cfg(feature = "execvpe")]
use std::ffi::CString;
@@ -434,11 +389,7 @@ mod linux {
})
})));
- if res != 0 {
- return Err(Error::Sys(Errno::last()));
- }
-
- Ok(())
+ Errno::result(res).map(drop)
}
#[inline]