wxtla 0.3.1

Wired eXploring Target Layer Accessor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! Read-only TAR archive surface.

use std::{
  collections::{BTreeMap, HashMap, HashSet},
  sync::Arc,
};

use super::DESCRIPTOR;
use crate::{
  ByteSourceHandle, Error, NamespaceDirectoryEntry, NamespaceNodeId, NamespaceNodeKind,
  NamespaceNodeRecord, Result, SliceDataSource, SourceHints, archives::Archive,
};

const BLOCK_SIZE: u64 = 512;
const HEADER_SIZE: usize = 512;
const ROOT_ENTRY_ID: u64 = 0;
const TYPE_FILE: u8 = b'0';
const TYPE_HARD_LINK: u8 = b'1';
const TYPE_SYMLINK: u8 = b'2';
const TYPE_CHAR: u8 = b'3';
const TYPE_BLOCK: u8 = b'4';
const TYPE_DIR: u8 = b'5';
const TYPE_FIFO: u8 = b'6';
const TYPE_CONTIGUOUS: u8 = b'7';
const TYPE_PAX_LOCAL: u8 = b'x';
const TYPE_PAX_GLOBAL: u8 = b'g';
const TYPE_GNU_LONG_NAME: u8 = b'L';
const TYPE_GNU_LONG_LINK: u8 = b'K';

pub struct TarArchive {
  source: ByteSourceHandle,
  entries: Vec<TarEntry>,
  path_to_id: HashMap<String, NamespaceNodeId>,
}

#[derive(Clone)]
struct TarEntry {
  record: NamespaceNodeRecord,
  children: Vec<NamespaceDirectoryEntry>,
  data: TarEntryData,
}

#[derive(Clone)]
enum TarEntryData {
  None,
  File { offset: u64, size: u64 },
  HardLink { target_path: String },
}

#[derive(Clone)]
struct TarEntryBuilder {
  kind: NamespaceNodeKind,
  size: u64,
  data: TarEntryData,
}

#[derive(Clone, Debug)]
pub(crate) struct TarHeader {
  pub name: String,
  pub size: u64,
  pub typeflag: u8,
  pub link_name: String,
  pub has_ustar_magic: bool,
}

impl TarArchive {
  pub fn open(source: ByteSourceHandle) -> Result<Self> {
    Self::open_with_hints(source, SourceHints::new())
  }

