Skip to content

Commit

Permalink
feature-gate most Nix functions
Browse files Browse the repository at this point in the history
Using features reduces build time and size for consumer crates.  By
default all features are enabled.
  • Loading branch information
vdagonneau authored and asomers committed Oct 9, 2021
1 parent da49e4f commit bf93bc0
Show file tree
Hide file tree
Showing 20 changed files with 755 additions and 141 deletions.
20 changes: 15 additions & 5 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ build: &BUILD
- $TOOL +$TOOLCHAIN $BUILD $ZFLAGS --target $TARGET --all-targets
- $TOOL +$TOOLCHAIN doc $ZFLAGS --no-deps --target $TARGET
- $TOOL +$TOOLCHAIN clippy $ZFLAGS --target $TARGET -- $CLIPPYFLAGS
- if [ -z "$NOHACK" ]; then $TOOL install cargo-hack; fi
- if [ -z "$NOHACK" ]; then $TOOL hack $ZFLAGS check --target $TARGET --each-feature; fi

# Tests that do require executing the binaries
test: &TEST
Expand Down Expand Up @@ -50,14 +52,16 @@ task:
- cargo build --target i686-unknown-freebsd
- cargo doc --no-deps --target i686-unknown-freebsd
- cargo test --target i686-unknown-freebsd
i386_feature_script:
- . $HOME/.cargo/env
- cargo hack check --each-feature --target i686-unknown-freebsd
before_cache_script: rm -rf $CARGO_HOME/registry/index

# Test OSX in a full VM
task:
matrix:
- name: OSX x86_64
env:
TARGET: x86_64-apple-darwin
name: OSX x86_64
env:
TARGET: x86_64-apple-darwin
osx_instance:
image: catalina-xcode
setup_script:
Expand Down Expand Up @@ -192,13 +196,17 @@ task:
# https://github.com/rust-embedded/cross/issues/535
- name: iOS aarch64
env:
# cargo hack tries to invoke the iphonesimulator SDK for iOS
NOHACK: 1
TARGET: aarch64-apple-ios
# Rustup only supports cross-building from arbitrary hosts for iOS at
# 1.49.0 and above. Below that it's possible to cross-build from an OSX
# host, but OSX VMs are more expensive than Linux VMs.
TOOLCHAIN: 1.49.0
- name: iOS x86_64
env:
# cargo hack tries to invoke the iphonesimulator SDK for iOS
NOHACK: 1
TARGET: x86_64-apple-ios
TOOLCHAIN: 1.49.0
# Cross testing on powerpc fails with "undefined reference to renameat2".
Expand All @@ -222,6 +230,8 @@ task:
TARGET: x86_64-unknown-netbsd
- name: Redox x86_64
env:
# cargo-hack needs a newer compiler
NOHACK: 1
TARGET: x86_64-unknown-redox
# Redox requires a nightly compiler.
# If stuff breaks, change nightly to the date in the toolchain_*
Expand Down Expand Up @@ -269,6 +279,6 @@ task:
image: rustlang/rust:nightly
setup_script:
- cargo update -Zminimal-versions
script:
check_script:
- cargo check
before_cache_script: rm -rf $CARGO_HOME/registry/index
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [0.23.0] - 2021-09-28
### Added

- Added fine-grained features flags. Most Nix functionality can now be
conditionally enabled. By default, all features are enabled.
(#[TODO](https://github.com/nix-rust/nix/pull/TODO))
- Added the `LocalPeerCred` sockopt.
(#[1482](https://github.com/nix-rust/nix/pull/1482))
- Added `TimeSpec::from_duration` and `TimeSpec::from_timespec`
Expand Down
47 changes: 47 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ categories = ["os::unix-apis"]
include = ["src/**/*", "LICENSE", "README.md", "CHANGELOG.md"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
targets = [
"x86_64-unknown-linux-gnu",
"aarch64-linux-android",
Expand All @@ -32,6 +33,52 @@ cfg-if = "1.0"
[target.'cfg(not(target_os = "redox"))'.dependencies]
memoffset = "0.6.3"

[features]
default = [
"acct", "aio", "dir", "env", "event", "features", "fs",
"hostname",
"inotify", "ioctl", "kmod", "mman", "mount", "mqueue",
"net", "personality", "process", "poll", "pthread", "ptrace", "quota",
"reboot",
"resource", "sched", "signal",
"socket",
"term", "time", "ucontext", "uio",
"users", "zerocopy",
]

acct = []
aio = []
dir = ["fs"]
env = []
event = []
features = []
fs = []
hostname = []
inotify = []
ioctl = []
kmod = []
mman = []
mount = ["uio"]
mqueue = ["fs"]
net = ["socket"]
personality = []
poll = []
pthread = []
ptrace = ["process"]
quota = []
process = []
reboot = []
resource = []
sched = ["process"]
signal = ["process"]
socket = []
term = []
time = []
ucontext = ["signal"]
uio = []
users = ["features"]
zerocopy = ["fs", "uio"]

[target.'cfg(target_os = "dragonfly")'.build-dependencies]
cc = "1"

Expand Down
59 changes: 53 additions & 6 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use std::ffi::OsString;
use std::os::raw;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::io::RawFd;
use crate::sys::stat::Mode;
use crate::{NixPath, Result};

#[cfg(any(target_os = "android", target_os = "linux"))]
use std::ptr; // For splice and copy_file_range
#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::sys::uio::IoVec; // For vmsplice
#[cfg(feature = "fs")]
use crate::{
NixPath,
Result,
sys::stat::Mode
};

#[cfg(any(
target_os = "linux",
Expand All @@ -22,10 +24,13 @@ use crate::sys::uio::IoVec; // For vmsplice
target_env = "uclibc",
target_os = "freebsd"
))]
pub use self::posix_fadvise::*;
#[cfg(feature = "fs")]
pub use self::posix_fadvise::{PosixFadviseAdvice, posix_fadvise};

#[cfg(not(target_os = "redox"))]
#[cfg(any(feature = "fs", feature = "process"))]
libc_bitflags! {
#[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "process"))))]
pub struct AtFlags: c_int {
AT_REMOVEDIR;
AT_SYMLINK_FOLLOW;
Expand All @@ -39,8 +44,10 @@ libc_bitflags! {
}
}

#[cfg(any(feature = "fs", feature = "term"))]
libc_bitflags!(
/// Configuration options for opened files.
#[cfg_attr(docsrs, doc(cfg(any(feature = "fs", feature = "term"))))]
pub struct OFlag: c_int {
/// Mask for the access mode of the file.
O_ACCMODE;
Expand Down Expand Up @@ -165,6 +172,9 @@ libc_bitflags!(
}
);

feature!{
#![feature = "fs"]

// The conversion is not identical on all operating systems.
#[allow(clippy::useless_conversion)]
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
Expand Down Expand Up @@ -209,19 +219,24 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
})??;
Errno::result(res).map(drop)
}
}

