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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
extern crate libc;
extern crate winapi;
extern crate kernel32;
#[cfg(unix)] use std::os::unix::prelude::*;
#[cfg(windows)] use std::os::windows::prelude::*;
use std::fmt;
use std::fs;
use std::io;
use std::path::Path;
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
pub struct FileTime {
seconds: u64,
nanos: u32,
}
impl FileTime {
pub fn zero() -> FileTime {
FileTime { seconds: 0, nanos: 0 }
}
pub fn from_seconds_since_1970(seconds: u64, nanos: u32) -> FileTime {
FileTime {
seconds: seconds + if cfg!(windows) {11644473600} else {0},
nanos: nanos,
}
}
pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime {
#[cfg(unix)]
fn imp(meta: &fs::Metadata) -> FileTime {
FileTime::from_os_repr(meta.mtime() as u64, meta.mtime_nsec() as u32)
}
#[cfg(windows)]
fn imp(meta: &fs::Metadata) -> FileTime {
FileTime::from_os_repr(meta.last_write_time())
}
imp(meta)
}
pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime {
#[cfg(unix)]
fn imp(meta: &fs::Metadata) -> FileTime {
FileTime::from_os_repr(meta.atime() as u64, meta.atime_nsec() as u32)
}
#[cfg(windows)]
fn imp(meta: &fs::Metadata) -> FileTime {
FileTime::from_os_repr(meta.last_access_time())
}
imp(meta)
}
pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> {
macro_rules! birthtim {
($(($e:expr, $i:ident)),*) => {
#[cfg(any($(target_os = $e),*))]
fn imp(meta: &fs::Metadata) -> Option<FileTime> {
$(
#[cfg(target_os = $e)]
use std::os::$i::fs::MetadataExt;
)*
let raw = meta.as_raw_stat();
Some(FileTime::from_os_repr(raw.st_birthtime as u64,
raw.st_birthtime_nsec as u32))
}
#[cfg(all(not(windows),
$(not(target_os = $e)),*))]
fn imp(_meta: &fs::Metadata) -> Option<FileTime> {
None
}
}
}
birthtim! {
("bitrig", bitrig),
("freebsd", freebsd),
("ios", ios),
("macos", macos),
("openbsd", openbsd)
}
#[cfg(windows)]
fn imp(meta: &fs::Metadata) -> Option<FileTime> {
Some(FileTime::from_os_repr(meta.last_access_time()))
}
imp(meta)
}
#[cfg(windows)]
fn from_os_repr(time: u64) -> FileTime {
FileTime {
seconds: time / (1_000_000_000 / 100),
nanos: ((time % (1_000_000_000 / 100)) * 100) as u32,
}
}
#[cfg(unix)]
fn from_os_repr(seconds: u64, nanos: u32) -> FileTime {
FileTime { seconds: seconds, nanos: nanos }
}
pub fn seconds(&self) -> u64 { self.seconds }
pub fn seconds_relative_to_1970(&self) -> u64 {
self.seconds - if cfg!(windows) {11644473600} else {0}
}
pub fn nanoseconds(&self) -> u32 { self.nanos }
}
impl fmt::Display for FileTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{:09}s", self.seconds, self.nanos)
}
}
pub fn set_file_times<P>(p: P, atime: FileTime, mtime: FileTime)
-> io::Result<()> where P: AsRef<Path> {
set_file_times_(p.as_ref(), atime, mtime)
}
#[cfg(unix)]
fn set_file_times_(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
use std::ffi::CString;
use libc::{timeval, time_t, c_char, c_int, suseconds_t};
extern {
fn utimes(name: *const c_char, times: *const timeval) -> c_int;
}
let times = [to_timeval(&atime), to_timeval(&mtime)];
let p = try!(CString::new(p.as_os_str().as_bytes()));
return unsafe {
if utimes(p.as_ptr(), times.as_ptr()) == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
};
fn to_timeval(ft: &FileTime) -> timeval {
timeval {
tv_sec: ft.seconds() as time_t,
tv_usec: (ft.nanoseconds() / 1000) as suseconds_t,
}
}
}
#[cfg(windows)]
fn set_file_times_(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
use std::fs::OpenOptions;
use winapi::{FILETIME, DWORD};
let f = try!(OpenOptions::new().write(true).open(p));
let atime = to_filetime(&atime);
let mtime = to_filetime(&mtime);
return unsafe {
let ret = kernel32::SetFileTime(f.as_raw_handle() as *mut _,
0 as *const _,
&atime, &mtime);
if ret != 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
};
fn to_filetime(ft: &FileTime) -> FILETIME {
let intervals = ft.seconds() * (1_000_000_000 / 100) +
((ft.nanoseconds() as u64) / 100);
FILETIME {
dwLowDateTime: intervals as DWORD,
dwHighDateTime: (intervals >> 32) as DWORD,
}
}
}
#[cfg(test)]
mod tests {
extern crate tempdir;
use std::fs::{self, File};
use self::tempdir::TempDir;
use super::{FileTime, set_file_times};
#[test]
fn set_file_times_test() {
let td = TempDir::new("filetime").unwrap();
let path = td.path().join("foo.txt");
File::create(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_seconds_since_1970(10_000, 0);
set_file_times(&path, atime, new_mtime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
}
}