tarzan 0.4.0

Random-access, seekable .tar.zst archives with an embedded table-of-contents index
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::rc::Rc;

use anyhow::{Context, Result, bail};
use sha2::{Digest, Sha256};
use tracing::debug;

use crate::format::{
    footer::{Footer, encode_footer_frame},
    identity,
    toc::{ChunkInfo, EntryType, TocFrame, TocMember},
};
use crate::io::{CountingWriter, HashingWriter};

#[derive(Debug, Clone)]
pub struct WrapOptions {
    pub chunk_size: usize,
    pub level: i32,
}

impl Default for WrapOptions {
    fn default() -> Self {
        Self {
            chunk_size: 4 * 1024 * 1024,
            level: 3,
        }
    }
}

impl WrapOptions {
    pub fn chunk_size(mut self, chunk_size: usize) -> Self {
        self.chunk_size = chunk_size;
        self
    }

    pub fn level(mut self, level: i32) -> Self {
        self.level = level;
        self
    }
}

/// A sliding window of raw tar bytes captured from the input stream.
///
/// Holds the bytes from absolute offset `base` up to whatever the tar reader
/// has consumed so far. Frames are sliced out of this buffer and the consumed
/// prefix is drained, so peak memory stays bounded by the configured chunk
/// size rather than by the size of the whole archive.
struct Window {
    buf: Vec<u8>,
    base: u64,
}

impl Window {
    /// Absolute offset one past the last captured byte.
    fn end(&self) -> u64 {
        self.base + self.buf.len() as u64
    }

    /// Borrows the captured bytes for an absolute range derived from
    /// untrusted tar metadata.
    fn try_slice(&self, start: u64, end: u64) -> Result<&[u8]> {
        if start > end {
            bail!("invalid tar byte range: {start}..{end}");
        }
        if start < self.base || end > self.end() {
            bail!(
                "tar stream ended before byte range {start}..{end} was captured \
                 (captured {}..{})",
                self.base,
                self.end()
            );
        }
        let lo = (start - self.base) as usize;
        let hi = (end - self.base) as usize;
        Ok(&self.buf[lo..hi])
    }

    /// Discards captured bytes before absolute offset `offset`.
    fn drain_to(&mut self, offset: u64) {
        let n = (offset - self.base) as usize;
        self.buf.drain(..n);
        self.base = offset;

        let target = self.buf.len().max(8 * 1024 * 1024);
        if self.buf.capacity() > target.saturating_mul(2) {
            self.buf.shrink_to(target);
        }
    }
}

/// A `Read` adapter that copies every byte it serves into a shared [`Window`].
///
/// The tar reader reads its headers and member data through this adapter,
/// which lets `wrap` recover the exact raw tar bytes — including PAX/GNU
/// extension headers, which the `tar` crate consumes without exposing — and
/// compress them verbatim.
struct CapturingReader<R> {
    inner: R,
    window: Rc<RefCell<Window>>,
}

impl<R: Read> Read for CapturingReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.inner.read(buf)?;
        self.window.borrow_mut().buf.extend_from_slice(&buf[..n]);
        Ok(n)
    }
}

/// A member parsed but not yet emitted: its trailing padding is only captured
/// once the *next* entry's header has been read.
enum Pending {
    /// A small member, awaiting placement into the current group.
    Small { idx: usize, region_size: u64 },
    /// A large member whose final (sub-chunk-size) frame is not yet emitted.
    Large { idx: usize, entry_end: u64 },
}

/// Wraps an existing tar stream into a tarzan archive.
///
/// The input is processed as a stream: member data is compressed and written
/// incrementally, so peak memory is bounded by `opts.chunk_size` rather than
/// by the size of the input.
pub fn wrap<R: Read, W: Write>(input: R, output: W, opts: WrapOptions) -> Result<()> {
    wrap_with(input, output, opts, |_| {})
}