  pub fn open_with_hints(source: ByteSourceHandle, _hints: SourceHints<'_>) -> Result<Self> {
    let source_size = source.size()?;
    let mut offset = 0u64;
    let mut pending_long_name = None::<String>;
    let mut pending_long_link = None::<String>;
    let mut pending_pax = BTreeMap::<String, String>::new();
    let mut global_pax = BTreeMap::<String, String>::new();
    let mut builders = BTreeMap::<String, TarEntryBuilder>::new();

    while offset
      .checked_add(BLOCK_SIZE)
      .is_some_and(|end| end <= source_size)
    {
      let header_bytes = source.read_bytes_at(offset, HEADER_SIZE)?;
      if header_bytes.iter().all(|byte| *byte == 0) {
        break;
      }

      let header = TarHeader::from_bytes(&header_bytes)?;
      let data_offset = offset
        .checked_add(BLOCK_SIZE)
        .ok_or_else(|| Error::invalid_range("tar data offset overflow"))?;
      let padded_size = round_up(header.size, BLOCK_SIZE)?;
      let next_offset = data_offset
        .checked_add(padded_size)
        .ok_or_else(|| Error::invalid_range("tar entry end overflow"))?;
      if next_offset > source_size {
        return Err(Error::invalid_format(
          "tar entry payload exceeds the source size".to_string(),
        ));
      }

      match header.typeflag {
        TYPE_PAX_GLOBAL | TYPE_PAX_LOCAL => {
          let raw = source.read_bytes_at(
            data_offset,
            usize::try_from(header.size)
              .map_err(|_| Error::invalid_range("tar pax payload is too large"))?,
          )?;
          let parsed = parse_pax_records(&raw)?;
          if header.typeflag == TYPE_PAX_GLOBAL {
            global_pax.extend(parsed);
          } else {
            pending_pax = parsed;
          }
        }
        TYPE_GNU_LONG_NAME => {
          let raw = source.read_bytes_at(
            data_offset,
            usize::try_from(header.size)
              .map_err(|_| Error::invalid_range("tar long name payload is too large"))?,
          )?;
          pending_long_name = Some(read_c_string(&raw));
        }
        TYPE_GNU_LONG_LINK => {
          let raw = source.read_bytes_at(
            data_offset,
            usize::try_from(header.size)
              .map_err(|_| Error::invalid_range("tar long link payload is too large"))?,
          )?;
          pending_long_link = Some(read_c_string(&raw));
        }
        _ => {
          let mut effective_pax = global_pax.clone();
          effective_pax.extend(pending_pax.clone());
          let mut path = effective_pax
            .get("path")
            .cloned()
            .or_else(|| pending_long_name.clone())
            .unwrap_or_else(|| header.name.clone());
          let mut link_name = effective_pax
            .get("linkpath")
            .cloned()
            .or_else(|| pending_long_link.clone())
            .unwrap_or_else(|| header.link_name.clone());
          let size = effective_pax
            .get("size")
            .and_then(|value| value.parse::<u64>().ok())
            .unwrap_or(header.size);

          let kind = classify_kind(header.typeflag);
          let is_dir = kind == NamespaceNodeKind::Directory;
          path = normalize_path(&path, is_dir)?;
          link_name = normalize_link_path(&link_name)?;
          if path.is_empty() {
            return Err(Error::invalid_format(
              "tar archive entries must have a non-empty path".to_string(),
            ));
          }

          ensure_parent_directories(&mut builders, &path)?;
          let data = match header.typeflag {
            0 | TYPE_FILE | TYPE_CONTIGUOUS => TarEntryData::File {
              offset: data_offset,
              size,
            },
            TYPE_HARD_LINK => TarEntryData::HardLink {
              target_path: link_name,
            },
            TYPE_SYMLINK | TYPE_DIR | TYPE_CHAR | TYPE_BLOCK | TYPE_FIFO => TarEntryData::None,
            other => {
              return Err(Error::invalid_format(format!(
                "unsupported tar entry typeflag: 0x{other:02x}"
              )));
            }
          };

          let builder = builders.entry(path).or_insert_with(|| TarEntryBuilder {
            kind,
            size,
            data: TarEntryData::None,
          });
          builder.kind = kind;
          builder.size = size;
          builder.data = data;

          pending_long_name = None;
          pending_long_link = None;
          pending_pax.clear();
        }
      }

      offset = next_offset;
    }

    let mut path_to_id = HashMap::new();
    let mut ordered_paths = builders.keys().cloned().collect::<Vec<_>>();
    ordered_paths.sort();
    for (index, path) in ordered_paths.iter().enumerate() {
      path_to_id.insert(path.clone(), NamespaceNodeId::from_u64(index as u64 + 1));
    }

    let mut entries = Vec::with_capacity(ordered_paths.len() + 1);
    entries.push(TarEntry {
      record: NamespaceNodeRecord::new(
        NamespaceNodeId::from_u64(ROOT_ENTRY_ID),
        NamespaceNodeKind::Directory,
        0,
      ),
      children: Vec::new(),
      data: TarEntryData::None,
    });

    for path in &ordered_paths {
      let builder = builders.get(path).ok_or_else(|| {
        Error::invalid_format(format!("missing tar entry builder for path: {path}"))
      })?;
      let id = path_to_id.get(path).cloned().ok_or_else(|| {
        Error::invalid_format(format!("missing tar entry identifier for path: {path}"))
      })?;
      entries.push(TarEntry {
        record: NamespaceNodeRecord::new(id, builder.kind, builder.size).with_path(path.clone()),
        children: Vec::new(),
        data: builder.data.clone(),
      });
    }

    for path in &ordered_paths {
      let child_id = path_to_id.get(path).cloned().ok_or_else(|| {
        Error::invalid_format(format!("missing tar path mapping for path: {path}"))
      })?;
      let child_index = entry_id_to_index(&child_id)?;
      let child_kind = entries
        .get(child_index)
        .ok_or_else(|| Error::not_found(format!("missing tar child entry index: {child_index}")))?
        .record
        .kind;
      let name = relative_name(path);
      let parent_index = match parent_path(path) {
        Some(parent) => entry_id_to_index(path_to_id.get(parent).ok_or_else(|| {
          Error::invalid_format(format!("missing tar parent directory mapping: {parent}"))
        })?)?,
        None => 0,
      };
      entries[parent_index]
        .children
        .push(NamespaceDirectoryEntry::new(name, child_id, child_kind));
    }
    for entry in &mut entries {
      entry
        .children
        .sort_by(|left, right| left.name.cmp(&right.name));
    }

    Ok(Self {
      source,
      entries,
      path_to_id,
    })
  }

