Skip to content
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
6 changes: 3 additions & 3 deletions src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use uucore::translate;
use uucore::uio_error;
use walkdir::{DirEntry, WalkDir};

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

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

#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if options.set_selinux_context {
set_selinux_context(y, options.context.as_ref())?;
}
Expand Down
10 changes: 5 additions & 5 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,11 +896,11 @@ impl Attributes {
mode: Preserve::Yes { required: true },
timestamps: Preserve::Yes { required: true },
context: {
#[cfg(feature = "feat_selinux")]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
{
Preserve::Yes { required: false }
}
#[cfg(not(feature = "feat_selinux"))]
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
{
Preserve::No { explicit: false }
}
Expand Down Expand Up @@ -1141,7 +1141,7 @@ impl Options {
}
}

#[cfg(not(feature = "selinux"))]
#[cfg(not(all(feature = "selinux", any(target_os = "linux", target_os = "android"))))]
if let Preserve::Yes { required } = attributes.context {
let selinux_disabled_error = CpError::Error(translate!("cp-error-selinux-not-enabled"));
if required {
Expand Down Expand Up @@ -1692,7 +1692,7 @@ fn handle_preserve<F: Fn() -> CopyResult<()>>(p: Preserve, f: F) -> CopyResult<(
Ok(())
}

#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
pub(crate) fn set_selinux_context(path: &Path, context: Option<&String>) -> CopyResult<()> {
if !uucore::selinux::is_selinux_enabled() {
return Ok(());
Expand Down Expand Up @@ -2670,7 +2670,7 @@ fn copy_file(
fs::File::create(dest).map(|f| f.set_len(0)).ok();
})?;

#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if options.set_selinux_context {
set_selinux_context(dest, options.context.as_ref())?;
}
Expand Down
20 changes: 13 additions & 7 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ fn directory(paths: &[OsString], b: &Behavior) -> UResult<()> {
}

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

// Set SELinux context for directory if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.default_context {
show_if_err!(set_selinux_default_context(path));
} else if b.context.is_some() {
Expand Down Expand Up @@ -683,7 +683,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
}

// Set SELinux context for all created directories if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(
feature = "selinux",
any(target_os = "linux", target_os = "android")
))]
if should_set_selinux_context(b) {
let context = get_context_for_selinux(b);
set_selinux_context_for_directories_install(to_create, context);
Expand Down Expand Up @@ -717,7 +720,10 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
}

// Set SELinux context for all created directories if needed
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(
feature = "selinux",
any(target_os = "linux", target_os = "android")
))]
if should_set_selinux_context(b) {
let context = get_context_for_selinux(b);
set_selinux_context_for_directories_install(to_create, context);
Expand Down Expand Up @@ -1090,7 +1096,7 @@ fn finalize_installed_file(
preserve_timestamps(from, to)?;
}

#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.privileged {
if b.preserve_context {
uucore::selinux::preserve_security_context(from, to)
Expand Down Expand Up @@ -1154,7 +1160,7 @@ fn get_context_for_selinux(b: &Behavior) -> Option<&String> {
}
}

#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
fn should_set_selinux_context(b: &Behavior) -> bool {
b.privileged && (b.context.is_some() || b.default_context)
}
Expand Down Expand Up @@ -1250,7 +1256,7 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool {
}

if b.privileged {
#[cfg(all(feature = "selinux", target_os = "linux"))]
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
if b.preserve_context && contexts_differ(from, to) {
return true;
}
Expand Down
15 changes: 12 additions & 3 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,10 @@ fn test_cp_preserve_xattr() {
}

#[test]
#[cfg(all(target_os = "linux", not(feature = "feat_selinux")))]
#[cfg(all(
not(feature = "feat_selinux"),
any(target_os = "linux", target_os = "android")
))]
fn test_cp_preserve_all_context_fails_on_non_selinux() {
new_ucmd!()
.arg(TEST_COPY_FROM_FOLDER_FILE)
Expand Down Expand Up @@ -6802,7 +6805,10 @@ fn test_cp_selinux() {
}

#[test]
#[cfg(feature = "feat_selinux")]
#[cfg(all(
feature = "feat_selinux",
any(target_os = "linux", target_os = "android")
))]
fn test_cp_selinux_invalid() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
Expand Down Expand Up @@ -7871,7 +7877,10 @@ fn test_cp_a_preserves_context() {
}

#[test]
#[cfg(feature = "feat_selinux")]
#[cfg(all(
feature = "feat_selinux",
any(target_os = "linux", target_os = "android")
))]
fn test_cp_preserve_context_with_z_fails() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("src");
Expand Down
Loading