From 82d87b66d42cc81352a649300dbf48238805bd8b Mon Sep 17 00:00:00 2001 From: arcnmx Date: Mon, 25 Jan 2016 21:57:17 -0500 Subject: [PATCH] Errno::result() --- src/errno.rs | 89 +++++++++++++++------- src/fcntl.rs | 22 ++---- src/lib.rs | 18 +---- src/mount.rs | 7 +- src/mqueue.rs | 33 ++------ src/poll.rs | 9 +-- src/sched.rs | 27 ++----- src/sys/epoll.rs | 18 ++--- src/sys/event.rs | 21 +----- src/sys/eventfd.rs | 8 +- src/sys/ioctl/platform/linux.rs | 6 +- src/sys/memfd.rs | 6 +- src/sys/mman.rs | 41 +++------- src/sys/ptrace.rs | 19 ++--- src/sys/quota.rs | 7 +- src/sys/select.rs | 9 +-- src/sys/signal.rs | 43 +++-------- src/sys/signalfd.rs | 8 +- src/sys/socket/addr.rs | 4 +- src/sys/socket/mod.rs | 102 +++++++------------------ src/sys/socket/sockopt.rs | 9 +-- src/sys/stat.rs | 19 ++--- src/sys/statfs.rs | 9 ++- src/sys/statvfs.rs | 9 ++- src/sys/termios.rs | 35 ++++----- src/sys/uio.rs | 42 +++-------- src/sys/wait.rs | 14 ++-- src/unistd.rs | 129 ++++++++++---------------------- 28 files changed, 258 insertions(+), 505 deletions(-) diff --git a/src/errno.rs b/src/errno.rs index 5ca8ef08b3..8de55b8021 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>(value: S) -> Result { + 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 for io::Error { + fn from(err: Errno) -> Self { + io::Error::from_raw_os_error(err as i32) + } +} + +pub type Result = result::Result; + 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 7ff2719531..197f0059f9 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(path: &P, oflag: OFlag, mode: Mode) -> Result { @@ -157,11 +153,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result { } }; - 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 512b5403e5..cc72f704e6 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; @@ -42,12 +43,12 @@ pub mod unistd; /* * - * ===== Result / Error ===== + * ===== Error ===== * */ use libc::c_char; -use std::{ptr, result}; +use std::ptr; use std::ffi::CStr; use std::path::{Path, PathBuf}; use std::os::unix::ffi::OsStrExt; @@ -56,8 +57,6 @@ use std::fmt; use std::error; use libc::PATH_MAX; -pub type Result = result::Result; - #[derive(Clone, Copy, Debug, PartialEq)] pub enum Error { Sys(errno::Errno), @@ -185,12 +184,3 @@ impl NixPath for PathBuf { self.as_os_str().as_bytes().with_nix_path(f) } } - -#[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 683cabb885..0535cdd048 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 { @@ -105,7 +106,7 @@ pub fn umount(target: &P) -> Result<()> { unsafe { ffi::umount(cstr.as_ptr()) } })); - from_ffi(res) + Errno::result(res).map(drop) } pub fn umount2(target: &P, flags: MntFlags) -> Result<()> { @@ -113,5 +114,5 @@ pub fn umount2(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 e24b680011..1625d4ef9b 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 { 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 Result { 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 { 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 { pub fn mq_setattr(mqd: MQd, newattr: &MqAttr) -> Result { 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 d43e358bd3..631a47cc48 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 { 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 0c8837f57a..7d7ec0f0fc 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::() 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 { @@ -190,29 +185,17 @@ pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags) -> Result 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 2258764184..a079649a36 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 { 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 6498cb97f1..eb99f7d983 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 { 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 80f7f47cef..d2cd6b9313 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 { 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 b0fa6bee5c..603112241e 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 e142218f48..c5b7b803bb 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 { 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 f74f0b8e57..cb36cf7bfd 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(name: &P, flag: OFlag, mode: Mode) -> Result { @@ -244,11 +229,7 @@ pub fn shm_open(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(name: &P) -> Result<()> { @@ -256,9 +237,5 @@ pub fn shm_unlink(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 7c8c3e8e46..9d8e6932a2 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::(ret) } fn ptrace_other(request: ptrace::PtraceRequest, pid: pid_t, addr: *mut c_void, data: *mut c_void) -> Result { - 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 4786f80d74..7a7718e45a 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(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 5cae88fdde..db0a55b933 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 e3d5e40725..76984f2c0e 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 { 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 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 66eaa04e16..7d1455e605 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 { 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 943df26e6e..49a1ecf5a9 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 8a74029f7a..60b138eddd 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 { 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 { #[inline] fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result { - 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 Result<(usize, SockAddr)> { let addr: sockaddr_storage = mem::zeroed(); let mut len = mem::size_of::() 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 { 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 { 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 { 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 4fea8d5318..a9d6e67c2f 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 2a34d2828f..391924dac0 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(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(path: &P) -> Result { } })); - if res < 0 { - return Err(Error::Sys(Errno::last())); - } + try!(Errno::result(res)); Ok(dst) } @@ -95,9 +94,7 @@ pub fn lstat(path: &P) -> Result { } })); - 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 { 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 63ee1ac477..0552e3d064 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(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(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 0d996afe09..abfc6c9b3d 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(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(path: &P, stat: &mut vfs::Statvfs) -> Result pub fn fstatvfs(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 4e6b2e519f..9c26661f47 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 { @@ -458,9 +457,7 @@ pub fn tcgetattr(fd: RawFd) -> Result { 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 { 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 b43ede3c9b..7e734f84d7 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 { 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 { 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 { @@ -95,11 +81,8 @@ pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result { 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{ @@ -107,11 +90,8 @@ pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result{ 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 530ef7fa66..ab91a8484c 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) -> Result { 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 { diff --git a/src/unistd.rs b/src/unistd.rs index 3d210c4428..f2c1fdced0 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 { - 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 { 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 { 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 { @@ -171,11 +155,7 @@ pub fn chdir(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 { 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 { 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 { 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(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(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]