/// Like [`wrap`], but invokes `on_member` with each member's TOC entry as soon
/// as that member has been fully compressed. Useful for progress reporting.
///
/// Members smaller than `opts.chunk_size` are grouped together into a shared
/// zstd frame, so a member is reported once the group it belongs to has been
/// flushed.
pub fn wrap_with<R, W, F>(input: R, output: W, opts: WrapOptions, mut on_member: F) -> Result<()>
where
    R: Read,
    W: Write,
    F: FnMut(&TocMember),
{
    if !crate::io::is_nonzero(opts.chunk_size) {
        bail!("chunk size must be greater than zero");
    }
    let chunk_size = opts.chunk_size as u64;
    let level = opts.level;

    let window = Rc::new(RefCell::new(Window {
        buf: Vec::new(),
        base: 0,
    }));
    let mut archive = tar::Archive::new(CapturingReader {
        inner: input,
        window: Rc::clone(&window),
    });

    // Everything from the identity frame through the TOC frame is hashed; the
    // footer (which carries the hash) is written outside the hashed region.
    let mut output = HashingWriter::new(output);

    let id_frame = identity::identity_frame();
    output
        .write_all(&id_frame)
        .context("failed to write identity frame")?;
    let mut pos = id_frame.len() as u64;

    let mut members: Vec<TocMember> = Vec::new();
    // Members accumulated for the current shared frame, with each member's
    // region size; the group covers `[next_chunk_start, next_chunk_start + group_size)`.
    let mut group: Vec<(usize, u64)> = Vec::new();
    let mut group_size: u64 = 0;
    // Absolute tar offset where the next not-yet-emitted frame begins; kept
    // equal to the window's base.
    let mut next_chunk_start: u64 = 0;
    // Absolute tar offset where the most recently indexed member's region ends.
    // Non-indexed entries (e.g. PAX global headers) are folded into the next
    // indexed member's region so extraction offsets remain consistent.
    let mut prev_indexed_entry_end: u64 = 0;
    // Absolute tar offset where the most recently parsed tar entry ends.
    let mut last_entry_end: u64 = 0;
    let mut pending: Option<Pending> = None;
    let mut scratch = vec![0u8; 64 * 1024];

    {
        let entries = archive.entries().context("failed to read tar entries")?;
        for entry in entries {
            let mut entry = entry.context("failed to read tar entry")?;

            // The previous member's region is now fully captured (the tar
            // reader consumed this entry's extension headers and 512-byte
            // header to reach it).
            match pending.take() {
                Some(Pending::Small { idx, region_size }) => {
                    add_to_group(
                        &mut output,
                        &mut pos,
                        &window,
                        level,
                        &mut members,
                        &mut group,
                        &mut group_size,
                        &mut next_chunk_start,
                        &mut on_member,
                        chunk_size,
                        idx,
                        region_size,
                    )?;
                }
                Some(Pending::Large { idx, entry_end }) => {
                    let end = entry_end.min(window.borrow().end());
                    push_frame(
                        &mut output,
                        &mut pos,
                        &window,
                        next_chunk_start,
                        end,
                        level,
                        &mut members[idx].chunks,
                    )?;
                    window.borrow_mut().drain_to(end);
                    next_chunk_start = end;
                    on_member(&members[idx]);
                }
                None => {}
            }

            let metadata = read_member_metadata(&mut entry)?;
            // PAX local/global extension headers can override entry size. If
            // both PAX size and in-header size are present (non-zero) on a
            // non-sparse entry, they must agree; disagreement is hostile.
            if matches!(
                entry.header().entry_type(),
                tar::EntryType::Regular | tar::EntryType::Continuous
            ) && !metadata.is_sparse
                && let Some(pax_size) = metadata.pax_size
            {
                let header_size = entry
                    .header()
                    .entry_size()
                    .context("failed to read in-header entry size")?;
                if header_size != 0 && header_size != pax_size {
                    bail!(
                        "refusing to wrap {}: PAX size={} disagrees with in-header size={}",
                        metadata.member.path,
                        pax_size,
                        header_size
                    );
                }
            }
            // For GNU sparse the tar reader consumes the main header *and*
            // any continuation headers before yielding the entry, so the
            // data starts wherever the window currently ends. The on-disk
            // data length is the raw size field (`entry_size`), not the
            // expanded logical size that `entry.size()` reports. For every
            // other entry type the two are identical and the window's end is
            // exactly `header_pos + 512`.
            let on_disk_size = if metadata.is_sparse {
                entry
                    .header()
                    .entry_size()
                    .context("failed to read sparse entry on-disk size")?
            } else {
                metadata.member.size
            };
            let data_start = window.borrow().end();
            let entry_end = data_start
                .checked_add(padded_data_size(on_disk_size)?)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "tar entry {} ending at {data_start}+padded({on_disk_size}) overflows",
                        metadata.member.path
                    )
                })?;
            let region_size = entry_end
                .checked_sub(prev_indexed_entry_end)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "tar entry {} starts before the previous entry ended",
                        metadata.member.path
                    )
                })?;
            last_entry_end = entry_end;

            // Keep global PAX headers in the compressed byte stream but omit
            // them from TOC member indexing.
            if metadata.skip_toc {
                continue;
            }

            prev_indexed_entry_end = entry_end;
            let idx = members.len();
            members.push(metadata.member);

            {
                let w = window.borrow();
                debug!(
                    members_len = members.len(),
                    window_len = w.buf.len(),
                    window_capacity = w.buf.capacity(),
                    region_size,
                    pos,
                    "wrap loop state"
                );
            }

            if region_size >= chunk_size {
                // Large member: it gets its own frames, split at chunk_size.
                // Flush any pending group so the large member starts a frame.
                flush_group(
                    &mut output,
                    &mut pos,
                    &window,
                    level,
                    &mut members,
                    &mut group,
                    &mut group_size,
                    &mut next_chunk_start,
                    &mut on_member,
                )?;

                // Pull the data through the window ourselves, emitting a frame
                // whenever a full chunk_size has accumulated. Letting the tar
                // reader skip the data instead would buffer the whole member.
                // For regular files we also fold each scratch read into a
                // streaming SHA-256 of the member's content; chunks get drained
                // from the window before we could go back and hash from there.
                let is_file = matches!(members[idx].entry_type, EntryType::File);
                let mut content_hasher = is_file.then(Sha256::new);
                let mut md5_ctx = is_file.then(md5::Context::new);
                let mut data_left = members[idx].size;
                while data_left > 0 {
                    let want = data_left.min(scratch.len() as u64) as usize;
                    let n = entry
                        .read(&mut scratch[..want])
                        .context("failed to read entry data")?;
                    if n == 0 {
                        bail!(
                            "unexpected end of input while reading {}",
                            members[idx].path
                        );
                    }
                    if let Some(h) = &mut content_hasher {
                        h.update(&scratch[..n]);
                    }
                    if let Some(ctx) = &mut md5_ctx {
                        ctx.consume(&scratch[..n]);
                    }
                    data_left -= n as u64;
                    while window.borrow().end() - next_chunk_start >= chunk_size {
                        let end = next_chunk_start + chunk_size;
                        push_frame(
                            &mut output,
                            &mut pos,
                            &window,
                            next_chunk_start,
                            end,
                            level,
                            &mut members[idx].chunks,
                        )?;
                        window.borrow_mut().drain_to(end);
                        next_chunk_start = end;
                    }
                }
                if let Some(h) = content_hasher {
                    members[idx].content_sha256 = Some(finalize_sha256_hex(h));
                }
                if let Some(ctx) = md5_ctx {
                    members[idx].content_md5 = Some(format!("{:x}", ctx.compute()));
                }

                pending = Some(Pending::Large { idx, entry_end });
            } else {
                // Small member: grouped into a shared frame. Its data is left
                // for the tar reader to skip, which still captures it.
                pending = Some(Pending::Small { idx, region_size });
            }
        }
    }

    // Drain whatever the tar reader left unread: the second end-of-archive zero
    // block and any blocking-factor padding.
    let mut reader = archive.into_inner();
    std::io::copy(&mut reader, &mut std::io::sink()).context("failed to drain trailing bytes")?;
    let total = window.borrow().end();

    match pending.take() {
        Some(Pending::Small { idx, region_size }) => {
            add_to_group(
                &mut output,
                &mut pos,
                &window,
                level,
                &mut members,
                &mut group,
                &mut group_size,
                &mut next_chunk_start,
                &mut on_member,
                chunk_size,
                idx,
                region_size,
            )?;
        }
        Some(Pending::Large { idx, entry_end }) => {
            let end = entry_end.min(total);
            push_frame(
                &mut output,
                &mut pos,
                &window,
                next_chunk_start,
                end,
                level,
                &mut members[idx].chunks,
            )?;
            window.borrow_mut().drain_to(end);
            next_chunk_start = end;
            on_member(&members[idx]);
        }
        None => {}
    }

    validate_end_of_archive(&window, last_entry_end, total)?;

    flush_group(
        &mut output,
        &mut pos,
        &window,
        level,
        &mut members,
        &mut group,
        &mut group_size,
        &mut next_chunk_start,
        &mut on_member,
    )?;

    // Trailing frame: end-of-archive marker and padding. It has no TOC member,
    // so its ChunkInfo is discarded.
    if total > next_chunk_start {
        let mut discard = Vec::new();
        push_frame(
            &mut output,
            &mut pos,
            &window,
            next_chunk_start,
            total,
            level,
            &mut discard,
        )?;
    }

    let toc = TocFrame {
        tarzan_version: 2,
        members,
    };
    let toc_offset = pos;
    // Stream the TOC straight to `output` rather than buffering the whole
    // compressed frame in memory. For large archives the uncompressed JSON
    // alone can be many GB.
    let toc_frame_size = crate::format::toc::write_toc_frame(&mut output, &toc, opts.level)
        .context("failed to write TOC frame")?;

    // The footer sits outside the hashed region and carries the hash of
    // everything before it (identity + data frames + TOC).
    let (mut inner, archive_xxhash64) = output.finish();
    let footer = encode_footer_frame(&Footer {
        toc_offset,
        toc_frame_size,
        archive_xxhash64,
    });
    inner
        .write_all(&footer)
        .context("failed to write footer frame")?;
    inner.flush().context("failed to flush wrapped archive")?;

    Ok(())
}

