/rust/registry/src/index.crates.io-6f17d22bba15001f/filetime-0.2.25/src/unix/linux.rs
Line | Count | Source (jump to first uncovered line) |
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::fs; |
8 | | use std::io; |
9 | | use std::os::unix::prelude::*; |
10 | | use std::path::Path; |
11 | | use std::ptr; |
12 | | use std::sync::atomic::AtomicBool; |
13 | | use std::sync::atomic::Ordering::SeqCst; |
14 | | |
15 | 0 | pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { |
16 | 0 | set_times(p, Some(atime), Some(mtime), false) |
17 | 0 | } |
18 | | |
19 | 0 | pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { |
20 | 0 | set_times(p, None, Some(mtime), false) |
21 | 0 | } |
22 | | |
23 | 0 | pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { |
24 | 0 | set_times(p, Some(atime), None, false) |
25 | 0 | } |
26 | | |
27 | 1.60k | pub fn set_file_handle_times( |
28 | 1.60k | f: &fs::File, |
29 | 1.60k | atime: Option<FileTime>, |
30 | 1.60k | mtime: Option<FileTime>, |
31 | 1.60k | ) -> 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 | 1.60k | if !INVALID.load(SeqCst) { |
36 | 1.60k | let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; |
37 | 1.60k | |
38 | 1.60k | // We normally use a syscall because the `utimensat` function is documented |
39 | 1.60k | // as not accepting a file descriptor in the first argument (even though, on |
40 | 1.60k | // Linux, the syscall itself can accept a file descriptor there). |
41 | 1.60k | #[cfg(not(target_env = "musl"))] |
42 | 1.60k | let rc = unsafe { |
43 | 1.60k | libc::syscall( |
44 | 1.60k | libc::SYS_utimensat, |
45 | 1.60k | f.as_raw_fd(), |
46 | 1.60k | ptr::null::<libc::c_char>(), |
47 | 1.60k | times.as_ptr(), |
48 | 1.60k | 0, |
49 | 1.60k | ) |
50 | 1.60k | }; |
51 | 1.60k | // However, on musl, we call the musl libc function instead. This is because |
52 | 1.60k | // on newer musl versions starting with musl 1.2, `timespec` is always a 64-bit |
53 | 1.60k | // value even on 32-bit targets. As a result, musl internally converts their |
54 | 1.60k | // `timespec` values to the correct ABI before invoking the syscall. Since we |
55 | 1.60k | // use `timespec` from the libc crate, it matches musl's definition and not |
56 | 1.60k | // the Linux kernel's version (for some platforms) so we must use musl's |
57 | 1.60k | // `utimensat` function to properly convert the value. musl's `utimensat` |
58 | 1.60k | // function allows file descriptors in the path argument so this is fine. |
59 | 1.60k | #[cfg(target_env = "musl")] |
60 | 1.60k | let rc = unsafe { |
61 | 1.60k | libc::utimensat( |
62 | 1.60k | f.as_raw_fd(), |
63 | 1.60k | ptr::null::<libc::c_char>(), |
64 | 1.60k | times.as_ptr(), |
65 | 1.60k | 0, |
66 | 1.60k | ) |
67 | 1.60k | }; |
68 | 1.60k | |
69 | 1.60k | if rc == 0 { |
70 | 1.60k | return Ok(()); |
71 | 0 | } |
72 | 0 | let err = io::Error::last_os_error(); |
73 | 0 | if err.raw_os_error() == Some(libc::ENOSYS) { |
74 | 0 | INVALID.store(true, SeqCst); |
75 | 0 | } else { |
76 | 0 | return Err(err); |
77 | | } |
78 | 0 | } |
79 | | |
80 | 0 | super::utimes::set_file_handle_times(f, atime, mtime) |
81 | 1.60k | } |
82 | | |
83 | 375 | pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { |
84 | 375 | set_times(p, Some(atime), Some(mtime), true) |
85 | 375 | } |
86 | | |
87 | 375 | fn set_times( |
88 | 375 | p: &Path, |
89 | 375 | atime: Option<FileTime>, |
90 | 375 | mtime: Option<FileTime>, |
91 | 375 | symlink: bool, |
92 | 375 | ) -> io::Result<()> { |
93 | 375 | let flags = if symlink { |
94 | 375 | libc::AT_SYMLINK_NOFOLLOW |
95 | | } else { |
96 | 0 | 0 |
97 | | }; |
98 | | |
99 | | // Same as the `if` statement above. |
100 | | static INVALID: AtomicBool = AtomicBool::new(false); |
101 | 375 | if !INVALID.load(SeqCst) { |
102 | 375 | let p = CString::new(p.as_os_str().as_bytes())?; |
103 | 375 | let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; |
104 | 375 | let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) }; |
105 | 375 | if rc == 0 { |
106 | 375 | return Ok(()); |
107 | 0 | } |
108 | 0 | let err = io::Error::last_os_error(); |
109 | 0 | if err.raw_os_error() == Some(libc::ENOSYS) { |
110 | 0 | INVALID.store(true, SeqCst); |
111 | 0 | } else { |
112 | 0 | return Err(err); |
113 | | } |
114 | 0 | } |
115 | | |
116 | 0 | super::utimes::set_times(p, atime, mtime, symlink) |
117 | 375 | } |