  pub fn find_entry_by_path(&self, path: &str) -> Option<NamespaceNodeId> {
    self.path_to_id.get(path).cloned()
  }

  fn entry_ref(&self, entry_id: &NamespaceNodeId) -> Result<&TarEntry> {
    let index = entry_id_to_index(entry_id)?;
    self
      .entries
      .get(index)
      .ok_or_else(|| Error::not_found(format!("missing tar archive entry index: {index}")))
  }

  fn open_file_resolved(
    &self, entry_id: &NamespaceNodeId, seen: &mut HashSet<usize>,
  ) -> Result<ByteSourceHandle> {
    let index = entry_id_to_index(entry_id)?;
    if !seen.insert(index) {
      return Err(Error::invalid_format(
        "tar hard links must not form cycles".to_string(),
      ));
    }
    let entry = self.entry_ref(entry_id)?;
    match &entry.data {
      TarEntryData::File { offset, size } => {
        Ok(Arc::new(SliceDataSource::new(self.source.clone(), *offset, *size)) as ByteSourceHandle)
      }
      TarEntryData::HardLink { target_path } => {
        let target = self.path_to_id.get(target_path).ok_or_else(|| {
          Error::not_found(format!("missing tar hard-link target: {target_path}"))
        })?;
        self.open_file_resolved(target, seen)
      }
      TarEntryData::None => Err(Error::invalid_format(
        "tar entry does not expose readable file data".to_string(),
      )),
    }
  }
}

impl Archive for TarArchive {
  fn descriptor(&self) -> crate::FormatDescriptor {
    DESCRIPTOR
  }

  fn root_entry_id(&self) -> NamespaceNodeId {
    NamespaceNodeId::from_u64(ROOT_ENTRY_ID)
  }

  fn entry(&self, entry_id: &NamespaceNodeId) -> Result<NamespaceNodeRecord> {
    Ok(self.entry_ref(entry_id)?.record.clone())
  }

  fn read_dir(&self, directory_id: &NamespaceNodeId) -> Result<Vec<NamespaceDirectoryEntry>> {
    let entry = self.entry_ref(directory_id)?;
    if entry.record.kind != NamespaceNodeKind::Directory {
      return Err(Error::invalid_format(
        "tar directory reads require a directory entry".to_string(),
      ));
    }
    Ok(entry.children.clone())
  }

  fn open_file(&self, entry_id: &NamespaceNodeId) -> Result<ByteSourceHandle> {
    let entry = self.entry_ref(entry_id)?;
    if !matches!(entry.record.kind, NamespaceNodeKind::File) {
      return Err(Error::invalid_format(
        "tar file opens require a regular file or hard link entry".to_string(),
      ));
    }
    self.open_file_resolved(entry_id, &mut HashSet::new())
  }
}

impl TarHeader {
  pub fn from_bytes(data: &[u8]) -> Result<Self> {
    if data.len() != HEADER_SIZE {
      return Err(Error::invalid_format(
        "tar headers must be exactly 512 bytes".to_string(),
      ));
    }
    if data.iter().all(|byte| *byte == 0) {
      return Err(Error::invalid_format(
        "tar headers must not be all zeroes".to_string(),
      ));
    }

    let stored_checksum = parse_numeric_field(&data[148..156])?;
    let mut checksum_header = [0u8; HEADER_SIZE];
    checksum_header.copy_from_slice(data);
    checksum_header[148..156].fill(b' ');
    let calculated_checksum = checksum_header
      .iter()
      .map(|byte| u64::from(*byte))
      .sum::<u64>();
    if stored_checksum != calculated_checksum {
      return Err(Error::invalid_format(format!(
        "invalid tar header checksum: stored={stored_checksum} calculated={calculated_checksum}"
      )));
    }

    let typeflag = data[156];
    let name = read_c_string(&data[0..100]);
    let prefix = read_c_string(&data[345..500]);
    let full_name = if prefix.is_empty() {
      name.clone()
    } else if name.is_empty() {
      prefix
    } else {
      format!("{prefix}/{name}")
    };
    Ok(Self {
      name: full_name,
      size: parse_numeric_field(&data[124..136])?,
      typeflag,
      link_name: read_c_string(&data[157..257]),
      has_ustar_magic: &data[257..263] == b"ustar\0" || &data[257..263] == b"ustar ",
    })
  }
}

