Debian Codegraph @

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

raw
1 use crate::FileTime;
2 use std::ffi::CString;
3 use std::io;
4 use std::os::unix::prelude::*;
5 use std::path::Path;
6
7 pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> {
8 set_times(p, Some(atime), Some(mtime), true)
9 }
10
11 fn set_times(
12 p: &Path,
13 atime: Option<FileTime>,
14 mtime: Option<FileTime>,
15 symlink: bool,
16 ) -> io::Result<()> {
17 let flags = if symlink {
18 if cfg!(target_os = "emscripten") {
19 return Err(io::Error::new(
20 io::ErrorKind::Other,
21 "emscripten does not support utimensat for symlinks",
22 ));
23 }
24 libc::AT_SYMLINK_NOFOLLOW
25 } else {
26 0
27 };
28
29 let p = CString::new(p.as_os_str().as_bytes())?;
30 let times = [super::to_timespec(&atime), super::to_timespec(&mtime)];
31 let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) };
32 if rc == 0 {
33 Ok(())
34 } else {
35 Err(io::Error::last_os_error())
36 }
37 }