Skip to content

Commit

Permalink
WIP: apply cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bachp committed Aug 9, 2018
1 parent 6c727f8 commit 8847d62
Show file tree
Hide file tree
Showing 70 changed files with 5,076 additions and 3,295 deletions.
1,904 changes: 1,046 additions & 858 deletions src/errno.rs

Large diffs are not rendered by default.

87 changes: 61 additions & 26 deletions src/fcntl.rs
@@ -1,13 +1,13 @@
use {Error, Result, NixPath};
use errno::Errno;
use libc::{self, c_int, c_uint, c_char, size_t, ssize_t};
use sys::stat::Mode;
use std::os::unix::io::RawFd;
use libc::{self, c_char, c_int, c_uint, size_t, ssize_t};
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::RawFd;
use sys::stat::Mode;
use {Error, NixPath, Result};

#[cfg(any(target_os = "android", target_os = "linux"))]
use sys::uio::IoVec; // For vmsplice
use sys::uio::IoVec; // For vmsplice

libc_bitflags!{
pub struct AtFlags: c_int {
Expand Down Expand Up @@ -138,21 +138,26 @@ libc_bitflags!(
);

pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| {
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
let fd = try!(path.with_nix_path(|cstr| unsafe {
libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint)
}));

Errno::result(fd)
}

pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| {
unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
pub fn openat<P: ?Sized + NixPath>(
dirfd: RawFd,
path: &P,
oflag: OFlag,
mode: Mode,
) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| unsafe {
libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint)
}));
Errno::result(fd)
}

