Skip to content

Commit ead5084

Browse files
ChrisDrydensylvestre
authored andcommitted
cp: move copy_xattrs_skip_selinux to uucore
1 parent 395dd81 commit ead5084

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/uu/cp/src/cp.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::os::unix::net::UnixListener;
1616
use std::path::{Path, PathBuf, StripPrefixError};
1717
use std::{fmt, io};
1818
#[cfg(all(unix, not(target_os = "android")))]
19-
use uucore::fsxattr::copy_xattrs;
19+
use uucore::fsxattr::{copy_xattrs, copy_xattrs_skip_selinux};
2020
use uucore::translate;
2121

2222
use clap::{Arg, ArgAction, ArgMatches, Command, builder::ValueParser, value_parser};
@@ -1748,21 +1748,6 @@ fn copy_extended_attrs(source: &Path, dest: &Path, skip_selinux: bool) -> CopyRe
17481748
Ok(())
17491749
}
17501750

1751-
/// Copy extended attributes but skip security.selinux
1752-
#[cfg(all(unix, not(target_os = "android")))]
1753-
fn copy_xattrs_skip_selinux(source: &Path, dest: &Path) -> std::io::Result<()> {
1754-
for attr_name in xattr::list(source)? {
1755-
// Skip security.selinux when -Z is used to set default context
1756-
if attr_name.to_string_lossy() == "security.selinux" {
1757-
continue;
1758-
}
1759-
if let Some(value) = xattr::get(source, &attr_name)? {
1760-
xattr::set(dest, &attr_name, &value)?;
1761-
}
1762-
}
1763-
Ok(())
1764-
}
1765-
17661751
/// Copy the specified attributes from one path to another.
17671752
/// If `skip_selinux_xattr` is true, the security.selinux xattr will not be copied
17681753
/// (used when -Z is specified to set the default context instead).

src/uucore/src/lib/features/fsxattr.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ pub fn copy_xattrs<P: AsRef<Path>>(source: P, dest: P) -> std::io::Result<()> {
3232
Ok(())
3333
}
3434

35+
/// Like `copy_xattrs`, but skips the security.selinux attribute.
36+
#[cfg(unix)]
37+
pub fn copy_xattrs_skip_selinux<P: AsRef<Path>>(source: P, dest: P) -> std::io::Result<()> {
38+
for attr_name in xattr::list(&source)? {
39+
if attr_name.to_string_lossy() != "security.selinux" {
40+
if let Some(value) = xattr::get(&source, &attr_name)? {
41+
xattr::set(&dest, &attr_name, &value)?;
42+
}
43+
}
44+
}
45+
Ok(())
46+
}
47+
3548
/// Retrieves the extended attributes (xattrs) of a given file or directory.
3649
///
3750
/// # Arguments

0 commit comments

Comments
 (0)