;
}
/// Windows-specific extensions to [`fs::OpenOptions`].
#[cfg(windows)]
pub trait OpenOptionsExt {
/// Overrides the `dwDesiredAccess` argument to the call to [`CreateFile`]
/// with the specified value.
fn access_mode(&mut self, access: u32) -> &mut Self;
/// Overrides the `dwShareMode` argument to the call to [`CreateFile`] with
/// the specified value.
fn share_mode(&mut self, val: u32) -> &mut Self;
/// Sets extra flags for the `dwFileFlags` argument to the call to
/// [`CreateFile2`] to the specified value (or combines it with
/// `attributes` and `security_qos_flags` to set the `dwFlagsAndAttributes`
/// for [`CreateFile`]).
///
/// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
/// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
fn custom_flags(&mut self, flags: u32) -> &mut Self;
/// Sets the `dwFileAttributes` argument to the call to [`CreateFile2`] to
/// the specified value (or combines it with `custom_flags` and
/// `security_qos_flags` to set the `dwFlagsAndAttributes` for
/// [`CreateFile`]).
///
/// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
/// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
fn attributes(&mut self, val: u32) -> &mut Self;
/// Sets the `dwSecurityQosFlags` argument to the call to [`CreateFile2`] to
/// the specified value (or combines it with `custom_flags` and `attributes`
/// to set the `dwFlagsAndAttributes` for [`CreateFile`]).
///
/// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
/// [`CreateFile2`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2
/// [Impersonation Levels]:
/// https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-security_impersonation_level
fn security_qos_flags(&mut self, flags: u32) -> &mut Self;
}
#[cfg(unix)]
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(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(&self, dirfd: &std::fs::File, path: P) -> Result
where
P: AsRef,
{
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
}
/// To prevent race conditions on Windows, handles for directories must be
/// opened without `FILE_SHARE_DELETE`.
#[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
}
#[inline]
fn attributes(&mut self, val: u32) -> &mut Self {
self.ext.attributes(val);
self
}
#[inline]
fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
self.ext.security_qos_flags(flags);
self
}
}
#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for OpenOptions {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result {
use arbitrary::Arbitrary;
let (read, write) = match u.int_in_range(0..=2)? {
0 => (true, false),
1 => (false, true),
2 => (true, true),
_ => panic!(),
};
// TODO: `OpenOptionsExt` options.
Ok(Self::new()
.read(read)
.write(write)
.create(::arbitrary(u)?)
.append(::arbitrary(u)?)
.truncate(::arbitrary(u)?)
.create(::arbitrary(u)?)
.create_new(::arbitrary(u)?)
.dir_required(::arbitrary(u)?)
.maybe_dir(::arbitrary(u)?)
.sync(::arbitrary(u)?)
.dsync(::arbitrary(u)?)
.rsync(::arbitrary(u)?)
.nonblock(::arbitrary(u)?)
.readdir_required(::arbitrary(u)?)
.follow(::arbitrary(u)?)
.clone())
}
}