1use alloc::string::{String, ToString};
8use alloc::vec::Vec;
9
10use crate::meta::{EntryKind, Timestamp};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum PathEncoding {
16 Bytes,
18 Utf8,
20 Utf16Le,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
26pub struct ArchivePath {
27 raw: Vec<u8>,
28 encoding: PathEncoding,
29}
30
31impl ArchivePath {
32 #[must_use]
34 pub fn from_bytes(raw: impl Into<Vec<u8>>) -> Self {
35 Self {
36 raw: raw.into(),
37 encoding: PathEncoding::Bytes,
38 }
39 }
40
41 #[must_use]
43 pub fn from_utf8(path: impl Into<String>) -> Self {
44 Self {
45 raw: path.into().into_bytes(),
46 encoding: PathEncoding::Utf8,
47 }
48 }
49
50 #[must_use]
52 pub fn from_encoded(raw: impl Into<Vec<u8>>, encoding: PathEncoding) -> Self {
53 Self {
54 raw: raw.into(),
55 encoding,
56 }
57 }
58
59 #[must_use]
61 pub fn as_bytes(&self) -> &[u8] {
62 &self.raw
63 }
64
65 #[must_use]
67 pub const fn encoding(&self) -> PathEncoding {
68 self.encoding
69 }
70
71 #[must_use]
73 pub fn display_lossy(&self) -> String {
74 match self.encoding {
75 PathEncoding::Utf16Le => {
76 let units = self
77 .raw
78 .chunks_exact(2)
79 .map(|c| u16::from_le_bytes([c[0], c[1]]));
80 char::decode_utf16(units)
81 .map(|c| c.unwrap_or(char::REPLACEMENT_CHARACTER))
82 .collect()
83 },
84 PathEncoding::Bytes | PathEncoding::Utf8 => {
85 String::from_utf8_lossy(&self.raw).to_string()
86 },
87 }
88 }
89}
90
91#[derive(Debug, Clone, Default, PartialEq, Eq)]
93pub struct Owner {
94 pub uid: Option<u64>,
96 pub gid: Option<u64>,
98 pub user: Option<Vec<u8>>,
100 pub group: Option<Vec<u8>>,
102}
103
104#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
106pub struct EntryTimes {
107 pub modified: Option<Timestamp>,
109 pub accessed: Option<Timestamp>,
111 pub changed: Option<Timestamp>,
113 pub created: Option<Timestamp>,
115}
116
117#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
119pub struct Device {
120 pub major: u64,
122 pub minor: u64,
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128pub struct SparseExtent {
129 pub offset: u64,
131 pub length: u64,
133}
134
135#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct Extension {
138 namespace: String,
139 key: Vec<u8>,
140 value: Vec<u8>,
141}
142
143impl Extension {
144 #[must_use]
146 pub fn new(
147 namespace: impl Into<String>,
148 key: impl Into<Vec<u8>>,
149 value: impl Into<Vec<u8>>,
150 ) -> Self {
151 Self {
152 namespace: namespace.into(),
153 key: key.into(),
154 value: value.into(),
155 }
156 }
157
158 #[must_use]
160 pub fn namespace(&self) -> &str {
161 &self.namespace
162 }
163
164 #[must_use]
166 pub fn key(&self) -> &[u8] {
167 &self.key
168 }
169
170 #[must_use]
172 pub fn value(&self) -> &[u8] {
173 &self.value
174 }
175}
176
177#[derive(Debug, Clone, PartialEq, Eq)]
179pub struct EntryMetadata {
180 kind: EntryKind,
181 path: ArchivePath,
182 size: Option<u64>,
183 link_target: Option<ArchivePath>,
184 mode: Option<u32>,
185 owner: Owner,
186 times: EntryTimes,
187 inode: Option<u64>,
188 links: Option<u64>,
189 device: Option<Device>,
190 referenced_device: Option<Device>,
191 sparse: Vec<SparseExtent>,
192 xattrs: Vec<(Vec<u8>, Vec<u8>)>,
193 acl: Vec<Vec<u8>>,
194 file_flags: u64,
195 checksum: Option<Vec<u8>>,
196 encrypted: bool,
197 comment: Option<Vec<u8>>,
198 extensions: Vec<Extension>,
199}
200
201impl EntryMetadata {
202 #[must_use]
204 pub fn builder(kind: EntryKind, path: ArchivePath) -> EntryMetadataBuilder {
205 EntryMetadataBuilder {
206 inner: Self {
207 kind,
208 path,
209 size: None,
210 link_target: None,
211 mode: None,
212 owner: Owner::default(),
213 times: EntryTimes::default(),
214 inode: None,
215 links: None,
216 device: None,
217 referenced_device: None,
218 sparse: Vec::new(),
219 xattrs: Vec::new(),
220 acl: Vec::new(),
221 file_flags: 0,
222 checksum: None,
223 encrypted: false,
224 comment: None,
225 extensions: Vec::new(),
226 },
227 }
228 }
229
230 #[must_use]
232 pub const fn kind(&self) -> EntryKind {
233 self.kind
234 }
235
236 #[must_use]
238 pub const fn path(&self) -> &ArchivePath {
239 &self.path
240 }
241
242 #[must_use]
244 pub const fn size(&self) -> Option<u64> {
245 self.size
246 }
247
248 #[must_use]
250 pub const fn link_target(&self) -> Option<&ArchivePath> {
251 self.link_target.as_ref()
252 }
253
254 #[must_use]
256 pub const fn mode(&self) -> Option<u32> {
257 self.mode
258 }
259
260 #[must_use]
262 pub const fn owner(&self) -> &Owner {
263 &self.owner
264 }
265
266 #[must_use]
268 pub const fn times(&self) -> EntryTimes {
269 self.times
270 }
271
272 #[must_use]
274 pub const fn inode(&self) -> Option<u64> {
275 self.inode
276 }
277
278 #[must_use]
280 pub const fn links(&self) -> Option<u64> {
281 self.links
282 }
283
284 #[must_use]
286 pub const fn device(&self) -> Option<Device> {
287 self.device
288 }
289
290 #[must_use]
292 pub const fn referenced_device(&self) -> Option<Device> {
293 self.referenced_device
294 }
295
296 #[must_use]
298 pub fn sparse_extents(&self) -> &[SparseExtent] {
299 &self.sparse
300 }
301
302 #[must_use]
304 pub fn xattrs(&self) -> &[(Vec<u8>, Vec<u8>)] {
305 &self.xattrs
306 }
307
308 #[must_use]
310 pub fn acl(&self) -> &[Vec<u8>] {
311 &self.acl
312 }
313
314 #[must_use]
316 pub const fn file_flags(&self) -> u64 {
317 self.file_flags
318 }
319
320 #[must_use]
322 pub fn checksum(&self) -> Option<&[u8]> {
323 self.checksum.as_deref()
324 }
325
326 #[must_use]
328 pub const fn is_encrypted(&self) -> bool {
329 self.encrypted
330 }
331
332 #[must_use]
334 pub fn comment(&self) -> Option<&[u8]> {
335 self.comment.as_deref()
336 }
337
338 #[must_use]
340 pub fn extensions(&self) -> &[Extension] {
341 &self.extensions
342 }
343
344 #[must_use]
350 pub fn into_builder(self) -> EntryMetadataBuilder {
351 EntryMetadataBuilder { inner: self }
352 }
353}
354
355#[derive(Debug)]
357pub struct EntryMetadataBuilder {
358 inner: EntryMetadata,
359}
360
361impl EntryMetadataBuilder {
362 #[must_use]
364 pub const fn kind(mut self, kind: EntryKind) -> Self {
365 self.inner.kind = kind;
366 self
367 }
368
369 #[must_use]
371 pub fn path(mut self, path: ArchivePath) -> Self {
372 self.inner.path = path;
373 self
374 }
375
376 #[must_use]
378 pub const fn size(mut self, size: Option<u64>) -> Self {
379 self.inner.size = size;
380 self
381 }
382
383 #[must_use]
385 pub const fn mode(mut self, mode: Option<u32>) -> Self {
386 self.inner.mode = mode;
387 self
388 }
389
390 #[must_use]
392 pub fn owner(mut self, owner: Owner) -> Self {
393 self.inner.owner = owner;
394 self
395 }
396
397 #[must_use]
399 pub const fn times(mut self, times: EntryTimes) -> Self {
400 self.inner.times = times;
401 self
402 }
403
404 #[must_use]
406 pub fn link_target(mut self, target: Option<ArchivePath>) -> Self {
407 self.inner.link_target = target;
408 self
409 }
410
411 #[must_use]
413 pub const fn inode_and_links(mut self, inode: Option<u64>, links: Option<u64>) -> Self {
414 self.inner.inode = inode;
415 self.inner.links = links;
416 self
417 }
418
419 #[must_use]
421 pub const fn devices(
422 mut self,
423 device: Option<Device>,
424 referenced_device: Option<Device>,
425 ) -> Self {
426 self.inner.device = device;
427 self.inner.referenced_device = referenced_device;
428 self
429 }
430
431 #[must_use]
433 pub fn sparse_extent(mut self, extent: SparseExtent) -> Self {
434 self.inner.sparse.push(extent);
435 self
436 }
437
438 #[must_use]
440 pub fn xattr(mut self, name: impl Into<Vec<u8>>, value: impl Into<Vec<u8>>) -> Self {
441 self.inner.xattrs.push((name.into(), value.into()));
442 self
443 }
444
445 #[must_use]
447 pub fn acl(mut self, value: impl Into<Vec<u8>>) -> Self {
448 self.inner.acl.push(value.into());
449 self
450 }
451
452 #[must_use]
454 pub const fn file_flags(mut self, flags: u64) -> Self {
455 self.inner.file_flags = flags;
456 self
457 }
458
459 #[must_use]
461 pub fn checksum(mut self, checksum: Option<Vec<u8>>) -> Self {
462 self.inner.checksum = checksum;
463 self
464 }
465
466 #[must_use]
468 pub const fn encrypted(mut self, encrypted: bool) -> Self {
469 self.inner.encrypted = encrypted;
470 self
471 }
472
473 #[must_use]
475 pub fn comment(mut self, comment: Option<Vec<u8>>) -> Self {
476 self.inner.comment = comment;
477 self
478 }
479
480 #[must_use]
482 pub fn extension(mut self, extension: Extension) -> Self {
483 self.inner.extensions.push(extension);
484 self
485 }
486
487 #[must_use]
489 pub fn build(self) -> EntryMetadata {
490 self.inner
491 }
492}
493
494#[derive(Debug, Clone, Default, PartialEq, Eq)]
496pub struct ArchiveMetadata {
497 volume_name: Option<ArchivePath>,
498 comment: Option<Vec<u8>>,
499 extensions: Vec<Extension>,
500}
501
502impl ArchiveMetadata {
503 #[must_use]
505 pub const fn new() -> Self {
506 Self {
507 volume_name: None,
508 comment: None,
509 extensions: Vec::new(),
510 }
511 }
512
513 #[must_use]
515 pub const fn volume_name(&self) -> Option<&ArchivePath> {
516 self.volume_name.as_ref()
517 }
518
519 #[must_use]
521 pub fn comment(&self) -> Option<&[u8]> {
522 self.comment.as_deref()
523 }
524
525 #[must_use]
527 pub fn extensions(&self) -> &[Extension] {
528 &self.extensions
529 }
530
531 #[must_use]
533 pub fn with_volume_name(mut self, volume_name: ArchivePath) -> Self {
534 self.volume_name = Some(volume_name);
535 self
536 }
537
538 #[must_use]
540 pub fn with_comment(mut self, comment: impl Into<Vec<u8>>) -> Self {
541 self.comment = Some(comment.into());
542 self
543 }
544
545 #[must_use]
547 pub fn with_extension(mut self, extension: Extension) -> Self {
548 self.extensions.push(extension);
549 self
550 }
551}