1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Missing utime function for Rust
//!
//! Standard library of Rust doesn't provide the way to set atime/mtime of a file. This crate
//! provides stable way to change a file's last modification and access time.

#[cfg(unix)]
extern crate libc;

use std::path::Path;
use std::io;

/// 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
/// `accessed` and its modification time set to `modified`. The times specified
/// should be in seconds.
pub fn set_file_times<P: AsRef<Path>>(path: P, accessed: u64, modified: u64) -> io::Result<()> {
    utime(path, accessed, modified)
}

#[cfg(unix)]
fn utime<P: AsRef<Path>>(path: P, atime: u64, mtime: u64) -> io::Result<()> {
    use std::os::unix::prelude::*;
    use std::ffi::CString;
    use libc::{timeval, time_t, c_char, c_int};
    extern {
        fn utimes(name: *const c_char, times: *const timeval) -> c_int;
    }

    let path = try!(CString::new(path.as_ref().as_os_str().as_bytes()));
    let atime = timeval { tv_sec: atime as time_t, tv_usec: 0, };
    let mtime = timeval { tv_sec: mtime as time_t, tv_usec: 0, };
    let times = [atime, mtime];
    let ret = unsafe { utimes(path.as_ptr(), times.as_ptr()) };

    if ret == 0 {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }
}

#[cfg(windows)]
fn utime<P: AsRef<Path>>(path: P, atime: u64, mtime: u64) -> io::Result<()> {
    use std::fs::OpenOptions;
    use std::os::windows::prelude::*;
    use winapi::{FILETIME, DWORD};
    use kernel32::SetFileTime;

    let f = try!(OpenOptions::new().write(true).open(p));
    let atime = to_filetime(atime);
    let mtime = to_filetime(mtime);
    let ret = unsafe { SetFileTime(f.as_raw_handle() as *mut _, 0 as *const _, &atime, &mtime) };

    if ret != 0 {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }

    // FILETIME is a count of 100ns intervals, and there are 10^7 of these in a second
    fn to_filetime(seconds: u64) -> FILETIME {
        let seconds = seconds * 10_000_000;
        FILETIME {
            dwLowDateTime: seconds as DWORD,
            dwHighDateTime: (seconds >> 32) as DWORD,
        }
    }
}