/// Adds a small member to the current group, flushing the group as needed to
/// keep it within `chunk_size`.
#[allow(clippy::too_many_arguments)]
fn add_to_group<W, F>(
    output: &mut W,
    pos: &mut u64,
    window: &Rc<RefCell<Window>>,
    level: i32,
    members: &mut [TocMember],
    group: &mut Vec<(usize, u64)>,
    group_size: &mut u64,
    next_chunk_start: &mut u64,
    on_member: &mut F,
    chunk_size: u64,
    idx: usize,
    region_size: u64,
) -> Result<()>
where
    W: Write,
    F: FnMut(&TocMember),
{
    // Hash the member's content from the window before any flush could drain
    // it. For small members the tar reader captured the content bytes when it
    // skipped past them to reach the next entry; those bytes live in the
    // window until the group they belong to is flushed.
    if matches!(members[idx].entry_type, EntryType::File) {
        let content_start = members[idx].tar_offset + 512;
        let content_end = content_start
            .checked_add(members[idx].size)
            .ok_or_else(|| {
                anyhow::anyhow!("member {} content range overflows", members[idx].path)
            })?;
        let w = window.borrow();
        let content = w.try_slice(content_start, content_end)?;
        members[idx].content_sha256 = Some(sha256_hex(content));
        members[idx].content_md5 = Some(format!("{:x}", md5::compute(content)));
    }

    let would_exceed_chunk = match group_size.checked_add(region_size) {
        Some(size) => size > chunk_size,
        None => true,
    };
    if !group.is_empty() && would_exceed_chunk {
        flush_group(
            output,
            pos,
            window,
            level,
            members,
            group,
            group_size,
            next_chunk_start,
            on_member,
        )?;
    }
    group.push((idx, region_size));
    *group_size = group_size
        .checked_add(region_size)
        .ok_or_else(|| anyhow::anyhow!("grouped tar byte range overflows"))?;
    if *group_size >= chunk_size {
        flush_group(
            output,
            pos,
            window,
            level,
            members,
            group,
            group_size,
            next_chunk_start,
            on_member,
        )?;
    }
    Ok(())
}

