cap_primitives/rustix/fs/
permissions_ext.rs
use crate::fs::Permissions;
use rustix::fs::RawMode;
use std::fs;
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct ImplPermissionsExt {
#[cfg(not(target_os = "wasi"))]
mode: RawMode,
}
#[cfg(not(target_os = "wasi"))]
impl ImplPermissionsExt {
#[inline]
pub(crate) fn from_std(std: fs::Permissions) -> Self {
use std::os::unix::fs::PermissionsExt;
Self {
mode: std.mode() as RawMode,
}
}
#[inline]
pub(crate) const fn from_raw_mode(mode: RawMode) -> Permissions {
Permissions {
readonly: Self::readonly(mode),
ext: Self { mode },
}
}
#[inline]
pub(crate) const fn readonly(mode: RawMode) -> bool {
mode & 0o222 == 0
}
#[inline]
pub(crate) fn set_readonly(&mut self, readonly: bool) {
if readonly {
self.mode &= !0o222;
} else {
self.mode |= 0o222;
}
}
}
#[cfg(not(target_os = "wasi"))]
impl crate::fs::PermissionsExt for ImplPermissionsExt {
fn mode(&self) -> u32 {
self.mode as u32
}
fn set_mode(&mut self, mode: u32) {
self.mode = mode as RawMode & 0o7777;
}
fn from_mode(mode: u32) -> Self {
Self {
mode: mode as RawMode & 0o7777,
}
}
}
#[cfg(target_os = "wasi")]
impl ImplPermissionsExt {
pub(crate) fn default() -> Permissions {
Permissions { readonly: false }
}
}