From 38dd8c58366ad2c68e7f8e09b377031fa72c5b9a Mon Sep 17 00:00:00 2001 From: Matt8898 Date: Tue, 15 Aug 2017 11:42:50 +0200 Subject: [PATCH] cp: use filetime to set timestamps. --- src/cp/Cargo.toml | 1 + src/cp/cp.rs | 17 ++++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/cp/Cargo.toml b/src/cp/Cargo.toml index e12413b66..38ca8b40c 100644 --- a/src/cp/Cargo.toml +++ b/src/cp/Cargo.toml @@ -17,6 +17,7 @@ walkdir = "1.0.7" clap = "2.20.0" quick-error = "1.1.0" uucore = { path="../uucore" } +filetime = "0.1" [target.'cfg(target_os = "linux")'.dependencies] ioctl-sys = "0.5.2" diff --git a/src/cp/cp.rs b/src/cp/cp.rs index dfef452a2..9b188750a 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -13,6 +13,8 @@ extern crate libc; extern crate clap; extern crate walkdir; +extern crate filetime; +use filetime::FileTime; #[cfg(target_os = "linux")] #[macro_use] extern crate ioctl_sys; #[macro_use] extern crate uucore; @@ -790,19 +792,8 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu fs::set_permissions(dest, metadata.permissions()).context(context)?; }, Attribute::Timestamps => { - let meta = fs::metadata(source)?; - let modified = meta.modified()?; - let accessed = meta.accessed()?; - let src_path = CString::new(source.as_os_str().to_str().unwrap()).unwrap(); - let dest_path = CString::new(dest.as_os_str().to_str().unwrap()).unwrap(); - unsafe { - let mut stat: libc::stat = mem::zeroed(); - libc::stat(src_path.as_ptr(), &mut stat); - libc::utime(dest_path.as_ptr(), &libc::utimbuf{ - actime: stat.st_atime, - modtime: stat.st_mtime - }); - } + let metadata = fs::metadata(source)?; + filetime::set_file_times(Path::new(dest), FileTime::from_last_access_time(&metadata), FileTime::from_last_modification_time(&metadata))?; }, Attribute::Context => return Err(Error::NotImplemented("preserving context not implemented".to_string())), Attribute::Links => return Err(Error::NotImplemented("preserving links not implemented".to_string())),