use alloc::borrow::Cow;
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EntryKind {
File,
Dir,
Symlink,
Hardlink,
Char,
Block,
Fifo,
Socket,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Timestamp {
pub secs: i64,
pub nanos: u32,
}
type PaxRecord<'a> = (Cow<'a, [u8]>, Cow<'a, [u8]>);
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PaxMap<'a> {
entries: Vec<PaxRecord<'a>>,
}
impl<'a> PaxMap<'a> {
#[must_use]
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
#[must_use]
pub fn get(&self, key: &[u8]) -> Option<&[u8]> {
self.entries
.iter()
.find(|(k, _)| k.as_ref() == key)
.map(|(_, v)| v.as_ref())
}
pub fn insert(&mut self, key: Cow<'a, [u8]>, value: Cow<'a, [u8]>) {
self.entries.push((key, value));
}
#[must_use]
pub fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
#[must_use]
pub fn to_static(&self) -> PaxMap<'static> {
PaxMap {
entries: self
.entries
.iter()
.map(|(k, v)| {
(
Cow::Owned(k.clone().into_owned()),
Cow::Owned(v.clone().into_owned()),
)
})
.collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntryMeta<'a> {
pub kind: EntryKind,
pub path: Cow<'a, [u8]>,
pub mode: u32,
pub uid: u64,
pub gid: u64,
pub mtime: Option<Timestamp>,
pub size: u64,
pub link_target: Option<Cow<'a, [u8]>>,
pub pax: PaxMap<'a>,
}
impl<'a> EntryMeta<'a> {
#[must_use]
pub fn new(kind: EntryKind, path: Cow<'a, [u8]>) -> Self {
Self {
kind,
path,
mode: 0,
uid: 0,
gid: 0,
mtime: None,
size: 0,
link_target: None,
pax: PaxMap::new(),
}
}
#[must_use]
pub fn to_static(&self) -> EntryMeta<'static> {
EntryMeta {
kind: self.kind,
path: Cow::Owned(self.path.clone().into_owned()),
mode: self.mode,
uid: self.uid,
gid: self.gid,
mtime: self.mtime,
size: self.size,
link_target: self
.link_target
.as_ref()
.map(|t| Cow::Owned(t.clone().into_owned())),
pax: self.pax.to_static(),
}
}
}