fn parse_pax_records(data: &[u8]) -> Result<BTreeMap<String, String>> {
  let mut offset = 0usize;
  let mut records = BTreeMap::new();
  while offset < data.len() {
    let len_end = data[offset..]
      .iter()
      .position(|byte| *byte == b' ')
      .ok_or_else(|| Error::invalid_format("invalid tar pax record length"))?
      + offset;
    let record_len = std::str::from_utf8(&data[offset..len_end])
      .map_err(|_| Error::invalid_format("tar pax lengths must be ASCII"))?
      .parse::<usize>()
      .map_err(|_| Error::invalid_format("invalid tar pax length value"))?;
    if record_len == 0 || offset + record_len > data.len() {
      return Err(Error::invalid_format(
        "tar pax record extends beyond the payload".to_string(),
      ));
    }
    let record = &data[len_end + 1..offset + record_len - 1];
    let separator = record
      .iter()
      .position(|byte| *byte == b'=')
      .ok_or_else(|| Error::invalid_format("invalid tar pax record contents"))?;
    let key = &record[..separator];
    let value = &record[separator + 1..];
    let key =
      std::str::from_utf8(key).map_err(|_| Error::invalid_format("tar pax keys must be UTF-8"))?;
    let value = String::from_utf8(value.to_vec())
      .map_err(|_| Error::invalid_format("tar pax values must be UTF-8"))?;
    records.insert(key.to_string(), value);
    offset += record_len;
  }
  Ok(records)
}

fn classify_kind(typeflag: u8) -> NamespaceNodeKind {
  match typeflag {
    0 | TYPE_FILE | TYPE_CONTIGUOUS | TYPE_HARD_LINK => NamespaceNodeKind::File,
    TYPE_DIR => NamespaceNodeKind::Directory,
    TYPE_SYMLINK => NamespaceNodeKind::Symlink,
    TYPE_CHAR | TYPE_BLOCK | TYPE_FIFO => NamespaceNodeKind::Special,
    _ => NamespaceNodeKind::Special,
  }
}

fn parse_numeric_field(field: &[u8]) -> Result<u64> {
  if field.is_empty() {
    return Ok(0);
  }
  if field[0] & 0x80 != 0 {
    if field[0] & 0x40 != 0 {
      return Err(Error::invalid_format(
        "negative base-256 tar numbers are not supported".to_string(),
      ));
    }
    let mut value = u64::from(field[0] & 0x3F);
    for byte in &field[1..] {
      value = (value << 8) | u64::from(*byte);
    }
    return Ok(value);
  }

  let trimmed = field
    .iter()
    .copied()
    .skip_while(|byte| *byte == b' ')
    .take_while(|byte| *byte != 0 && *byte != b' ')
    .collect::<Vec<_>>();
  if trimmed.is_empty() {
    return Ok(0);
  }
  let text = std::str::from_utf8(&trimmed)
    .map_err(|_| Error::invalid_format("tar numeric fields must be ASCII"))?;
  u64::from_str_radix(text, 8)
    .map_err(|_| Error::invalid_format(format!("invalid tar numeric field: {text}")))
}

fn read_c_string(field: &[u8]) -> String {
  let end = field
    .iter()
    .position(|byte| *byte == 0)
    .unwrap_or(field.len());
  String::from_utf8_lossy(&field[..end])
    .trim_end()
    .to_string()
}

fn normalize_path(path: &str, is_dir: bool) -> Result<String> {
  let path = path.trim_matches('').trim();
  let path = path.strip_prefix("./").unwrap_or(path);
  let components = path
    .split('/')
    .filter(|component| !component.is_empty() && *component != ".")
    .collect::<Vec<_>>();
  if components.contains(&"..") {
    return Err(Error::invalid_format(
      "tar paths must not contain parent directory traversals".to_string(),
    ));
  }
  let normalized = components.join("/");
  if normalized.is_empty() && !is_dir {
    return Err(Error::invalid_format(
      "tar file entries must contain a non-empty path".to_string(),
    ));
  }
  Ok(normalized)
}

fn normalize_link_path(path: &str) -> Result<String> {
  if path.is_empty() {
    return Ok(String::new());
  }
  normalize_path(path, false)
}

fn ensure_parent_directories(
  builders: &mut BTreeMap<String, TarEntryBuilder>, path: &str,
) -> Result<()> {
  let mut current = path;
  while let Some(parent) = parent_path(current) {
    let entry = builders
      .entry(parent.to_string())
      .or_insert_with(|| TarEntryBuilder {
        kind: NamespaceNodeKind::Directory,
        size: 0,
        data: TarEntryData::None,
      });
    if entry.kind != NamespaceNodeKind::Directory {
      return Err(Error::invalid_format(
        "tar parent path collides with a non-directory entry".to_string(),
      ));
    }
    current = parent;
  }
  Ok(())
}

