cap_primitives/rustix/fs/
is_file_read_write_impl.rs1use rustix::fd::{AsFd, BorrowedFd};
2use rustix::fs::{fcntl_getfl, OFlags};
3use std::{fs, io};
4
5#[inline]
6pub(crate) fn is_file_read_write_impl(file: &fs::File) -> io::Result<(bool, bool)> {
7 Ok(is_file_read_write(file)?)
8}
9
10#[inline]
17fn is_file_read_write<Fd: AsFd>(fd: Fd) -> io::Result<(bool, bool)> {
18 _is_file_read_write(fd.as_fd())
19}
20
21fn _is_file_read_write(fd: BorrowedFd<'_>) -> io::Result<(bool, bool)> {
22 let mode = fcntl_getfl(fd)?;
23
24 #[cfg(any(
26 target_os = "linux",
27 target_os = "android",
28 target_os = "emscripten",
29 target_os = "fuchsia"
30 ))]
31 if mode.contains(OFlags::PATH) {
32 return Ok((false, false));
33 }
34
35 match mode & OFlags::RWMODE {
38 OFlags::RDONLY => Ok((true, false)),
39 OFlags::RDWR => Ok((true, true)),
40 OFlags::WRONLY => Ok((false, true)),
41 _ => unreachable!(),
42 }
43}