filetime/unix/
linux.rs

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.
4
5use 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;
14
15pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
16    set_times(p, Some(atime), Some(mtime), false)
17}
18
19pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> {
20    set_times(p, None, Some(mtime), false)
21}
22
23pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> {
24    set_times(p, Some(atime), None, false)
25}
26
27pub 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.
34    static INVALID: AtomicBool = AtomicBool::new(false);
35    if !INVALID.load(SeqCst) {
36        let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
37        let rc = unsafe {
38            libc::syscall(
39                libc::SYS_utimensat,
40                f.as_raw_fd(),
41                ptr::null::<libc::c_char>(),
42                times.as_ptr(),
43                0,
44            )
45        };
46        if rc == 0 {
47            return Ok(());
48        }
49        let err = io::Error::last_os_error();
50        if err.raw_os_error() == Some(libc::ENOSYS) {
51            INVALID.store(true, SeqCst);
52        } else {
53            return Err(err);
54        }
55    }
56
57    super::utimes::set_file_handle_times(f, atime, mtime)
58}
59
60pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
61    set_times(p, Some(atime), Some(mtime), true)
62}
63
64fn set_times(
65    p: &Path,
66    atime: Option<FileTime>,
67    mtime: Option<FileTime>,
68    symlink: bool,
69) -> io::Result<()> {
70    let flags = if symlink {
71        libc::AT_SYMLINK_NOFOLLOW
72    } else {
73        0
74    };
75
76    // Same as the `if` statement above.
77    static INVALID: AtomicBool = AtomicBool::new(false);
78    if !INVALID.load(SeqCst) {
79        let p = CString::new(p.as_os_str().as_bytes())?;
80        let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
81        let rc = unsafe {
82            libc::syscall(
83                libc::SYS_utimensat,
84                libc::AT_FDCWD,
85                p.as_ptr(),
86                times.as_ptr(),
87                flags,
88            )
89        };
90        if rc == 0 {
91            return Ok(());
92        }
93        let err = io::Error::last_os_error();
94        if err.raw_os_error() == Some(libc::ENOSYS) {
95            INVALID.store(true, SeqCst);
96        } else {
97            return Err(err);
98        }
99    }
100
101    super::utimes::set_times(p, atime, mtime, symlink)
102}