Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the is_file_read_write implementation out of rustix. #228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions cap-primitives/src/rustix/fs/is_file_read_write_impl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
use rustix::fs::is_file_read_write;
use rustix::fs::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)?)
let mode = rustix::fs::fcntl_getfl(file)?;

// Check for `O_PATH`.
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "emscripten"
))]
if mode.contains(OFlags::PATH) {
return Ok((false, false));
}

// Use `RWMODE` rather than `ACCMODE` as `ACCMODE` may include `O_PATH`.
// We handled `O_PATH` above.
match mode & OFlags::RWMODE {
OFlags::RDONLY => Ok((true, false)),
OFlags::RDWR => Ok((true, true)),
OFlags::WRONLY => Ok((false, true)),
_ => unreachable!(),
}
}