#[cfg(all(
target_os = "linux",
target_env = "gnu",
))]
#[cfg(feature = "fs")]
libc_bitflags! {
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct RenameFlags: u32 {
RENAME_EXCHANGE;
RENAME_NOREPLACE;
RENAME_WHITEOUT;
}
}

feature!{
#![feature = "fs"]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
Expand Down Expand Up @@ -348,6 +363,7 @@ pub(crate) fn at_rawfd(fd: Option<RawFd>) -> raw::c_int {
Some(fd) => fd,
}
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
libc_bitflags!(
Expand All @@ -364,14 +380,19 @@ libc_bitflags!(
}
);

#[cfg(feature = "fs")]
libc_bitflags!(
/// Additional configuration flags for `fcntl`'s `F_SETFD`.
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct FdFlag: c_int {
/// The file descriptor will automatically be closed during a successful `execve(2)`.
FD_CLOEXEC;
}
);

feature!{
#![feature = "fs"]

#[cfg(not(target_os = "redox"))]
#[derive(Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -455,6 +476,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
Errno::result(res)
}

// TODO: convert to libc_enum
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum FlockArg {
Expand Down Expand Up @@ -483,10 +505,13 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {

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

#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(feature = "zerocopy")]
libc_bitflags! {
/// Additional flags to `splice` and friends.
#[cfg_attr(docsrs, doc(cfg(feature = "zerocopy")))]
pub struct SpliceFFlags: c_uint {
/// Request that pages be moved instead of copied.
///
Expand All @@ -505,6 +530,9 @@ libc_bitflags! {
}
}

feature!{
#![feature = "zerocopy"]

/// Copy a range of data from one file to another
///
/// The `copy_file_range` system call performs an in-kernel copy between
Expand Down Expand Up @@ -577,7 +605,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> {
pub fn vmsplice(
fd: RawFd,
iov: &[crate::sys::uio::IoVec<&[u8]>],
flags: SpliceFFlags
) -> Result<usize>
{
let ret = unsafe {
libc::vmsplice(
fd,
Expand All @@ -588,10 +621,13 @@ pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result<
};
Errno::result(ret).map(|r| r as usize)
}
}

#[cfg(any(target_os = "linux"))]
#[cfg(feature = "fs")]
libc_bitflags!(
/// Mode argument flags for fallocate determining operation performed on a given range.
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub struct FallocateFlags: c_int {
/// File size is not changed.
///
Expand Down Expand Up @@ -620,11 +656,15 @@ libc_bitflags!(
}
);

feature!{
#![feature = "fs"]

/// Manipulates file space.
///
/// Allows the caller to directly manipulate the allocated disk space for the
/// file referred to by fd.
#[cfg(any(target_os = "linux"))]
#[cfg(feature = "fs")]
pub fn fallocate(
fd: RawFd,
mode: FallocateFlags,
Expand All @@ -635,6 +675,7 @@ pub fn fallocate(
Errno::result(res).map(drop)
}


#[cfg(any(
target_os = "linux",
target_os = "android",
Expand All @@ -649,9 +690,11 @@ mod posix_fadvise {
use std::os::unix::io::RawFd;
use crate::Result;

#[cfg(feature = "fs")]
libc_enum! {
#[repr(i32)]
#[non_exhaustive]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub enum PosixFadviseAdvice {
POSIX_FADV_NORMAL,
POSIX_FADV_SEQUENTIAL,
Expand All @@ -662,6 +705,8 @@ mod posix_fadvise {
}
}

feature!{
#![feature = "fs"]
pub fn posix_fadvise(
fd: RawFd,
offset: libc::off_t,
Expand All @@ -676,6 +721,7 @@ mod posix_fadvise {
Err(Errno::from_i32(res))
}
}
}
}

#[cfg(any(
Expand All @@ -694,3 +740,4 @@ pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Resu
Ok(errno) => Err(Errno::from_i32(errno)),
}
}
}

0 comments on commit bf93bc0

Please sign in to comment.