/// Compresses the grouped members as one shared zstd frame and records a
/// `ChunkInfo` for each, then drains the window and reports the members.
#[allow(clippy::too_many_arguments)]
fn flush_group<W, F>(
    output: &mut W,
    pos: &mut u64,
    window: &Rc<RefCell<Window>>,
    level: i32,
    members: &mut [TocMember],
    group: &mut Vec<(usize, u64)>,
    group_size: &mut u64,
    next_chunk_start: &mut u64,
    on_member: &mut F,
) -> Result<()>
where
    W: Write,
    F: FnMut(&TocMember),
{
    if group.is_empty() {
        return Ok(());
    }
    let start = *next_chunk_start;
    let end = start
        .checked_add(*group_size)
        .ok_or_else(|| anyhow::anyhow!("grouped tar byte range overflows"))?;

    if let Some((compressed_offset, compressed_size)) =
        compress_frame(output, pos, window, start, end, level)?
    {
        let mut frame_offset = 0u64;
        for (idx, region_size) in group.iter() {
            members[*idx].chunks.push(ChunkInfo {
                compressed_offset,
                compressed_size,
                uncompressed_size: *region_size,
                frame_offset,
            });
            frame_offset = frame_offset
                .checked_add(*region_size)
                .ok_or_else(|| anyhow::anyhow!("grouped frame offset overflows"))?;
        }
    }

    window.borrow_mut().drain_to(end);
    *next_chunk_start = end;
    for (idx, _) in group.iter() {
        on_member(&members[*idx]);
    }
    group.clear();
    *group_size = 0;
    {
        let w = window.borrow();
        debug!(
            members_len = members.len(),
            window_len = w.buf.len(),
            window_capacity = w.buf.capacity(),
            pos = *pos,
            "flush_group state"
        );
    }
    Ok(())
}

