| 1 |
//! libc syscalls supporting `rustix::fs`. |
| 2 |
|
| 3 |
use crate::backend::c; |
| 4 |
#[cfg(any( |
| 5 |
not(target_os = "redox"), |
| 6 |
feature = "alloc", |
| 7 |
all(linux_kernel, feature = "procfs") |
| 8 |
))] |
| 9 |
use crate::backend::conv::ret_usize; |
| 10 |
use crate::backend::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_off_t, ret_owned_fd}; |
| 11 |
use crate::fd::{BorrowedFd, OwnedFd}; |
| 12 |
use crate::ffi::CStr; |
| 13 |
#[cfg(all(apple, feature = "alloc"))] |
| 14 |
use crate::ffi::CString; |
| 15 |
#[cfg(not(any(target_os = "espidf", target_os = "vita")))] |
| 16 |
use crate::fs::Access; |
| 17 |
#[cfg(not(any( |
| 18 |
apple, |
| 19 |
netbsdlike, |
| 20 |
solarish, |
| 21 |
target_os = "dragonfly", |
| 22 |
target_os = "espidf", |
| 23 |
target_os = "haiku", |
| 24 |
target_os = "redox", |
| 25 |
target_os = "vita", |
| 26 |
)))] |
| 27 |
use crate::fs::Advice; |
| 28 |
#[cfg(not(any(target_os = "espidf", target_os = "redox")))] |
| 29 |
use crate::fs::AtFlags; |
| 30 |
#[cfg(not(any( |
| 31 |
netbsdlike, |
| 32 |
solarish, |
| 33 |
target_os = "dragonfly", |
| 34 |
target_os = "espidf", |
| 35 |
target_os = "nto", |
| 36 |
target_os = "redox", |
| 37 |
target_os = "vita", |
| 38 |
)))] |
| 39 |
use crate::fs::FallocateFlags; |
| 40 |
#[cfg(not(any(target_os = "espidf", target_os = "vita", target_os = "wasi")))] |
| 41 |
use crate::fs::FlockOperation; |
| 42 |
#[cfg(any(linux_kernel, target_os = "freebsd"))] |
| 43 |
use crate::fs::MemfdFlags; |
| 44 |
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))] |
| 45 |
use crate::fs::SealFlags; |
| 46 |
#[cfg(not(any( |
| 47 |
solarish, |
| 48 |
target_os = "espidf", |
| 49 |
target_os = "haiku", |
| 50 |
target_os = "netbsd", |
| 51 |
target_os = "nto", |
| 52 |
target_os = "redox", |
| 53 |
target_os = "vita", |
| 54 |
target_os = "wasi", |
| 55 |
)))] |
| 56 |
use crate::fs::StatFs; |
| 57 |
#[cfg(not(any(target_os = "espidf", target_os = "vita")))] |
| 58 |
use crate::fs::Timestamps; |
| 59 |
#[cfg(not(any( |
| 60 |
apple, |
| 61 |
target_os = "espidf", |
| 62 |
target_os = "redox", |
| 63 |
target_os = "vita", |
| 64 |
target_os = "wasi" |
| 65 |
)))] |
| 66 |
use crate::fs::{Dev, FileType}; |
| 67 |
use crate::fs::{Mode, OFlags, SeekFrom, Stat}; |
| 68 |
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))] |
| 69 |
use crate::fs::{StatVfs, StatVfsMountFlags}; |
| 70 |
use crate::io; |
| 71 |
#[cfg(all(target_env = "gnu", fix_y2038))] |
| 72 |
use crate::timespec::LibcTimespec; |
| 73 |
#[cfg(not(target_os = "wasi"))] |
| 74 |
use crate::ugid::{Gid, Uid}; |
| 75 |
#[cfg(all(apple, feature = "alloc"))] |
| 76 |
use alloc::vec; |
| 77 |
use core::mem::MaybeUninit; |
| 78 |
#[cfg(apple)] |
| 79 |
use { |
| 80 |
crate::backend::conv::nonnegative_ret, |
| 81 |
crate::fs::{copyfile_state_t, CloneFlags, CopyfileFlags}, |
| 82 |
}; |
| 83 |
#[cfg(any(apple, linux_kernel))] |
| 84 |
use {crate::fs::XattrFlags, core::mem::size_of, core::ptr::null_mut}; |
| 85 |
#[cfg(linux_kernel)] |
| 86 |
use { |
| 87 |
crate::fs::{RenameFlags, ResolveFlags, Statx, StatxFlags, CWD}, |
| 88 |
core::ptr::null, |
| 89 |
}; |
| 90 |
|
| 91 |
#[cfg(all(target_env = "gnu", fix_y2038))] |
| 92 |
weak!(fn __utimensat64(c::c_int, *const c::c_char, *const LibcTimespec, c::c_int) -> c::c_int); |
| 93 |
#[cfg(all(target_env = "gnu", fix_y2038))] |
| 94 |
weak!(fn __futimens64(c::c_int, *const LibcTimespec) -> c::c_int); |
| 95 |
|
| 96 |
/// Use a direct syscall (via libc) for `open`. |
| 97 |
/// |
| 98 |
/// This is only currently necessary as a workaround for old glibc; see below. |
| 99 |
#[cfg(all(unix, target_env = "gnu"))] |
| 100 |
fn open_via_syscall(path: &CStr, oflags: OFlags, mode: Mode) -> io::Result<OwnedFd> { |
| 101 |
// Linux on aarch64, loongarch64 and riscv64 has no `open` syscall so use |
| 102 |
// `openat`. |
| 103 |
#[cfg(any( |
| 104 |
target_arch = "aarch64", |
| 105 |
target_arch = "riscv32", |
| 106 |
target_arch = "riscv64", |
| 107 |
target_arch = "csky", |
| 108 |
target_arch = "loongarch64" |
| 109 |
))] |
| 110 |
{ |
| 111 |
openat_via_syscall(CWD, path, oflags, mode) |
| 112 |
} |
| 113 |
|
| 114 |
// Use the `open` syscall. |
| 115 |
#[cfg(not(any( |
| 116 |
target_arch = "aarch64", |
| 117 |
target_arch = "riscv32", |
| 118 |
target_arch = "riscv64", |
| 119 |
target_arch = "csky", |
| 120 |
target_arch = "loongarch64" |
| 121 |
)))] |
| 122 |
unsafe { |
| 123 |
syscall! { |
| 124 |
fn open( |
| 125 |
pathname: *const c::c_char, |
| 126 |
oflags: c::c_int, |
| 127 |
mode: c::mode_t |
| 128 |
) via SYS_open -> c::c_int |
| 129 |
} |
| 130 |
|
| 131 |
ret_owned_fd(open( |
| 132 |
c_str(path), |
| 133 |
bitflags_bits!(oflags), |
| 134 |
bitflags_bits!(mode), |
| 135 |
)) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
pub(crate) fn open(path: &CStr, oflags: OFlags, mode: Mode) -> io::Result<OwnedFd> { |
| 140 |
// Work around <https://sourceware.org/bugzilla/show_bug.cgi?id=17523>. |
| 141 |
// glibc versions before 2.25 don't handle `O_TMPFILE` correctly. |
| 142 |
#[cfg(all( |
| 143 |
unix, |
| 144 |
target_env = "gnu", |
| 145 |
not(target_os = "hurd"), |
| 146 |
not(target_os = "freebsd") |
| 147 |
))] |
| 148 |
if oflags.contains(OFlags::TMPFILE) && crate::backend::if_glibc_is_less_than_2_25() { |
| 149 |
return open_via_syscall(path, oflags, mode); |
| 150 |
} |
| 151 |
|
| 152 |
// On these platforms, `mode_t` is `u16` and can't be passed directly to a |
| 153 |
// variadic function. |
| 154 |
#[cfg(any( |
| 155 |
apple, |
| 156 |
freebsdlike, |
| 157 |
all(target_os = "android", target_pointer_width = "32") |
| 158 |
))] |
| 159 |
let mode: c::c_uint = mode.bits().into(); |
| 160 |
|
| 161 |
// Otherwise, cast to `mode_t` as that's what `open` is documented to take. |
| 162 |
#[cfg(not(any( |
| 163 |
apple, |
| 164 |
freebsdlike, |
| 165 |
all(target_os = "android", target_pointer_width = "32") |
| 166 |
)))] |
| 167 |
let mode: c::mode_t = mode.bits() as _; |
| 168 |
|
| 169 |
unsafe { ret_owned_fd(c::open(c_str(path), bitflags_bits!(oflags), mode)) } |
| 170 |
} |
| 171 |
|
| 172 |
/// Use a direct syscall (via libc) for `openat`. |
| 173 |
/// |
| 174 |
/// This is only currently necessary as a workaround for old glibc; see below. |
| 175 |
#[cfg(all(unix, target_env = "gnu", not(target_os = "hurd")))] |
| 176 |
fn openat_via_syscall( |
| 177 |
dirfd: BorrowedFd<'_>, |
| 178 |
path: &CStr, |
| 179 |
oflags: OFlags, |
| 180 |
mode: Mode, |
| 181 |
) -> io::Result<OwnedFd> { |
| 182 |
syscall! { |
| 183 |
fn openat( |
| 184 |
base_dirfd: c::c_int, |
| 185 |
pathname: *const c::c_char, |
| 186 |
oflags: c::c_int, |
| 187 |
mode: c::mode_t |
| 188 |
) via SYS_openat -> c::c_int |
| 189 |
} |
| 190 |
|
| 191 |
unsafe { |
| 192 |
ret_owned_fd(openat( |
| 193 |
borrowed_fd(dirfd), |
| 194 |
c_str(path), |
| 195 |
bitflags_bits!(oflags), |
| 196 |
bitflags_bits!(mode), |
| 197 |
)) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
#[cfg(not(target_os = "redox"))] |
| 202 |
pub(crate) fn openat( |
| 203 |
dirfd: BorrowedFd<'_>, |
| 204 |
path: &CStr, |
| 205 |
oflags: OFlags, |
| 206 |
mode: Mode, |
| 207 |
) -> io::Result<OwnedFd> { |
| 208 |
// Work around <https://sourceware.org/bugzilla/show_bug.cgi?id=17523>. |
| 209 |
// glibc versions before 2.25 don't handle `O_TMPFILE` correctly. |
| 210 |
#[cfg(all( |
| 211 |
unix, |
| 212 |
target_env = "gnu", |
| 213 |
not(target_os = "hurd"), |
| 214 |
not(target_os = "freebsd") |
| 215 |
))] |
| 216 |
if oflags.contains(OFlags::TMPFILE) && crate::backend::if_glibc_is_less_than_2_25() { |
| 217 |
return openat_via_syscall(dirfd, path, oflags, mode); |
| 218 |
} |
| 219 |
|
| 220 |
// On these platforms, `mode_t` is `u16` and can't be passed directly to a |
| 221 |
// variadic function. |
| 222 |
#[cfg(any( |
| 223 |
apple, |
| 224 |
freebsdlike, |
| 225 |
all(target_os = "android", target_pointer_width = "32") |
| 226 |
))] |
| 227 |
let mode: c::c_uint = mode.bits().into(); |
| 228 |
|
| 229 |
// Otherwise, cast to `mode_t` as that's what `open` is documented to take. |
| 230 |
#[cfg(not(any( |
| 231 |
apple, |
| 232 |
freebsdlike, |
| 233 |
all(target_os = "android", target_pointer_width = "32") |
| 234 |
)))] |
| 235 |
let mode: c::mode_t = mode.bits() as _; |
| 236 |
|
| 237 |
unsafe { |
| 238 |
ret_owned_fd(c::openat( |
| 239 |
borrowed_fd(dirfd), |
| 240 |
c_str(path), |
| 241 |
bitflags_bits!(oflags), |
| 242 |
mode, |
| 243 |
)) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
#[cfg(not(any( |
| 248 |
solarish, |
| 249 |
target_os = "espidf", |
| 250 |
target_os = "haiku", |
| 251 |
target_os = "netbsd", |
| 252 |
target_os = "nto", |
| 253 |
target_os = "redox", |
| 254 |
target_os = "vita", |
| 255 |
target_os = "wasi", |
| 256 |
)))] |
| 257 |
#[inline] |
| 258 |
pub(crate) fn statfs(filename: &CStr) -> io::Result<StatFs> { |
| 259 |
unsafe { |
| 260 |
let mut result = MaybeUninit::<StatFs>::uninit(); |
| 261 |
ret(c::statfs(c_str(filename), result.as_mut_ptr()))?; |
| 262 |
Ok(result.assume_init()) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))] |
| 267 |
#[inline] |
| 268 |
pub(crate) fn statvfs(filename: &CStr) -> io::Result<StatVfs> { |
| 269 |
unsafe { |
| 270 |
let mut result = MaybeUninit::<c::statvfs>::uninit(); |
| 271 |
ret(c::statvfs(c_str(filename), result.as_mut_ptr()))?; |
| 272 |
Ok(libc_statvfs_to_statvfs(result.assume_init())) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
#[cfg(feature = "alloc")] |
| 277 |
#[inline] |
| 278 |
pub(crate) fn readlink(path: &CStr, buf: &mut [u8]) -> io::Result<usize> { |
| 279 |
unsafe { |
| 280 |
ret_usize( |
| 281 |
c::readlink(c_str(path), buf.as_mut_ptr().cast::<c::c_char>(), buf.len()) as isize, |
| 282 |
) |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
#[cfg(not(target_os = "redox"))] |
| 287 |
#[inline] |
| 288 |
pub(crate) fn readlinkat( |
| 289 |
dirfd: BorrowedFd<'_>, |
| 290 |
path: &CStr, |
| 291 |
buf: &mut [MaybeUninit<u8>], |
| 292 |
) -> io::Result<usize> { |
| 293 |
unsafe { |
| 294 |
ret_usize(c::readlinkat( |
| 295 |
borrowed_fd(dirfd), |
| 296 |
c_str(path), |
| 297 |
buf.as_mut_ptr().cast::<c::c_char>(), |
| 298 |
buf.len(), |
| 299 |
) as isize) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
pub(crate) fn mkdir(path: &CStr, mode: Mode) -> io::Result<()> { |
| 304 |
unsafe { ret(c::mkdir(c_str(path), mode.bits() as c::mode_t)) } |
| 305 |
} |
| 306 |
|
| 307 |
#[cfg(not(target_os = "redox"))] |
| 308 |
pub(crate) fn mkdirat(dirfd: BorrowedFd<'_>, path: &CStr, mode: Mode) -> io::Result<()> { |
| 309 |
unsafe { |
| 310 |
ret(c::mkdirat( |
| 311 |
borrowed_fd(dirfd), |
| 312 |
c_str(path), |
| 313 |
mode.bits() as c::mode_t, |
| 314 |
)) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#[cfg(linux_kernel)] |
| 319 |
pub(crate) fn getdents_uninit( |
| 320 |
fd: BorrowedFd<'_>, |
| 321 |
buf: &mut [MaybeUninit<u8>], |
| 322 |
) -> io::Result<usize> { |
| 323 |
syscall! { |
| 324 |
fn getdents64( |
| 325 |
fd: c::c_int, |
| 326 |
dirp: *mut c::c_void, |
| 327 |
count: usize |
| 328 |
) via SYS_getdents64 -> c::ssize_t |
| 329 |
} |
| 330 |
unsafe { |
| 331 |
ret_usize(getdents64( |
| 332 |
borrowed_fd(fd), |
| 333 |
buf.as_mut_ptr().cast::<c::c_void>(), |
| 334 |
buf.len(), |
| 335 |
)) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
pub(crate) fn link(old_path: &CStr, new_path: &CStr) -> io::Result<()> { |
| 340 |
unsafe { ret(c::link(c_str(old_path), c_str(new_path))) } |
| 341 |
} |
| 342 |
|
| 343 |
#[cfg(not(any(target_os = "espidf", target_os = "redox")))] |
| 344 |
pub(crate) fn linkat( |
| 345 |
old_dirfd: BorrowedFd<'_>, |
| 346 |
old_path: &CStr, |
| 347 |
new_dirfd: BorrowedFd<'_>, |
| 348 |
new_path: &CStr, |
| 349 |
flags: AtFlags, |
| 350 |
) -> io::Result<()> { |
| 351 |
// macOS <= 10.9 lacks `linkat`. |
| 352 |
#[cfg(target_os = "macos")] |
| 353 |
unsafe { |
| 354 |
weak! { |
| 355 |
fn linkat( |
| 356 |
c::c_int, |
| 357 |
*const c::c_char, |
| 358 |
c::c_int, |
| 359 |
*const c::c_char, |
| 360 |
c::c_int |
| 361 |
) -> c::c_int |
| 362 |
} |
| 363 |
// If we have `linkat`, use it. |
| 364 |
if let Some(libc_linkat) = linkat.get() { |
| 365 |
return ret(libc_linkat( |
| 366 |
borrowed_fd(old_dirfd), |
| 367 |
c_str(old_path), |
| 368 |
borrowed_fd(new_dirfd), |
| 369 |
c_str(new_path), |
| 370 |
bitflags_bits!(flags), |
| 371 |
)); |
| 372 |
} |
| 373 |
// Otherwise, see if we can emulate the `AT_FDCWD` case. |
| 374 |
if borrowed_fd(old_dirfd) != c::AT_FDCWD || borrowed_fd(new_dirfd) != c::AT_FDCWD { |
| 375 |
return Err(io::Errno::NOSYS); |
| 376 |
} |
| 377 |
if flags.intersects(!AtFlags::SYMLINK_FOLLOW) { |
| 378 |
return Err(io::Errno::INVAL); |
| 379 |
} |
| 380 |
if !flags.is_empty() { |
| 381 |
return Err(io::Errno::OPNOTSUPP); |
| 382 |
} |
| 383 |
ret(c::link(c_str(old_path), c_str(new_path))) |
| 384 |
} |
| 385 |
|
| 386 |
#[cfg(not(target_os = "macos"))] |
| 387 |
unsafe { |
| 388 |
ret(c::linkat( |
| 389 |
borrowed_fd(old_dirfd), |
| 390 |
c_str(old_path), |
| 391 |
borrowed_fd(new_dirfd), |
| 392 |
c_str(new_path), |
| 393 |
bitflags_bits!(flags), |
| 394 |
)) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
pub(crate) fn rmdir(path: &CStr) -> io::Result<()> { |
| 399 |
unsafe { ret(c::rmdir(c_str(path))) } |
| 400 |
} |
| 401 |
|
| 402 |
pub(crate) fn unlink(path: &CStr) -> io::Result<()> { |
| 403 |
unsafe { ret(c::unlink(c_str(path))) } |
| 404 |
} |
| 405 |
|
| 406 |
#[cfg(not(any(target_os = "espidf", target_os = "redox")))] |
| 407 |
pub(crate) fn unlinkat(dirfd: BorrowedFd<'_>, path: &CStr, flags: AtFlags) -> io::Result<()> { |
| 408 |
// macOS <= 10.9 lacks `unlinkat`. |
| 409 |
#[cfg(target_os = "macos")] |
| 410 |
unsafe { |
| 411 |
weak! { |
| 412 |
fn unlinkat( |
| 413 |
c::c_int, |
| 414 |
*const c::c_char, |
| 415 |
c::c_int |
| 416 |
) -> c::c_int |
| 417 |
} |
| 418 |
// If we have `unlinkat`, use it. |
| 419 |
if let Some(libc_unlinkat) = unlinkat.get() { |
| 420 |
return ret(libc_unlinkat( |
| 421 |
borrowed_fd(dirfd), |
| 422 |
c_str(path), |
| 423 |
bitflags_bits!(flags), |
| 424 |
)); |
| 425 |
} |
| 426 |
// Otherwise, see if we can emulate the `AT_FDCWD` case. |
| 427 |
if borrowed_fd(dirfd) != c::AT_FDCWD { |
| 428 |
return Err(io::Errno::NOSYS); |
| 429 |
} |
| 430 |
if flags.intersects(!AtFlags::REMOVEDIR) { |
| 431 |
return Err(io::Errno::INVAL); |
| 432 |
} |
| 433 |
if flags.contains(AtFlags::REMOVEDIR) { |
| 434 |
ret(c::rmdir(c_str(path))) |
| 435 |
} else { |
| 436 |
ret(c::unlink(c_str(path))) |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
#[cfg(not(target_os = "macos"))] |
| 441 |
unsafe { |
| 442 |
ret(c::unlinkat( |
| 443 |
borrowed_fd(dirfd), |
| 444 |
c_str(path), |
| 445 |
bitflags_bits!(flags), |
| 446 |
)) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
pub(crate) fn rename(old_path: &CStr, new_path: &CStr) -> io::Result<()> { |
| 451 |
unsafe { ret(c::rename(c_str(old_path), c_str(new_path))) } |
| 452 |
} |
| 453 |
|
| 454 |
#[cfg(not(target_os = "redox"))] |
| 455 |
pub(crate) fn renameat( |
| 456 |
old_dirfd: BorrowedFd<'_>, |
| 457 |
old_path: &CStr, |
| 458 |
new_dirfd: BorrowedFd<'_>, |
| 459 |
new_path: &CStr, |
| 460 |
) -> io::Result<()> { |
| 461 |
// macOS <= 10.9 lacks `renameat`. |
| 462 |
#[cfg(target_os = "macos")] |
| 463 |
unsafe { |
| 464 |
weak! { |
| 465 |
fn renameat( |
| 466 |
c::c_int, |
| 467 |
*const c::c_char, |
| 468 |
c::c_int, |
| 469 |
*const c::c_char |
| 470 |
) -> c::c_int |
| 471 |
} |
| 472 |
// If we have `renameat`, use it. |
| 473 |
if let Some(libc_renameat) = renameat.get() { |
| 474 |
return ret(libc_renameat( |
| 475 |
borrowed_fd(old_dirfd), |
| 476 |
c_str(old_path), |
| 477 |
borrowed_fd(new_dirfd), |
| 478 |
c_str(new_path), |
| 479 |
)); |
| 480 |
} |
| 481 |
// Otherwise, see if we can emulate the `AT_FDCWD` case. |
| 482 |
if borrowed_fd(old_dirfd) != c::AT_FDCWD || borrowed_fd(new_dirfd) != c::AT_FDCWD { |
| 483 |
return Err(io::Errno::NOSYS); |
| 484 |
} |
| 485 |
ret(c::rename(c_str(old_path), c_str(new_path))) |
| 486 |
} |
| 487 |
|
| 488 |
#[cfg(not(target_os = "macos"))] |
| 489 |
unsafe { |
| 490 |
ret(c::renameat( |
| 491 |
borrowed_fd(old_dirfd), |
| 492 |
c_str(old_path), |
| 493 |
borrowed_fd(new_dirfd), |
| 494 |
c_str(new_path), |
| 495 |
)) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
#[cfg(all(target_os = "linux", target_env = "gnu"))] |
| 500 |
pub(crate) fn renameat2( |
| 501 |
old_dirfd: BorrowedFd<'_>, |
| 502 |
old_path: &CStr, |
| 503 |
new_dirfd: BorrowedFd<'_>, |
| 504 |
new_path: &CStr, |
| 505 |
flags: RenameFlags, |
| 506 |
) -> io::Result<()> { |
| 507 |
// `renameat2` wasn't supported in glibc until 2.28. |
| 508 |
weak_or_syscall! { |
| 509 |
fn renameat2( |
| 510 |
olddirfd: c::c_int, |
| 511 |
oldpath: *const c::c_char, |
| 512 |
newdirfd: c::c_int, |
| 513 |
newpath: *const c::c_char, |
| 514 |
flags: c::c_uint |
| 515 |
) via SYS_renameat2 -> c::c_int |
| 516 |
} |
| 517 |
|
| 518 |
unsafe { |
| 519 |
ret(renameat2( |
| 520 |
borrowed_fd(old_dirfd), |
| 521 |
c_str(old_path), |
| 522 |
borrowed_fd(new_dirfd), |
| 523 |
c_str(new_path), |
| 524 |
flags.bits(), |
| 525 |
)) |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
#[cfg(any( |
| 530 |
target_os = "android", |
| 531 |
all(target_os = "linux", not(target_env = "gnu")), |
| 532 |
))] |
| 533 |
#[inline] |
| 534 |
pub(crate) fn renameat2( |
| 535 |
old_dirfd: BorrowedFd<'_>, |
| 536 |
old_path: &CStr, |
| 537 |
new_dirfd: BorrowedFd<'_>, |
| 538 |
new_path: &CStr, |
| 539 |
flags: RenameFlags, |
| 540 |
) -> io::Result<()> { |
| 541 |
// At present, `libc` only has `renameat2` defined for glibc. If we have |
| 542 |
// no flags, we can use plain `renameat`, but otherwise we use `syscall!`. |
| 543 |
// to call `renameat2` ourselves. |
| 544 |
if flags.is_empty() { |
| 545 |
renameat(old_dirfd, old_path, new_dirfd, new_path) |
| 546 |
} else { |
| 547 |
syscall! { |
| 548 |
fn renameat2( |
| 549 |
olddirfd: c::c_int, |
| 550 |
oldpath: *const c::c_char, |
| 551 |
newdirfd: c::c_int, |
| 552 |
newpath: *const c::c_char, |
| 553 |
flags: c::c_uint |
| 554 |
) via SYS_renameat2 -> c::c_int |
| 555 |
} |
| 556 |
|
| 557 |
unsafe { |
| 558 |
ret(renameat2( |
| 559 |
borrowed_fd(old_dirfd), |
| 560 |
c_str(old_path), |
| 561 |
borrowed_fd(new_dirfd), |
| 562 |
c_str(new_path), |
| 563 |
flags.bits(), |
| 564 |
)) |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
pub(crate) fn symlink(old_path: &CStr, new_path: &CStr) -> io::Result<()> { |
| 570 |
unsafe { ret(c::symlink(c_str(old_path), c_str(new_path))) } |
| 571 |
} |
| 572 |
|
| 573 |
#[cfg(not(target_os = "redox"))] |
| 574 |
pub(crate) fn symlinkat( |
| 575 |
old_path: &CStr, |
| 576 |
new_dirfd: BorrowedFd<'_>, |
| 577 |
new_path: &CStr, |
| 578 |
) -> io::Result<()> { |
| 579 |
unsafe { |
| 580 |
ret(c::symlinkat( |
| 581 |
c_str(old_path), |
| 582 |
borrowed_fd(new_dirfd), |
| 583 |
c_str(new_path), |
| 584 |
)) |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
pub(crate) fn stat(path: &CStr) -> io::Result<Stat> { |
| 589 |
// See the comments in `fstat` about using `crate::fs::statx` here. |
| 590 |
#[cfg(all( |
| 591 |
linux_kernel, |
| 592 |
any( |
| 593 |
target_pointer_width = "32", |
| 594 |
target_arch = "mips64", |
| 595 |
target_arch = "mips64r6" |
| 596 |
) |
| 597 |
))] |
| 598 |
{ |
| 599 |
match crate::fs::statx( |
| 600 |
crate::fs::CWD, |
| 601 |
path, |
| 602 |
AtFlags::empty(), |
| 603 |
StatxFlags::BASIC_STATS, |
| 604 |
) { |
| 605 |
Ok(x) => statx_to_stat(x), |
| 606 |
Err(io::Errno::NOSYS) => statat_old(crate::fs::CWD, path, AtFlags::empty()), |
| 607 |
Err(err) => Err(err), |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// Main version: libc is y2038 safe. Or, the platform is not y2038 safe and |
| 612 |
// there's nothing practical we can do. |
| 613 |
#[cfg(not(all( |
| 614 |
linux_kernel, |
| 615 |
any( |
| 616 |
target_pointer_width = "32", |
| 617 |
target_arch = "mips64", |
| 618 |
target_arch = "mips64r6" |
| 619 |
) |
| 620 |
)))] |
| 621 |
unsafe { |
| 622 |
let mut stat = MaybeUninit::<Stat>::uninit(); |
| 623 |
ret(c::stat(c_str(path), stat.as_mut_ptr()))?; |
| 624 |
Ok(stat.assume_init()) |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
pub(crate) fn lstat(path: &CStr) -> io::Result<Stat> { |
| 629 |
// See the comments in `fstat` about using `crate::fs::statx` here. |
| 630 |
#[cfg(all( |
| 631 |
linux_kernel, |
| 632 |
any( |
| 633 |
target_pointer_width = "32", |
| 634 |
target_arch = "mips64", |
| 635 |
target_arch = "mips64r6" |
| 636 |
) |
| 637 |
))] |
| 638 |
{ |
| 639 |
match crate::fs::statx( |
| 640 |
crate::fs::CWD, |
| 641 |
path, |
| 642 |
AtFlags::SYMLINK_NOFOLLOW, |
| 643 |
StatxFlags::BASIC_STATS, |
| 644 |
) { |
| 645 |
Ok(x) => statx_to_stat(x), |
| 646 |
Err(io::Errno::NOSYS) => statat_old(crate::fs::CWD, path, AtFlags::SYMLINK_NOFOLLOW), |
| 647 |
Err(err) => Err(err), |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
// Main version: libc is y2038 safe. Or, the platform is not y2038 safe and |
| 652 |
// there's nothing practical we can do. |
| 653 |
#[cfg(not(all( |
| 654 |
linux_kernel, |
| 655 |
any( |
| 656 |
target_pointer_width = "32", |
| 657 |
target_arch = "mips64", |
| 658 |
target_arch = "mips64r6" |
| 659 |
) |
| 660 |
)))] |
| 661 |
unsafe { |
| 662 |
let mut stat = MaybeUninit::<Stat>::uninit(); |
| 663 |
ret(c::lstat(c_str(path), stat.as_mut_ptr()))?; |
| 664 |
Ok(stat.assume_init()) |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
#[cfg(not(any(target_os = "espidf", target_os = "redox")))] |
| 669 |
pub(crate) fn statat(dirfd: BorrowedFd<'_>, path: &CStr, flags: AtFlags) -> io::Result<Stat> { |
| 670 |
// See the comments in `fstat` about using `crate::fs::statx` here. |
| 671 |
#[cfg(all( |
| 672 |
linux_kernel, |
| 673 |
any( |
| 674 |
target_pointer_width = "32", |
| 675 |
target_arch = "mips64", |
| 676 |
target_arch = "mips64r6" |
| 677 |
) |
| 678 |
))] |
| 679 |
{ |
| 680 |
match crate::fs::statx(dirfd, path, flags, StatxFlags::BASIC_STATS) { |
| 681 |
Ok(x) => statx_to_stat(x), |
| 682 |
Err(io::Errno::NOSYS) => statat_old(dirfd, path, flags), |
| 683 |
Err(err) => Err(err), |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
// Main version: libc is y2038 safe. Or, the platform is not y2038 safe and |
| 688 |
// there's nothing practical we can do. |
| 689 |
#[cfg(not(all( |
| 690 |
linux_kernel, |
| 691 |
any( |
| 692 |
target_pointer_width = "32", |
| 693 |
target_arch = "mips64", |
| 694 |
target_arch = "mips64r6" |
| 695 |
) |
| 696 |
)))] |
| 697 |
unsafe { |
| 698 |
let mut stat = MaybeUninit::<Stat>::uninit(); |
| 699 |
ret(c::fstatat( |
| 700 |
borrowed_fd(dirfd), |
| 701 |
c_str(path), |
| 702 |
stat.as_mut_ptr(), |
| 703 |
bitflags_bits!(flags), |
| 704 |
))?; |
| 705 |
Ok(stat.assume_init()) |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
#[cfg(all( |
| 710 |
linux_kernel, |
| 711 |
any( |
| 712 |
target_pointer_width = "32", |
| 713 |
target_arch = "mips64", |
| 714 |
target_arch = "mips64r6" |
| 715 |
) |
| 716 |
))] |
| 717 |
fn statat_old(dirfd: BorrowedFd<'_>, path: &CStr, flags: AtFlags) -> io::Result<Stat> { |
| 718 |
unsafe { |
| 719 |
let mut result = MaybeUninit::<c::stat64>::uninit(); |
| 720 |
ret(c::fstatat( |
| 721 |
borrowed_fd(dirfd), |
| 722 |
c_str(path), |
| 723 |
result.as_mut_ptr(), |
| 724 |
bitflags_bits!(flags), |
| 725 |
))?; |
| 726 |
stat64_to_stat(result.assume_init()) |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
#[cfg(not(any(target_os = "espidf", target_os = "emscripten", target_os = "vita")))] |
| 731 |
pub(crate) fn access(path: &CStr, access: Access) -> io::Result<()> { |
| 732 |
unsafe { ret(c::access(c_str(path), access.bits())) } |
| 733 |
} |
| 734 |
|
| 735 |
#[cfg(not(any( |
| 736 |
target_os = "emscripten", |
| 737 |
target_os = "espidf", |
| 738 |
target_os = "redox", |
| 739 |
target_os = "vita" |
| 740 |
)))] |
| 741 |
pub(crate) fn accessat( |
| 742 |
dirfd: BorrowedFd<'_>, |
| 743 |
path: &CStr, |
| 744 |
access: Access, |
| 745 |
flags: AtFlags, |
| 746 |
) -> io::Result<()> { |
| 747 |
// macOS <= 10.9 lacks `faccessat`. |
| 748 |
#[cfg(target_os = "macos")] |
| 749 |
unsafe { |
| 750 |
weak! { |
| 751 |
fn faccessat( |
| 752 |
c::c_int, |
| 753 |
*const c::c_char, |
| 754 |
c::c_int, |
| 755 |
c::c_int |
| 756 |
) -> c::c_int |
| 757 |
} |
| 758 |
// If we have `faccessat`, use it. |
| 759 |
if let Some(libc_faccessat) = faccessat.get() { |
| 760 |
return ret(libc_faccessat( |
| 761 |
borrowed_fd(dirfd), |
| 762 |
c_str(path), |
| 763 |
bitflags_bits!(access), |
| 764 |
bitflags_bits!(flags), |
| 765 |
)); |
| 766 |
} |
| 767 |
// Otherwise, see if we can emulate the `AT_FDCWD` case. |
| 768 |
if borrowed_fd(dirfd) != c::AT_FDCWD { |
| 769 |
return Err(io::Errno::NOSYS); |
| 770 |
} |
| 771 |
if flags.intersects(!(AtFlags::EACCESS | AtFlags::SYMLINK_NOFOLLOW)) { |
| 772 |
return Err(io::Errno::INVAL); |
| 773 |
} |
| 774 |
if !flags.is_empty() { |
| 775 |
return Err(io::Errno::OPNOTSUPP); |
| 776 |
} |
| 777 |
ret(c::access(c_str(path), bitflags_bits!(access))) |
| 778 |
} |
| 779 |
|
| 780 |
#[cfg(not(target_os = "macos"))] |
| 781 |
unsafe { |
| 782 |
ret(c::faccessat( |
| 783 |
borrowed_fd(dirfd), |
| 784 |
c_str(path), |
| 785 |
bitflags_bits!(access), |
| 786 |
bitflags_bits!(flags), |
| 787 |
)) |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
#[cfg(target_os = "emscripten")] |
| 792 |
pub(crate) fn access(_path: &CStr, _access: Access) -> io::Result<()> { |
| 793 |
Ok(()) |
| 794 |
} |
| 795 |
|
| 796 |
#[cfg(target_os = "emscripten")] |
| 797 |
pub(crate) fn accessat( |
| 798 |
_dirfd: BorrowedFd<'_>, |
| 799 |
_path: &CStr, |
| 800 |
_access: Access, |
| 801 |
_flags: AtFlags, |
| 802 |
) -> io::Result<()> { |
| 803 |
Ok(()) |
| 804 |
} |
| 805 |
|
| 806 |
#[cfg(not(any(target_os = "espidf", target_os = "redox", target_os = "vita")))] |
| 807 |
pub(crate) fn utimensat( |
| 808 |
dirfd: BorrowedFd<'_>, |
| 809 |
path: &CStr, |
| 810 |
times: &Timestamps, |
| 811 |
flags: AtFlags, |
| 812 |
) -> io::Result<()> { |
| 813 |
// Old 32-bit version: libc has `utimensat` but it is not y2038 safe by |
| 814 |
// default. But there may be a `__utimensat64` we can use. |
| 815 |
#[cfg(all(fix_y2038, not(apple)))] |
| 816 |
{ |
| 817 |
#[cfg(target_env = "gnu")] |
| 818 |
if let Some(libc_utimensat) = __utimensat64.get() { |
| 819 |
let libc_times: [LibcTimespec; 2] = [ |
| 820 |
times.last_access.clone().into(), |
| 821 |
times.last_modification.clone().into(), |
| 822 |
]; |
| 823 |
|
| 824 |
unsafe { |
| 825 |
return ret(libc_utimensat( |
| 826 |
borrowed_fd(dirfd), |
| 827 |
c_str(path), |
| 828 |
libc_times.as_ptr(), |
| 829 |
bitflags_bits!(flags), |
| 830 |
)); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
utimensat_old(dirfd, path, times, flags) |
| 835 |
} |
| 836 |
|
| 837 |
// Main version: libc is y2038 safe and has `utimensat`. Or, the platform |
| 838 |
// is not y2038 safe and there's nothing practical we can do. |
| 839 |
#[cfg(not(any(apple, fix_y2038)))] |
| 840 |
unsafe { |
| 841 |
use crate::utils::as_ptr; |
| 842 |
|
| 843 |
ret(c::utimensat( |
| 844 |
borrowed_fd(dirfd), |
| 845 |
c_str(path), |
| 846 |
as_ptr(times).cast(), |
| 847 |
bitflags_bits!(flags), |
| 848 |
)) |
| 849 |
} |
| 850 |
|
| 851 |
// Apple version: `utimensat` was introduced in macOS 10.13. |
| 852 |
#[cfg(apple)] |
| 853 |
unsafe { |
| 854 |
use crate::utils::as_ptr; |
| 855 |
|
| 856 |
// ABI details |
| 857 |
weak! { |
| 858 |
fn utimensat( |
| 859 |
c::c_int, |
| 860 |
*const c::c_char, |
| 861 |
*const c::timespec, |
| 862 |
c::c_int |
| 863 |
) -> c::c_int |
| 864 |
} |
| 865 |
extern "C" { |
| 866 |
fn setattrlist( |
| 867 |
path: *const c::c_char, |
| 868 |
attr_list: *const Attrlist, |
| 869 |
attr_buf: *const c::c_void, |
| 870 |
attr_buf_size: c::size_t, |
| 871 |
options: c::c_ulong, |
| 872 |
) -> c::c_int; |
| 873 |
} |
| 874 |
const FSOPT_NOFOLLOW: c::c_ulong = 0x0000_0001; |
| 875 |
|
| 876 |
// If we have `utimensat`, use it. |
| 877 |
if let Some(have_utimensat) = utimensat.get() { |
| 878 |
return ret(have_utimensat( |
| 879 |
borrowed_fd(dirfd), |
| 880 |
c_str(path), |
| 881 |
as_ptr(times).cast(), |
| 882 |
bitflags_bits!(flags), |
| 883 |
)); |
| 884 |
} |
| 885 |
|
| 886 |
// Convert `times`. We only need this in the child, but do it before |
| 887 |
// calling `fork` because it might fail. |
| 888 |
let (attrbuf_size, times, attrs) = times_to_attrlist(times)?; |
| 889 |
|
| 890 |
// `setattrlistat` was introduced in 10.13 along with `utimensat`, so |
| 891 |
// if we don't have `utimensat`, we don't have `setattrlistat` either. |
| 892 |
// Emulate it using `fork`, and `fchdir` and [`setattrlist`]. |
| 893 |
// |
| 894 |
// [`setattrlist`]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/setattrlist.2.html |
| 895 |
match c::fork() { |
| 896 |
-1 => Err(io::Errno::IO), |
| 897 |
0 => { |
| 898 |
if c::fchdir(borrowed_fd(dirfd)) != 0 { |
| 899 |
let code = match libc_errno::errno().0 { |
| 900 |
c::EACCES => 2, |
| 901 |
c::ENOTDIR => 3, |
| 902 |
_ => 1, |
| 903 |
}; |
| 904 |
c::_exit(code); |
| 905 |
} |
| 906 |
|
| 907 |
let mut flags_arg = 0; |
| 908 |
if flags.contains(AtFlags::SYMLINK_NOFOLLOW) { |
| 909 |
flags_arg |= FSOPT_NOFOLLOW; |
| 910 |
} |
| 911 |
|
| 912 |
if setattrlist( |
| 913 |
c_str(path), |
| 914 |
&attrs, |
| 915 |
as_ptr(×).cast(), |
| 916 |
attrbuf_size, |
| 917 |
flags_arg, |
| 918 |
) != 0 |
| 919 |
{ |
| 920 |
// Translate expected `errno` codes into ad-hoc integer |
| 921 |
// values suitable for exit statuses. |
| 922 |
let code = match libc_errno::errno().0 { |
| 923 |
c::EACCES => 2, |
| 924 |
c::ENOTDIR => 3, |
| 925 |
c::EPERM => 4, |
| 926 |
c::EROFS => 5, |
| 927 |
c::ELOOP => 6, |
| 928 |
c::ENOENT => 7, |
| 929 |
c::ENAMETOOLONG => 8, |
| 930 |
c::EINVAL => 9, |
| 931 |
c::ESRCH => 10, |
| 932 |
c::ENOTSUP => 11, |
| 933 |
_ => 1, |
| 934 |
}; |
| 935 |
c::_exit(code); |
| 936 |
} |
| 937 |
|
| 938 |
c::_exit(0); |
| 939 |
} |
| 940 |
child_pid => { |
| 941 |
let mut wstatus = 0; |
| 942 |
let _ = ret_c_int(c::waitpid(child_pid, &mut wstatus, 0))?; |
| 943 |
if c::WIFEXITED(wstatus) { |
| 944 |
// Translate our ad-hoc exit statuses back to `errno` |
| 945 |
// codes. |
| 946 |
match c::WEXITSTATUS(wstatus) { |
| 947 |
0 => Ok(()), |
| 948 |
2 => Err(io::Errno::ACCESS), |
| 949 |
3 => Err(io::Errno::NOTDIR), |
| 950 |
4 => Err(io::Errno::PERM), |
| 951 |
5 => Err(io::Errno::ROFS), |
| 952 |
6 => Err(io::Errno::LOOP), |
| 953 |
7 => Err(io::Errno::NOENT), |
| 954 |
8 => Err(io::Errno::NAMETOOLONG), |
| 955 |
9 => Err(io::Errno::INVAL), |
| 956 |
10 => Err(io::Errno::SRCH), |
| 957 |
11 => Err(io::Errno::NOTSUP), |
| 958 |
_ => Err(io::Errno::IO), |
| 959 |
} |
| 960 |
} else { |
| 961 |
Err(io::Errno::IO) |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
#[cfg(all(fix_y2038, not(apple)))] |
| 969 |
fn utimensat_old( |
| 970 |
dirfd: BorrowedFd<'_>, |
| 971 |
path: &CStr, |
| 972 |
times: &Timestamps, |
| 973 |
flags: AtFlags, |
| 974 |
) -> io::Result<()> { |
| 975 |
let old_times = [ |
| 976 |
c::timespec { |
| 977 |
tv_sec: times |
| 978 |
.last_access |
| 979 |
.tv_sec |
| 980 |
.try_into() |
| 981 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 982 |
tv_nsec: times |
| 983 |
.last_access |
| 984 |
.tv_nsec |
| 985 |
.try_into() |
| 986 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 987 |
}, |
| 988 |
c::timespec { |
| 989 |
tv_sec: times |
| 990 |
.last_modification |
| 991 |
.tv_sec |
| 992 |
.try_into() |
| 993 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 994 |
tv_nsec: times |
| 995 |
.last_modification |
| 996 |
.tv_nsec |
| 997 |
.try_into() |
| 998 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 999 |
}, |
| 1000 |
]; |
| 1001 |
unsafe { |
| 1002 |
ret(c::utimensat( |
| 1003 |
borrowed_fd(dirfd), |
| 1004 |
c_str(path), |
| 1005 |
old_times.as_ptr(), |
| 1006 |
bitflags_bits!(flags), |
| 1007 |
)) |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
#[cfg(not(target_os = "wasi"))] |
| 1012 |
pub(crate) fn chmod(path: &CStr, mode: Mode) -> io::Result<()> { |
| 1013 |
unsafe { ret(c::chmod(c_str(path), mode.bits() as c::mode_t)) } |
| 1014 |
} |
| 1015 |
|
| 1016 |
#[cfg(not(any( |
| 1017 |
linux_kernel, |
| 1018 |
target_os = "espidf", |
| 1019 |
target_os = "redox", |
| 1020 |
target_os = "wasi" |
| 1021 |
)))] |
| 1022 |
pub(crate) fn chmodat( |
| 1023 |
dirfd: BorrowedFd<'_>, |
| 1024 |
path: &CStr, |
| 1025 |
mode: Mode, |
| 1026 |
flags: AtFlags, |
| 1027 |
) -> io::Result<()> { |
| 1028 |
unsafe { |
| 1029 |
ret(c::fchmodat( |
| 1030 |
borrowed_fd(dirfd), |
| 1031 |
c_str(path), |
| 1032 |
mode.bits() as c::mode_t, |
| 1033 |
bitflags_bits!(flags), |
| 1034 |
)) |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[cfg(linux_kernel)] |
| 1039 |
pub(crate) fn chmodat( |
| 1040 |
dirfd: BorrowedFd<'_>, |
| 1041 |
path: &CStr, |
| 1042 |
mode: Mode, |
| 1043 |
flags: AtFlags, |
| 1044 |
) -> io::Result<()> { |
| 1045 |
// Linux's `fchmodat` does not have a flags argument. |
| 1046 |
// |
| 1047 |
// Use `c::syscall` rather than `c::fchmodat` because some libc |
| 1048 |
// implementations, such as musl, add extra logic to `fchmod` to emulate |
| 1049 |
// support for `AT_SYMLINK_NOFOLLOW`, which uses `/proc` outside our |
| 1050 |
// control. |
| 1051 |
syscall! { |
| 1052 |
fn fchmodat( |
| 1053 |
base_dirfd: c::c_int, |
| 1054 |
pathname: *const c::c_char, |
| 1055 |
mode: c::mode_t |
| 1056 |
) via SYS_fchmodat -> c::c_int |
| 1057 |
} |
| 1058 |
if flags == AtFlags::SYMLINK_NOFOLLOW { |
| 1059 |
return Err(io::Errno::OPNOTSUPP); |
| 1060 |
} |
| 1061 |
if !flags.is_empty() { |
| 1062 |
return Err(io::Errno::INVAL); |
| 1063 |
} |
| 1064 |
unsafe { |
| 1065 |
ret(fchmodat( |
| 1066 |
borrowed_fd(dirfd), |
| 1067 |
c_str(path), |
| 1068 |
mode.bits() as c::mode_t, |
| 1069 |
)) |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[cfg(apple)] |
| 1074 |
pub(crate) fn fclonefileat( |
| 1075 |
srcfd: BorrowedFd<'_>, |
| 1076 |
dst_dirfd: BorrowedFd<'_>, |
| 1077 |
dst: &CStr, |
| 1078 |
flags: CloneFlags, |
| 1079 |
) -> io::Result<()> { |
| 1080 |
syscall! { |
| 1081 |
fn fclonefileat( |
| 1082 |
srcfd: BorrowedFd<'_>, |
| 1083 |
dst_dirfd: BorrowedFd<'_>, |
| 1084 |
dst: *const c::c_char, |
| 1085 |
flags: c::c_int |
| 1086 |
) via SYS_fclonefileat -> c::c_int |
| 1087 |
} |
| 1088 |
|
| 1089 |
unsafe { |
| 1090 |
ret(fclonefileat( |
| 1091 |
srcfd, |
| 1092 |
dst_dirfd, |
| 1093 |
c_str(dst), |
| 1094 |
bitflags_bits!(flags), |
| 1095 |
)) |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
#[cfg(not(any(target_os = "espidf", target_os = "redox", target_os = "wasi")))] |
| 1100 |
pub(crate) fn chownat( |
| 1101 |
dirfd: BorrowedFd<'_>, |
| 1102 |
path: &CStr, |
| 1103 |
owner: Option<Uid>, |
| 1104 |
group: Option<Gid>, |
| 1105 |
flags: AtFlags, |
| 1106 |
) -> io::Result<()> { |
| 1107 |
unsafe { |
| 1108 |
let (ow, gr) = crate::ugid::translate_fchown_args(owner, group); |
| 1109 |
ret(c::fchownat( |
| 1110 |
borrowed_fd(dirfd), |
| 1111 |
c_str(path), |
| 1112 |
ow, |
| 1113 |
gr, |
| 1114 |
bitflags_bits!(flags), |
| 1115 |
)) |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
#[cfg(not(any( |
| 1120 |
apple, |
| 1121 |
target_os = "espidf", |
| 1122 |
target_os = "redox", |
| 1123 |
target_os = "vita", |
| 1124 |
target_os = "wasi" |
| 1125 |
)))] |
| 1126 |
pub(crate) fn mknodat( |
| 1127 |
dirfd: BorrowedFd<'_>, |
| 1128 |
path: &CStr, |
| 1129 |
file_type: FileType, |
| 1130 |
mode: Mode, |
| 1131 |
dev: Dev, |
| 1132 |
) -> io::Result<()> { |
| 1133 |
unsafe { |
| 1134 |
ret(c::mknodat( |
| 1135 |
borrowed_fd(dirfd), |
| 1136 |
c_str(path), |
| 1137 |
(mode.bits() | file_type.as_raw_mode()) as c::mode_t, |
| 1138 |
dev.try_into().map_err(|_e| io::Errno::PERM)?, |
| 1139 |
)) |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
#[cfg(linux_kernel)] |
| 1144 |
pub(crate) fn copy_file_range( |
| 1145 |
fd_in: BorrowedFd<'_>, |
| 1146 |
off_in: Option<&mut u64>, |
| 1147 |
fd_out: BorrowedFd<'_>, |
| 1148 |
off_out: Option<&mut u64>, |
| 1149 |
len: usize, |
| 1150 |
) -> io::Result<usize> { |
| 1151 |
syscall! { |
| 1152 |
fn copy_file_range( |
| 1153 |
fd_in: c::c_int, |
| 1154 |
off_in: *mut c::loff_t, |
| 1155 |
fd_out: c::c_int, |
| 1156 |
off_out: *mut c::loff_t, |
| 1157 |
len: usize, |
| 1158 |
flags: c::c_uint |
| 1159 |
) via SYS_copy_file_range -> c::ssize_t |
| 1160 |
} |
| 1161 |
|
| 1162 |
let mut off_in_val: c::loff_t = 0; |
| 1163 |
let mut off_out_val: c::loff_t = 0; |
| 1164 |
// Silently cast; we'll get `EINVAL` if the value is negative. |
| 1165 |
let off_in_ptr = if let Some(off_in) = &off_in { |
| 1166 |
off_in_val = **off_in as i64; |
| 1167 |
&mut off_in_val |
| 1168 |
} else { |
| 1169 |
null_mut() |
| 1170 |
}; |
| 1171 |
let off_out_ptr = if let Some(off_out) = &off_out { |
| 1172 |
off_out_val = **off_out as i64; |
| 1173 |
&mut off_out_val |
| 1174 |
} else { |
| 1175 |
null_mut() |
| 1176 |
}; |
| 1177 |
let copied = unsafe { |
| 1178 |
ret_usize(copy_file_range( |
| 1179 |
borrowed_fd(fd_in), |
| 1180 |
off_in_ptr, |
| 1181 |
borrowed_fd(fd_out), |
| 1182 |
off_out_ptr, |
| 1183 |
len, |
| 1184 |
0, // no flags are defined yet |
| 1185 |
))? |
| 1186 |
}; |
| 1187 |
if let Some(off_in) = off_in { |
| 1188 |
*off_in = off_in_val as u64; |
| 1189 |
} |
| 1190 |
if let Some(off_out) = off_out { |
| 1191 |
*off_out = off_out_val as u64; |
| 1192 |
} |
| 1193 |
Ok(copied) |
| 1194 |
} |
| 1195 |
|
| 1196 |
#[cfg(not(any( |
| 1197 |
apple, |
| 1198 |
netbsdlike, |
| 1199 |
solarish, |
| 1200 |
target_os = "dragonfly", |
| 1201 |
target_os = "espidf", |
| 1202 |
target_os = "haiku", |
| 1203 |
target_os = "redox", |
| 1204 |
target_os = "vita", |
| 1205 |
)))] |
| 1206 |
pub(crate) fn fadvise(fd: BorrowedFd<'_>, offset: u64, len: u64, advice: Advice) -> io::Result<()> { |
| 1207 |
let offset = offset as i64; |
| 1208 |
let len = len as i64; |
| 1209 |
|
| 1210 |
// FreeBSD returns `EINVAL` on invalid offsets; emulate the POSIX behavior. |
| 1211 |
#[cfg(target_os = "freebsd")] |
| 1212 |
let offset = if (offset as i64) < 0 { |
| 1213 |
i64::MAX |
| 1214 |
} else { |
| 1215 |
offset |
| 1216 |
}; |
| 1217 |
|
| 1218 |
// FreeBSD returns `EINVAL` on overflow; emulate the POSIX behavior. |
| 1219 |
#[cfg(target_os = "freebsd")] |
| 1220 |
let len = if len > 0 && offset.checked_add(len).is_none() { |
| 1221 |
i64::MAX - offset |
| 1222 |
} else { |
| 1223 |
len |
| 1224 |
}; |
| 1225 |
|
| 1226 |
let err = unsafe { c::posix_fadvise(borrowed_fd(fd), offset, len, advice as c::c_int) }; |
| 1227 |
|
| 1228 |
// `posix_fadvise` returns its error status rather than using `errno`. |
| 1229 |
if err == 0 { |
| 1230 |
Ok(()) |
| 1231 |
} else { |
| 1232 |
Err(io::Errno(err)) |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
pub(crate) fn fcntl_getfl(fd: BorrowedFd<'_>) -> io::Result<OFlags> { |
| 1237 |
let flags = unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_GETFL))? }; |
| 1238 |
Ok(OFlags::from_bits_retain(bitcast!(flags))) |
| 1239 |
} |
| 1240 |
|
| 1241 |
pub(crate) fn fcntl_setfl(fd: BorrowedFd<'_>, flags: OFlags) -> io::Result<()> { |
| 1242 |
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_SETFL, flags.bits())) } |
| 1243 |
} |
| 1244 |
|
| 1245 |
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))] |
| 1246 |
pub(crate) fn fcntl_get_seals(fd: BorrowedFd<'_>) -> io::Result<SealFlags> { |
| 1247 |
let flags = unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_GET_SEALS))? }; |
| 1248 |
Ok(SealFlags::from_bits_retain(bitcast!(flags))) |
| 1249 |
} |
| 1250 |
|
| 1251 |
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))] |
| 1252 |
pub(crate) fn fcntl_add_seals(fd: BorrowedFd<'_>, seals: SealFlags) -> io::Result<()> { |
| 1253 |
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_ADD_SEALS, seals.bits())) } |
| 1254 |
} |
| 1255 |
|
| 1256 |
#[cfg(not(any( |
| 1257 |
target_os = "emscripten", |
| 1258 |
target_os = "espidf", |
| 1259 |
target_os = "fuchsia", |
| 1260 |
target_os = "redox", |
| 1261 |
target_os = "vita", |
| 1262 |
target_os = "wasi" |
| 1263 |
)))] |
| 1264 |
#[inline] |
| 1265 |
pub(crate) fn fcntl_lock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> { |
| 1266 |
use c::{flock, F_RDLCK, F_SETLK, F_SETLKW, F_UNLCK, F_WRLCK, SEEK_SET}; |
| 1267 |
|
| 1268 |
let (cmd, l_type) = match operation { |
| 1269 |
FlockOperation::LockShared => (F_SETLKW, F_RDLCK), |
| 1270 |
FlockOperation::LockExclusive => (F_SETLKW, F_WRLCK), |
| 1271 |
FlockOperation::Unlock => (F_SETLKW, F_UNLCK), |
| 1272 |
FlockOperation::NonBlockingLockShared => (F_SETLK, F_RDLCK), |
| 1273 |
FlockOperation::NonBlockingLockExclusive => (F_SETLK, F_WRLCK), |
| 1274 |
FlockOperation::NonBlockingUnlock => (F_SETLK, F_UNLCK), |
| 1275 |
}; |
| 1276 |
|
| 1277 |
unsafe { |
| 1278 |
let mut lock: flock = core::mem::zeroed(); |
| 1279 |
lock.l_type = l_type as _; |
| 1280 |
|
| 1281 |
// When `l_len` is zero, this locks all the bytes from |
| 1282 |
// `l_whence`/`l_start` to the end of the file, even as the |
| 1283 |
// file grows dynamically. |
| 1284 |
lock.l_whence = SEEK_SET as _; |
| 1285 |
lock.l_start = 0; |
| 1286 |
lock.l_len = 0; |
| 1287 |
|
| 1288 |
ret(c::fcntl(borrowed_fd(fd), cmd, &lock)) |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> { |
| 1293 |
let (whence, offset) = match pos { |
| 1294 |
SeekFrom::Start(pos) => { |
| 1295 |
let pos: u64 = pos; |
| 1296 |
// Silently cast; we'll get `EINVAL` if the value is negative. |
| 1297 |
(c::SEEK_SET, pos as i64) |
| 1298 |
} |
| 1299 |
SeekFrom::End(offset) => (c::SEEK_END, offset), |
| 1300 |
SeekFrom::Current(offset) => (c::SEEK_CUR, offset), |
| 1301 |
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))] |
| 1302 |
SeekFrom::Data(offset) => (c::SEEK_DATA, offset), |
| 1303 |
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))] |
| 1304 |
SeekFrom::Hole(offset) => (c::SEEK_HOLE, offset), |
| 1305 |
}; |
| 1306 |
|
| 1307 |
// ESP-IDF and Vita don't support 64-bit offsets. |
| 1308 |
#[cfg(any(target_os = "espidf", target_os = "vita"))] |
| 1309 |
let offset: i32 = offset.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1310 |
|
| 1311 |
let offset = unsafe { ret_off_t(c::lseek(borrowed_fd(fd), offset, whence))? }; |
| 1312 |
Ok(offset as u64) |
| 1313 |
} |
| 1314 |
|
| 1315 |
pub(crate) fn tell(fd: BorrowedFd<'_>) -> io::Result<u64> { |
| 1316 |
let offset = unsafe { ret_off_t(c::lseek(borrowed_fd(fd), 0, c::SEEK_CUR))? }; |
| 1317 |
Ok(offset as u64) |
| 1318 |
} |
| 1319 |
|
| 1320 |
#[cfg(not(any(linux_kernel, target_os = "wasi")))] |
| 1321 |
pub(crate) fn fchmod(fd: BorrowedFd<'_>, mode: Mode) -> io::Result<()> { |
| 1322 |
unsafe { ret(c::fchmod(borrowed_fd(fd), bitflags_bits!(mode))) } |
| 1323 |
} |
| 1324 |
|
| 1325 |
#[cfg(linux_kernel)] |
| 1326 |
pub(crate) fn fchmod(fd: BorrowedFd<'_>, mode: Mode) -> io::Result<()> { |
| 1327 |
// Use `c::syscall` rather than `c::fchmod` because some libc |
| 1328 |
// implementations, such as musl, add extra logic to `fchmod` to emulate |
| 1329 |
// support for `O_PATH`, which uses `/proc` outside our control and |
| 1330 |
// interferes with our own use of `O_PATH`. |
| 1331 |
syscall! { |
| 1332 |
fn fchmod( |
| 1333 |
fd: c::c_int, |
| 1334 |
mode: c::mode_t |
| 1335 |
) via SYS_fchmod -> c::c_int |
| 1336 |
} |
| 1337 |
unsafe { ret(fchmod(borrowed_fd(fd), mode.bits() as c::mode_t)) } |
| 1338 |
} |
| 1339 |
|
| 1340 |
#[cfg(not(target_os = "wasi"))] |
| 1341 |
pub(crate) fn chown(path: &CStr, owner: Option<Uid>, group: Option<Gid>) -> io::Result<()> { |
| 1342 |
unsafe { |
| 1343 |
let (ow, gr) = crate::ugid::translate_fchown_args(owner, group); |
| 1344 |
ret(c::chown(c_str(path), ow, gr)) |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
#[cfg(linux_kernel)] |
| 1349 |
pub(crate) fn fchown(fd: BorrowedFd<'_>, owner: Option<Uid>, group: Option<Gid>) -> io::Result<()> { |
| 1350 |
// Use `c::syscall` rather than `c::fchown` because some libc |
| 1351 |
// implementations, such as musl, add extra logic to `fchown` to emulate |
| 1352 |
// support for `O_PATH`, which uses `/proc` outside our control and |
| 1353 |
// interferes with our own use of `O_PATH`. |
| 1354 |
syscall! { |
| 1355 |
fn fchown( |
| 1356 |
fd: c::c_int, |
| 1357 |
owner: c::uid_t, |
| 1358 |
group: c::gid_t |
| 1359 |
) via SYS_fchown -> c::c_int |
| 1360 |
} |
| 1361 |
unsafe { |
| 1362 |
let (ow, gr) = crate::ugid::translate_fchown_args(owner, group); |
| 1363 |
ret(fchown(borrowed_fd(fd), ow, gr)) |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
#[cfg(not(any(linux_kernel, target_os = "wasi")))] |
| 1368 |
pub(crate) fn fchown(fd: BorrowedFd<'_>, owner: Option<Uid>, group: Option<Gid>) -> io::Result<()> { |
| 1369 |
unsafe { |
| 1370 |
let (ow, gr) = crate::ugid::translate_fchown_args(owner, group); |
| 1371 |
ret(c::fchown(borrowed_fd(fd), ow, gr)) |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
#[cfg(not(any( |
| 1376 |
target_os = "espidf", |
| 1377 |
target_os = "solaris", |
| 1378 |
target_os = "vita", |
| 1379 |
target_os = "wasi" |
| 1380 |
)))] |
| 1381 |
pub(crate) fn flock(fd: BorrowedFd<'_>, operation: FlockOperation) -> io::Result<()> { |
| 1382 |
unsafe { ret(c::flock(borrowed_fd(fd), operation as c::c_int)) } |
| 1383 |
} |
| 1384 |
|
| 1385 |
#[cfg(linux_kernel)] |
| 1386 |
pub(crate) fn syncfs(fd: BorrowedFd<'_>) -> io::Result<()> { |
| 1387 |
// Some versions of Android libc lack a `syncfs` function. |
| 1388 |
#[cfg(target_os = "android")] |
| 1389 |
syscall! { |
| 1390 |
fn syncfs(fd: c::c_int) via SYS_syncfs -> c::c_int |
| 1391 |
} |
| 1392 |
|
| 1393 |
// `syncfs` was added to glibc in 2.20. |
| 1394 |
#[cfg(not(target_os = "android"))] |
| 1395 |
weak_or_syscall! { |
| 1396 |
fn syncfs(fd: c::c_int) via SYS_syncfs -> c::c_int |
| 1397 |
} |
| 1398 |
|
| 1399 |
unsafe { ret(syncfs(borrowed_fd(fd))) } |
| 1400 |
} |
| 1401 |
|
| 1402 |
#[cfg(not(any( |
| 1403 |
target_os = "espidf", |
| 1404 |
target_os = "redox", |
| 1405 |
target_os = "vita", |
| 1406 |
target_os = "wasi" |
| 1407 |
)))] |
| 1408 |
pub(crate) fn sync() { |
| 1409 |
unsafe { c::sync() } |
| 1410 |
} |
| 1411 |
|
| 1412 |
pub(crate) fn fstat(fd: BorrowedFd<'_>) -> io::Result<Stat> { |
| 1413 |
// 32-bit and mips64 Linux: `struct stat64` is not y2038 compatible; use |
| 1414 |
// `statx`. |
| 1415 |
// |
| 1416 |
// And, some old platforms don't support `statx`, and some fail with a |
| 1417 |
// confusing error code, so we call `crate::fs::statx` to handle that. If |
| 1418 |
// `statx` isn't available, fall back to the buggy system call. |
| 1419 |
#[cfg(all( |
| 1420 |
linux_kernel, |
| 1421 |
any( |
| 1422 |
target_pointer_width = "32", |
| 1423 |
target_arch = "mips64", |
| 1424 |
target_arch = "mips64r6" |
| 1425 |
) |
| 1426 |
))] |
| 1427 |
{ |
| 1428 |
match crate::fs::statx(fd, cstr!(""), AtFlags::EMPTY_PATH, StatxFlags::BASIC_STATS) { |
| 1429 |
Ok(x) => statx_to_stat(x), |
| 1430 |
Err(io::Errno::NOSYS) => fstat_old(fd), |
| 1431 |
Err(err) => Err(err), |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
// Main version: libc is y2038 safe. Or, the platform is not y2038 safe and |
| 1436 |
// there's nothing practical we can do. |
| 1437 |
#[cfg(not(all( |
| 1438 |
linux_kernel, |
| 1439 |
any( |
| 1440 |
target_pointer_width = "32", |
| 1441 |
target_arch = "mips64", |
| 1442 |
target_arch = "mips64r6" |
| 1443 |
) |
| 1444 |
)))] |
| 1445 |
unsafe { |
| 1446 |
let mut stat = MaybeUninit::<Stat>::uninit(); |
| 1447 |
ret(c::fstat(borrowed_fd(fd), stat.as_mut_ptr()))?; |
| 1448 |
Ok(stat.assume_init()) |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
#[cfg(all( |
| 1453 |
linux_kernel, |
| 1454 |
any( |
| 1455 |
target_pointer_width = "32", |
| 1456 |
target_arch = "mips64", |
| 1457 |
target_arch = "mips64r6" |
| 1458 |
) |
| 1459 |
))] |
| 1460 |
fn fstat_old(fd: BorrowedFd<'_>) -> io::Result<Stat> { |
| 1461 |
unsafe { |
| 1462 |
let mut result = MaybeUninit::<c::stat64>::uninit(); |
| 1463 |
ret(c::fstat(borrowed_fd(fd), result.as_mut_ptr()))?; |
| 1464 |
stat64_to_stat(result.assume_init()) |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
#[cfg(not(any( |
| 1469 |
solarish, |
| 1470 |
target_os = "espidf", |
| 1471 |
target_os = "haiku", |
| 1472 |
target_os = "netbsd", |
| 1473 |
target_os = "nto", |
| 1474 |
target_os = "redox", |
| 1475 |
target_os = "vita", |
| 1476 |
target_os = "wasi", |
| 1477 |
)))] |
| 1478 |
pub(crate) fn fstatfs(fd: BorrowedFd<'_>) -> io::Result<StatFs> { |
| 1479 |
let mut statfs = MaybeUninit::<StatFs>::uninit(); |
| 1480 |
unsafe { |
| 1481 |
ret(c::fstatfs(borrowed_fd(fd), statfs.as_mut_ptr()))?; |
| 1482 |
Ok(statfs.assume_init()) |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))] |
| 1487 |
pub(crate) fn fstatvfs(fd: BorrowedFd<'_>) -> io::Result<StatVfs> { |
| 1488 |
let mut statvfs = MaybeUninit::<c::statvfs>::uninit(); |
| 1489 |
unsafe { |
| 1490 |
ret(c::fstatvfs(borrowed_fd(fd), statvfs.as_mut_ptr()))?; |
| 1491 |
Ok(libc_statvfs_to_statvfs(statvfs.assume_init())) |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))] |
| 1496 |
fn libc_statvfs_to_statvfs(from: c::statvfs) -> StatVfs { |
| 1497 |
StatVfs { |
| 1498 |
f_bsize: from.f_bsize as u64, |
| 1499 |
f_frsize: from.f_frsize as u64, |
| 1500 |
f_blocks: from.f_blocks as u64, |
| 1501 |
f_bfree: from.f_bfree as u64, |
| 1502 |
f_bavail: from.f_bavail as u64, |
| 1503 |
f_files: from.f_files as u64, |
| 1504 |
f_ffree: from.f_ffree as u64, |
| 1505 |
f_favail: from.f_ffree as u64, |
| 1506 |
#[cfg(not(target_os = "aix"))] |
| 1507 |
f_fsid: from.f_fsid as u64, |
| 1508 |
#[cfg(target_os = "aix")] |
| 1509 |
f_fsid: ((from.f_fsid.val[0] as u64) << 32) | from.f_fsid.val[1], |
| 1510 |
f_flag: StatVfsMountFlags::from_bits_retain(from.f_flag as u64), |
| 1511 |
f_namemax: from.f_namemax as u64, |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
#[cfg(not(any(target_os = "espidf", target_os = "vita")))] |
| 1516 |
pub(crate) fn futimens(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()> { |
| 1517 |
// Old 32-bit version: libc has `futimens` but it is not y2038 safe by |
| 1518 |
// default. But there may be a `__futimens64` we can use. |
| 1519 |
#[cfg(all(fix_y2038, not(apple)))] |
| 1520 |
{ |
| 1521 |
#[cfg(target_env = "gnu")] |
| 1522 |
if let Some(libc_futimens) = __futimens64.get() { |
| 1523 |
let libc_times: [LibcTimespec; 2] = [ |
| 1524 |
times.last_access.clone().into(), |
| 1525 |
times.last_modification.clone().into(), |
| 1526 |
]; |
| 1527 |
|
| 1528 |
unsafe { |
| 1529 |
return ret(libc_futimens(borrowed_fd(fd), libc_times.as_ptr())); |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
futimens_old(fd, times) |
| 1534 |
} |
| 1535 |
|
| 1536 |
// Main version: libc is y2038 safe and has `futimens`. Or, the platform |
| 1537 |
// is not y2038 safe and there's nothing practical we can do. |
| 1538 |
#[cfg(not(any(apple, fix_y2038)))] |
| 1539 |
unsafe { |
| 1540 |
use crate::utils::as_ptr; |
| 1541 |
|
| 1542 |
ret(c::futimens(borrowed_fd(fd), as_ptr(times).cast())) |
| 1543 |
} |
| 1544 |
|
| 1545 |
// Apple version: `futimens` was introduced in macOS 10.13. |
| 1546 |
#[cfg(apple)] |
| 1547 |
unsafe { |
| 1548 |
use crate::utils::as_ptr; |
| 1549 |
|
| 1550 |
// ABI details. |
| 1551 |
weak! { |
| 1552 |
fn futimens(c::c_int, *const c::timespec) -> c::c_int |
| 1553 |
} |
| 1554 |
extern "C" { |
| 1555 |
fn fsetattrlist( |
| 1556 |
fd: c::c_int, |
| 1557 |
attr_list: *const Attrlist, |
| 1558 |
attr_buf: *const c::c_void, |
| 1559 |
attr_buf_size: c::size_t, |
| 1560 |
options: c::c_ulong, |
| 1561 |
) -> c::c_int; |
| 1562 |
} |
| 1563 |
|
| 1564 |
// If we have `futimens`, use it. |
| 1565 |
if let Some(have_futimens) = futimens.get() { |
| 1566 |
return ret(have_futimens(borrowed_fd(fd), as_ptr(times).cast())); |
| 1567 |
} |
| 1568 |
|
| 1569 |
// Otherwise use `fsetattrlist`. |
| 1570 |
let (attrbuf_size, times, attrs) = times_to_attrlist(times)?; |
| 1571 |
|
| 1572 |
ret(fsetattrlist( |
| 1573 |
borrowed_fd(fd), |
| 1574 |
&attrs, |
| 1575 |
as_ptr(×).cast(), |
| 1576 |
attrbuf_size, |
| 1577 |
0, |
| 1578 |
)) |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
#[cfg(all(fix_y2038, not(apple)))] |
| 1583 |
fn futimens_old(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()> { |
| 1584 |
let old_times = [ |
| 1585 |
c::timespec { |
| 1586 |
tv_sec: times |
| 1587 |
.last_access |
| 1588 |
.tv_sec |
| 1589 |
.try_into() |
| 1590 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1591 |
tv_nsec: times |
| 1592 |
.last_access |
| 1593 |
.tv_nsec |
| 1594 |
.try_into() |
| 1595 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1596 |
}, |
| 1597 |
c::timespec { |
| 1598 |
tv_sec: times |
| 1599 |
.last_modification |
| 1600 |
.tv_sec |
| 1601 |
.try_into() |
| 1602 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1603 |
tv_nsec: times |
| 1604 |
.last_modification |
| 1605 |
.tv_nsec |
| 1606 |
.try_into() |
| 1607 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1608 |
}, |
| 1609 |
]; |
| 1610 |
|
| 1611 |
unsafe { ret(c::futimens(borrowed_fd(fd), old_times.as_ptr())) } |
| 1612 |
} |
| 1613 |
|
| 1614 |
#[cfg(not(any( |
| 1615 |
apple, |
| 1616 |
netbsdlike, |
| 1617 |
solarish, |
| 1618 |
target_os = "dragonfly", |
| 1619 |
target_os = "espidf", |
| 1620 |
target_os = "nto", |
| 1621 |
target_os = "redox", |
| 1622 |
target_os = "vita", |
| 1623 |
)))] |
| 1624 |
pub(crate) fn fallocate( |
| 1625 |
fd: BorrowedFd<'_>, |
| 1626 |
mode: FallocateFlags, |
| 1627 |
offset: u64, |
| 1628 |
len: u64, |
| 1629 |
) -> io::Result<()> { |
| 1630 |
// Silently cast; we'll get `EINVAL` if the value is negative. |
| 1631 |
let offset = offset as i64; |
| 1632 |
let len = len as i64; |
| 1633 |
|
| 1634 |
#[cfg(any(linux_kernel, target_os = "fuchsia"))] |
| 1635 |
unsafe { |
| 1636 |
ret(c::fallocate( |
| 1637 |
borrowed_fd(fd), |
| 1638 |
bitflags_bits!(mode), |
| 1639 |
offset, |
| 1640 |
len, |
| 1641 |
)) |
| 1642 |
} |
| 1643 |
|
| 1644 |
#[cfg(not(any(linux_kernel, target_os = "fuchsia")))] |
| 1645 |
{ |
| 1646 |
assert!(mode.is_empty()); |
| 1647 |
let err = unsafe { c::posix_fallocate(borrowed_fd(fd), offset, len) }; |
| 1648 |
|
| 1649 |
// `posix_fallocate` returns its error status rather than using |
| 1650 |
// `errno`. |
| 1651 |
if err == 0 { |
| 1652 |
Ok(()) |
| 1653 |
} else { |
| 1654 |
Err(io::Errno(err)) |
| 1655 |
} |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
#[cfg(apple)] |
| 1660 |
pub(crate) fn fallocate( |
| 1661 |
fd: BorrowedFd<'_>, |
| 1662 |
mode: FallocateFlags, |
| 1663 |
offset: u64, |
| 1664 |
len: u64, |
| 1665 |
) -> io::Result<()> { |
| 1666 |
let offset: i64 = offset.try_into().map_err(|_e| io::Errno::INVAL)?; |
| 1667 |
let len = len as i64; |
| 1668 |
|
| 1669 |
assert!(mode.is_empty()); |
| 1670 |
|
| 1671 |
let new_len = offset.checked_add(len).ok_or(io::Errno::FBIG)?; |
| 1672 |
let mut store = c::fstore_t { |
| 1673 |
fst_flags: c::F_ALLOCATECONTIG, |
| 1674 |
fst_posmode: c::F_PEOFPOSMODE, |
| 1675 |
fst_offset: 0, |
| 1676 |
fst_length: new_len, |
| 1677 |
fst_bytesalloc: 0, |
| 1678 |
}; |
| 1679 |
unsafe { |
| 1680 |
if c::fcntl(borrowed_fd(fd), c::F_PREALLOCATE, &store) == -1 { |
| 1681 |
// Unable to allocate contiguous disk space; attempt to allocate |
| 1682 |
// non-contiguously. |
| 1683 |
store.fst_flags = c::F_ALLOCATEALL; |
| 1684 |
let _ = ret_c_int(c::fcntl(borrowed_fd(fd), c::F_PREALLOCATE, &store))?; |
| 1685 |
} |
| 1686 |
ret(c::ftruncate(borrowed_fd(fd), new_len)) |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
pub(crate) fn fsync(fd: BorrowedFd<'_>) -> io::Result<()> { |
| 1691 |
unsafe { ret(c::fsync(borrowed_fd(fd))) } |
| 1692 |
} |
| 1693 |
|
| 1694 |
#[cfg(not(any( |
| 1695 |
apple, |
| 1696 |
target_os = "dragonfly", |
| 1697 |
target_os = "espidf", |
| 1698 |
target_os = "haiku", |
| 1699 |
target_os = "redox", |
| 1700 |
target_os = "vita", |
| 1701 |
)))] |
| 1702 |
pub(crate) fn fdatasync(fd: BorrowedFd<'_>) -> io::Result<()> { |
| 1703 |
unsafe { ret(c::fdatasync(borrowed_fd(fd))) } |
| 1704 |
} |
| 1705 |
|
| 1706 |
pub(crate) fn ftruncate(fd: BorrowedFd<'_>, length: u64) -> io::Result<()> { |
| 1707 |
let length = length.try_into().map_err(|_overflow_err| io::Errno::FBIG)?; |
| 1708 |
unsafe { ret(c::ftruncate(borrowed_fd(fd), length)) } |
| 1709 |
} |
| 1710 |
|
| 1711 |
#[cfg(any(linux_kernel, target_os = "freebsd"))] |
| 1712 |
pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result<OwnedFd> { |
| 1713 |
#[cfg(target_os = "freebsd")] |
| 1714 |
weakcall! { |
| 1715 |
fn memfd_create( |
| 1716 |
name: *const c::c_char, |
| 1717 |
flags: c::c_uint |
| 1718 |
) -> c::c_int |
| 1719 |
} |
| 1720 |
|
| 1721 |
#[cfg(linux_kernel)] |
| 1722 |
weak_or_syscall! { |
| 1723 |
fn memfd_create( |
| 1724 |
name: *const c::c_char, |
| 1725 |
flags: c::c_uint |
| 1726 |
) via SYS_memfd_create -> c::c_int |
| 1727 |
} |
| 1728 |
|
| 1729 |
unsafe { ret_owned_fd(memfd_create(c_str(name), bitflags_bits!(flags))) } |
| 1730 |
} |
| 1731 |
|
| 1732 |
#[cfg(linux_kernel)] |
| 1733 |
pub(crate) fn openat2( |
| 1734 |
dirfd: BorrowedFd<'_>, |
| 1735 |
path: &CStr, |
| 1736 |
oflags: OFlags, |
| 1737 |
mode: Mode, |
| 1738 |
resolve: ResolveFlags, |
| 1739 |
) -> io::Result<OwnedFd> { |
| 1740 |
use linux_raw_sys::general::open_how; |
| 1741 |
|
| 1742 |
syscall! { |
| 1743 |
fn openat2( |
| 1744 |
base_dirfd: c::c_int, |
| 1745 |
pathname: *const c::c_char, |
| 1746 |
how: *mut open_how, |
| 1747 |
size: usize |
| 1748 |
) via SYS_OPENAT2 -> c::c_int |
| 1749 |
} |
| 1750 |
|
| 1751 |
let oflags = oflags.bits(); |
| 1752 |
let mut open_how = open_how { |
| 1753 |
flags: u64::from(oflags), |
| 1754 |
mode: u64::from(mode.bits()), |
| 1755 |
resolve: resolve.bits(), |
| 1756 |
}; |
| 1757 |
|
| 1758 |
unsafe { |
| 1759 |
ret_owned_fd(openat2( |
| 1760 |
borrowed_fd(dirfd), |
| 1761 |
c_str(path), |
| 1762 |
&mut open_how, |
| 1763 |
size_of::<open_how>(), |
| 1764 |
)) |
| 1765 |
} |
| 1766 |
} |
| 1767 |
#[cfg(all(linux_kernel, target_pointer_width = "32"))] |
| 1768 |
const SYS_OPENAT2: i32 = 437; |
| 1769 |
#[cfg(all(linux_kernel, target_pointer_width = "64"))] |
| 1770 |
const SYS_OPENAT2: i64 = 437; |
| 1771 |
|
| 1772 |
#[cfg(target_os = "linux")] |
| 1773 |
pub(crate) fn sendfile( |
| 1774 |
out_fd: BorrowedFd<'_>, |
| 1775 |
in_fd: BorrowedFd<'_>, |
| 1776 |
offset: Option<&mut u64>, |
| 1777 |
count: usize, |
| 1778 |
) -> io::Result<usize> { |
| 1779 |
unsafe { |
| 1780 |
ret_usize(c::sendfile64( |
| 1781 |
borrowed_fd(out_fd), |
| 1782 |
borrowed_fd(in_fd), |
| 1783 |
offset.map_or(null_mut(), crate::utils::as_mut_ptr).cast(), |
| 1784 |
count, |
| 1785 |
)) |
| 1786 |
} |
| 1787 |
} |
| 1788 |
|
| 1789 |
/// Convert from a Linux `statx` value to rustix's `Stat`. |
| 1790 |
#[cfg(all(linux_kernel, target_pointer_width = "32"))] |
| 1791 |
#[allow(deprecated)] // for `st_[amc]time` u64->i64 transition |
| 1792 |
fn statx_to_stat(x: crate::fs::Statx) -> io::Result<Stat> { |
| 1793 |
Ok(Stat { |
| 1794 |
st_dev: crate::fs::makedev(x.stx_dev_major, x.stx_dev_minor).into(), |
| 1795 |
st_mode: x.stx_mode.into(), |
| 1796 |
st_nlink: x.stx_nlink.into(), |
| 1797 |
st_uid: x.stx_uid.into(), |
| 1798 |
st_gid: x.stx_gid.into(), |
| 1799 |
st_rdev: crate::fs::makedev(x.stx_rdev_major, x.stx_rdev_minor).into(), |
| 1800 |
st_size: x.stx_size.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1801 |
st_blksize: x.stx_blksize.into(), |
| 1802 |
st_blocks: x.stx_blocks.into(), |
| 1803 |
st_atime: bitcast!(i64::from(x.stx_atime.tv_sec)), |
| 1804 |
st_atime_nsec: x.stx_atime.tv_nsec as _, |
| 1805 |
st_mtime: bitcast!(i64::from(x.stx_mtime.tv_sec)), |
| 1806 |
st_mtime_nsec: x.stx_mtime.tv_nsec as _, |
| 1807 |
st_ctime: bitcast!(i64::from(x.stx_ctime.tv_sec)), |
| 1808 |
st_ctime_nsec: x.stx_ctime.tv_nsec as _, |
| 1809 |
st_ino: x.stx_ino.into(), |
| 1810 |
}) |
| 1811 |
} |
| 1812 |
|
| 1813 |
/// Convert from a Linux `statx` value to rustix's `Stat`. |
| 1814 |
/// |
| 1815 |
/// mips64' `struct stat64` in libc has private fields, and `stx_blocks` |
| 1816 |
#[cfg(all(linux_kernel, any(target_arch = "mips64", target_arch = "mips64r6")))] |
| 1817 |
fn statx_to_stat(x: crate::fs::Statx) -> io::Result<Stat> { |
| 1818 |
let mut result: Stat = unsafe { core::mem::zeroed() }; |
| 1819 |
|
| 1820 |
result.st_dev = crate::fs::makedev(x.stx_dev_major, x.stx_dev_minor); |
| 1821 |
result.st_mode = x.stx_mode.into(); |
| 1822 |
result.st_nlink = x.stx_nlink.into(); |
| 1823 |
result.st_uid = x.stx_uid.into(); |
| 1824 |
result.st_gid = x.stx_gid.into(); |
| 1825 |
result.st_rdev = crate::fs::makedev(x.stx_rdev_major, x.stx_rdev_minor); |
| 1826 |
result.st_size = x.stx_size.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1827 |
result.st_blksize = x.stx_blksize.into(); |
| 1828 |
result.st_blocks = x.stx_blocks.try_into().map_err(|_e| io::Errno::OVERFLOW)?; |
| 1829 |
result.st_atime = bitcast!(i64::from(x.stx_atime.tv_sec)); |
| 1830 |
result.st_atime_nsec = x.stx_atime.tv_nsec as _; |
| 1831 |
result.st_mtime = bitcast!(i64::from(x.stx_mtime.tv_sec)); |
| 1832 |
result.st_mtime_nsec = x.stx_mtime.tv_nsec as _; |
| 1833 |
result.st_ctime = bitcast!(i64::from(x.stx_ctime.tv_sec)); |
| 1834 |
result.st_ctime_nsec = x.stx_ctime.tv_nsec as _; |
| 1835 |
result.st_ino = x.stx_ino.into(); |
| 1836 |
|
| 1837 |
Ok(result) |
| 1838 |
} |
| 1839 |
|
| 1840 |
/// Convert from a Linux `stat64` value to rustix's `Stat`. |
| 1841 |
#[cfg(all(linux_kernel, target_pointer_width = "32"))] |
| 1842 |
#[allow(deprecated)] // for `st_[amc]time` u64->i64 transition |
| 1843 |
fn stat64_to_stat(s64: c::stat64) -> io::Result<Stat> { |
| 1844 |
Ok(Stat { |
| 1845 |
st_dev: s64.st_dev.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1846 |
st_mode: s64.st_mode.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1847 |
st_nlink: s64.st_nlink.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1848 |
st_uid: s64.st_uid.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1849 |
st_gid: s64.st_gid.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1850 |
st_rdev: s64.st_rdev.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1851 |
st_size: s64.st_size.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1852 |
st_blksize: s64.st_blksize.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1853 |
st_blocks: s64.st_blocks.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1854 |
st_atime: bitcast!(i64::from(s64.st_atime)), |
| 1855 |
st_atime_nsec: s64 |
| 1856 |
.st_atime_nsec |
| 1857 |
.try_into() |
| 1858 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1859 |
st_mtime: bitcast!(i64::from(s64.st_mtime)), |
| 1860 |
st_mtime_nsec: s64 |
| 1861 |
.st_mtime_nsec |
| 1862 |
.try_into() |
| 1863 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1864 |
st_ctime: bitcast!(i64::from(s64.st_ctime)), |
| 1865 |
st_ctime_nsec: s64 |
| 1866 |
.st_ctime_nsec |
| 1867 |
.try_into() |
| 1868 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 1869 |
st_ino: s64.st_ino.try_into().map_err(|_| io::Errno::OVERFLOW)?, |
| 1870 |
}) |
| 1871 |
} |
| 1872 |
|
| 1873 |
/// Convert from a Linux `stat64` value to rustix's `Stat`. |
| 1874 |
/// |
| 1875 |
/// mips64' `struct stat64` in libc has private fields, and `st_blocks` has |
| 1876 |
/// type `i64`. |
| 1877 |
#[cfg(all(linux_kernel, any(target_arch = "mips64", target_arch = "mips64r6")))] |
| 1878 |
fn stat64_to_stat(s64: c::stat64) -> io::Result<Stat> { |
| 1879 |
let mut result: Stat = unsafe { core::mem::zeroed() }; |
| 1880 |
|
| 1881 |
result.st_dev = s64.st_dev.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1882 |
result.st_mode = s64.st_mode.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1883 |
result.st_nlink = s64.st_nlink.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1884 |
result.st_uid = s64.st_uid.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1885 |
result.st_gid = s64.st_gid.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1886 |
result.st_rdev = s64.st_rdev.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1887 |
result.st_size = s64.st_size.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1888 |
result.st_blksize = s64.st_blksize.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1889 |
result.st_blocks = s64.st_blocks.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1890 |
result.st_atime = i64::from(s64.st_atime) as _; |
| 1891 |
result.st_atime_nsec = s64 |
| 1892 |
.st_atime_nsec |
| 1893 |
.try_into() |
| 1894 |
.map_err(|_| io::Errno::OVERFLOW)?; |
| 1895 |
result.st_mtime = i64::from(s64.st_mtime) as _; |
| 1896 |
result.st_mtime_nsec = s64 |
| 1897 |
.st_mtime_nsec |
| 1898 |
.try_into() |
| 1899 |
.map_err(|_| io::Errno::OVERFLOW)?; |
| 1900 |
result.st_ctime = i64::from(s64.st_ctime) as _; |
| 1901 |
result.st_ctime_nsec = s64 |
| 1902 |
.st_ctime_nsec |
| 1903 |
.try_into() |
| 1904 |
.map_err(|_| io::Errno::OVERFLOW)?; |
| 1905 |
result.st_ino = s64.st_ino.try_into().map_err(|_| io::Errno::OVERFLOW)?; |
| 1906 |
|
| 1907 |
Ok(result) |
| 1908 |
} |
| 1909 |
|
| 1910 |
#[cfg(linux_kernel)] |
| 1911 |
#[allow(non_upper_case_globals)] |
| 1912 |
mod sys { |
| 1913 |
use super::{c, BorrowedFd, Statx}; |
| 1914 |
|
| 1915 |
weak_or_syscall! { |
| 1916 |
pub(super) fn statx( |
| 1917 |
dirfd_: BorrowedFd<'_>, |
| 1918 |
path: *const c::c_char, |
| 1919 |
flags: c::c_int, |
| 1920 |
mask: c::c_uint, |
| 1921 |
buf: *mut Statx |
| 1922 |
) via SYS_statx -> c::c_int |
| 1923 |
} |
| 1924 |
} |
| 1925 |
|
| 1926 |
#[cfg(linux_kernel)] |
| 1927 |
#[allow(non_upper_case_globals)] |
| 1928 |
pub(crate) fn statx( |
| 1929 |
dirfd: BorrowedFd<'_>, |
| 1930 |
path: &CStr, |
| 1931 |
flags: AtFlags, |
| 1932 |
mask: StatxFlags, |
| 1933 |
) -> io::Result<Statx> { |
| 1934 |
// If a future Linux kernel adds more fields to `struct statx` and users |
| 1935 |
// passing flags unknown to rustix in `StatxFlags`, we could end up |
| 1936 |
// writing outside of the buffer. To prevent this possibility, we mask off |
| 1937 |
// any flags that we don't know about. |
| 1938 |
// |
| 1939 |
// This includes `STATX__RESERVED`, which has a value that we know, but |
| 1940 |
// which could take on arbitrary new meaning in the future. Linux currently |
| 1941 |
// rejects this flag with `EINVAL`, so we do the same. |
| 1942 |
// |
| 1943 |
// This doesn't rely on `STATX_ALL` because [it's deprecated] and already |
| 1944 |
// doesn't represent all the known flags. |
| 1945 |
// |
| 1946 |
// [it's deprecated]: https://patchwork.kernel.org/project/linux-fsdevel/patch/[email protected]/ |
| 1947 |
#[cfg(not(any(target_os = "android", target_env = "musl")))] |
| 1948 |
const STATX__RESERVED: u32 = c::STATX__RESERVED as u32; |
| 1949 |
#[cfg(any(target_os = "android", target_env = "musl"))] |
| 1950 |
const STATX__RESERVED: u32 = linux_raw_sys::general::STATX__RESERVED; |
| 1951 |
if (mask.bits() & STATX__RESERVED) == STATX__RESERVED { |
| 1952 |
return Err(io::Errno::INVAL); |
| 1953 |
} |
| 1954 |
let mask = mask & StatxFlags::all(); |
| 1955 |
|
| 1956 |
let mut statx_buf = MaybeUninit::<Statx>::uninit(); |
| 1957 |
unsafe { |
| 1958 |
ret(sys::statx( |
| 1959 |
dirfd, |
| 1960 |
c_str(path), |
| 1961 |
bitflags_bits!(flags), |
| 1962 |
mask.bits(), |
| 1963 |
statx_buf.as_mut_ptr(), |
| 1964 |
))?; |
| 1965 |
Ok(statx_buf.assume_init()) |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|
| 1969 |
#[cfg(linux_kernel)] |
| 1970 |
#[inline] |
| 1971 |
pub(crate) fn is_statx_available() -> bool { |
| 1972 |
unsafe { |
| 1973 |
// Call `statx` with null pointers so that if it fails for any reason |
| 1974 |
// other than `EFAULT`, we know it's not supported. |
| 1975 |
matches!( |
| 1976 |
ret(sys::statx(CWD, null(), 0, 0, null_mut())), |
| 1977 |
Err(io::Errno::FAULT) |
| 1978 |
) |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
#[cfg(apple)] |
| 1983 |
pub(crate) unsafe fn fcopyfile( |
| 1984 |
from: BorrowedFd<'_>, |
| 1985 |
to: BorrowedFd<'_>, |
| 1986 |
state: copyfile_state_t, |
| 1987 |
flags: CopyfileFlags, |
| 1988 |
) -> io::Result<()> { |
| 1989 |
extern "C" { |
| 1990 |
fn fcopyfile( |
| 1991 |
from: c::c_int, |
| 1992 |
to: c::c_int, |
| 1993 |
state: copyfile_state_t, |
| 1994 |
flags: c::c_uint, |
| 1995 |
) -> c::c_int; |
| 1996 |
} |
| 1997 |
|
| 1998 |
nonnegative_ret(fcopyfile( |
| 1999 |
borrowed_fd(from), |
| 2000 |
borrowed_fd(to), |
| 2001 |
state, |
| 2002 |
bitflags_bits!(flags), |
| 2003 |
)) |
| 2004 |
} |
| 2005 |
|
| 2006 |
#[cfg(apple)] |
| 2007 |
pub(crate) fn copyfile_state_alloc() -> io::Result<copyfile_state_t> { |
| 2008 |
extern "C" { |
| 2009 |
fn copyfile_state_alloc() -> copyfile_state_t; |
| 2010 |
} |
| 2011 |
|
| 2012 |
let result = unsafe { copyfile_state_alloc() }; |
| 2013 |
if result.0.is_null() { |
| 2014 |
Err(io::Errno::last_os_error()) |
| 2015 |
} else { |
| 2016 |
Ok(result) |
| 2017 |
} |
| 2018 |
} |
| 2019 |
|
| 2020 |
#[cfg(apple)] |
| 2021 |
pub(crate) unsafe fn copyfile_state_free(state: copyfile_state_t) -> io::Result<()> { |
| 2022 |
extern "C" { |
| 2023 |
fn copyfile_state_free(state: copyfile_state_t) -> c::c_int; |
| 2024 |
} |
| 2025 |
|
| 2026 |
nonnegative_ret(copyfile_state_free(state)) |
| 2027 |
} |
| 2028 |
|
| 2029 |
#[cfg(apple)] |
| 2030 |
const COPYFILE_STATE_COPIED: u32 = 8; |
| 2031 |
|
| 2032 |
#[cfg(apple)] |
| 2033 |
pub(crate) unsafe fn copyfile_state_get_copied(state: copyfile_state_t) -> io::Result<u64> { |
| 2034 |
let mut copied = MaybeUninit::<u64>::uninit(); |
| 2035 |
copyfile_state_get(state, COPYFILE_STATE_COPIED, copied.as_mut_ptr().cast())?; |
| 2036 |
Ok(copied.assume_init()) |
| 2037 |
} |
| 2038 |
|
| 2039 |
#[cfg(apple)] |
| 2040 |
pub(crate) unsafe fn copyfile_state_get( |
| 2041 |
state: copyfile_state_t, |
| 2042 |
flag: u32, |
| 2043 |
dst: *mut c::c_void, |
| 2044 |
) -> io::Result<()> { |
| 2045 |
extern "C" { |
| 2046 |
fn copyfile_state_get(state: copyfile_state_t, flag: u32, dst: *mut c::c_void) -> c::c_int; |
| 2047 |
} |
| 2048 |
|
| 2049 |
nonnegative_ret(copyfile_state_get(state, flag, dst)) |
| 2050 |
} |
| 2051 |
|
| 2052 |
#[cfg(all(apple, feature = "alloc"))] |
| 2053 |
pub(crate) fn getpath(fd: BorrowedFd<'_>) -> io::Result<CString> { |
| 2054 |
// The use of `PATH_MAX` is generally not encouraged, but it |
| 2055 |
// is inevitable in this case because macOS defines `fcntl` with |
| 2056 |
// `F_GETPATH` in terms of `MAXPATHLEN`, and there are no |
| 2057 |
// alternatives. If a better method is invented, it should be used |
| 2058 |
// instead. |
| 2059 |
let mut buf = vec![0; c::PATH_MAX as usize]; |
| 2060 |
|
| 2061 |
// From the [macOS `fcntl` manual page]: |
| 2062 |
// `F_GETPATH` - Get the path of the file descriptor `Fildes`. The argument |
| 2063 |
// must be a buffer of size `MAXPATHLEN` or greater. |
| 2064 |
// |
| 2065 |
// [macOS `fcntl` manual page]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html |
| 2066 |
unsafe { |
| 2067 |
ret(c::fcntl(borrowed_fd(fd), c::F_GETPATH, buf.as_mut_ptr()))?; |
| 2068 |
} |
| 2069 |
|
| 2070 |
let l = buf.iter().position(|&c| c == 0).unwrap(); |
| 2071 |
buf.truncate(l); |
| 2072 |
buf.shrink_to_fit(); |
| 2073 |
|
| 2074 |
Ok(CString::new(buf).unwrap()) |
| 2075 |
} |
| 2076 |
|
| 2077 |
#[cfg(apple)] |
| 2078 |
pub(crate) fn fcntl_rdadvise(fd: BorrowedFd<'_>, offset: u64, len: u64) -> io::Result<()> { |
| 2079 |
// From the [macOS `fcntl` manual page]: |
| 2080 |
// `F_RDADVISE` - Issue an advisory read async with no copy to user. |
| 2081 |
// |
| 2082 |
// The `F_RDADVISE` command operates on the following structure which holds |
| 2083 |
// information passed from the user to the system: |
| 2084 |
// |
| 2085 |
// ```c |
| 2086 |
// struct radvisory { |
| 2087 |
// off_t ra_offset; /* offset into the file */ |
| 2088 |
// int ra_count; /* size of the read */ |
| 2089 |
// }; |
| 2090 |
// ``` |
| 2091 |
// |
| 2092 |
// [macOS `fcntl` manual page]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html |
| 2093 |
let ra_offset = match offset.try_into() { |
| 2094 |
Ok(len) => len, |
| 2095 |
// If this conversion fails, the user is providing an offset outside |
| 2096 |
// any possible file extent, so just ignore it. |
| 2097 |
Err(_) => return Ok(()), |
| 2098 |
}; |
| 2099 |
let ra_count = match len.try_into() { |
| 2100 |
Ok(len) => len, |
| 2101 |
// If this conversion fails, the user is providing a dubiously large |
| 2102 |
// hint which is unlikely to improve performance. |
| 2103 |
Err(_) => return Ok(()), |
| 2104 |
}; |
| 2105 |
unsafe { |
| 2106 |
let radvisory = c::radvisory { |
| 2107 |
ra_offset, |
| 2108 |
ra_count, |
| 2109 |
}; |
| 2110 |
ret(c::fcntl(borrowed_fd(fd), c::F_RDADVISE, &radvisory)) |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
#[cfg(apple)] |
| 2115 |
pub(crate) fn fcntl_fullfsync(fd: BorrowedFd<'_>) -> io::Result<()> { |
| 2116 |
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_FULLFSYNC)) } |
| 2117 |
} |
| 2118 |
|
| 2119 |
#[cfg(apple)] |
| 2120 |
pub(crate) fn fcntl_nocache(fd: BorrowedFd<'_>, value: bool) -> io::Result<()> { |
| 2121 |
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_NOCACHE, value as c::c_int)) } |
| 2122 |
} |
| 2123 |
|
| 2124 |
#[cfg(apple)] |
| 2125 |
pub(crate) fn fcntl_global_nocache(fd: BorrowedFd<'_>, value: bool) -> io::Result<()> { |
| 2126 |
unsafe { |
| 2127 |
ret(c::fcntl( |
| 2128 |
borrowed_fd(fd), |
| 2129 |
c::F_GLOBAL_NOCACHE, |
| 2130 |
value as c::c_int, |
| 2131 |
)) |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
/// Convert `times` from a `futimens`/`utimensat` argument into `setattrlist` |
| 2136 |
/// arguments. |
| 2137 |
#[cfg(apple)] |
| 2138 |
fn times_to_attrlist(times: &Timestamps) -> io::Result<(c::size_t, [c::timespec; 2], Attrlist)> { |
| 2139 |
// ABI details. |
| 2140 |
const ATTR_CMN_MODTIME: u32 = 0x0000_0400; |
| 2141 |
const ATTR_CMN_ACCTIME: u32 = 0x0000_1000; |
| 2142 |
const ATTR_BIT_MAP_COUNT: u16 = 5; |
| 2143 |
|
| 2144 |
let mut times = times.clone(); |
| 2145 |
|
| 2146 |
// If we have any `UTIME_NOW` elements, replace them with the current time. |
| 2147 |
if times.last_access.tv_nsec == c::UTIME_NOW.into() |
| 2148 |
|| times.last_modification.tv_nsec == c::UTIME_NOW.into() |
| 2149 |
{ |
| 2150 |
let now = { |
| 2151 |
let mut tv = c::timeval { |
| 2152 |
tv_sec: 0, |
| 2153 |
tv_usec: 0, |
| 2154 |
}; |
| 2155 |
unsafe { |
| 2156 |
let r = c::gettimeofday(&mut tv, null_mut()); |
| 2157 |
assert_eq!(r, 0); |
| 2158 |
} |
| 2159 |
c::timespec { |
| 2160 |
tv_sec: tv.tv_sec, |
| 2161 |
tv_nsec: (tv.tv_usec * 1000) as _, |
| 2162 |
} |
| 2163 |
}; |
| 2164 |
if times.last_access.tv_nsec == c::UTIME_NOW.into() { |
| 2165 |
times.last_access = crate::timespec::Timespec { |
| 2166 |
tv_sec: now.tv_sec.into(), |
| 2167 |
tv_nsec: now.tv_nsec as _, |
| 2168 |
}; |
| 2169 |
} |
| 2170 |
if times.last_modification.tv_nsec == c::UTIME_NOW.into() { |
| 2171 |
times.last_modification = crate::timespec::Timespec { |
| 2172 |
tv_sec: now.tv_sec.into(), |
| 2173 |
tv_nsec: now.tv_nsec as _, |
| 2174 |
}; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
// Pack the return values following the rules for [`getattrlist`]. |
| 2179 |
// |
| 2180 |
// [`getattrlist`]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getattrlist.2.html |
| 2181 |
let mut times_size = 0; |
| 2182 |
let mut attrs = Attrlist { |
| 2183 |
bitmapcount: ATTR_BIT_MAP_COUNT, |
| 2184 |
reserved: 0, |
| 2185 |
commonattr: 0, |
| 2186 |
volattr: 0, |
| 2187 |
dirattr: 0, |
| 2188 |
fileattr: 0, |
| 2189 |
forkattr: 0, |
| 2190 |
}; |
| 2191 |
let mut return_times = [c::timespec { |
| 2192 |
tv_sec: 0, |
| 2193 |
tv_nsec: 0, |
| 2194 |
}; 2]; |
| 2195 |
let mut times_index = 0; |
| 2196 |
if times.last_modification.tv_nsec != c::UTIME_OMIT.into() { |
| 2197 |
attrs.commonattr |= ATTR_CMN_MODTIME; |
| 2198 |
return_times[times_index] = c::timespec { |
| 2199 |
tv_sec: times |
| 2200 |
.last_modification |
| 2201 |
.tv_sec |
| 2202 |
.try_into() |
| 2203 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 2204 |
tv_nsec: times.last_modification.tv_nsec as _, |
| 2205 |
}; |
| 2206 |
times_index += 1; |
| 2207 |
times_size += size_of::<c::timespec>(); |
| 2208 |
} |
| 2209 |
if times.last_access.tv_nsec != c::UTIME_OMIT.into() { |
| 2210 |
attrs.commonattr |= ATTR_CMN_ACCTIME; |
| 2211 |
return_times[times_index] = c::timespec { |
| 2212 |
tv_sec: times |
| 2213 |
.last_access |
| 2214 |
.tv_sec |
| 2215 |
.try_into() |
| 2216 |
.map_err(|_| io::Errno::OVERFLOW)?, |
| 2217 |
tv_nsec: times.last_access.tv_nsec as _, |
| 2218 |
}; |
| 2219 |
times_size += size_of::<c::timespec>(); |
| 2220 |
} |
| 2221 |
|
| 2222 |
Ok((times_size, return_times, attrs)) |
| 2223 |
} |
| 2224 |
|
| 2225 |
/// Support type for `Attrlist`. |
| 2226 |
#[cfg(apple)] |
| 2227 |
type Attrgroup = u32; |
| 2228 |
|
| 2229 |
/// Attribute list for use with `setattrlist`. |
| 2230 |
#[cfg(apple)] |
| 2231 |
#[repr(C)] |
| 2232 |
struct Attrlist { |
| 2233 |
bitmapcount: u16, |
| 2234 |
reserved: u16, |
| 2235 |
commonattr: Attrgroup, |
| 2236 |
volattr: Attrgroup, |
| 2237 |
dirattr: Attrgroup, |
| 2238 |
fileattr: Attrgroup, |
| 2239 |
forkattr: Attrgroup, |
| 2240 |
} |
| 2241 |
|
| 2242 |
#[cfg(any(apple, linux_kernel))] |
| 2243 |
pub(crate) fn getxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> { |
| 2244 |
let value_ptr = value.as_mut_ptr(); |
| 2245 |
|
| 2246 |
#[cfg(not(apple))] |
| 2247 |
unsafe { |
| 2248 |
ret_usize(c::getxattr( |
| 2249 |
path.as_ptr(), |
| 2250 |
name.as_ptr(), |
| 2251 |
value_ptr.cast::<c::c_void>(), |
| 2252 |
value.len(), |
| 2253 |
)) |
| 2254 |
} |
| 2255 |
|
| 2256 |
#[cfg(apple)] |
| 2257 |
{ |
| 2258 |
// Passing an empty to slice to getxattr leads to ERANGE on macOS. Pass null |
| 2259 |
// instead. |
| 2260 |
let ptr = if value.is_empty() { |
| 2261 |
core::ptr::null_mut() |
| 2262 |
} else { |
| 2263 |
value_ptr.cast::<c::c_void>() |
| 2264 |
}; |
| 2265 |
unsafe { |
| 2266 |
ret_usize(c::getxattr( |
| 2267 |
path.as_ptr(), |
| 2268 |
name.as_ptr(), |
| 2269 |
ptr, |
| 2270 |
value.len(), |
| 2271 |
0, |
| 2272 |
0, |
| 2273 |
)) |
| 2274 |
} |
| 2275 |
} |
| 2276 |
} |
| 2277 |
|
| 2278 |
#[cfg(any(apple, linux_kernel))] |
| 2279 |
pub(crate) fn lgetxattr(path: &CStr, name: &CStr, value: &mut [u8]) -> io::Result<usize> { |
| 2280 |
let value_ptr = value.as_mut_ptr(); |
| 2281 |
|
| 2282 |
#[cfg(not(apple))] |
| 2283 |
unsafe { |
| 2284 |
ret_usize(c::lgetxattr( |
| 2285 |
path.as_ptr(), |
| 2286 |
name.as_ptr(), |
| 2287 |
value_ptr.cast::<c::c_void>(), |
| 2288 |
value.len(), |
| 2289 |
)) |
| 2290 |
} |
| 2291 |
|
| 2292 |
#[cfg(apple)] |
| 2293 |
{ |
| 2294 |
// Passing an empty to slice to getxattr leads to ERANGE on macOS. Pass null |
| 2295 |
// instead. |
| 2296 |
let ptr = if value.is_empty() { |
| 2297 |
core::ptr::null_mut() |
| 2298 |
} else { |
| 2299 |
value_ptr.cast::<c::c_void>() |
| 2300 |
}; |
| 2301 |
|
| 2302 |
unsafe { |
| 2303 |
ret_usize(c::getxattr( |
| 2304 |
path.as_ptr(), |
| 2305 |
name.as_ptr(), |
| 2306 |
ptr, |
| 2307 |
value.len(), |
| 2308 |
0, |
| 2309 |
c::XATTR_NOFOLLOW, |
| 2310 |
)) |
| 2311 |
} |
| 2312 |
} |
| 2313 |
} |
| 2314 |
|
| 2315 |
#[cfg(any(apple, linux_kernel))] |
| 2316 |
pub(crate) fn fgetxattr(fd: BorrowedFd<'_>, name: &CStr, value: &mut [u8]) -> io::Result<usize> { |
| 2317 |
let value_ptr = value.as_mut_ptr(); |
| 2318 |
|
| 2319 |
#[cfg(not(apple))] |
| 2320 |
unsafe { |
| 2321 |
ret_usize(c::fgetxattr( |
| 2322 |
borrowed_fd(fd), |
| 2323 |
name.as_ptr(), |
| 2324 |
value_ptr.cast::<c::c_void>(), |
| 2325 |
value.len(), |
| 2326 |
)) |
| 2327 |
} |
| 2328 |
|
| 2329 |
#[cfg(apple)] |
| 2330 |
{ |
| 2331 |
// Passing an empty to slice to getxattr leads to ERANGE on macOS. Pass null |
| 2332 |
// instead. |
| 2333 |
let ptr = if value.is_empty() { |
| 2334 |
core::ptr::null_mut() |
| 2335 |
} else { |
| 2336 |
value_ptr.cast::<c::c_void>() |
| 2337 |
}; |
| 2338 |
unsafe { |
| 2339 |
ret_usize(c::fgetxattr( |
| 2340 |
borrowed_fd(fd), |
| 2341 |
name.as_ptr(), |
| 2342 |
ptr, |
| 2343 |
value.len(), |
| 2344 |
0, |
| 2345 |
0, |
| 2346 |
)) |
| 2347 |
} |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
#[cfg(any(apple, linux_kernel))] |
| 2352 |
pub(crate) fn setxattr( |
| 2353 |
path: &CStr, |
| 2354 |
name: &CStr, |
| 2355 |
value: &[u8], |
| 2356 |
flags: XattrFlags, |
| 2357 |
) -> io::Result<()> { |
| 2358 |
#[cfg(not(apple))] |
| 2359 |
unsafe { |
| 2360 |
ret(c::setxattr( |
| 2361 |
path.as_ptr(), |
| 2362 |
name.as_ptr(), |
| 2363 |
value.as_ptr().cast::<c::c_void>(), |
| 2364 |
value.len(), |
| 2365 |
flags.bits() as i32, |
| 2366 |
)) |
| 2367 |
} |
| 2368 |
|
| 2369 |
#[cfg(apple)] |
| 2370 |
unsafe { |
| 2371 |
ret(c::setxattr( |
| 2372 |
path.as_ptr(), |
| 2373 |
name.as_ptr(), |
| 2374 |
value.as_ptr().cast::<c::c_void>(), |
| 2375 |
value.len(), |
| 2376 |
0, |
| 2377 |
flags.bits() as i32, |
| 2378 |
)) |
| 2379 |
} |
| 2380 |
} |
| 2381 |
|
| 2382 |
#[cfg(any(apple, linux_kernel))] |
| 2383 |
pub(crate) fn lsetxattr( |
| 2384 |
path: &CStr, |
| 2385 |
name: &CStr, |
| 2386 |
value: &[u8], |
| 2387 |
flags: XattrFlags, |
| 2388 |
) -> io::Result<()> { |
| 2389 |
#[cfg(not(apple))] |
| 2390 |
unsafe { |
| 2391 |
ret(c::lsetxattr( |
| 2392 |
path.as_ptr(), |
| 2393 |
name.as_ptr(), |
| 2394 |
value.as_ptr().cast::<c::c_void>(), |
| 2395 |
value.len(), |
| 2396 |
flags.bits() as i32, |
| 2397 |
)) |
| 2398 |
} |
| 2399 |
|
| 2400 |
#[cfg(apple)] |
| 2401 |
unsafe { |
| 2402 |
ret(c::setxattr( |
| 2403 |
path.as_ptr(), |
| 2404 |
name.as_ptr(), |
| 2405 |
value.as_ptr().cast::<c::c_void>(), |
| 2406 |
value.len(), |
| 2407 |
0, |
| 2408 |
flags.bits() as i32 | c::XATTR_NOFOLLOW, |
| 2409 |
)) |
| 2410 |
} |
| 2411 |
} |
| 2412 |
|
| 2413 |
#[cfg(any(apple, linux_kernel))] |
| 2414 |
pub(crate) fn fsetxattr( |
| 2415 |
fd: BorrowedFd<'_>, |
| 2416 |
name: &CStr, |
| 2417 |
value: &[u8], |
| 2418 |
flags: XattrFlags, |
| 2419 |
) -> io::Result<()> { |
| 2420 |
#[cfg(not(apple))] |
| 2421 |
unsafe { |
| 2422 |
ret(c::fsetxattr( |
| 2423 |
borrowed_fd(fd), |
| 2424 |
name.as_ptr(), |
| 2425 |
value.as_ptr().cast::<c::c_void>(), |
| 2426 |
value.len(), |
| 2427 |
flags.bits() as i32, |
| 2428 |
)) |
| 2429 |
} |
| 2430 |
|
| 2431 |
#[cfg(apple)] |
| 2432 |
unsafe { |
| 2433 |
ret(c::fsetxattr( |
| 2434 |
borrowed_fd(fd), |
| 2435 |
name.as_ptr(), |
| 2436 |
value.as_ptr().cast::<c::c_void>(), |
| 2437 |
value.len(), |
| 2438 |
0, |
| 2439 |
flags.bits() as i32, |
| 2440 |
)) |
| 2441 |
} |
| 2442 |
} |
| 2443 |
|
| 2444 |
#[cfg(any(apple, linux_kernel))] |
| 2445 |
pub(crate) fn listxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result<usize> { |
| 2446 |
#[cfg(not(apple))] |
| 2447 |
unsafe { |
| 2448 |
ret_usize(c::listxattr(path.as_ptr(), list.as_mut_ptr(), list.len())) |
| 2449 |
} |
| 2450 |
|
| 2451 |
#[cfg(apple)] |
| 2452 |
unsafe { |
| 2453 |
ret_usize(c::listxattr( |
| 2454 |
path.as_ptr(), |
| 2455 |
list.as_mut_ptr(), |
| 2456 |
list.len(), |
| 2457 |
0, |
| 2458 |
)) |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
#[cfg(any(apple, linux_kernel))] |
| 2463 |
pub(crate) fn llistxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result<usize> { |
| 2464 |
#[cfg(not(apple))] |
| 2465 |
unsafe { |
| 2466 |
ret_usize(c::llistxattr(path.as_ptr(), list.as_mut_ptr(), list.len())) |
| 2467 |
} |
| 2468 |
|
| 2469 |
#[cfg(apple)] |
| 2470 |
unsafe { |
| 2471 |
ret_usize(c::listxattr( |
| 2472 |
path.as_ptr(), |
| 2473 |
list.as_mut_ptr(), |
| 2474 |
list.len(), |
| 2475 |
c::XATTR_NOFOLLOW, |
| 2476 |
)) |
| 2477 |
} |
| 2478 |
} |
| 2479 |
|
| 2480 |
#[cfg(any(apple, linux_kernel))] |
| 2481 |
pub(crate) fn flistxattr(fd: BorrowedFd<'_>, list: &mut [c::c_char]) -> io::Result<usize> { |
| 2482 |
let fd = borrowed_fd(fd); |
| 2483 |
|
| 2484 |
#[cfg(not(apple))] |
| 2485 |
unsafe { |
| 2486 |
ret_usize(c::flistxattr(fd, list.as_mut_ptr(), list.len())) |
| 2487 |
} |
| 2488 |
|
| 2489 |
#[cfg(apple)] |
| 2490 |
unsafe { |
| 2491 |
ret_usize(c::flistxattr(fd, list.as_mut_ptr(), list.len(), 0)) |
| 2492 |
} |
| 2493 |
} |
| 2494 |
|
| 2495 |
#[cfg(any(apple, linux_kernel))] |
| 2496 |
pub(crate) fn removexattr(path: &CStr, name: &CStr) -> io::Result<()> { |
| 2497 |
#[cfg(not(apple))] |
| 2498 |
unsafe { |
| 2499 |
ret(c::removexattr(path.as_ptr(), name.as_ptr())) |
| 2500 |
} |
| 2501 |
|
| 2502 |
#[cfg(apple)] |
| 2503 |
unsafe { |
| 2504 |
ret(c::removexattr(path.as_ptr(), name.as_ptr(), 0)) |
| 2505 |
} |
| 2506 |
} |
| 2507 |
|
| 2508 |
#[cfg(any(apple, linux_kernel))] |
| 2509 |
pub(crate) fn lremovexattr(path: &CStr, name: &CStr) -> io::Result<()> { |
| 2510 |
#[cfg(not(apple))] |
| 2511 |
unsafe { |
| 2512 |
ret(c::lremovexattr(path.as_ptr(), name.as_ptr())) |
| 2513 |
} |
| 2514 |
|
| 2515 |
#[cfg(apple)] |
| 2516 |
unsafe { |
| 2517 |
ret(c::removexattr( |
| 2518 |
path.as_ptr(), |
| 2519 |
name.as_ptr(), |
| 2520 |
c::XATTR_NOFOLLOW, |
| 2521 |
)) |
| 2522 |
} |
| 2523 |
} |
| 2524 |
|
| 2525 |
#[cfg(any(apple, linux_kernel))] |
| 2526 |
pub(crate) fn fremovexattr(fd: BorrowedFd<'_>, name: &CStr) -> io::Result<()> { |
| 2527 |
let fd = borrowed_fd(fd); |
| 2528 |
|
| 2529 |
#[cfg(not(apple))] |
| 2530 |
unsafe { |
| 2531 |
ret(c::fremovexattr(fd, name.as_ptr())) |
| 2532 |
} |
| 2533 |
|
| 2534 |
#[cfg(apple)] |
| 2535 |
unsafe { |
| 2536 |
ret(c::fremovexattr(fd, name.as_ptr(), 0)) |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
#[test] |
| 2541 |
fn test_sizes() { |
| 2542 |
#[cfg(linux_kernel)] |
| 2543 |
assert_eq_size!(c::loff_t, u64); |
| 2544 |
|
| 2545 |
// Assert that `Timestamps` has the expected layout. If we're not fixing |
| 2546 |
// y2038, libc's type should match ours. If we are, it's smaller. |
| 2547 |
#[cfg(not(fix_y2038))] |
| 2548 |
assert_eq_size!([c::timespec; 2], Timestamps); |
| 2549 |
#[cfg(fix_y2038)] |
| 2550 |
assert!(core::mem::size_of::<[c::timespec; 2]>() < core::mem::size_of::<Timestamps>()); |
| 2551 |
} |