Skip to content
Draft
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"cSpell.import": [
"./.vscode/cSpell.json"
],
"rust-analyzer.check.command": "clippy"
"rust-analyzer.check.command": "clippy",
"rust-analyzer.vfs.extraIncludes": ["${workspaceFolder}/cfg_aliases.rs"]
}
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ nix = { workspace = true, features = ["fs", "signal"] }
rustix = { workspace = true, features = ["net", "process"] }

[build-dependencies]
cfg_aliases.workspace = true
phf_codegen.workspace = true

[features]
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;

#[path = "cfg_aliases.rs"]
mod cfg_aliases;

pub fn main() {
const ENV_FEATURE_PREFIX: &str = "CARGO_FEATURE_";
const FEATURE_PREFIX: &str = "feat_";
const OVERRIDE_PREFIX: &str = "uu_";

cfg_aliases::register();

// Do not rebuild build script unless the script itself or the enabled features are modified
// See <https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection>
println!("cargo:rerun-if-changed=build.rs");
Expand Down
30 changes: 30 additions & 0 deletions cfg_aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

pub fn register() {
cfg_aliases::cfg_aliases! {
bsd: { any(
target_vendor = "apple",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
) },
has_o_path: { any(target_os = "linux", target_os = "android") },
has_o_search: { any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",

Check warning on line 19 in cfg_aliases.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'tvos' (file:'cfg_aliases.rs', line:19)
target_os = "watchos",

Check warning on line 20 in cfg_aliases.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'watchos' (file:'cfg_aliases.rs', line:20)
target_os = "visionos",

Check warning on line 21 in cfg_aliases.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'visionos' (file:'cfg_aliases.rs', line:21)
target_os = "freebsd",
target_os = "netbsd",
target_os = "illumos",
target_os = "solaris"
) },
selinux: { all(feature = "selinux", any(target_os = "android", target_os = "linux")) },
smack: { all(feature = "smack", target_os = "linux") },
}
}
3 changes: 3 additions & 0 deletions src/uu/cp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ divan = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }

[build-dependencies]
cfg_aliases = { workspace = true }

[[bench]]
name = "cp_bench"
harness = false
Expand Down
11 changes: 11 additions & 0 deletions src/uu/cp/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[path = "../../../cfg_aliases.rs"]
mod cfg_aliases;

