//! POSIX-style `*at` functions. //! //! The `dirfd` argument to these functions may be a file descriptor for a //! directory, or the special value [`CWD`]. //! //! [`cwd`]: crate::fs::CWD
/// `UTIME_NOW` for use with [`utimensat`]. /// /// [`utimensat`]: crate::fs::utimensat #[cfg(not(any(target_os = "espidf", target_os = "redox", target_os = "vita")))] pubconst UTIME_NOW: Nsecs = backend::c::UTIME_NOW as Nsecs;
/// `UTIME_OMIT` for use with [`utimensat`]. /// /// [`utimensat`]: crate::fs::utimensat #[cfg(not(any(target_os = "espidf", target_os = "redox", target_os = "vita")))] pubconst UTIME_OMIT: Nsecs = backend::c::UTIME_OMIT as Nsecs;
/// `openat(dirfd, path, oflags, mode)`—Opens a file. /// /// POSIX guarantees that `openat` will use the lowest unused file descriptor, /// however it is not safe in general to rely on this, as file descriptors may /// be unexpectedly allocated on other threads or in libraries. /// /// The `Mode` argument is only significant when creating a file. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html /// [Linux]: https://man7.org/linux/man-pages/man2/openat.2.html #[inline] pubfn openat<P: path::Arg, Fd: AsFd>(
dirfd: Fd,
path: P,
oflags: OFlags,
create_mode: Mode,
) -> io::Result<OwnedFd> {
path.into_with_c_str(|path| {
backend::fs::syscalls::openat(dirfd.as_fd(), path, oflags, create_mode)
})
}
/// `readlinkat(fd, path)`—Reads the contents of a symlink. /// /// If `reuse` already has available capacity, reuse it if possible. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html /// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html #[cfg(feature = "alloc")] #[inline] pubfn readlinkat<P: path::Arg, Fd: AsFd, B: Into<Vec<u8>>>(
dirfd: Fd,
path: P,
reuse: B,
) -> io::Result<CString> {
path.into_with_c_str(|path| _readlinkat(dirfd.as_fd(), path, reuse.into()))
}
loop { let nread =
backend::fs::syscalls::readlinkat(dirfd.as_fd(), path, buffer.spare_capacity_mut())?;
debug_assert!(nread <= buffer.capacity()); if nread < buffer.capacity() { // SAFETY: From the [documentation]: “On success, these calls // return the number of bytes placed in buf.” // // [documentation]: https://man7.org/linux/man-pages/man2/readlinkat.2.html unsafe {
buffer.set_len(nread);
}
// SAFETY: // - “readlink places the contents of the symbolic link pathname in // the buffer buf” // - [POSIX definition 3.271: Pathname]: “A string that is used to // identify a file.” // - [POSIX definition 3.375: String]: “A contiguous sequence of // bytes terminated by and including the first null byte.” // - “readlink does not append a terminating null byte to buf.” // // Thus, there will be no NUL bytes in the string. // // [POSIX definition 3.271: Pathname]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271 // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_375 unsafe { return Ok(CString::from_vec_unchecked(buffer));
}
}
// Use `Vec` reallocation strategy to grow capacity exponentially.
buffer.reserve(buffer.capacity() + 1);
}
}
/// `readlinkat(fd, path)`—Reads the contents of a symlink, without /// allocating. /// /// This is the "raw" version which avoids allocating, but which is /// significantly trickier to use; most users should use plain [`readlinkat`]. /// /// This version writes bytes into the buffer and returns two slices, one /// containing the written bytes, and one containing the remaining /// uninitialized space. If the number of written bytes is equal to the length /// of the buffer, it means the buffer wasn't big enough to hold the full /// string, and callers should try again with a bigger buffer. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlinkat.html /// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html #[inline] pubfn readlinkat_raw<P: path::Arg, Fd: AsFd>(
dirfd: Fd,
path: P,
buf: &mut [MaybeUninit<u8>],
) -> io::Result<(&mut [u8], &mut [MaybeUninit<u8>])> {
path.into_with_c_str(|path| _readlinkat_raw(dirfd.as_fd(), path, buf))
}
/// `fstatat(dirfd, path, flags)`—Queries metadata for a file or directory. /// /// [`Mode::from_raw_mode`] and [`FileType::from_raw_mode`] may be used to /// interpret the `st_mode` field. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html /// [Linux]: https://man7.org/linux/man-pages/man2/fstatat.2.html /// [`Mode::from_raw_mode`]: crate::fs::Mode::from_raw_mode /// [`FileType::from_raw_mode`]: crate::fs::FileType::from_raw_mode #[cfg(not(target_os = "espidf"))] #[inline] #[doc(alias = "fstatat")] pubfn statat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, flags: AtFlags) -> io::Result<Stat> {
path.into_with_c_str(|path| backend::fs::syscalls::statat(dirfd.as_fd(), path, flags))
}
/// `faccessat(dirfd, path, access, flags)`—Tests permissions for a file or /// directory. /// /// On Linux before 5.8, this function uses the `faccessat` system call which /// doesn't support any flags. This function emulates support for the /// [`AtFlags::EACCESS`] flag by checking whether the uid and gid of the /// process match the effective uid and gid, in which case the `EACCESS` flag /// can be ignored. In Linux 5.8 and beyond `faccessat2` is used, which /// supports flags. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/faccessat.html /// [Linux]: https://man7.org/linux/man-pages/man2/faccessat.2.html #[cfg(not(any(target_os = "espidf", target_os = "vita")))] #[inline] #[doc(alias = "faccessat")] pubfn accessat<P: path::Arg, Fd: AsFd>(
dirfd: Fd,
path: P,
access: Access,
flags: AtFlags,
) -> io::Result<()> {
path.into_with_c_str(|path| backend::fs::syscalls::accessat(dirfd.as_fd(), path, access, flags))
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.