use alloc::string::{String, ToString};
use alloc::vec::Vec;
use crate::meta::{EntryKind, Timestamp};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PathEncoding {
Bytes,
Utf8,
Utf16Le,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArchivePath {
raw: Vec<u8>,
encoding: PathEncoding,
}
impl ArchivePath {
#[must_use]
pub fn from_bytes(raw: impl Into<Vec<u8>>) -> Self {
Self {
raw: raw.into(),
encoding: PathEncoding::Bytes,
}
}
#[must_use]
pub fn from_utf8(path: impl Into<String>) -> Self {
Self {
raw: path.into().into_bytes(),
encoding: PathEncoding::Utf8,
}
}
#[must_use]
pub fn from_encoded(raw: impl Into<Vec<u8>>, encoding: PathEncoding) -> Self {
Self {
raw: raw.into(),
encoding,
}
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.raw
}
#[must_use]
pub const fn encoding(&self) -> PathEncoding {
self.encoding
}
#[must_use]
pub fn display_lossy(&self) -> String {
match self.encoding {
PathEncoding::Utf16Le => {
let units = self
.raw
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]));
char::decode_utf16(units)
.map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER))
.collect()
},
PathEncoding::Bytes | PathEncoding::Utf8 => {
String::from_utf8_lossy(&self.raw).to_string()
},
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Owner {
pub uid: Option<u64>,
pub gid: Option<u64>,
pub user: Option<Vec<u8>>,
pub group: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EntryTimes {
pub modified: Option<Timestamp>,
pub accessed: Option<Timestamp>,
pub changed: Option<Timestamp>,
pub created: Option<Timestamp>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Device {
pub major: u64,
pub minor: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SparseExtent {
pub offset: u64,
pub length: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Extension {
namespace: String,
key: Vec<u8>,
value: Vec<u8>,
}
impl Extension {
#[must_use]
pub fn new(
namespace: impl Into<String>,
key: impl Into<Vec<u8>>,
value: impl Into<Vec<u8>>,
) -> Self {
Self {
namespace: namespace.into(),
key: key.into(),
value: value.into(),
}
}
#[must_use]
pub fn namespace(&self) -> &str {
&self.namespace
}
#[must_use]
pub fn key(&self) -> &[u8] {
&self.key
}
#[must_use]
pub fn value(&self) -> &[u8] {
&self.value
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntryMetadata {
kind: EntryKind,
path: ArchivePath,
size: Option<u64>,
link_target: Option<ArchivePath>,
mode: Option<u32>,
owner: Owner,
times: EntryTimes,
inode: Option<u64>,
links: Option<u64>,
device: Option<Device>,
referenced_device: Option<Device>,
sparse: Vec<SparseExtent>,
xattrs: Vec<(Vec<u8>, Vec<u8>)>,
acl: Vec<Vec<u8>>,
file_flags: u64,
checksum: Option<Vec<u8>>,
encrypted: bool,
comment: Option<Vec<u8>>,
extensions: Vec<Extension>,
}
impl EntryMetadata {
#[must_use]
pub fn builder(kind: EntryKind, path: ArchivePath) -> EntryMetadataBuilder {
EntryMetadataBuilder {
inner: Self {
kind,
path,
size: None,
link_target: None,
mode: None,
owner: Owner::default(),
times: EntryTimes::default(),
inode: None,
links: None,
device: None,
referenced_device: None,
sparse: Vec::new(),
xattrs: Vec::new(),
acl: Vec::new(),
file_flags: 0,
checksum: None,
encrypted: false,
comment: None,
extensions: Vec::new(),
},
}
}
#[must_use]
pub const fn kind(&self) -> EntryKind {
self.kind
}
#[must_use]
pub const fn path(&self) -> &ArchivePath {
&self.path
}
#[must_use]
pub const fn size(&self) -> Option<u64> {
self.size
}
#[must_use]
pub const fn link_target(&self) -> Option<&ArchivePath> {
self.link_target.as_ref()
}
#[must_use]
pub const fn mode(&self) -> Option<u32> {
self.mode
}
#[must_use]
pub const fn owner(&self) -> &Owner {
&self.owner
}
#[must_use]
pub const fn times(&self) -> EntryTimes {
self.times
}
#[must_use]
pub const fn inode(&self) -> Option<u64> {
self.inode
}
#[must_use]
pub const fn links(&self) -> Option<u64> {
self.links
}
#[must_use]
pub const fn device(&self) -> Option<Device> {
self.device
}
#[must_use]
pub const fn referenced_device(&self) -> Option<Device> {
self.referenced_device
}
#[must_use]
pub fn sparse_extents(&self) -> &[SparseExtent] {
&self.sparse
}
#[must_use]
pub fn xattrs(&self) -> &[(Vec<u8>, Vec<u8>)] {
&self.xattrs
}
#[must_use]
pub fn acl(&self) -> &[Vec<u8>] {
&self.acl
}
#[must_use]
pub const fn file_flags(&self) -> u64 {
self.file_flags
}
#[must_use]
pub fn checksum(&self) -> Option<&[u8]> {
self.checksum.as_deref()
}
#[must_use]
pub const fn is_encrypted(&self) -> bool {
self.encrypted
}
#[must_use]
pub fn comment(&self) -> Option<&[u8]> {
self.comment.as_deref()
}
#[must_use]
pub fn extensions(&self) -> &[Extension] {
&self.extensions
}
#[must_use]
pub fn into_builder(self) -> EntryMetadataBuilder {
EntryMetadataBuilder { inner: self }
}
}
#[derive(Debug)]
pub struct EntryMetadataBuilder {
inner: EntryMetadata,
}
impl EntryMetadataBuilder {
#[must_use]
pub const fn kind(mut self, kind: EntryKind) -> Self {
self.inner.kind = kind;
self
}
#[must_use]
pub fn path(mut self, path: ArchivePath) -> Self {
self.inner.path = path;
self
}
#[must_use]
pub const fn size(mut self, size: Option<u64>) -> Self {
self.inner.size = size;
self
}
#[must_use]
pub const fn mode(mut self, mode: Option<u32>) -> Self {
self.inner.mode = mode;
self
}
#[must_use]
pub fn owner(mut self, owner: Owner) -> Self {
self.inner.owner = owner;
self
}
#[must_use]
pub const fn times(mut self, times: EntryTimes) -> Self {
self.inner.times = times;
self
}
#[must_use]
pub fn link_target(mut self, target: Option<ArchivePath>) -> Self {
self.inner.link_target = target;
self
}
#[must_use]
pub const fn inode_and_links(mut self, inode: Option<u64>, links: Option<u64>) -> Self {
self.inner.inode = inode;
self.inner.links = links;
self
}
#[must_use]
pub const fn devices(
mut self,
device: Option<Device>,
referenced_device: Option<Device>,
) -> Self {
self.inner.device = device;
self.inner.referenced_device = referenced_device;
self
}
#[must_use]
pub fn sparse_extent(mut self, extent: SparseExtent) -> Self {
self.inner.sparse.push(extent);
self
}
#[must_use]
pub fn xattr(mut self, name: impl Into<Vec<u8>>, value: impl Into<Vec<u8>>) -> Self {
self.inner.xattrs.push((name.into(), value.into()));
self
}
#[must_use]
pub fn acl(mut self, value: impl Into<Vec<u8>>) -> Self {
self.inner.acl.push(value.into());
self
}
#[must_use]
pub const fn file_flags(mut self, flags: u64) -> Self {
self.inner.file_flags = flags;
self
}
#[must_use]
pub fn checksum(mut self, checksum: Option<Vec<u8>>) -> Self {
self.inner.checksum = checksum;
self
}
#[must_use]
pub const fn encrypted(mut self, encrypted: bool) -> Self {
self.inner.encrypted = encrypted;
self
}
#[must_use]
pub fn comment(mut self, comment: Option<Vec<u8>>) -> Self {
self.inner.comment = comment;
self
}
#[must_use]
pub fn extension(mut self, extension: Extension) -> Self {
self.inner.extensions.push(extension);
self
}
#[must_use]
pub fn build(self) -> EntryMetadata {
self.inner
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ArchiveMetadata {
volume_name: Option<ArchivePath>,
comment: Option<Vec<u8>>,
extensions: Vec<Extension>,
}
impl ArchiveMetadata {
#[must_use]
pub const fn new() -> Self {
Self {
volume_name: None,
comment: None,
extensions: Vec::new(),
}
}
#[must_use]
pub const fn volume_name(&self) -> Option<&ArchivePath> {
self.volume_name.as_ref()
}
#[must_use]
pub fn comment(&self) -> Option<&[u8]> {
self.comment.as_deref()
}
#[must_use]
pub fn extensions(&self) -> &[Extension] {
&self.extensions
}
#[must_use]
pub fn with_volume_name(mut self, volume_name: ArchivePath) -> Self {
self.volume_name = Some(volume_name);
self
}
#[must_use]
pub fn with_comment(mut self, comment: impl Into<Vec<u8>>) -> Self {
self.comment = Some(comment.into());
self
}
#[must_use]
pub fn with_extension(mut self, extension: Extension) -> Self {
self.extensions.push(extension);
self
}
}