/// Compresses the window's `[start, end)` bytes as a standalone zstd frame and
/// records a single `ChunkInfo` for it (`frame_offset` is zero).
fn push_frame<W: Write>(
    output: &mut W,
    pos: &mut u64,
    window: &Rc<RefCell<Window>>,
    start: u64,
    end: u64,
    level: i32,
    chunks: &mut Vec<ChunkInfo>,
) -> Result<()> {
    if let Some((compressed_offset, compressed_size)) =
        compress_frame(output, pos, window, start, end, level)?
    {
        chunks.push(ChunkInfo {
            compressed_offset,
            compressed_size,
            uncompressed_size: end - start,
            frame_offset: 0,
        });
    }
    Ok(())
}

/// Compresses the window's `[start, end)` bytes as an independent zstd frame,
/// appends it to `output`, and advances `pos`.
///
/// The encoder is configured to embed a 4-byte XXHash64 content checksum in
/// every frame; the standard zstd decoder verifies it automatically on the
/// way out, so a corrupted chunk fails at decompress time without any
/// further work on our side.
///
/// Returns the frame's compressed offset and size — or `None` if the range
/// is empty.
fn compress_frame<W: Write>(
    output: &mut W,
    pos: &mut u64,
    window: &Rc<RefCell<Window>>,
    start: u64,
    end: u64,
    level: i32,
) -> Result<Option<(u64, u64)>> {
    let window = window.borrow();
    let bytes = window.try_slice(start, end)?;
    if bytes.is_empty() {
        return Ok(None);
    }

    let compressed_offset = *pos;
    let compressed_size = {
        let mut counting = CountingWriter::new(&mut *output);
        let mut encoder = crate::zstd_impl::Encoder::new(&mut counting, level)
            .context("failed to create zstd encoder")?;
        encoder
            .include_checksum(true)
            .context("failed to enable zstd content checksum")?;
        encoder
            .write_all(bytes)
            .context("failed to compress chunk")?;
        encoder.finish().context("failed to finish zstd frame")?;
        counting.bytes_written()
    };
    *pos += compressed_size;

    Ok(Some((compressed_offset, compressed_size)))
}

