Skip to content

Commit 945a586

Browse files
committed
install: add selinux cfg alias
1 parent ea049d7 commit 945a586

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/install/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ workspace = true
1818
[lib]
1919
path = "src/install.rs"
2020

21+
[build-dependencies]
22+
cfg_aliases.workspace = true
23+
2124
[dependencies]
2225
clap = { workspace = true }
2326
filetime = { workspace = true }

src/uu/install/build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use cfg_aliases::cfg_aliases;
7+
8+
pub fn main() {
9+
cfg_aliases! {
10+
selinux: { all(feature = "selinux", any(target_os = "android", target_os = "linux")) },
11+
}
12+
}

src/uu/install/src/install.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod mode;
1010
use clap::{Arg, ArgAction, ArgMatches, Command};
1111
use file_diff::diff;
1212
use filetime::{FileTime, set_file_times};
13-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
13+
#[cfg(selinux)]
1414
use selinux::SecurityContext;
1515
use std::ffi::OsString;
1616
use std::fmt::Debug;
@@ -30,7 +30,7 @@ use uucore::perms::{Verbosity, VerbosityLevel, wrap_chown};
3030
use uucore::process::{getegid, geteuid};
3131
#[cfg(unix)]
3232
use uucore::safe_traversal::{DirFd, SymlinkBehavior, create_dir_all_safe};
33-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
33+
#[cfg(selinux)]
3434
use uucore::selinux::{
3535
SeLinuxError, contexts_differ, get_selinux_security_context, is_selinux_enabled,
3636
selinux_error_description, set_selinux_security_context,
@@ -121,7 +121,7 @@ enum InstallError {
121121
#[error("{}", translate!("install-error-extra-operand", "operand" => .0.quote(), "usage" => .1.clone()))]
122122
ExtraOperand(OsString, String),
123123

124-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
124+
#[cfg(selinux)]
125125
#[error("{}", .0)]
126126
SelinuxContextFailed(String),
127127
}
@@ -489,7 +489,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
489489
}
490490

491491
// Set SELinux context for all created directories if needed
492-
#[cfg(all(feature = "selinux", target_os = "linux"))]
492+
#[cfg(selinux)]
493493
if should_set_selinux_context(b) {
494494
let context = get_context_for_selinux(b);
495495
set_selinux_context_for_directories_install(path_to_create.as_path(), context);
@@ -514,7 +514,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
514514
show_if_err!(chown_optional_user_group(path, b));
515515

516516
// Set SELinux context for directory if needed
517-
#[cfg(all(feature = "selinux", target_os = "linux"))]
517+
#[cfg(selinux)]
518518
if b.default_context {
519519
show_if_err!(set_selinux_default_context(path));
520520
} else if b.context.is_some() {
@@ -683,7 +683,7 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
683683
}
684684

685685
// Set SELinux context for all created directories if needed
686-
#[cfg(all(feature = "selinux", target_os = "linux"))]
686+
#[cfg(selinux)]
687687
if should_set_selinux_context(b) {
688688
let context = get_context_for_selinux(b);
689689
set_selinux_context_for_directories_install(to_create, context);
@@ -717,7 +717,7 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
717717
}
718718

719719
// Set SELinux context for all created directories if needed
720-
#[cfg(all(feature = "selinux", target_os = "linux"))]
720+
#[cfg(selinux)]
721721
if should_set_selinux_context(b) {
722722
let context = get_context_for_selinux(b);
723723
set_selinux_context_for_directories_install(to_create, context);
@@ -1090,7 +1090,7 @@ fn finalize_installed_file(
10901090
preserve_timestamps(from, to)?;
10911091
}
10921092

1093-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1093+
#[cfg(selinux)]
10941094
if b.privileged {
10951095
if b.preserve_context {
10961096
uucore::selinux::preserve_security_context(from, to)
@@ -1149,7 +1149,7 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
11491149
finalize_installed_file(from, to, b, backup_path)
11501150
}
11511151

1152-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1152+
#[cfg(selinux)]
11531153
fn get_context_for_selinux(b: &Behavior) -> Option<&String> {
11541154
if b.default_context {
11551155
None
@@ -1158,7 +1158,7 @@ fn get_context_for_selinux(b: &Behavior) -> Option<&String> {
11581158
}
11591159
}
11601160

1161-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1161+
#[cfg(selinux)]
11621162
fn should_set_selinux_context(b: &Behavior) -> bool {
11631163
b.privileged && (b.context.is_some() || b.default_context)
11641164
}
@@ -1254,7 +1254,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
12541254
}
12551255

12561256
if b.privileged {
1257-
#[cfg(all(feature = "selinux", target_os = "linux"))]
1257+
#[cfg(selinux)]
12581258
if b.preserve_context && contexts_differ(from, to) {
12591259
return true;
12601260
}
@@ -1283,7 +1283,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
12831283
false
12841284
}
12851285

1286-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1286+
#[cfg(selinux)]
12871287
/// Sets the `SELinux` security context for install's -Z flag behavior.
12881288
///
12891289
/// This function implements the specific behavior needed for install's -Z flag,
@@ -1317,7 +1317,7 @@ pub fn set_selinux_default_context(path: &Path) -> Result<(), SeLinuxError> {
13171317
}
13181318
}
13191319

1320-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1320+
#[cfg(selinux)]
13211321
/// Gets the default `SELinux` context for a path based on the system's security policy.
13221322
///
13231323
/// This function attempts to determine what the "correct" `SELinux` context should be
@@ -1373,7 +1373,7 @@ fn get_default_context_for_path(path: &Path) -> Result<Option<String>, SeLinuxEr
13731373
Ok(None)
13741374
}
13751375

1376-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1376+
#[cfg(selinux)]
13771377
/// Derives an appropriate `SELinux` context based on a parent directory context.
13781378
///
13791379
/// This is a heuristic function that attempts to generate an appropriate
@@ -1411,7 +1411,7 @@ fn derive_context_from_parent(parent_context: &str) -> String {
14111411
}
14121412
}
14131413

1414-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1414+
#[cfg(selinux)]
14151415
/// Helper function to collect paths that need `SELinux` context setting.
14161416
///
14171417
/// Traverses from the given starting path up to existing parent directories.
@@ -1425,7 +1425,7 @@ fn collect_paths_for_context_setting(starting_path: &Path) -> Vec<&Path> {
14251425
paths
14261426
}
14271427

1428-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1428+
#[cfg(selinux)]
14291429
/// Sets the `SELinux` security context for a directory hierarchy.
14301430
///
14311431
/// This function traverses from the given starting path up to existing parent directories
@@ -1465,7 +1465,7 @@ fn set_selinux_context_for_directories(target_path: &Path, context: Option<&Stri
14651465
}
14661466
}
14671467

1468-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1468+
#[cfg(selinux)]
14691469
/// Sets `SELinux` context for created directories using install's -Z default behavior.
14701470
///
14711471
/// Similar to `set_selinux_context_for_directories` but uses install's
@@ -1489,10 +1489,10 @@ pub fn set_selinux_context_for_directories_install(target_path: &Path, context:
14891489

14901490
#[cfg(test)]
14911491
mod tests {
1492-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1492+
#[cfg(selinux)]
14931493
use super::derive_context_from_parent;
14941494

1495-
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
1495+
#[cfg(selinux)]
14961496
#[test]
14971497
fn test_derive_context_from_parent() {
14981498
// Test cases: (input_context, file_type, expected_output, description)

0 commit comments

Comments
 (0)