use crate::filesystem::primitives::{FollowSymlinks, ImplOpenOptionsExt};
#[derive(Debug, Clone)]
pub struct OpenOptions {
pub(crate) read: bool,
pub(crate) write: bool,
pub(crate) append: bool,
pub(crate) truncate: bool,
pub(crate) create: bool,
pub(crate) create_new: bool,
pub(crate) dir_required: bool,
#[cfg(windows)]
pub(crate) maybe_dir: bool,
pub(crate) sync: bool,
pub(crate) dsync: bool,
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
target_os = "freebsd",
target_os = "fuchsia",
windows,
)))]
pub(crate) rsync: bool,
#[cfg(not(windows))]
pub(crate) nonblock: bool,
pub(crate) readdir_required: bool,
pub(crate) follow: FollowSymlinks,
#[cfg(any(unix, windows, target_os = "vxworks"))]
pub(crate) ext: ImplOpenOptionsExt,
}
impl OpenOptions {
#[allow(clippy::new_without_default)]
#[inline]
pub const fn new() -> Self {
Self {
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
dir_required: false,
#[cfg(windows)]
maybe_dir: false,
sync: false,
dsync: false,
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
target_os = "freebsd",
target_os = "fuchsia",
windows,
)))]
rsync: false,
#[cfg(not(windows))]
nonblock: false,
readdir_required: false,
follow: FollowSymlinks::Yes,
#[cfg(any(unix, windows, target_os = "vxworks"))]
ext: ImplOpenOptionsExt::new(),
}
}
#[inline]
pub fn read(&mut self, read: bool) -> &mut Self {
self.read = read;
self
}
#[inline]
pub fn write(&mut self, write: bool) -> &mut Self {
self.write = write;
self
}
#[inline]
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.truncate = truncate;
self
}
#[inline]
pub fn create(&mut self, create: bool) -> &mut Self {
self.create = create;
self
}
#[inline]
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.create_new = create_new;
self
}
#[inline]
pub(crate) fn follow(&mut self, follow: FollowSymlinks) -> &mut Self {
self.follow = follow;
self
}
#[inline]
pub(crate) fn dir_required(&mut self, dir_required: bool) -> &mut Self {
self.dir_required = dir_required;
self
}
#[inline]
pub(crate) fn readdir_required(&mut self, readdir_required: bool) -> &mut Self {
self.readdir_required = readdir_required;
self
}
#[doc(hidden)]
#[inline]
pub fn _cap_fs_ext_follow(&mut self, follow: FollowSymlinks) -> &mut Self {
self.follow(follow)
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub trait OpenOptionsExt {
fn custom_flags(&mut self, flags: i32) -> &mut Self;
}
#[cfg(target_os = "wasi")]
pub trait OpenOptionsExt {
fn lookup_flags(&mut self, flags: u32) -> &mut Self;
fn directory(&mut self, dir: bool) -> &mut Self;
fn dsync(&mut self, dsync: bool) -> &mut Self;
fn nonblock(&mut self, nonblock: bool) -> &mut Self;
fn rsync(&mut self, rsync: bool) -> &mut Self;
fn sync(&mut self, sync: bool) -> &mut Self;
fn fs_rights_base(&mut self, rights: u64) -> &mut Self;
fn fs_rights_inheriting(&mut self, rights: u64) -> &mut Self;
fn open_at<P: AsRef<std::path::Path>>(
&self,
file: &std::fs::File,
path: P,
) -> std::io::Result<std::fs::File>;
}
#[cfg(windows)]
pub trait OpenOptionsExt {
fn access_mode(&mut self, access: u32) -> &mut Self;
fn share_mode(&mut self, val: u32) -> &mut Self;
fn custom_flags(&mut self, flags: u32) -> &mut Self;
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl OpenOptionsExt for OpenOptions {
#[inline]
fn custom_flags(&mut self, flags: i32) -> &mut Self {
self.ext.custom_flags(flags);
self
}
}
#[cfg(target_os = "wasi")]
impl OpenOptionsExt for OpenOptions {
fn lookup_flags(&mut self, _: u32) -> &mut Self {
todo!()
}
fn directory(&mut self, dir_required: bool) -> &mut Self {
self.dir_required = dir_required;
self
}
fn dsync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn nonblock(&mut self, _: bool) -> &mut Self {
todo!()
}
fn rsync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn sync(&mut self, _: bool) -> &mut Self {
todo!()
}
fn fs_rights_base(&mut self, _: u64) -> &mut Self {
todo!()
}
fn fs_rights_inheriting(&mut self, _: u64) -> &mut Self {
todo!()
}
fn open_at<P>(&self, dirfd: &std::fs::File, path: P) -> Result<std::fs::File, std::io::Error>
where
P: AsRef<std::path::Path>,
{
crate::fs::open(dirfd, path.as_ref(), self)
}
}
#[cfg(target_os = "vxworks")]
impl OpenOptionsExt for OpenOptions {
#[inline]
fn mode(&mut self, mode: u32) -> &mut Self {
self.ext.mode(mode);
self
}
#[inline]
fn custom_flags(&mut self, flags: i32) -> &mut Self {
self.ext.custom_flags(flags);
self
}
}
#[cfg(windows)]
impl OpenOptionsExt for OpenOptions {
#[inline]
fn access_mode(&mut self, access: u32) -> &mut Self {
self.ext.access_mode(access);
self
}
#[inline]
fn share_mode(&mut self, val: u32) -> &mut Self {
self.ext.share_mode(val);
self
}
#[inline]
fn custom_flags(&mut self, flags: u32) -> &mut Self {
self.ext.custom_flags(flags);
self
}
}