diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/filetime/src/unix | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/filetime/src/unix')
-rw-r--r-- | vendor/filetime/src/unix/android.rs | 63 | ||||
-rw-r--r-- | vendor/filetime/src/unix/linux.rs | 102 | ||||
-rw-r--r-- | vendor/filetime/src/unix/macos.rs | 108 | ||||
-rw-r--r-- | vendor/filetime/src/unix/mod.rs | 114 | ||||
-rw-r--r-- | vendor/filetime/src/unix/utimensat.rs | 58 | ||||
-rw-r--r-- | vendor/filetime/src/unix/utimes.rs | 130 |
6 files changed, 575 insertions, 0 deletions
diff --git a/vendor/filetime/src/unix/android.rs b/vendor/filetime/src/unix/android.rs new file mode 100644 index 000000000..afcd44866 --- /dev/null +++ b/vendor/filetime/src/unix/android.rs @@ -0,0 +1,63 @@ +use crate::FileTime; +use std::ffi::CString; +use std::fs::File; +use std::io; +use std::os::unix::prelude::*; +use std::path::Path; + +pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { + set_times(p, None, Some(mtime), false) +} + +pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), None, false) +} + +pub fn set_file_handle_times( + f: &File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + + // On Android NDK before version 19, `futimens` is not available. + // + // For better compatibility, we reimplement `futimens` using `utimensat`, + // the same way as bionic libc uses it to implement `futimens`. + let rc = unsafe { libc::utimensat(f.as_raw_fd(), core::ptr::null(), times.as_ptr(), 0) }; + if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } +} + +pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), true) +} + +fn set_times( + p: &Path, + atime: Option<FileTime>, + mtime: Option<FileTime>, + symlink: bool, +) -> io::Result<()> { + let flags = if symlink { + libc::AT_SYMLINK_NOFOLLOW + } else { + 0 + }; + + let p = CString::new(p.as_os_str().as_bytes())?; + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) }; + if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } +} diff --git a/vendor/filetime/src/unix/linux.rs b/vendor/filetime/src/unix/linux.rs new file mode 100644 index 000000000..c803e0217 --- /dev/null +++ b/vendor/filetime/src/unix/linux.rs @@ -0,0 +1,102 @@ +//! On Linux we try to use the more accurate `utimensat` syscall but this isn't +//! always available so we also fall back to `utimes` if we couldn't find +//! `utimensat` at runtime. + +use crate::FileTime; +use std::ffi::CString; +use std::fs; +use std::io; +use std::os::unix::prelude::*; +use std::path::Path; +use std::ptr; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering::SeqCst; + +pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { + set_times(p, None, Some(mtime), false) +} + +pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), None, false) +} + +pub fn set_file_handle_times( + f: &fs::File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + // Attempt to use the `utimensat` syscall, but if it's not supported by the + // current kernel then fall back to an older syscall. + static INVALID: AtomicBool = AtomicBool::new(false); + if !INVALID.load(SeqCst) { + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { + libc::syscall( + libc::SYS_utimensat, + f.as_raw_fd(), + ptr::null::<libc::c_char>(), + times.as_ptr(), + 0, + ) + }; + if rc == 0 { + return Ok(()); + } + let err = io::Error::last_os_error(); + if err.raw_os_error() == Some(libc::ENOSYS) { + INVALID.store(true, SeqCst); + } else { + return Err(err); + } + } + + super::utimes::set_file_handle_times(f, atime, mtime) +} + +pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), true) +} + +fn set_times( + p: &Path, + atime: Option<FileTime>, + mtime: Option<FileTime>, + symlink: bool, +) -> io::Result<()> { + let flags = if symlink { + libc::AT_SYMLINK_NOFOLLOW + } else { + 0 + }; + + // Same as the `if` statement above. + static INVALID: AtomicBool = AtomicBool::new(false); + if !INVALID.load(SeqCst) { + let p = CString::new(p.as_os_str().as_bytes())?; + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { + libc::syscall( + libc::SYS_utimensat, + libc::AT_FDCWD, + p.as_ptr(), + times.as_ptr(), + flags, + ) + }; + if rc == 0 { + return Ok(()); + } + let err = io::Error::last_os_error(); + if err.raw_os_error() == Some(libc::ENOSYS) { + INVALID.store(true, SeqCst); + } else { + return Err(err); + } + } + + super::utimes::set_times(p, atime, mtime, symlink) +} diff --git a/vendor/filetime/src/unix/macos.rs b/vendor/filetime/src/unix/macos.rs new file mode 100644 index 000000000..efe92d4ae --- /dev/null +++ b/vendor/filetime/src/unix/macos.rs @@ -0,0 +1,108 @@ +//! Beginning with macOS 10.13, `utimensat` is supported by the OS, so here, we check if the symbol exists +//! and if not, we fallback to `utimes`. +use crate::FileTime; +use libc::{c_char, c_int, timespec}; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::os::unix::prelude::*; +use std::path::Path; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering::SeqCst; +use std::{io, mem}; + +pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { + set_times(p, None, Some(mtime), false) +} + +pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), None, false) +} + +pub fn set_file_handle_times( + f: &File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + // Attempt to use the `futimens` syscall, but if it's not supported by the + // current kernel then fall back to an older syscall. + if let Some(func) = futimens() { + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { func(f.as_raw_fd(), times.as_ptr()) }; + if rc == 0 { + return Ok(()); + } else { + return Err(io::Error::last_os_error()); + } + } + + super::utimes::set_file_handle_times(f, atime, mtime) +} + +pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), true) +} + +fn set_times( + p: &Path, + atime: Option<FileTime>, + mtime: Option<FileTime>, + symlink: bool, +) -> io::Result<()> { + // Attempt to use the `utimensat` syscall, but if it's not supported by the + // current kernel then fall back to an older syscall. + if let Some(func) = utimensat() { + let flags = if symlink { + libc::AT_SYMLINK_NOFOLLOW + } else { + 0 + }; + + let p = CString::new(p.as_os_str().as_bytes())?; + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { func(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) }; + if rc == 0 { + return Ok(()); + } else { + return Err(io::Error::last_os_error()); + } + } + + super::utimes::set_times(p, atime, mtime, symlink) +} + +fn utimensat() -> Option<unsafe extern "C" fn(c_int, *const c_char, *const timespec, c_int) -> c_int> +{ + static ADDR: AtomicUsize = AtomicUsize::new(0); + unsafe { + fetch(&ADDR, CStr::from_bytes_with_nul_unchecked(b"utimensat\0")) + .map(|sym| mem::transmute(sym)) + } +} + +fn futimens() -> Option<unsafe extern "C" fn(c_int, *const timespec) -> c_int> { + static ADDR: AtomicUsize = AtomicUsize::new(0); + unsafe { + fetch(&ADDR, CStr::from_bytes_with_nul_unchecked(b"futimens\0")) + .map(|sym| mem::transmute(sym)) + } +} + +fn fetch(cache: &AtomicUsize, name: &CStr) -> Option<usize> { + match cache.load(SeqCst) { + 0 => {} + 1 => return None, + n => return Some(n), + } + let sym = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) }; + let (val, ret) = if sym.is_null() { + (1, None) + } else { + (sym as usize, Some(sym as usize)) + }; + cache.store(val, SeqCst); + return ret; +} diff --git a/vendor/filetime/src/unix/mod.rs b/vendor/filetime/src/unix/mod.rs new file mode 100644 index 000000000..66f9fc219 --- /dev/null +++ b/vendor/filetime/src/unix/mod.rs @@ -0,0 +1,114 @@ +use crate::FileTime; +use libc::{time_t, timespec}; +use std::fs; +use std::os::unix::prelude::*; + +cfg_if::cfg_if! { + if #[cfg(target_os = "linux")] { + mod utimes; + mod linux; + pub use self::linux::*; + } else if #[cfg(target_os = "android")] { + mod android; + pub use self::android::*; + } else if #[cfg(target_os = "macos")] { + mod utimes; + mod macos; + pub use self::macos::*; + } else if #[cfg(any(target_os = "solaris", + target_os = "illumos", + target_os = "emscripten", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "haiku"))] { + mod utimensat; + pub use self::utimensat::*; + } else { + mod utimes; + pub use self::utimes::*; + } +} + +#[allow(dead_code)] +fn to_timespec(ft: &Option<FileTime>) -> timespec { + cfg_if::cfg_if! { + if #[cfg(any(target_os = "macos", + target_os = "illumos", + target_os = "freebsd"))] { + // https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/sys/stat.h#L541 + // https://github.com/illumos/illumos-gate/blob/master/usr/src/boot/sys/sys/stat.h#L312 + // https://svnweb.freebsd.org/base/head/sys/sys/stat.h?view=markup#l359 + const UTIME_OMIT: i64 = -2; + } else if #[cfg(target_os = "openbsd")] { + // https://github.com/openbsd/src/blob/master/sys/sys/stat.h#L189 + const UTIME_OMIT: i64 = -1; + } else if #[cfg(target_os = "haiku")] { + // https://git.haiku-os.org/haiku/tree/headers/posix/sys/stat.h?#n106 + const UTIME_OMIT: i64 = 1000000001; + } else { + // http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/stat.h?annotate=1.68.30.1 + // https://github.com/emscripten-core/emscripten/blob/master/system/include/libc/sys/stat.h#L71 + const UTIME_OMIT: i64 = 1_073_741_822; + } + } + + if let &Some(ft) = ft { + timespec { + tv_sec: ft.seconds() as time_t, + tv_nsec: ft.nanoseconds() as _, + } + } else { + timespec { + tv_sec: 0, + tv_nsec: UTIME_OMIT as _, + } + } +} + +pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime { + FileTime { + seconds: meta.mtime(), + nanos: meta.mtime_nsec() as u32, + } +} + +pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime { + FileTime { + seconds: meta.atime(), + nanos: meta.atime_nsec() as u32, + } +} + +pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> { + macro_rules! birthtim { + ($(($e:expr, $i:ident)),*) => { + #[cfg(any($(target_os = $e),*))] + fn imp(meta: &fs::Metadata) -> Option<FileTime> { + $( + #[cfg(target_os = $e)] + use std::os::$i::fs::MetadataExt; + )* + Some(FileTime { + seconds: meta.st_birthtime(), + nanos: meta.st_birthtime_nsec() as u32, + }) + } + + #[cfg(all($(not(target_os = $e)),*))] + fn imp(_meta: &fs::Metadata) -> Option<FileTime> { + None + } + } + } + + birthtim! { + ("bitrig", bitrig), + ("freebsd", freebsd), + ("ios", ios), + ("macos", macos), + ("openbsd", openbsd) + } + + imp(meta) +} diff --git a/vendor/filetime/src/unix/utimensat.rs b/vendor/filetime/src/unix/utimensat.rs new file mode 100644 index 000000000..42a1ada2c --- /dev/null +++ b/vendor/filetime/src/unix/utimensat.rs @@ -0,0 +1,58 @@ +use crate::FileTime; +use std::ffi::CString; +use std::fs::File; +use std::io; +use std::os::unix::prelude::*; +use std::path::Path; + +pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { + set_times(p, None, Some(mtime), false) +} + +pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), None, false) +} + +pub fn set_file_handle_times( + f: &File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { libc::futimens(f.as_raw_fd(), times.as_ptr()) }; + if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } +} + +pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +fn set_times( + p: &Path, + atime: Option<FileTime>, + mtime: Option<FileTime>, + symlink: bool, +) -> io::Result<()> { + let flags = if symlink { + libc::AT_SYMLINK_NOFOLLOW + } else { + 0 + }; + + let p = CString::new(p.as_os_str().as_bytes())?; + let times = [super::to_timespec(&atime), super::to_timespec(&mtime)]; + let rc = unsafe { libc::utimensat(libc::AT_FDCWD, p.as_ptr(), times.as_ptr(), flags) }; + if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + } +} diff --git a/vendor/filetime/src/unix/utimes.rs b/vendor/filetime/src/unix/utimes.rs new file mode 100644 index 000000000..34bb882a2 --- /dev/null +++ b/vendor/filetime/src/unix/utimes.rs @@ -0,0 +1,130 @@ +use crate::FileTime; +use std::ffi::CString; +use std::fs; +use std::io; +use std::os::unix::prelude::*; +use std::path::Path; + +#[allow(dead_code)] +pub fn set_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), false) +} + +#[allow(dead_code)] +pub fn set_file_mtime(p: &Path, mtime: FileTime) -> io::Result<()> { + set_times(p, None, Some(mtime), false) +} + +#[allow(dead_code)] +pub fn set_file_atime(p: &Path, atime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), None, false) +} + +#[cfg(not(target_env = "uclibc"))] +#[allow(dead_code)] +pub fn set_file_handle_times( + f: &fs::File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + let (atime, mtime) = match get_times(atime, mtime, || f.metadata())? { + Some(pair) => pair, + None => return Ok(()), + }; + let times = [to_timeval(&atime), to_timeval(&mtime)]; + let rc = unsafe { libc::futimes(f.as_raw_fd(), times.as_ptr()) }; + return if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + }; +} + +#[cfg(target_env = "uclibc")] +#[allow(dead_code)] +pub fn set_file_handle_times( + f: &fs::File, + atime: Option<FileTime>, + mtime: Option<FileTime>, +) -> io::Result<()> { + let (atime, mtime) = match get_times(atime, mtime, || f.metadata())? { + Some(pair) => pair, + None => return Ok(()), + }; + let times = [to_timespec(&atime), to_timespec(&mtime)]; + let rc = unsafe { libc::futimens(f.as_raw_fd(), times.as_ptr()) }; + return if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + }; +} + +fn get_times( + atime: Option<FileTime>, + mtime: Option<FileTime>, + current: impl FnOnce() -> io::Result<fs::Metadata>, +) -> io::Result<Option<(FileTime, FileTime)>> { + let pair = match (atime, mtime) { + (Some(a), Some(b)) => (a, b), + (None, None) => return Ok(None), + (Some(a), None) => { + let meta = current()?; + (a, FileTime::from_last_modification_time(&meta)) + } + (None, Some(b)) => { + let meta = current()?; + (FileTime::from_last_access_time(&meta), b) + } + }; + Ok(Some(pair)) +} + +#[allow(dead_code)] +pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { + set_times(p, Some(atime), Some(mtime), true) +} + +pub fn set_times( + p: &Path, + atime: Option<FileTime>, + mtime: Option<FileTime>, + symlink: bool, +) -> io::Result<()> { + let (atime, mtime) = match get_times(atime, mtime, || p.metadata())? { + Some(pair) => pair, + None => return Ok(()), + }; + let p = CString::new(p.as_os_str().as_bytes())?; + let times = [to_timeval(&atime), to_timeval(&mtime)]; + let rc = unsafe { + if symlink { + libc::lutimes(p.as_ptr(), times.as_ptr()) + } else { + libc::utimes(p.as_ptr(), times.as_ptr()) + } + }; + return if rc == 0 { + Ok(()) + } else { + Err(io::Error::last_os_error()) + }; +} + +fn to_timeval(ft: &FileTime) -> libc::timeval { + libc::timeval { + tv_sec: ft.seconds() as libc::time_t, + tv_usec: (ft.nanoseconds() / 1000) as libc::suseconds_t, + } +} + +#[cfg(target_env = "uclibc")] +fn to_timespec(ft: &FileTime) -> libc::timespec { + libc::timespec { + tv_sec: ft.seconds() as libc::time_t, + #[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))] + tv_nsec: (ft.nanoseconds()) as i64, + #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] + tv_nsec: (ft.nanoseconds()) as libc::c_long, + } +} |