fn validate_end_of_archive(
    window: &Rc<RefCell<Window>>,
    marker_start: u64,
    total: u64,
) -> Result<()> {
    let marker_end = marker_start
        .checked_add(1024)
        .ok_or_else(|| anyhow::anyhow!("tar end-of-archive marker offset overflows"))?;
    if total < marker_end {
        bail!(
            "tar stream ended before the two-block end-of-archive marker \
             (need at least {marker_end} bytes, got {total})"
        );
    }

    let window = window.borrow();
    let marker = window.try_slice(marker_start, marker_end)?;
    if !marker.iter().all(|byte| *byte == 0) {
        bail!("tar stream is missing the two-block end-of-archive marker");
    }

    Ok(())
}

fn padded_data_size(size: u64) -> Result<u64> {
    size.checked_add(511)
        .map(|size| size / 512 * 512)
        .ok_or_else(|| anyhow::anyhow!("tar entry size {size} overflows"))
}

#[derive(Default)]
struct PaxData {
    has_gnu_sparse_keys: bool,
    pax_size: Option<u64>,
    mtime: Option<(i64, u32)>,
    atime: Option<(i64, u32)>,
    ctime: Option<(i64, u32)>,
    mode: Option<u32>,
    uname: Option<String>,
    gname: Option<String>,
    xattrs: BTreeMap<String, Vec<u8>>,
}

struct MemberMetadata {
    member: TocMember,
    is_sparse: bool,
    skip_toc: bool,
    pax_size: Option<u64>,
}

