//! POSIX-style `*at` functions. //! //! The `dirfd` argument to these functions may be a file descriptor for a //! directory, the special value [`CWD`], or the special value [`ABS`]. //! //! [`CWD`]: crate::fs::CWD //! [`ABS`]: crate::fs::ABS
/// `UTIME_NOW` for use with [`utimensat`]. /// /// [`utimensat`]: crate::fs::utimensat #[cfg(not(any(
target_os = "espidf",
target_os = "horizon",
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 = "horizon",
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/9799919799/functions/openat.html /// [Linux]: https://man7.org/linux/man-pages/man2/openat.2.html #[cfg(not(target_os = "redox"))] #[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/9799919799/functions/readlinkat.html /// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html #[cfg(all(feature = "alloc", not(target_os = "redox")))] #[cfg_attr(docsrs, doc(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()))
}
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/9799919799/basedefs/V1_chap03.html#tag_03_271 // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9799919799/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 truncates the /// string if it doesn't fit in the provided buffer, and doesn't NUL-terminate /// the string. /// /// # References /// - [POSIX] /// - [Linux] /// /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/readlinkat.html /// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html #[cfg(not(target_os = "redox"))] #[inline] pubfn readlinkat_raw<P: path::Arg, Fd: AsFd, Buf: Buffer<u8>>(
dirfd: Fd,
path: P, mut buf: Buf,
) -> io::Result<Buf::Output> { // SAFETY: `readlinkat` behaves. let len = path.into_with_c_str(|path| unsafe {
backend::fs::syscalls::readlinkat(dirfd.as_fd(), path, buf.parts_mut())
})?; // SAFETY: `readlinkat` behaves. unsafe { Ok(buf.assume_init(len)) }
}
/// `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/9799919799/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(any(target_os = "espidf", target_os = "redox")))] #[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/9799919799/functions/faccessat.html /// [Linux]: https://man7.org/linux/man-pages/man2/faccessat.2.html #[cfg(not(any(
target_os = "espidf",
target_os = "horizon",
target_os = "vita",
target_os = "redox"
)))] #[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.