use rustix::fd::{AsFd, BorrowedFd};
use rustix::fs::{fcntl_getfl, OFlags};
use std::{fs, io};
#[inline]
pub(crate) fn is_file_read_write_impl(file: &fs::File) -> io::Result<(bool, bool)> {
Ok(is_file_read_write(file)?)
}
#[inline]
fn is_file_read_write<Fd: AsFd>(fd: Fd) -> io::Result<(bool, bool)> {
_is_file_read_write(fd.as_fd())
}
fn _is_file_read_write(fd: BorrowedFd<'_>) -> io::Result<(bool, bool)> {
let mode = fcntl_getfl(fd)?;
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "emscripten",
target_os = "fuchsia"
))]
if mode.contains(OFlags::PATH) {
return Ok((false, false));
}
match mode & OFlags::RWMODE {
OFlags::RDONLY => Ok((true, false)),
OFlags::RDWR => Ok((true, true)),
OFlags::WRONLY => Ok((false, true)),
_ => unreachable!(),
}
}