From f9abd998d6a5368c54162deb0bf187e94e31dc27 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 5 Nov 2013 15:48:27 -0800 Subject: Add bindings to uv's utime function This exposes the ability to change the modification and access times on a file. Closes #10266 --- src/libstd/rt/io/fs.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/libstd/rt/io/mod.rs | 5 +++-- src/libstd/rt/rtio.rs | 2 ++ 3 files changed, 47 insertions(+), 4 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/rt/io/fs.rs b/src/libstd/rt/io/fs.rs index 22d7ea55f3b..f9e622b1f1e 100644 --- a/src/libstd/rt/io/fs.rs +++ b/src/libstd/rt/io/fs.rs @@ -587,6 +587,21 @@ pub fn rmdir_recursive(path: &Path) { rmdir(path); } +/// Changes the timestamps for a file's last modification and access time. +/// The file at the path specified will have its last access time set to +/// `atime` and its modification time set to `mtime`. +/// +/// # Errors +/// +/// This function will raise on the `io_error` condition if an error +/// happens. +// FIXME(#10301) these arguments should not be u64 +pub fn change_file_times(path: &Path, atime: u64, mtime: u64) { + do io_raise |io| { + io.fs_utime(&path.to_c_str(), atime, mtime) + }; +} + impl Reader for File { fn read(&mut self, buf: &mut [u8]) -> Option { match self.fd.read(buf) { @@ -704,8 +719,8 @@ mod test { use rt::io; use str; use super::{File, rmdir, mkdir, readdir, rmdir_recursive, mkdir_recursive, - copy, unlink, stat, symlink, link, readlink, chmod, chown, - lstat}; + copy, unlink, stat, symlink, link, readlink, chmod, + lstat, change_file_times}; fn tmpdir() -> Path { use os; @@ -1244,4 +1259,29 @@ mod test { rmdir_recursive(&tmpdir); } + + #[test] + fn utime() { + let tmpdir = tmpdir(); + let path = tmpdir.join("a"); + File::create(&path); + + change_file_times(&path, 100, 200); + assert_eq!(path.stat().accessed, 100); + assert_eq!(path.stat().modified, 200); + + rmdir_recursive(&tmpdir); + } + + #[test] + fn utime_noexist() { + let tmpdir = tmpdir(); + + match io::result(|| change_file_times(&tmpdir.join("a"), 100, 200)) { + Ok(*) => fail!(), + Err(*) => {} + } + + rmdir_recursive(&tmpdir); + } } diff --git a/src/libstd/rt/io/mod.rs b/src/libstd/rt/io/mod.rs index f01ce5012eb..e8ab4670233 100644 --- a/src/libstd/rt/io/mod.rs +++ b/src/libstd/rt/io/mod.rs @@ -1142,8 +1142,9 @@ pub struct FileStat { /// The file permissions currently on the file perm: FilePermission, - // XXX: These time fields are pretty useless without an actual time - // representation, what are the milliseconds relative to? + // FIXME(#10301): These time fields are pretty useless without an actual + // time representation, what are the milliseconds relative + // to? /// The time that the file was created at, in platform-dependent /// milliseconds diff --git a/src/libstd/rt/rtio.rs b/src/libstd/rt/rtio.rs index f8b87abb9f6..96ba5123456 100644 --- a/src/libstd/rt/rtio.rs +++ b/src/libstd/rt/rtio.rs @@ -125,6 +125,8 @@ pub trait IoFactory { fn fs_readlink(&mut self, path: &CString) -> Result; fn fs_symlink(&mut self, src: &CString, dst: &CString) -> Result<(), IoError>; fn fs_link(&mut self, src: &CString, dst: &CString) -> Result<(), IoError>; + fn fs_utime(&mut self, src: &CString, atime: u64, mtime: u64) -> + Result<(), IoError>; // misc fn timer_init(&mut self) -> Result<~RtioTimer, IoError>; -- cgit 1.4.1-3-g733a5