/// Reads an entry's metadata into a partial `TocMember` (with no `chunks`).
///
/// A regular-file entry whose merged PAX header carries any `GNU.sparse.*`
/// key is a PAX-encoded sparse file (GNU tar formats 0.0, 0.1, 1.0). The
/// `tar` crate does not interpret these as sparse, so the data on disk is
/// the sparse-encoded blob, not the logical file content. Recording such
/// an entry as `EntryType::File` would let consumers extract or hash the
/// encoded blob and believe they had the original file. We downgrade it
/// to `EntryType::Other` so the small-/large-member content-hashing path
/// is skipped and tarzan's random-access tools refuse to treat it as a
/// regular file. The raw tar bytes still round-trip verbatim, so
/// `zstd -d | tar x` recovers the original file correctly.
fn read_member_metadata<R: Read>(entry: &mut tar::Entry<'_, R>) -> Result<MemberMetadata> {
    let pax = read_pax_data(entry)?;
    let header_type = entry.header().entry_type();
    let is_pax_sparse = matches!(header_type, tar::EntryType::Regular) && pax.has_gnu_sparse_keys;
    let skip_toc = header_type.is_pax_global_extensions();
    let entry_type = if is_pax_sparse {
        EntryType::Other
    } else {
        to_entry_type(header_type)
    };
    let header = entry.header();
    let path_bytes_full = entry.path_bytes();
    let path_bytes = path_bytes_full.as_ref();
    let path = String::from_utf8_lossy(path_bytes).into_owned();
    let path_bytes = std::str::from_utf8(path_bytes)
        .is_err()
        .then(|| path_bytes.to_vec());
    // `entry.size()` honours PAX `size=` overrides, which the ustar octal
    // size field cannot encode beyond 8 GB; `header.size()` would return the
    // (typically zero) in-header value and misroute the giant member into
    // the small-member group, leaving its data unflushed in the window.
    //
    // For GNU sparse entries `entry.size()` is the *expanded* (logical) size
    // — what the file looks like after the holes are filled in. The raw
    // on-disk data length (sum of the sparse extent lengths) is the
    // `entry_size` field, and is what we use for tar-layout arithmetic in
    // the wrap loop. We still record the logical size here so listings and
    // extraction tooling show what users expect.
    let size = entry.size();
    let mut mode = header.mode().context("failed to read entry mode")?;
    if let Some(pax_mode) = pax.mode {
        mode = pax_mode;
    }
    let uid = header.uid().context("failed to read entry uid")?;
    let gid = header.gid().context("failed to read entry gid")?;
    let mut mtime = header.mtime().context("failed to read entry mtime")? as i64;
    let mut mtime_ns = None;
    if let Some((sec, nsec)) = pax.mtime {
        mtime = sec;
        mtime_ns = Some(nsec);
    }
    let tar_offset = entry.raw_header_position();
    let (link_target, link_target_bytes) = match entry.link_name_bytes().map(|c| c.into_owned()) {
        None => (None, None),
        Some(bytes) => {
            let display = String::from_utf8_lossy(&bytes).into_owned();
            let raw = std::str::from_utf8(&bytes).is_err().then_some(bytes);
            (Some(display), raw)
        }
    };

    let pax_size = pax.pax_size;
    let atime = pax.atime;
    let ctime = pax.ctime;
    let uname = pax.uname.clone();
    let gname = pax.gname.clone();
    let xattrs = (!pax.xattrs.is_empty()).then_some(pax.xattrs.clone());
    let raw_type_byte = (entry_type == EntryType::Other).then(|| header.as_bytes()[156]);

    let mut member = TocMember {
        path,
        path_bytes,
        entry_type,
        raw_type_byte,
        size,
        mode,
        uid,
        gid,
        mtime,
        mtime_ns,
        atime: atime.map(|(sec, _)| sec),
        atime_ns: atime.map(|(_, nsec)| nsec),
        ctime: ctime.map(|(sec, _)| sec),
        ctime_ns: ctime.map(|(_, nsec)| nsec),
        uname,
        gname,
        xattrs,
        tar_offset,
        link_target,
        link_target_bytes,
        // Filled in later: from the window during small-member grouping,
        // from a streaming hasher in the large-member read loop.
        content_sha256: None,
        content_md5: None,
        chunks: Vec::new(),
    };
    if skip_toc {
        // A global PAX header is metadata, not a file-like member.
        member.content_sha256 = None;
        member.content_md5 = None;
    }

    Ok(MemberMetadata {
        member,
        is_sparse: entry.header().entry_type().is_gnu_sparse(),
        skip_toc,
        pax_size,
    })
}

fn sha256_hex(data: &[u8]) -> String {
    let hash = Sha256::digest(data);
    hash.iter().map(|b| format!("{b:02x}")).collect()
}

fn finalize_sha256_hex(hasher: Sha256) -> String {
    hasher
        .finalize()
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect()
}

