Skip to content
Open
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,10 @@ workspace = true
[workspace.lints.rust]
# Allow "fuzzing" as a "cfg" condition name and "cygwin" as a value for "target_os"
# https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html
# "linux_android" is a cfg alias for any(target_os = "linux", target_os = "android")
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(fuzzing)',
'cfg(linux_android)',
'cfg(target_os, values("cygwin"))',
] }
#unused_qualifications = "warn" // TODO: fix warnings in uucore, then re-enable this lint
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub fn main() {
// See <https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection>
println!("cargo:rerun-if-changed=build.rs");

// Define cfg alias for Linux and Android platforms
// This avoids repetitive `any(target_os = "linux", target_os = "android")` patterns
#[cfg(any(target_os = "linux", target_os = "android"))]
println!("cargo:rustc-cfg=linux_android");

// Check for tldr.zip when building uudoc to warn users once at build time
// instead of repeatedly at runtime for each utility
if env::var("CARGO_FEATURE_UUDOC").is_ok() && !Path::new("docs/tldr.zip").exists() {
Expand Down
6 changes: 3 additions & 3 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use uucore::translate;
use uucore::{fast_inc::fast_inc_one, format_usage};

/// Linux splice support
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
mod splice;

// Allocate 32 digits for the line number.
Expand Down Expand Up @@ -85,7 +85,7 @@ enum CatError {
#[error("{0}")]
Io(#[from] io::Error),
/// Wrapper around `nix::Error`
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
#[error("{0}")]
Nix(#[from] nix::Error),
/// Unknown file type; it's not a regular file, socket, etc.
Expand Down Expand Up @@ -477,7 +477,7 @@ fn get_input_type(path: &OsString) -> CatResult<InputType> {
fn write_fast<R: FdReadable>(handle: &mut InputHandle<R>) -> CatResult<()> {
let stdout = io::stdout();
let mut stdout_lock = stdout.lock();
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
{
// If we're on Linux or Android, try to use the splice() system call
// for faster writing. If it works, we're done.
Expand Down
2 changes: 1 addition & 1 deletion src/uu/chcon/src/chcon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// spell-checker:ignore (vars) RFILE

#![cfg(any(target_os = "linux", target_os = "android"))]
#![cfg(linux_android)]
#![allow(clippy::upper_case_acronyms)]

use clap::builder::ValueParser;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/chcon/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#![cfg(any(target_os = "linux", target_os = "android"))]
#![cfg(linux_android)]

use std::ffi::OsString;
use std::fmt::Write;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/chcon/src/fts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#![cfg(any(target_os = "linux", target_os = "android"))]
#![cfg(linux_android)]

use std::ffi::{CStr, CString, OsStr};
use std::marker::PhantomData;
Expand Down
4 changes: 2 additions & 2 deletions src/uu/chcon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
//! entirely (via #![cfg(...)]), which can break tooling and cross builds that
//! expect this binary to exist even when it's a no-op off Linux.

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
uucore::bin!(uu_chcon);

#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[cfg(not(linux_android))]
fn main() {
eprintln!("chcon: SELinux is not supported on this platform");
std::process::exit(1);
Expand Down
2 changes: 1 addition & 1 deletion src/uu/chroot/src/chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn set_supplemental_gids(gids: &[libc::gid_t]) -> std::io::Result<()> {
target_os = "cygwin"
))]
let n = gids.len() as libc::c_int;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
let n = gids.len() as libc::size_t;
let err = unsafe { setgroups(n, gids.as_ptr()) };
if err == 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ pub(crate) fn copy_attributes(
Ok(())
})?;

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(all(feature = "selinux", linux_android))]
handle_preserve(&attributes.context, || -> CopyResult<()> {
// Get the source context and apply it to the destination
if let Ok(context) = selinux::SecurityContext::of_path(source, false, false) {
Expand Down Expand Up @@ -2586,7 +2586,7 @@ fn copy_file(
copy_attributes(source, dest, &options.attributes)?;
}

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(all(feature = "selinux", linux_android))]
if options.set_selinux_context && uucore::selinux::is_selinux_enabled() {
// Set the given selinux permissions on the copied file.
if let Err(e) =
Expand Down
12 changes: 6 additions & 6 deletions src/uu/cp/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum CopyMethod {
/// Use the Linux `ioctl_ficlone` API to do a copy-on-write clone.
///
/// `fallback` controls what to do if the system call fails.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn clone<P>(source: P, dest: P, fallback: CloneFallback) -> std::io::Result<()>
where
P: AsRef<Path>,
Expand All @@ -77,7 +77,7 @@ where
/// Checks whether a file contains any non null bytes i.e. any byte != 0x0
/// This function returns a tuple of (bool, u64, u64) signifying a tuple of (whether a file has
/// data, its size, no of blocks it has allocated in disk)
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn check_for_data(source: &Path) -> Result<(bool, u64, u64), std::io::Error> {
let mut src_file = File::open(source)?;
let metadata = src_file.metadata()?;
Expand All @@ -102,7 +102,7 @@ fn check_for_data(source: &Path) -> Result<(bool, u64, u64), std::io::Error> {
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
/// Checks whether a file is sparse i.e. it contains holes, uses the crude heuristic blocks < size / 512
/// Reference:`<https://doc.rust-lang.org/std/os/unix/fs/trait.MetadataExt.html#tymethod.blocks>`
fn check_sparse_detection(source: &Path) -> Result<bool, std::io::Error> {
Expand All @@ -119,7 +119,7 @@ fn check_sparse_detection(source: &Path) -> Result<bool, std::io::Error> {

/// Optimized [`sparse_copy`] doesn't create holes for large sequences of zeros in non `sparse_files`
/// Used when `--sparse=auto`
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn sparse_copy_without_hole<P>(source: P, dest: P) -> std::io::Result<()>
where
P: AsRef<Path>,
Expand Down Expand Up @@ -169,7 +169,7 @@ where
}
/// Perform a sparse copy from one file to another.
/// Creates a holes for large sequences of zeros in `non_sparse_files`, used for `--sparse=always`
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn sparse_copy<P>(source: P, dest: P) -> std::io::Result<()>
where
P: AsRef<Path>,
Expand Down Expand Up @@ -201,7 +201,7 @@ where
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
/// Checks whether an existing destination is a fifo
fn check_dest_is_fifo(dest: &Path) -> bool {
// If our destination file exists and its a fifo , we do a standard copy .
Expand Down
4 changes: 2 additions & 2 deletions src/uu/cp/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ mod macos;
#[cfg(target_os = "macos")]
pub(crate) use self::macos::copy_on_write;

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
mod linux;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
pub(crate) use self::linux::copy_on_write;

#[cfg(not(any(
Expand Down
26 changes: 13 additions & 13 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ mod progress;
use crate::bufferedoutput::BufferedOutput;
use blocks::conv_block_unblock_helper;
use datastructures::*;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
use nix::fcntl::FcntlArg::F_SETFL;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
use nix::fcntl::OFlag;
use parseargs::Parser;
use progress::ProgUpdateType;
Expand All @@ -31,9 +31,9 @@ use std::env;
use std::ffi::OsString;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
use std::os::fd::AsFd;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(unix)]
use std::os::unix::{
Expand Down Expand Up @@ -405,7 +405,7 @@ impl<'a> Input<'a> {
let mut opts = OpenOptions::new();
opts.read(true);

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
if let Some(libc_flags) = make_linux_iflags(&settings.iflags) {
opts.custom_flags(libc_flags);
}
Expand All @@ -427,7 +427,7 @@ impl<'a> Input<'a> {
fn new_fifo(filename: &Path, settings: &'a Settings) -> UResult<Self> {
let mut opts = OpenOptions::new();
opts.read(true);
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
opts.custom_flags(make_linux_iflags(&settings.iflags).unwrap_or(0));
let mut src = Source::Fifo(opts.open(filename)?);
if settings.skip > 0 {
Expand All @@ -437,7 +437,7 @@ impl<'a> Input<'a> {
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn make_linux_iflags(iflags: &IFlags) -> Option<libc::c_int> {
let mut flag = 0;

Expand Down Expand Up @@ -720,7 +720,7 @@ fn is_sparse(buf: &[u8]) -> bool {

/// Handle O_DIRECT write errors by temporarily removing the flag and retrying.
/// This follows GNU dd behavior for partial block writes with O_DIRECT.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
fn handle_o_direct_write(f: &mut File, buf: &[u8], original_error: io::Error) -> io::Result<usize> {
use nix::fcntl::{FcntlArg, OFlag, fcntl};

Expand Down Expand Up @@ -757,7 +757,7 @@ fn handle_o_direct_write(f: &mut File, buf: &[u8], original_error: io::Error) ->
}

/// Stub for non-Linux platforms - just return the original error.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[cfg(not(linux_android))]
fn handle_o_direct_write(
_f: &mut File,
_buf: &[u8],
Expand Down Expand Up @@ -845,7 +845,7 @@ impl<'a> Output<'a> {
.create_new(cflags.excl)
.append(oflags.append);

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
if let Some(libc_flags) = make_linux_oflags(oflags) {
opts.custom_flags(libc_flags);
}
Expand Down Expand Up @@ -891,7 +891,7 @@ impl<'a> Output<'a> {
/// (current position) that shall be used.
fn new_file_from_stdout(settings: &'a Settings) -> UResult<Self> {
let fx = OwnedFileDescriptorOrHandle::from(io::stdout())?;
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
if let Some(libc_flags) = make_linux_oflags(&settings.oflags) {
nix::fcntl::fcntl(
fx.as_raw().as_fd(),
Expand Down Expand Up @@ -925,7 +925,7 @@ impl<'a> Output<'a> {
.create(!settings.oconv.nocreat)
.create_new(settings.oconv.excl)
.append(settings.oflags.append);
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
opts.custom_flags(make_linux_oflags(&settings.oflags).unwrap_or(0));
let dst = Dest::Fifo(opts.open(filename)?);
Ok(Self { dst, settings })
Expand Down Expand Up @@ -1321,7 +1321,7 @@ fn finalize<T>(
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
#[allow(clippy::cognitive_complexity)]
fn make_linux_oflags(oflags: &OFlags) -> Option<libc::c_int> {
let mut flag = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ enum Block {
/// Return an Unimplemented error when the target is not Linux or Android
macro_rules! linux_only {
($s: expr, $val: expr) => {
if cfg!(any(target_os = "linux", target_os = "android")) {
if cfg!(linux_android) {
$val
} else {
return Err(ParseError::Unimplemented($s.to_string()).into());
Expand Down
6 changes: 3 additions & 3 deletions src/uu/dd/src/parseargs/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::conversion_tables::{
};
use crate::parseargs::Parser;

#[cfg(not(any(target_os = "linux", target_os = "android")))]
#[cfg(not(linux_android))]
#[allow(clippy::useless_vec)]
#[test]
fn unimplemented_flags_should_error_non_linux() {
Expand Down Expand Up @@ -384,7 +384,7 @@ fn parse_oflag_tokens() {
);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
#[test]
fn parse_iflag_tokens_linux() {
let args = ["iflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
Expand All @@ -407,7 +407,7 @@ fn parse_iflag_tokens_linux() {
);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(linux_android)]
#[test]
fn parse_oflag_tokens_linux() {
let args = ["oflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
Expand Down
Loading
Loading