Skip to content

Reformat everything #1854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,4 @@ task:
container:
image: rust:latest
setup_script: rustup +$TOOLCHAIN component add rustfmt
test_script: $TOOL +$TOOLCHAIN fmt --all -- --check
test_script: $TOOL +$TOOLCHAIN fmt --all -- --check **/*.rs
61 changes: 39 additions & 22 deletions src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! List directory contents

use crate::{Error, NixPath, Result};
use crate::errno::Errno;
use crate::fcntl::{self, OFlag};
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::ptr;
use std::ffi;
use crate::sys;
use crate::{Error, NixPath, Result};
use cfg_if::cfg_if;
use std::ffi;
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::ptr;

#[cfg(target_os = "linux")]
use libc::{dirent64 as dirent, readdir64_r as readdir_r};
Expand All @@ -29,21 +29,26 @@ use libc::{dirent, readdir_r};
/// * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc
/// does).
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Dir(
ptr::NonNull<libc::DIR>
);
pub struct Dir(ptr::NonNull<libc::DIR>);

impl Dir {
/// Opens the given path as with `fcntl::open`.
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag,
mode: sys::stat::Mode) -> Result<Self> {
pub fn open<P: ?Sized + NixPath>(
path: &P,
oflag: OFlag,
mode: sys::stat::Mode,
) -> Result<Self> {
let fd = fcntl::open(path, oflag, mode)?;
Dir::from_fd(fd)
}

/// Opens the given path as with `fcntl::openat`.
pub fn openat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P, oflag: OFlag,
mode: sys::stat::Mode) -> Result<Self> {
pub fn openat<P: ?Sized + NixPath>(
dirfd: RawFd,
path: &P,
oflag: OFlag,
mode: sys::stat::Mode,
) -> Result<Self> {
let fd = fcntl::openat(dirfd, path, oflag, mode)?;
Dir::from_fd(fd)
}
Expand All @@ -57,11 +62,13 @@ impl Dir {
/// Converts from a file descriptor, closing it on success or failure.
#[doc(alias("fdopendir"))]
pub fn from_fd(fd: RawFd) -> Result<Self> {
let d = ptr::NonNull::new(unsafe { libc::fdopendir(fd) }).ok_or_else(|| {
let e = Error::last();
unsafe { libc::close(fd) };
e
})?;
let d = ptr::NonNull::new(unsafe { libc::fdopendir(fd) }).ok_or_else(
|| {
let e = Error::last();
unsafe { libc::close(fd) };
e
},
)?;
Ok(Dir(d))
}

Expand Down Expand Up @@ -103,9 +110,11 @@ fn next(dir: &mut Dir) -> Option<Result<Entry>> {
// Probably fine here too then.
let mut ent = std::mem::MaybeUninit::<dirent>::uninit();
let mut result = ptr::null_mut();
if let Err(e) = Errno::result(
readdir_r(dir.0.as_ptr(), ent.as_mut_ptr(), &mut result))
{
if let Err(e) = Errno::result(readdir_r(
dir.0.as_ptr(),
ent.as_mut_ptr(),
&mut result,
)) {
return Some(Err(e));
}
if result.is_null() {
Expand Down Expand Up @@ -207,7 +216,7 @@ pub enum Type {

impl Entry {
/// Returns the inode number (`d_ino`) of the underlying `dirent`.
#[allow(clippy::useless_conversion)] // Not useless on all OSes
#[allow(clippy::useless_conversion)] // Not useless on all OSes
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
pub fn ino(&self) -> u64 {
Expand Down Expand Up @@ -240,7 +249,11 @@ impl Entry {
/// notably, some Linux filesystems don't implement this. The caller should use `stat` or
/// `fstat` if this returns `None`.
pub fn file_type(&self) -> Option<Type> {
#[cfg(not(any(target_os = "illumos", target_os = "solaris", target_os = "haiku")))]
#[cfg(not(any(
target_os = "illumos",
target_os = "solaris",
target_os = "haiku"
)))]
match self.0.d_type {
libc::DT_FIFO => Some(Type::Fifo),
libc::DT_CHR => Some(Type::CharacterDevice),
Expand All @@ -253,7 +266,11 @@ impl Entry {
}

// illumos, Solaris, and Haiku systems do not have the d_type member at all:
#[cfg(any(target_os = "illumos", target_os = "solaris", target_os = "haiku"))]
#[cfg(any(
target_os = "illumos",
target_os = "solaris",
target_os = "haiku"
))]
None
}
}
40 changes: 21 additions & 19 deletions src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ pub use self::os::*;

#[cfg(any(target_os = "linux", target_os = "android"))]
mod os {
use std::os::unix::ffi::OsStrExt;
use crate::sys::utsname::uname;
use crate::Result;
use std::os::unix::ffi::OsStrExt;

// Features:
// * atomic cloexec on socket: 2.6.27
// * pipe2: 2.6.27
// * 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 @@ -27,7 +27,7 @@ mod os {
fn parse_kernel_version() -> Result<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 @@ -41,13 +41,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 @@ -87,7 +85,9 @@ mod os {

/// Check if the OS supports atomic close-on-exec for sockets
pub fn socket_atomic_cloexec() -> bool {
kernel_version().map(|version| version >= VERS_2_6_27).unwrap_or(false)
kernel_version()
.map(|version| version >= VERS_2_6_27)
.unwrap_or(false)
}

#[test]
Expand All @@ -111,11 +111,13 @@ mod os {
}
}

#[cfg(any(target_os = "macos",
target_os = "ios",
target_os = "fuchsia",
target_os = "haiku",
target_os = "solaris"))]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "fuchsia",
target_os = "haiku",
target_os = "solaris"
))]
mod os {
/// Check if the OS supports atomic close-on-exec for sockets
pub const fn socket_atomic_cloexec() -> bool {
Expand Down
7 changes: 4 additions & 3 deletions src/ifaddrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::iter::Iterator;
use std::mem;
use std::option::Option;

use crate::{Result, Errno};
use crate::sys::socket::{SockaddrLike, SockaddrStorage};
use crate::net::if_::*;
use crate::sys::socket::{SockaddrLike, SockaddrStorage};
use crate::{Errno, Result};

/// Describes a single address for an interface as returned by `getifaddrs`.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -47,7 +47,8 @@ impl InterfaceAddress {
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
let netmask = unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
let netmask =
unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
let mut addr = InterfaceAddress {
interface_name: ifname.to_string_lossy().to_string(),
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
Expand Down
10 changes: 8 additions & 2 deletions src/kmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ libc_bitflags!(
/// ```
///
/// See [`man init_module(2)`](https://man7.org/linux/man-pages/man2/init_module.2.html) for more information.
pub fn finit_module<T: AsRawFd>(fd: &T, param_values: &CStr, flags: ModuleInitFlags) -> Result<()> {
pub fn finit_module<T: AsRawFd>(
fd: &T,
param_values: &CStr,
flags: ModuleInitFlags,
) -> Result<()> {
let res = unsafe {
libc::syscall(
libc::SYS_finit_module,
Expand Down Expand Up @@ -116,7 +120,9 @@ libc_bitflags!(
///
/// See [`man delete_module(2)`](https://man7.org/linux/man-pages/man2/delete_module.2.html) for more information.
pub fn delete_module(name: &CStr, flags: DeleteModuleFlags) -> Result<()> {
let res = unsafe { libc::syscall(libc::SYS_delete_module, name.as_ptr(), flags.bits()) };
let res = unsafe {
libc::syscall(libc::SYS_delete_module, name.as_ptr(), flags.bits())
};

Errno::result(res).map(drop)
}
Loading