fn wrap_readlink_result(buffer: &mut[u8], res: ssize_t) -> Result<&OsStr> {
fn wrap_readlink_result(buffer: &mut [u8], res: ssize_t) -> Result<&OsStr> {
match Errno::result(res) {
Err(err) => Err(err),
Ok(len) => {
Expand All @@ -166,17 +171,29 @@ fn wrap_readlink_result(buffer: &mut[u8], res: ssize_t) -> Result<&OsStr> {
}

pub fn readlink<'a, P: ?Sized + NixPath>(path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
let res = try!(path.with_nix_path(|cstr| {
unsafe { libc::readlink(cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
let res = try!(path.with_nix_path(|cstr| unsafe {
libc::readlink(
cstr.as_ptr(),
buffer.as_mut_ptr() as *mut c_char,
buffer.len() as size_t,
)
}));

wrap_readlink_result(buffer, res)
}


pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P, buffer: &'a mut [u8]) -> Result<&'a OsStr> {
let res = try!(path.with_nix_path(|cstr| {
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), buffer.as_mut_ptr() as *mut c_char, buffer.len() as size_t) }
pub fn readlinkat<'a, P: ?Sized + NixPath>(
dirfd: RawFd,
path: &P,
buffer: &'a mut [u8],
) -> Result<&'a OsStr> {
let res = try!(path.with_nix_path(|cstr| unsafe {
libc::readlinkat(
dirfd,
cstr.as_ptr(),
buffer.as_mut_ptr() as *mut c_char,
buffer.len() as size_t,
)
}));

wrap_readlink_result(buffer, res)
Expand Down Expand Up @@ -232,7 +249,6 @@ pub enum FcntlArg<'a> {
F_GETPIPE_SZ,
#[cfg(any(target_os = "linux", target_os = "android"))]
F_SETPIPE_SZ(c_int),

// TODO: Rest of flags
}
pub use self::FcntlArg::*;
Expand Down Expand Up @@ -261,7 +277,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
#[cfg(any(target_os = "linux", target_os = "android"))]
F_SETPIPE_SZ(size) => libc::fcntl(fd, libc::F_SETPIPE_SZ, size),
#[cfg(any(target_os = "linux", target_os = "android"))]
_ => unimplemented!()
_ => unimplemented!(),
}
};

Expand Down Expand Up @@ -318,12 +334,21 @@ libc_bitflags! {
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn splice(fd_in: RawFd, off_in: Option<&mut libc::loff_t>,
fd_out: RawFd, off_out: Option<&mut libc::loff_t>,
len: usize, flags: SpliceFFlags) -> Result<usize> {
pub fn splice(
fd_in: RawFd,
off_in: Option<&mut libc::loff_t>,
fd_out: RawFd,
off_out: Option<&mut libc::loff_t>,
len: usize,
flags: SpliceFFlags,
) -> Result<usize> {
use std::ptr;
let off_in = off_in.map(|offset| offset as *mut _).unwrap_or(ptr::null_mut());
let off_out = off_out.map(|offset| offset as *mut _).unwrap_or(ptr::null_mut());
let off_in = off_in
.map(|offset| offset as *mut _)
.unwrap_or(ptr::null_mut());
let off_out = off_out
.map(|offset| offset as *mut _)
.unwrap_or(ptr::null_mut());

let ret = unsafe { libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits()) };
Errno::result(ret).map(|r| r as usize)
Expand All @@ -338,7 +363,12 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<usize> {
let ret = unsafe {
libc::vmsplice(fd, iov.as_ptr() as *const libc::iovec, iov.len(), flags.bits())
libc::vmsplice(
fd,
iov.as_ptr() as *const libc::iovec,
iov.len(),
flags.bits(),
)
};
Errno::result(ret).map(|r| r as usize)
}
Expand Down Expand Up @@ -379,7 +409,12 @@ libc_bitflags!(
/// Allows the caller to directly manipulate the allocated disk space for the
/// file referred to by fd.
#[cfg(any(target_os = "linux"))]
pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc::off_t) -> Result<c_int> {
pub fn fallocate(
fd: RawFd,
mode: FallocateFlags,
offset: libc::off_t,
len: libc::off_t,
) -> Result<c_int> {
let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) };
Errno::result(res)
}
33 changes: 20 additions & 13 deletions src/features.rs
Expand Up @@ -11,10 +11,10 @@ mod os {
// * accept4: 2.6.28

static VERS_UNKNOWN: usize = 1;
static VERS_2_6_18: usize = 2;
static VERS_2_6_27: usize = 3;
static VERS_2_6_28: usize = 4;
static VERS_3: usize = 5;
static VERS_2_6_18: usize = 2;
static VERS_2_6_27: usize = 3;
static VERS_2_6_28: usize = 4;
static VERS_3: usize = 5;

#[inline]
fn digit(dst: &mut usize, b: u8) {
Expand All @@ -25,7 +25,7 @@ mod os {
fn parse_kernel_version() -> usize {
let u = uname();

let mut curr: usize = 0;
let mut curr: usize = 0;
let mut major: usize = 0;
let mut minor: usize = 0;
let mut patch: usize = 0;
Expand All @@ -39,13 +39,11 @@ mod os {
b'.' | b'-' => {
curr += 1;
}
b'0'...b'9' => {
match curr {
0 => digit(&mut major, b),
1 => digit(&mut minor, b),
_ => digit(&mut patch, b),
}
}
b'0'...b'9' => match curr {
0 => digit(&mut major, b),
1 => digit(&mut minor, b),
_ => digit(&mut patch, b),
},
_ => break,
}
}
Expand Down Expand Up @@ -94,7 +92,16 @@ mod os {
}
}

#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly", target_os = "ios", target_os = "openbsd", target_os = "netbsd"))]
#[cfg(
any(
target_os = "macos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "openbsd",
target_os = "netbsd"
)
)]
mod os {
/// Check if the OS supports atomic close-on-exec for sockets
pub fn socket_atomic_cloexec() -> bool {
Expand Down
12 changes: 5 additions & 7 deletions src/ifaddrs.rs
Expand Up @@ -10,9 +10,9 @@ use std::option::Option;

use libc;

use {Result, Errno};
use sys::socket::SockAddr;
use net::if_::*;
use sys::socket::SockAddr;
use {Errno, Result};

/// Describes a single address for an interface as returned by `getifaddrs`.
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
Expand Down Expand Up @@ -126,11 +126,9 @@ impl Iterator for InterfaceAddressIterator {
/// ```
pub fn getifaddrs() -> Result<InterfaceAddressIterator> {
let mut addrs: *mut libc::ifaddrs = unsafe { mem::uninitialized() };
Errno::result(unsafe { libc::getifaddrs(&mut addrs) }).map(|_| {
InterfaceAddressIterator {
base: addrs,
next: addrs,
}
Errno::result(unsafe { libc::getifaddrs(&mut addrs) }).map(|_| InterfaceAddressIterator {
base: addrs,
next: addrs,
})
}

Expand Down

0 comments on commit 8847d62

Please sign in to comment.