Skip to content

Commit

Permalink
Add bindings to uv's utime function
Browse files Browse the repository at this point in the history
This exposes the ability to change the modification and access times on a file.

Closes rust-lang#10266
  • Loading branch information
alexcrichton committed Nov 7, 2013
1 parent d941390 commit 57e4717
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/librustuv/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ impl FsRequest {
})
}

pub fn utime(loop_: &Loop, path: &CString, atime: u64, mtime: u64)
-> Result<(), UvError>
{
execute_nop(|req, cb| unsafe {
uvll::uv_fs_utime(loop_.handle, req, path.with_ref(|p| p),
atime as libc::c_double, mtime as libc::c_double,
cb)
})
}

pub fn get_result(&self) -> c_int {
unsafe { uvll::get_result_from_fs_req(self.req) }
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustuv/uvio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ impl IoFactory for UvIoFactory {
let r = FsRequest::readlink(self.uv_loop(), path);
r.map_err(uv_error_to_io_error)
}
fn fs_utime(&mut self, path: &CString, atime: u64, mtime: u64)
-> Result<(), IoError>
{
let r = FsRequest::utime(self.uv_loop(), path, atime, mtime);
r.map_err(uv_error_to_io_error)
}

fn spawn(&mut self, config: ProcessConfig)
-> Result<(~RtioProcess, ~[Option<~RtioPipe>]), IoError>
Expand Down
5 changes: 4 additions & 1 deletion src/librustuv/uvll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#[allow(non_camel_case_types)]; // C types

use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t};
use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t, c_double};
use std::libc::ssize_t;
use std::libc::{malloc, free};
use std::libc;
Expand Down Expand Up @@ -824,6 +824,9 @@ externfn!(fn uv_fs_symlink(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
dst: *c_char, flags: c_int, cb: uv_fs_cb) -> c_int)
externfn!(fn uv_fs_rename(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
dst: *c_char, cb: uv_fs_cb) -> c_int)
externfn!(fn uv_fs_utime(handle: *uv_loop_t, req: *uv_fs_t, path: *c_char,
atime: c_double, mtime: c_double,
cb: uv_fs_cb) -> c_int)
externfn!(fn uv_fs_link(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
dst: *c_char, cb: uv_fs_cb) -> c_int)
externfn!(fn uv_fs_chown(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
Expand Down
44 changes: 42 additions & 2 deletions src/libstd/rt/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint> {
match self.fd.read(buf) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions src/libstd/rt/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/rt/rtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub trait IoFactory {
fn fs_readlink(&mut self, path: &CString) -> Result<Path, IoError>;
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>;
Expand Down

0 comments on commit 57e4717

Please sign in to comment.