use crate::handle::{Filetype, Handle, HandleRights};
use crate::{Error, Result};
use std::ops::Deref;
use std::path::PathBuf;
use std::rc::Rc;
pub struct EntryHandle(Rc<dyn Handle>);
impl EntryHandle {
#[allow(dead_code)]
pub(crate) fn new<T: Handle + 'static>(handle: T) -> Self {
Self(Rc::new(handle))
}
pub(crate) fn get(&self) -> Self {
Self(Rc::clone(&self.0))
}
}
impl std::fmt::Debug for EntryHandle {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("EntryHandle").field("opaque", &()).finish()
}
}
impl From<Box<dyn Handle>> for EntryHandle {
fn from(handle: Box<dyn Handle>) -> Self {
Self(handle.into())
}
}
impl Deref for EntryHandle {
type Target = dyn Handle;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
pub(crate) struct Entry {
handle: EntryHandle,
pub(crate) preopen_path: Option<PathBuf>,
}
impl Entry {
pub(crate) fn new(handle: EntryHandle) -> Self {
let preopen_path = None;
Self {
handle,
preopen_path,
}
}
pub(crate) fn get_file_type(&self) -> Filetype {
self.handle.get_file_type()
}
pub(crate) fn get_rights(&self) -> HandleRights {
self.handle.get_rights()
}
pub(crate) fn set_rights(&self, rights: HandleRights) {
self.handle.set_rights(rights)
}
pub(crate) fn as_handle(&self, rights: &HandleRights) -> Result<EntryHandle> {
self.validate_rights(rights)?;
Ok(self.handle.get())
}
pub(crate) fn validate_rights(&self, rights: &HandleRights) -> Result<()> {
let this_rights = self.handle.get_rights();
if this_rights.contains(rights) {
Ok(())
} else {
tracing::trace!(
required = tracing::field::display(rights),
actual = tracing::field::display(this_rights),
"validate_rights failed",
);
Err(Error::Notcapable)
}
}
}