pub fn main() {
cfg_aliases::register();
}
6 changes: 3 additions & 3 deletions src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use uucore::show;
use uucore::translate;
use walkdir::{DirEntry, WalkDir};

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
use crate::set_selinux_context;
use crate::{
CopyMode, CopyResult, CpError, Options, aligned_ancestors, copy_attributes, copy_file,
Expand Down Expand Up @@ -582,7 +582,7 @@ pub(crate) fn copy_directory(
options.set_selinux_context,
)?;

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
if options.set_selinux_context {
set_selinux_context(&dir.dest, options.context.as_ref())?;
}
Expand All @@ -594,7 +594,7 @@ pub(crate) fn copy_directory(

// Even after a failed copy, so that no `--parents` directory stays owner-only.
let fixed = set_parent_dirs_attributes(&parent_dirs, options);
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
let fixed = fixed.and_then(|()| {
if !options.set_selinux_context {
return Ok(());
Expand Down
16 changes: 8 additions & 8 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,11 @@ impl Attributes {
mode: Preserve::Yes { required: true },
timestamps: Preserve::Yes { required: true },
context: {
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
{
Preserve::Yes { required: false }
}
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
#[cfg(not(selinux))]
{
Preserve::No { explicit: false }
}
Expand Down Expand Up @@ -1213,7 +1213,7 @@ impl Options {
}
}

#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
#[cfg(not(selinux))]
if let Preserve::Yes { required } = attributes.context {
let selinux_disabled_error = CpError::Error(translate!("cp-error-selinux-not-enabled"));
if required {
Expand All @@ -1227,9 +1227,9 @@ impl Options {
matches.get_flag(options::SELINUX) || matches.contains_id(options::CONTEXT);
let context = matches.get_one::<String>(options::CONTEXT).cloned();

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
let selinux_enabled = uucore::selinux::is_selinux_enabled();
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
#[cfg(not(selinux))]
let selinux_enabled = false;
if context.is_some() && !selinux_enabled {
show_warning!("{}", translate!("cp-warning-context-ignored"));
Expand Down Expand Up @@ -1794,7 +1794,7 @@ fn handle_preserve<F: Fn() -> CopyResult<()>>(p: Preserve, f: F) -> CopyResult<(
Ok(())
}

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
pub(crate) fn set_selinux_context(path: &Path, context: Option<&String>) -> CopyResult<()> {
if !uucore::selinux::is_selinux_enabled() {
return Ok(());
Expand Down Expand Up @@ -2029,7 +2029,7 @@ pub(crate) fn copy_attributes(
Ok(())
})?;

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

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
if options.set_selinux_context {
set_selinux_context(dest, options.context.as_ref())?;
}
Expand Down
3 changes: 3 additions & 0 deletions src/uu/id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ fluent = { workspace = true }
rustix = { workspace = true, features = ["process"] }
uucore = { workspace = true, features = ["entries", "process"] }

[build-dependencies]
cfg_aliases = { workspace = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
selinux = { workspace = true, optional = true }

Expand Down
11 changes: 11 additions & 0 deletions src/uu/id/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[path = "../../../cfg_aliases.rs"]
mod cfg_aliases;

pub fn main() {
cfg_aliases::register();
}
27 changes: 10 additions & 17 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,9 @@ macro_rules! cstr2cow {
}

fn get_context_help_text() -> String {
#[cfg(any(
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
all(feature = "smack", target_os = "linux"),
))]
#[cfg(any(selinux, smack))]
return translate!("id-context-help-enabled");

#[cfg(not(any(
all(feature = "selinux", any(target_os = "android", target_os = "linux")),
all(feature = "smack", target_os = "linux"),
)))]
#[cfg(not(any(selinux, smack)))]
return translate!("id-context-help-disabled");
}

Expand Down Expand Up @@ -89,9 +82,9 @@ struct State {
rflag: bool, // --real
zflag: bool, // --zero
cflag: bool, // --context
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
selinux_supported: bool,
#[cfg(all(feature = "smack", target_os = "linux"))]
#[cfg(smack)]
smack_supported: bool,
ids: Option<Ids>,
// The behavior for calling GNU's `id` and calling GNU's `id $USER` is similar but different.
Expand Down Expand Up @@ -130,9 +123,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
rflag: matches.get_flag(options::OPT_REAL_ID),
zflag: matches.get_flag(options::OPT_ZERO),
cflag: matches.get_flag(options::OPT_CONTEXT),
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
selinux_supported: uucore::selinux::is_selinux_enabled(),
#[cfg(all(feature = "smack", target_os = "linux"))]
#[cfg(smack)]
smack_supported: uucore::smack::is_smack_enabled(),
user_specified: !users.is_empty(),
ids: None,
Expand Down Expand Up @@ -168,7 +161,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

if state.cflag {
// SELinux context
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
if state.selinux_supported {
if let Ok(context) = selinux::SecurityContext::current(false) {
let bytes = context.as_bytes();
Expand All @@ -182,7 +175,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

// SMACK label
#[cfg(all(feature = "smack", target_os = "linux"))]
#[cfg(smack)]
if state.smack_supported {
match uucore::smack::get_smack_label_for_self() {
Ok(label) => {
Expand Down Expand Up @@ -719,7 +712,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
.join(",")
)?;

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
#[cfg(selinux)]
if state.selinux_supported
&& !state.user_specified
&& std::env::var_os("POSIXLY_CORRECT").is_none()
Expand All @@ -731,7 +724,7 @@ fn id_print(state: &State, groups: &[u32]) -> io::Result<()> {
}
}

#[cfg(all(feature = "smack", target_os = "linux"))]
#[cfg(smack)]
if state.smack_supported
&& !state.user_specified
&& std::env::var_os("POSIXLY_CORRECT").is_none()
Expand Down
3 changes: 3 additions & 0 deletions src/uu/install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ uucore = { workspace = true, default-features = true, features = [
"process",
] }

[build-dependencies]
cfg_aliases = { workspace = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
rustix = { workspace = true, features = ["fs"] }
selinux = { workspace = true, optional = true }
Expand Down
11 changes: 11 additions & 0 deletions src/uu/install/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[path = "../../../cfg_aliases.rs"]
mod cfg_aliases;

pub fn main() {
cfg_aliases::register();
}
Loading
Loading