Debian Codegraph @

debian/rust-vendor/filetime/src/unix/linux.rs

raw
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
5 use crate::FileTime;
6 use std::ffi::CString;
7 use std::io;
8 use std::os::unix::prelude::*;
9 use std::path::Path;
10 use std::sync::atomic::AtomicBool;
11 use std::sync::atomic::Ordering::SeqCst;
12
13 pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
14 set_times(p, Some(atime), Some(mtime), true)
15 }
16
17 fn set_times(
18 p: &Path,
19 atime: Option<FileTime>,
20 mtime: Option<FileTime>,
21 symlink: bool,
22 ) -> io::Result<()> {
23 let flags = if symlink {
24 libc::AT_SYMLINK_NOFOLLOW
25 } else {
26 0
27 };
28
29 // Same as the `if` statement above.
30 static INVALID: AtomicBool = AtomicBool::new(false);
31 if !INVALID.load(SeqCst) {
32 let p = CString::new(p.as_os_str().as_bytes())?;
33 let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
34 let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) };
35 if rc == 0 {
36 return Ok(());
37 }
38 let err = io::Error::last_os_error();
39 if err.raw_os_error() == Some(libc::ENOSYS) {
40 INVALID.store(true, SeqCst);
41 } else {
42 return Err(err);
43 }
44 }
45
46 super::utimes::set_times(p, atime, mtime, symlink)
47 }