1//! On Linux we try to use the more accurate `utimensat` syscall but this isn't
2//! always available so we also fall back to `utimes` if we couldn't find
3//! `utimensat` at runtime.
45use crate::FileTime;
6use std::ffi::CString;
7use std::fs;
8use std::io;
9use std::os::unix::prelude::*;
10use std::path::Path;
11use std::ptr;
12use std::sync::atomic::AtomicBool;
13use std::sync::atomic::Ordering::SeqCst;
1415pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
16 set_times(p, Some(atime), Some(mtime), false)
17}
1819pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> {
20 set_times(p, None, Some(mtime), false)
21}
2223pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> {
24 set_times(p, Some(atime), None, false)
25}
2627pub fn set_file_handle_times(
28 f: &fs::File,
29 atime: Option<FileTime>,
30 mtime: Option<FileTime>,
31) -> io::Result<()> {
32// Attempt to use the `utimensat` syscall, but if it's not supported by the
33 // current kernel then fall back to an older syscall.
34static INVALID: AtomicBool = AtomicBool::new(false);
35if !INVALID.load(SeqCst) {
36let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
3738// We normally use a syscall because the `utimensat` function is documented
39 // as not accepting a file descriptor in the first argument (even though, on
40 // Linux, the syscall itself can accept a file descriptor there).
41#[cfg(not(target_env = "musl"))]
42let rc = unsafe {
43 libc::syscall(
44 libc::SYS_utimensat,
45 f.as_raw_fd(),
46 ptr::null::<libc::c_char>(),
47 times.as_ptr(),
480,
49 )
50 };
51// However, on musl, we call the musl libc function instead. This is because
52 // on newer musl versions starting with musl 1.2, `timespec` is always a 64-bit
53 // value even on 32-bit targets. As a result, musl internally converts their
54 // `timespec` values to the correct ABI before invoking the syscall. Since we
55 // use `timespec` from the libc crate, it matches musl's definition and not
56 // the Linux kernel's version (for some platforms) so we must use musl's
57 // `utimensat` function to properly convert the value. musl's `utimensat`
58 // function allows file descriptors in the path argument so this is fine.
59#[cfg(target_env = "musl")]
60let rc = unsafe {
61 libc::utimensat(
62 f.as_raw_fd(),
63 ptr::null::<libc::c_char>(),
64 times.as_ptr(),
650,
66 )
67 };
6869if rc == 0 {
70return Ok(());
71 }
72let err = io::Error::last_os_error();
73if err.raw_os_error() == Some(libc::ENOSYS) {
74 INVALID.store(true, SeqCst);
75 } else {
76return Err(err);
77 }
78 }
7980super::utimes::set_file_handle_times(f, atime, mtime)
81}
8283pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
84 set_times(p, Some(atime), Some(mtime), true)
85}
8687fn set_times(
88 p: &Path,
89 atime: Option<FileTime>,
90 mtime: Option<FileTime>,
91 symlink: bool,
92) -> io::Result<()> {
93let flags = if symlink {
94 libc::AT_SYMLINK_NOFOLLOW
95 } else {
960
97};
9899// Same as the `if` statement above.
100static INVALID: AtomicBool = AtomicBool::new(false);
101if !INVALID.load(SeqCst) {
102let p = CString::new(p.as_os_str().as_bytes())?;
103let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
104let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) };
105if rc == 0 {
106return Ok(());
107 }
108let err = io::Error::last_os_error();
109if err.raw_os_error() == Some(libc::ENOSYS) {
110 INVALID.store(true, SeqCst);
111 } else {
112return Err(err);
113 }
114 }
115116super::utimes::set_times(p, atime, mtime, symlink)
117}