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
use std::time::SystemTime;
#[cfg(target_vendor = "apple")]
use std::os::darwin::fs::FileTimesExt;
#[cfg(windows)]
use std::os::windows::fs::FileTimesExt;
/// Timestamps for a file.
///
/// This is a specialized version of [`std::fs::FileTimes`] for usage with
/// [`File::set_times`](crate::fs::File::set_times). Unlike the standard library version,
/// this type records the timestamps itself, so a future `io-uring` file
/// implementation can consume them without converting through the standard
/// library type.
///
/// # Examples
///
/// ```no_run
/// use tokio::fs::FileTimes;
///
/// # async fn dox() -> std::io::Result<()> {
/// let times = FileTimes::new()
/// .set_accessed(std::time::SystemTime::now())
/// .set_modified(std::time::SystemTime::now());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
#[must_use = "must be applied to a file via `File::set_times` to have any effect"]
pub struct FileTimes {
accessed: Option<SystemTime>,
modified: Option<SystemTime>,
#[cfg(any(windows, target_vendor = "apple"))]
created: Option<SystemTime>,
}
impl FileTimes {
/// Creates a new empty [`FileTimes`].
///
/// Unset timestamps are left unchanged.
pub fn new() -> Self {
Self::default()
}
/// Sets the last access time.
pub fn set_accessed(mut self, t: SystemTime) -> Self {
self.accessed = Some(t);
self
}
/// Sets the last modification time.
pub fn set_modified(mut self, t: SystemTime) -> Self {
self.modified = Some(t);
self
}
/// Sets the creation time.
#[cfg(any(all(doc, docsrs), windows, target_vendor = "apple"))]
#[cfg_attr(docsrs, doc(cfg(any(windows, target_vendor = "apple"))))]
pub fn set_created(mut self, t: SystemTime) -> Self {
self.created = Some(t);
self
}
pub(crate) fn into_std(self) -> std::fs::FileTimes {
let mut std_times = std::fs::FileTimes::new();
if let Some(accessed) = self.accessed {
std_times = std_times.set_accessed(accessed);
}
if let Some(modified) = self.modified {
std_times = std_times.set_modified(modified);
}
#[cfg(any(windows, target_vendor = "apple"))]
if let Some(created) = self.created {
std_times = std_times.set_created(created);
}
std_times
}
}