fn parent_path(path: &str) -> Option<&str> {
  path.rsplit_once('/').map(|(parent, _)| parent)
}

fn relative_name(path: &str) -> String {
  path
    .rsplit_once('/')
    .map_or_else(|| path.to_string(), |(_, name)| name.to_string())
}

fn entry_id_to_index(entry_id: &NamespaceNodeId) -> Result<usize> {
  let bytes: [u8; 8] = entry_id.as_bytes().try_into().map_err(|_| {
    Error::invalid_format("tar archive entry identifiers must be native u64 values")
  })?;
  usize::try_from(u64::from_le_bytes(bytes))
    .map_err(|_| Error::invalid_range("tar archive entry index is too large"))
}

fn round_up(value: u64, alignment: u64) -> Result<u64> {
  if value == 0 {
    return Ok(0);
  }
  let remainder = value % alignment;
  if remainder == 0 {
    return Ok(value);
  }
  value
    .checked_add(alignment - remainder)
    .ok_or_else(|| Error::invalid_range("tar padded size overflow"))
}

#[cfg(test)]
mod tests {
  use std::{path::Path, sync::Arc};

  use super::*;
  use crate::ByteSource;

  struct MemDataSource {
    data: Vec<u8>,
  }

  impl ByteSource for MemDataSource {
    fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
      let offset = usize::try_from(offset)
        .map_err(|_| Error::invalid_range("test read offset is too large"))?;
      if offset >= self.data.len() {
        return Ok(0);
      }
      let read = buf.len().min(self.data.len() - offset);
      buf[..read].copy_from_slice(&self.data[offset..offset + read]);
      Ok(read)
    }

