mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
set_selinux_security_context: also display the error from the crate
+ fix comments from review
This commit is contained in:
parent
b3a2b74ca1
commit
5148ba12d6
3 changed files with 14 additions and 20 deletions
|
@ -436,7 +436,6 @@ pub(crate) fn copy_directory(
|
||||||
&entry.source_absolute,
|
&entry.source_absolute,
|
||||||
&entry.local_to_target,
|
&entry.local_to_target,
|
||||||
&options.attributes,
|
&options.attributes,
|
||||||
options,
|
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -467,7 +466,6 @@ pub(crate) fn copy_directory(
|
||||||
&entry.source_absolute,
|
&entry.source_absolute,
|
||||||
&entry.local_to_target,
|
&entry.local_to_target,
|
||||||
&options.attributes,
|
&options.attributes,
|
||||||
options,
|
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -478,7 +476,7 @@ pub(crate) fn copy_directory(
|
||||||
let dest = target.join(root.file_name().unwrap());
|
let dest = target.join(root.file_name().unwrap());
|
||||||
for (x, y) in aligned_ancestors(root, dest.as_path()) {
|
for (x, y) in aligned_ancestors(root, dest.as_path()) {
|
||||||
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
|
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
|
||||||
copy_attributes(&src, y, &options.attributes, options)?;
|
copy_attributes(&src, y, &options.attributes)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1492,7 +1492,7 @@ fn copy_source(
|
||||||
if options.parents {
|
if options.parents {
|
||||||
for (x, y) in aligned_ancestors(source, dest.as_path()) {
|
for (x, y) in aligned_ancestors(source, dest.as_path()) {
|
||||||
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
|
if let Ok(src) = canonicalize(x, MissingHandling::Normal, ResolveMode::Physical) {
|
||||||
copy_attributes(&src, y, &options.attributes, options)?;
|
copy_attributes(&src, y, &options.attributes)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1640,12 +1640,10 @@ fn copy_extended_attrs(source: &Path, dest: &Path) -> CopyResult<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy the specified attributes from one path to another.
|
/// Copy the specified attributes from one path to another.
|
||||||
#[allow(unused_variables)]
|
|
||||||
pub(crate) fn copy_attributes(
|
pub(crate) fn copy_attributes(
|
||||||
source: &Path,
|
source: &Path,
|
||||||
dest: &Path,
|
dest: &Path,
|
||||||
attributes: &Attributes,
|
attributes: &Attributes,
|
||||||
options: &Options,
|
|
||||||
) -> CopyResult<()> {
|
) -> CopyResult<()> {
|
||||||
let context = &*format!("{} -> {}", source.quote(), dest.quote());
|
let context = &*format!("{} -> {}", source.quote(), dest.quote());
|
||||||
let source_metadata = fs::symlink_metadata(source).context(context)?;
|
let source_metadata = fs::symlink_metadata(source).context(context)?;
|
||||||
|
@ -2442,7 +2440,7 @@ fn copy_file(
|
||||||
if options.dereference(source_in_command_line) {
|
if options.dereference(source_in_command_line) {
|
||||||
if let Ok(src) = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical) {
|
if let Ok(src) = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical) {
|
||||||
if src.exists() {
|
if src.exists() {
|
||||||
copy_attributes(&src, dest, &options.attributes, options)?;
|
copy_attributes(&src, dest, &options.attributes)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if source_is_stream && source.exists() {
|
} else if source_is_stream && source.exists() {
|
||||||
|
@ -2450,15 +2448,18 @@ fn copy_file(
|
||||||
// like anonymous pipes. Thus, we can't really copy its
|
// like anonymous pipes. Thus, we can't really copy its
|
||||||
// attributes. However, this is already handled in the stream
|
// attributes. However, this is already handled in the stream
|
||||||
// copy function (see `copy_stream` under platform/linux.rs).
|
// copy function (see `copy_stream` under platform/linux.rs).
|
||||||
copy_attributes(source, dest, &options.attributes, options)?;
|
|
||||||
} else {
|
} else {
|
||||||
copy_attributes(source, dest, &options.attributes, options)?;
|
copy_attributes(source, dest, &options.attributes)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "selinux")]
|
#[cfg(feature = "selinux")]
|
||||||
if options.set_selinux_context && uucore::selinux::is_selinux_enabled() {
|
if options.set_selinux_context && uucore::selinux::is_selinux_enabled() {
|
||||||
// Set the given selinux permissions on the copied file.
|
// Set the given selinux permissions on the copied file.
|
||||||
uucore::selinux::set_selinux_security_context(dest, options.context.as_ref())?;
|
if let Err(e) =
|
||||||
|
uucore::selinux::set_selinux_security_context(dest, options.context.as_ref())
|
||||||
|
{
|
||||||
|
return Err(Error::Error(format!("SELinux error: {}", e)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
copied_files.insert(
|
copied_files.insert(
|
||||||
|
|
|
@ -6292,9 +6292,7 @@ fn test_cp_selinux() {
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
selinux_perm.contains("unconfined_u"),
|
selinux_perm.contains("unconfined_u"),
|
||||||
"Expected '{}' not found in getfattr output:\n{}",
|
"Expected 'foo' not found in getfattr output:\n{selinux_perm}"
|
||||||
"foo",
|
|
||||||
selinux_perm
|
|
||||||
);
|
);
|
||||||
at.remove(&at.plus_as_string(TEST_HELLO_WORLD_DEST));
|
at.remove(&at.plus_as_string(TEST_HELLO_WORLD_DEST));
|
||||||
}
|
}
|
||||||
|
@ -6342,9 +6340,7 @@ fn test_cp_preserve_selinux() {
|
||||||
let selinux_perm_dest = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_DEST));
|
let selinux_perm_dest = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_DEST));
|
||||||
assert!(
|
assert!(
|
||||||
selinux_perm_dest.contains("unconfined_u"),
|
selinux_perm_dest.contains("unconfined_u"),
|
||||||
"Expected '{}' not found in getfattr output:\n{}",
|
"Expected 'foo' not found in getfattr output:\n{selinux_perm_dest}"
|
||||||
"foo",
|
|
||||||
selinux_perm_dest
|
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_SOURCE)),
|
get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_SOURCE)),
|
||||||
|
@ -6373,8 +6369,8 @@ fn test_cp_preserve_selinux_admin_context() {
|
||||||
at.touch(TEST_HELLO_WORLD_SOURCE);
|
at.touch(TEST_HELLO_WORLD_SOURCE);
|
||||||
|
|
||||||
// Get the default SELinux context for the destination file path
|
// Get the default SELinux context for the destination file path
|
||||||
// on Debian/Ubuntu, this program is provided by the selinux-utils package
|
// On Debian/Ubuntu, this program is provided by the selinux-utils package
|
||||||
// on Fedora/RHEL, this program is provided by the libselinux-devel package
|
// On Fedora/RHEL, this program is provided by the libselinux-devel package
|
||||||
let output = std::process::Command::new("matchpathcon")
|
let output = std::process::Command::new("matchpathcon")
|
||||||
.arg(at.plus_as_string(TEST_HELLO_WORLD_DEST))
|
.arg(at.plus_as_string(TEST_HELLO_WORLD_DEST))
|
||||||
.output()
|
.output()
|
||||||
|
@ -6432,7 +6428,6 @@ fn test_cp_selinux_context_priority() {
|
||||||
let ts = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = &ts.fixtures;
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
// Create two different files
|
|
||||||
at.write(TEST_HELLO_WORLD_SOURCE, "source content");
|
at.write(TEST_HELLO_WORLD_SOURCE, "source content");
|
||||||
|
|
||||||
// First, set a known context on source file (only if system supports it)
|
// First, set a known context on source file (only if system supports it)
|
||||||
|
@ -6642,7 +6637,7 @@ fn test_cp_preserve_context_root() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the file with preserved context
|
// Copy the file with preserved context
|
||||||
// Only works at root
|
// Only works as root
|
||||||
if let Ok(result) = run_ucmd_as_root(&scene, &["--preserve=context", source_file, dest_file]) {
|
if let Ok(result) = run_ucmd_as_root(&scene, &["--preserve=context", source_file, dest_file]) {
|
||||||
let src_ctx = get_getfattr_output(&at.plus_as_string(source_file));
|
let src_ctx = get_getfattr_output(&at.plus_as_string(source_file));
|
||||||
let dest_ctx = get_getfattr_output(&at.plus_as_string(dest_file));
|
let dest_ctx = get_getfattr_output(&at.plus_as_string(dest_file));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue