use std::{fmt::Debug, os::unix::fs::PermissionsExt};
#[cfg(unix)]
use libc::mode_t;
#[cfg(unix)]
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct FilePermissions {
pub(crate) mode: mode_t,
}
impl FilePermissions {
fn readonly(&self) -> bool {
self.mode & 0o222 == 0
}
#[cfg(target_os = "linux")]
fn mode(&self) -> u32 {
self.mode
}
fn set_readonly(&mut self, readonly: bool) {
if readonly {
self.mode &= !0o222;
} else {
self.mode |= 0o222;
}
}
#[cfg(not(target_os = "linux"))]
fn mode(&self) -> u32 {
unimplemented!()
}
}
#[cfg(unix)]
pub struct Permissions(pub(crate) FilePermissions);
impl Permissions {
pub fn readonly(&self) -> bool {
self.0.readonly()
}
#[allow(unused)]
pub fn set_readonly(&mut self, readonly: bool) {
self.0.set_readonly(readonly)
}
}
impl PermissionsExt for Permissions {
fn mode(&self) -> u32 {
self.0.mode()
}
fn set_mode(&mut self, mode: u32) {
*self = Self::from_mode(mode);
}
fn from_mode(mode: u32) -> Self {
Self(FilePermissions {
mode: mode as mode_t,
})
}
}
impl Debug for Permissions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Permissions")
.field("readonly", &self.readonly())
.finish_non_exhaustive()
}
}