From 4cb727f80b70936f52ad91a8158bad091578b630 Mon Sep 17 00:00:00 2001 From: Matt8898 Date: Fri, 18 Aug 2017 10:44:54 +0200 Subject: [PATCH] cp: Add support for preserving xattrs. --- src/cp/Cargo.toml | 3 +++ src/cp/cp.rs | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cp/Cargo.toml b/src/cp/Cargo.toml index 38ca8b40c..30ffda4b6 100644 --- a/src/cp/Cargo.toml +++ b/src/cp/Cargo.toml @@ -22,6 +22,9 @@ filetime = "0.1" [target.'cfg(target_os = "linux")'.dependencies] ioctl-sys = "0.5.2" +[target.'cfg(unix)'.dependencies] +xattr="0.2.1" + [[bin]] name = "cp" path = "main.rs" diff --git a/src/cp/cp.rs b/src/cp/cp.rs index b924b2604..6a8ce8b17 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -19,6 +19,8 @@ use filetime::FileTime; #[macro_use] extern crate ioctl_sys; #[macro_use] extern crate uucore; #[macro_use] extern crate quick_error; +#[cfg(unix)] +extern crate xattr; use std::mem; use std::ffi::CString; @@ -796,7 +798,21 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu }, Attribute::Context => return Err(Error::NotImplemented("preserving context not implemented".to_string())), Attribute::Links => return Err(Error::NotImplemented("preserving links not implemented".to_string())), - Attribute::Xattr => return Err(Error::NotImplemented("preserving xattr not implemented".to_string())), + Attribute::Xattr => { + #[cfg(unix)] + { + let xattrs = xattr::list(source)?; + for attr in xattrs { + if let Some(attr_value) = xattr::get(source, attr.clone())? { + xattr::set(dest, attr, &attr_value[..]); + } + } + } + #[cfg(not(unix))] + { + return Err(format!("XAttrs are only supported on unix.").into()); + } + }, Attribute::All => return Err(Error::NotImplemented("preserving a not implemented".to_string())), }) }