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
use crate::fs::SystemTimeSpec;
use crate::time::SystemClock;
use io_lifetimes::BorrowedFd;
use rustix::fs::{utimensat, AtFlags, Timestamps, UTIME_NOW, UTIME_OMIT};
use rustix::time::Timespec;
use std::convert::TryInto;
use std::path::Path;
use std::{fs, io};
#[allow(clippy::useless_conversion)]
fn to_timespec(ft: Option<SystemTimeSpec>) -> io::Result<Timespec> {
Ok(match ft {
None => Timespec {
tv_sec: 0,
tv_nsec: UTIME_OMIT.into(),
},
Some(SystemTimeSpec::SymbolicNow) => Timespec {
tv_sec: 0,
tv_nsec: UTIME_NOW.into(),
},
Some(SystemTimeSpec::Absolute(ft)) => {
let duration = ft.duration_since(SystemClock::UNIX_EPOCH).unwrap();
let nanoseconds = duration.subsec_nanos();
assert_ne!(i64::from(nanoseconds), i64::from(UTIME_OMIT));
assert_ne!(i64::from(nanoseconds), i64::from(UTIME_NOW));
Timespec {
tv_sec: duration
.as_secs()
.try_into()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
tv_nsec: nanoseconds.try_into().unwrap(),
}
}
})
}
pub(crate) fn set_times_nofollow_unchecked(
start: &fs::File,
path: &Path,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> io::Result<()> {
let times = Timestamps {
last_access: to_timespec(atime)?,
last_modification: to_timespec(mtime)?,
};
Ok(utimensat(start, path, ×, AtFlags::SYMLINK_NOFOLLOW)?)
}
#[allow(dead_code)]
pub(crate) fn set_times_follow_unchecked(
start: BorrowedFd<'_>,
path: &Path,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> io::Result<()> {
let times = Timestamps {
last_access: to_timespec(atime)?,
last_modification: to_timespec(mtime)?,
};
Ok(utimensat(&start, path, ×, AtFlags::empty())?)
}