    fn size(&self) -> Result<u64> {
      Ok(self.data.len() as u64)
    }
  }

  fn sample_source(relative_path: &str) -> ByteSourceHandle {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
      .join("formats")
      .join(relative_path);
    Arc::new(MemDataSource {
      data: std::fs::read(path).unwrap(),
    })
  }

  fn append_tar_header(
    archive: &mut Vec<u8>, name: &str, typeflag: u8, data: &[u8], link_name: Option<&str>,
  ) {
    let mut header = [0u8; HEADER_SIZE];
    let (prefix, basename) = split_name(name);
    header[0..basename.len()].copy_from_slice(basename.as_bytes());
    header[100..108].copy_from_slice(b"0000644\0");
    header[108..116].copy_from_slice(b"0000000\0");
    header[116..124].copy_from_slice(b"0000000\0");
    write_octal(&mut header[124..136], data.len() as u64);
    write_octal(&mut header[136..148], 0);
    header[156] = typeflag;
    if let Some(link_name) = link_name {
      header[157..157 + link_name.len()].copy_from_slice(link_name.as_bytes());
    }
    header[257..263].copy_from_slice(b"ustar\0");
    header[263..265].copy_from_slice(b"00");
    header[345..345 + prefix.len()].copy_from_slice(prefix.as_bytes());
    header[148..156].fill(b' ');
    let checksum = header.iter().map(|byte| u64::from(*byte)).sum::<u64>();
    write_octal(&mut header[148..156], checksum);

    archive.extend_from_slice(&header);
    archive.extend_from_slice(data);
    archive.resize(
      round_up(archive.len() as u64, BLOCK_SIZE).unwrap() as usize,
      0,
    );
  }

  fn split_name(name: &str) -> (String, String) {
    if name.len() <= 100 {
      return (String::new(), name.to_string());
    }
    let split_index = name.rfind('/').unwrap();
    (
      name[..split_index].to_string(),
      name[split_index + 1..].to_string(),
    )
  }

  fn write_octal(field: &mut [u8], value: u64) {
    let width = field.len() - 1;
    let text = format!("{value:0width$o}\0", width = width - 1);
    let start = field.len() - text.len();
    field.fill(b' ');
    field[start..start + text.len()].copy_from_slice(text.as_bytes());
  }

  fn synthetic_tar(
    entries: impl IntoIterator<Item = (String, u8, Vec<u8>, Option<String>)>,
  ) -> Vec<u8> {
    let mut archive = Vec::new();
    for (name, typeflag, data, link_name) in entries {
      append_tar_header(&mut archive, &name, typeflag, &data, link_name.as_deref());
    }
    archive.extend_from_slice(&[0u8; HEADER_SIZE * 2]);
    archive
  }

  fn pax_record(key: &str, value: &str) -> Vec<u8> {
    let mut len = key.len() + value.len() + 3;
    loop {
      let record = format!("{len} {key}={value}\n");
      if record.len() == len {
        return record.into_bytes();
      }
      len = record.len();
    }
  }

  fn md5_hex(data: &[u8]) -> String {
    format!("{:x}", md5::compute(data))
  }

  #[test]
  fn opens_fixture_metadata_and_contents() {
    let archive = TarArchive::open(sample_source("tar/sample.tar")).unwrap();

    let root = archive.read_dir(&archive.root_entry_id()).unwrap();
    assert_eq!(root.len(), 2);
    assert_eq!(root[0].name, "dir");
    assert_eq!(root[1].kind, NamespaceNodeKind::Symlink);

    let hello_id = archive.find_entry_by_path("dir/hello.txt").unwrap();
    let hello_data = archive.open_file(&hello_id).unwrap().read_all().unwrap();
    assert_eq!(hello_data, b"hello from tar\n");
  }

  #[test]
  fn synthesizes_missing_parent_directories() {
    let archive = TarArchive::open(Arc::new(MemDataSource {
      data: synthetic_tar([("a/b/c.txt".to_string(), TYPE_FILE, b"nested".to_vec(), None)]),
    }) as ByteSourceHandle)
    .unwrap();

    let root = archive.read_dir(&archive.root_entry_id()).unwrap();
    assert_eq!(root[0].name, "a");
    let a_id = archive.find_entry_by_path("a").unwrap();
    let a_children = archive.read_dir(&a_id).unwrap();
    assert_eq!(a_children[0].name, "b");
  }

  #[test]
  fn supports_pax_paths_and_hard_links() {
    let long_path = "very/long/path/name/that/exceeds/the/legacy/header/field/limit/file.txt";
    let pax_payload = pax_record("path", long_path);
    let archive_bytes = synthetic_tar([
      ("paxheader".to_string(), TYPE_PAX_LOCAL, pax_payload, None),
      (
        "ignored.txt".to_string(),
        TYPE_FILE,
        b"payload".to_vec(),
        None,
      ),
      (
        "copy.txt".to_string(),
        TYPE_HARD_LINK,
        Vec::new(),
        Some(long_path.to_string()),
      ),
    ]);
    let archive = TarArchive::open(Arc::new(MemDataSource {
      data: archive_bytes,
    }) as ByteSourceHandle)
    .unwrap();

    let file_id = archive.find_entry_by_path(long_path).unwrap();
    let copy_id = archive.find_entry_by_path("copy.txt").unwrap();
    assert_eq!(
      archive.open_file(&file_id).unwrap().read_all().unwrap(),
      b"payload"
    );
    assert_eq!(
      archive.open_file(&copy_id).unwrap().read_all().unwrap(),
      b"payload"
    );
  }

  #[test]
  fn supports_gnu_long_names() {
    let long_path = "this/is/a/gnu/long/path/that/exceeds/the/legacy/header/limit/by/design.txt";
    let archive_bytes = synthetic_tar([
      (
        "././@LongLink".to_string(),
        TYPE_GNU_LONG_NAME,
        [long_path.as_bytes(), &[0]].concat(),
        None,
      ),
      (
        "placeholder".to_string(),
        TYPE_FILE,
        b"gnu-data".to_vec(),
        None,
      ),
    ]);
    let archive = TarArchive::open(Arc::new(MemDataSource {
      data: archive_bytes,
    }) as ByteSourceHandle)
    .unwrap();

    let file_id = archive.find_entry_by_path(long_path).unwrap();
    assert_eq!(
      md5_hex(&archive.open_file(&file_id).unwrap().read_all().unwrap()),
      md5_hex(b"gnu-data")
    );
  }

  #[test]
  fn rejects_bad_header_checksums() {
    let mut archive_bytes =
      synthetic_tar([("file.txt".to_string(), TYPE_FILE, b"bad".to_vec(), None)]);
    archive_bytes[148] ^= 0x01;

    let result = TarArchive::open(Arc::new(MemDataSource {
      data: archive_bytes,
    }) as ByteSourceHandle);

    assert!(matches!(result, Err(Error::InvalidFormat(_))));
  }
}

crate::archives::driver::impl_archive_data_source!(TarArchive);