Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve times for fs::copy on Unix. #32067

Closed
wants to merge 2 commits into from
Closed
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
182 changes: 175 additions & 7 deletions src/libstd/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use path::{Path, PathBuf};
use ptr;
use sync::Arc;
use sys::fd::FileDesc;
use sys::time::SystemTime;
use sys::time::{self, SystemTime};
use sys::{cvt, cvt_r};
use sys_common::{AsInner, FromInner};

Expand Down Expand Up @@ -498,6 +498,65 @@ impl File {
Ok(n as u64)
}

pub fn set_times(&self,
accessed: SystemTime,
modified: SystemTime,
created: Option<SystemTime>) -> io::Result<()> {
if let Some(birthtime) = created {
// On some (and maybe all) versions of BSD the birth time can be set
// thanks to the guarantee that the birth time is always equal to or
// earlier then the last modified time.
// So the birth time can be set by setting the times twice:
// once with times[1] set to the birth time, and then with times[1]
// set to the modified time.
try!(self.set_times_inner(accessed, birthtime));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is there precedent for setting the creation time when copying files?

}
self.set_times_inner(accessed, modified)
}

#[cfg(any(target_os = "linux",
target_os = "openbsd",
target_os = "netbsd",
target_os = "bitrig"))]
fn set_times_inner(&self, accessed: SystemTime, modified: SystemTime)
-> io::Result<()> {
let atime = accessed.sub_time(&time::UNIX_EPOCH).unwrap();
let mtime = modified.sub_time(&time::UNIX_EPOCH).unwrap();
let times = [
libc::timespec {
tv_sec: atime.as_secs() as libc::time_t,
tv_nsec: atime.subsec_nanos() as libc::c_long,
},
libc::timespec {
tv_sec: mtime.as_secs() as libc::time_t,
tv_nsec: mtime.subsec_nanos() as libc::c_long,
}];
try!(cvt(unsafe { libc::futimens(self.0.raw(), times.as_ptr()) }));
Ok(())
}

#[cfg(not(any(target_os = "linux",
target_os = "openbsd",
target_os = "netbsd",
target_os = "bitrig")))]
// for all os'es that do not yet support the more accurate `futimens`
fn set_times_inner(&self, accessed: SystemTime, modified: SystemTime)
-> io::Result<()> {
let atime = accessed.sub_time(&time::UNIX_EPOCH).unwrap();
let mtime = modified.sub_time(&time::UNIX_EPOCH).unwrap();
let times = [
libc::timeval {
tv_sec: atime.as_secs() as libc::time_t,
tv_usec: (atime.subsec_nanos() / 1000) as libc::suseconds_t,
},
libc::timeval {
tv_sec: mtime.as_secs() as libc::time_t,
tv_usec: (mtime.subsec_nanos() / 1000) as libc::suseconds_t,
}];
try!(cvt(unsafe { libc::futimes(self.0.raw(), times.as_ptr()) }));
Ok(())
}

pub fn duplicate(&self) -> io::Result<File> {
self.0.duplicate().map(File)
}
Expand Down Expand Up @@ -732,17 +791,126 @@ pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
}

pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
use fs::{File, set_permissions};
if !from.is_file() {
use fs::OpenOptions;
let mut from_opts = OpenOptions::new();
let mut reader = try!(from_opts.read(true).open(&from));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the same as File::open?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I don't remember why I changed that...
If I don't find the reason I will change it back.

let metadata = try!(reader.metadata());
let attr = metadata.as_inner();
if attr.file_type().is_file() == false {
return Err(Error::new(ErrorKind::InvalidInput,
"the source path is not an existing regular file"))
}

let mut reader = try!(File::open(from));
let mut writer = try!(File::create(to));
let perm = try!(reader.metadata()).permissions();
let mut to_opts = OpenOptions::new();
let mut writer = try!(to_opts.write(true)
.create(true)
.truncate(true)
.mode(attr.perm().mode())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a writable version of a read-only file? (I'm just not sure if that works here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the permissions do not apply for the fd that created it, only to new opens.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, cool! Could you add a comment to that effect?

.open(&to));

let ret = try!(io::copy(&mut reader, &mut writer));
try!(set_permissions(to, perm));
try!(writer.as_inner().set_times(try!(attr.accessed()),
try!(attr.modified()),
attr.created().ok()
));
Ok(ret)
}

#[cfg(test)]
mod tests {
use prelude::v1::*;
use env;
use fs;
use path::PathBuf;
use rand::{self, Rng};
use time::Duration;

macro_rules! check { ($e:expr) => (
match $e {
Ok(t) => t,
Err(e) => panic!("{} failed with: {}", stringify!($e), e),
}
) }

macro_rules! error { ($e:expr, $s:expr) => (
match $e {
Ok(_) => panic!("Unexpected success. Should've been: {:?}", $s),
Err(ref err) => assert!(err.to_string().contains($s),
format!("`{}` did not contain `{}`", err, $s))
}
) }

// from std::time, but with a resolution of 1 second.
macro_rules! assert_almost_eq {
($a:expr, $b:expr) => ({
let (a, b) = ($a, $b);
if a != b {
let (a, b) = if a > b {(a, b)} else {(b, a)};
assert!(a.sub_duration(&Duration::new(1, 0)) <= b);
}
})
}

pub struct TempDir(PathBuf);

impl TempDir {
fn join(&self, path: &str) -> PathBuf {
let TempDir(ref p) = *self;
p.join(path)
}
}

impl Drop for TempDir {
fn drop(&mut self) {
// Gee, seeing how we're testing the fs module I sure hope that we
// at least implement this correctly!
let TempDir(ref p) = *self;
check!(fs::remove_dir_all(p));
}
}

pub fn tmpdir() -> TempDir {
let p = env::temp_dir();
let mut r = rand::thread_rng();
let ret = p.join(&format!("rust-{}", r.next_u32()));
check!(fs::create_dir(&ret));
TempDir(ret)
}

#[test]
fn copy_preserves_times() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be a cross-platform test? Windows should also copy file times, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and yes :). But we do not yet have a way to set file times cross-platform. Would it be better if added a way to do so?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now we probably shouldn't add functionality like that, but it's fine if the tests reaches into the sys modules to call various bits and pieces.

// This test is in this module and a bit clumsy because `set_times` only
// works with `sys::SystemTime`.
// note: we can't check the access time, as it may be updated as soon as
// we try to check it
let tmpdir = tmpdir();
let orig = tmpdir.join("file");
let copy = tmpdir.join("copy");

let mtime = {
// create a file with times set to up to two minutes ago
// (otherwise the time of the copy may be almost the same)
let mut opts = super::OpenOptions::new();
opts.create(true);
opts.write(true);
let file = check!(super::File::open(&orig, &opts));
let metadata = check!(file.file_attr());
let accessed = check!(metadata.accessed())
.sub_duration(&Duration::new(30, 0));
let modified = check!(metadata.modified())
.sub_duration(&Duration::new(60, 0));
let created = metadata.created().ok()
.map(|t| t.sub_duration(&Duration::new(120, 0)));
check!(file.set_times(accessed, modified, created));
modified
};

check!(fs::copy(&orig, &copy));

let mut opts = super::OpenOptions::new();
opts.read(true);
let file = check!(super::File::open(&orig, &opts));
let metadata = check!(file.file_attr());
assert_almost_eq!(check!(metadata.modified()), mtime);
}
}
4 changes: 4 additions & 0 deletions src/test/run-pass/paths-containing-nul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ fn assert_invalid_input<T>(on: &str, result: io::Result<T>) {
}

fn main() {
fs::File::create("a");

assert_invalid_input("File::open", fs::File::open("\0"));
assert_invalid_input("File::create", fs::File::create("\0"));
assert_invalid_input("remove_file", fs::remove_file("\0"));
Expand All @@ -45,4 +47,6 @@ fn main() {
assert_invalid_input("read_dir", fs::read_dir("\0"));
assert_invalid_input("set_permissions",
fs::set_permissions("\0", fs::metadata(".").unwrap().permissions()));

fs::remove_file("a");
}