/// Returns true if any of the entry's merged PAX keys begin with `GNU.sparse.`,
/// indicating a PAX-encoded sparse file (formats 0.0, 0.1, 1.0).
fn read_pax_data<R: Read>(entry: &mut tar::Entry<'_, R>) -> Result<PaxData> {
    let mut data = PaxData::default();
    let Some(exts) = entry
        .pax_extensions()
        .context("failed to read entry PAX extensions")?
    else {
        return Ok(data);
    };
    for ext in exts {
        let ext = ext.context("malformed PAX extension")?;
        let key = ext.key_bytes();
        let value_bytes = ext.value_bytes();
        if key.starts_with(b"GNU.sparse.") {
            data.has_gnu_sparse_keys = true;
        }

        match key {
            b"size" => {
                if let Ok(s) = std::str::from_utf8(value_bytes)
                    && let Ok(n) = s.trim().parse::<u64>()
                {
                    data.pax_size = Some(n);
                }
            }
            b"mtime" => {
                if let Ok(s) = std::str::from_utf8(value_bytes)
                    && let Some(ts) = parse_pax_timestamp(s)
                {
                    data.mtime = Some(ts);
                }
            }
            b"atime" => {
                if let Ok(s) = std::str::from_utf8(value_bytes)
                    && let Some(ts) = parse_pax_timestamp(s)
                {
                    data.atime = Some(ts);
                }
            }
            b"ctime" => {
                if let Ok(s) = std::str::from_utf8(value_bytes)
                    && let Some(ts) = parse_pax_timestamp(s)
                {
                    data.ctime = Some(ts);
                }
            }
            b"mode" => {
                if let Ok(s) = std::str::from_utf8(value_bytes)
                    && let Ok(mode) = s.parse::<u32>()
                {
                    data.mode = Some(mode);
                }
            }
            b"uname" => {
                if let Ok(s) = std::str::from_utf8(value_bytes) {
                    data.uname = Some(s.to_owned());
                }
            }
            b"gname" => {
                if let Ok(s) = std::str::from_utf8(value_bytes) {
                    data.gname = Some(s.to_owned());
                }
            }
            _ => {
                if let Some(suffix) = key.strip_prefix(b"SCHILY.xattr.") {
                    data.xattrs.insert(
                        String::from_utf8_lossy(suffix).into_owned(),
                        value_bytes.to_vec(),
                    );
                } else if let Some(suffix) = key.strip_prefix(b"LIBARCHIVE.xattr.") {
                    data.xattrs.insert(
                        String::from_utf8_lossy(suffix).into_owned(),
                        value_bytes.to_vec(),
                    );
                }
            }
        }
    }
    Ok(data)
}

fn parse_pax_timestamp(raw: &str) -> Option<(i64, u32)> {
    let s = raw.trim();
    if s.is_empty() {
        return None;
    }

    let (negative, body) = if let Some(rest) = s.strip_prefix('-') {
        (true, rest)
    } else if let Some(rest) = s.strip_prefix('+') {
        (false, rest)
    } else {
        (false, s)
    };

    let (whole_str, frac_str) = match body.split_once('.') {
        Some((w, f)) => (w, f),
        None => (body, ""),
    };

    let whole: i64 = if whole_str.is_empty() {
        0
    } else {
        whole_str.parse().ok()?
    };

    if !frac_str.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let mut ns = 0u32;
    for (i, b) in frac_str.bytes().enumerate() {
        if i >= 9 {
            break;
        }
        ns = ns.checked_mul(10)?.checked_add((b - b'0') as u32)?;
    }
    for _ in frac_str.len().min(9)..9 {
        ns = ns.checked_mul(10)?;
    }

    if !negative {
        return Some((whole, ns));
    }

    if ns == 0 {
        Some((whole.checked_neg()?, 0))
    } else {
        Some((whole.checked_neg()?.checked_sub(1)?, 1_000_000_000 - ns))
    }
}

fn to_entry_type(t: tar::EntryType) -> EntryType {
    match t {
        tar::EntryType::Regular | tar::EntryType::Continuous => EntryType::File,
        tar::EntryType::Directory => EntryType::Dir,
        tar::EntryType::Symlink => EntryType::Symlink,
        tar::EntryType::Link => EntryType::HardLink,
        tar::EntryType::Char => EntryType::CharDevice,
        tar::EntryType::Block => EntryType::BlockDevice,
        tar::EntryType::Fifo => EntryType::Fifo,
        _ => EntryType::Other,
    }
}