Skip to main content

znippy_common/
index.rs

1// index.rs — v0.6 format: blobs stored inline, Arrow IPC is a pure metadata index.
2//
3// File layout:
4//   [blob_0][blob_1]...[blob_N]  — compressed/raw chunk bytes, written as produced
5//   [Arrow IPC stream]           — metadata index, written after all blobs
6//   [8 bytes LE u64]             — byte offset where Arrow IPC starts (footer)
7//
8// Arrow schema columns:
9//   relative_path, chunk_seq, fdata_offset, checksum_group,
10//   compressed, uncompressed_size, blob_offset, blob_size, checksum
11
12use std::collections::HashMap;
13use std::fs::File;
14use std::io::{Read, Seek, SeekFrom};
15use std::path::{Path, PathBuf};
16use std::sync::Arc;
17
18use crate::common_config::StrategicConfig;
19use crate::meta::BlobMeta;
20use crate::plugin::ExtensionRow;
21use crate::{decompress_archive};
22use anyhow::Result;
23use arrow::array::{
24    Array, ArrayRef, BooleanBuilder, FixedSizeBinaryBuilder, Int8Builder, ListBuilder, StringBuilder,
25    UInt32Builder, UInt64Builder,
26};
27use arrow::datatypes::{DataType, Field, Schema};
28use arrow::record_batch::RecordBatch;
29use once_cell::sync::Lazy;
30
31/// Per-file extension metadata carried into the Arrow index.
32/// (plugin_type_id, extracted fields) — None for files with no matching plugin.
33pub type FileExtMeta = Option<(i8, ExtensionRow)>;
34
35/// v0.6 schema: Arrow IPC is a pure metadata index; blobs are stored inline before it.
36/// Base index columns — present in every archive, type-agnostic.
37/// Package-type modules contribute their own columns on top via `schema_fields()`;
38/// the writer composes the on-disk schema with [`compose_index_schema`].
39pub static ZNIPPY_INDEX_SCHEMA: Lazy<Arc<Schema>> = Lazy::new(|| {
40    Arc::new(Schema::new(base_index_fields()))
41});
42
43fn base_index_fields() -> Vec<Field> {
44    vec![
45        Field::new("relative_path", DataType::Utf8, false),
46        Field::new("chunk_seq", DataType::UInt32, false),
47        Field::new("fdata_offset", DataType::UInt64, false),
48        Field::new("compressed", DataType::Boolean, false),
49        Field::new("uncompressed_size", DataType::UInt64, false),
50        Field::new("blob_offset", DataType::UInt64, false),
51        Field::new("blob_size", DataType::UInt64, false),
52        Field::new("checksum", DataType::FixedSizeBinary(32), false),
53    ]
54}
55
56pub fn znippy_index_schema() -> &'static Arc<Schema> {
57    &ZNIPPY_INDEX_SCHEMA
58}
59
60/// Compose the on-disk index schema: base columns, plus — when a module contributes columns —
61/// a `pkg_type` discriminator followed by the module's own `ext_fields`.
62/// With no module fields, this is exactly the base schema (v0.6 layout, directly DuckDB-queryable).
63pub fn compose_index_schema(ext_fields: &[Field]) -> Arc<Schema> {
64    let mut fields = base_index_fields();
65    if !ext_fields.is_empty() {
66        fields.push(Field::new("pkg_type", DataType::Int8, true));
67        fields.extend(ext_fields.iter().cloned());
68    }
69    Arc::new(Schema::new(fields))
70}
71
72/// On-disk archive format version, recorded in the Arrow index schema metadata
73/// under `znippy_format_version`. The reader refuses any archive whose recorded
74/// version is greater than this — a newer format it cannot safely parse — with a
75/// clear error instead of panicking or mis-parsing. Bump only on a real on-disk
76/// format change.
77pub const ZNIPPY_FORMAT_VERSION: u32 = 3;
78
79/// Schema-metadata key holding the on-disk [`ZNIPPY_FORMAT_VERSION`].
80pub const FORMAT_VERSION_KEY: &str = "znippy_format_version";
81
82/// Enforce that an archive's recorded format version is one this reader supports.
83/// `metadata` is an Arrow schema's key/value metadata. A *newer* version yields a
84/// clear error instead of a panic or silent mis-parse; equal/older versions — and
85/// archives with no recorded version — read exactly as before.
86pub fn check_format_version(metadata: &HashMap<String, String>) -> Result<()> {
87    if let Some(raw) = metadata.get(FORMAT_VERSION_KEY) {
88        let version: u32 = raw
89            .parse()
90            .map_err(|_| anyhow::anyhow!("invalid znippy archive format version {raw:?}"))?;
91        anyhow::ensure!(
92            version <= ZNIPPY_FORMAT_VERSION,
93            "znippy archive format v{version} is newer than this reader supports \
94             (max v{ZNIPPY_FORMAT_VERSION}) — upgrade znippy",
95        );
96    }
97    Ok(())
98}
99
100/// Build Arrow schema metadata containing config (no checksum entries — those live in column).
101pub fn build_arrow_metadata_for_config(config: &StrategicConfig) -> HashMap<String, String> {
102    let mut m = HashMap::new();
103    m.insert(FORMAT_VERSION_KEY.into(), ZNIPPY_FORMAT_VERSION.to_string());
104    m.insert("max_core_in_flight".into(), config.max_core_in_flight.to_string());
105    m.insert("max_core_in_compress".into(), config.max_core_in_compress.to_string());
106    m.insert("max_mem_allowed".into(), config.max_mem_allowed.to_string());
107    m.insert("min_free_memory_ratio".into(), config.min_free_memory_ratio.to_string());
108    m.insert("file_split_block_size".into(), config.file_split_block_size.to_string());
109    m.insert("max_chunks".into(), config.max_chunks.to_string());
110    m.insert("compression_level".into(), config.compression_level.to_string());
111    m.insert("zstd_output_buffer_size".into(), config.zstd_output_buffer_size.to_string());
112    m
113}
114
115pub fn extract_config_from_arrow_metadata(
116    metadata: &HashMap<String, String>,
117) -> anyhow::Result<StrategicConfig> {
118    Ok(StrategicConfig {
119        max_core_allowed: 0,
120        max_core_in_flight: metadata
121            .get("max_core_in_flight")
122            .ok_or_else(|| anyhow::anyhow!("Missing 'max_core_in_flight'"))?
123            .parse()?,
124        max_core_in_compress: metadata
125            .get("max_core_in_compress")
126            .ok_or_else(|| anyhow::anyhow!("Missing 'max_core_in_compress'"))?
127            .parse()?,
128        max_mem_allowed: metadata
129            .get("max_mem_allowed")
130            .ok_or_else(|| anyhow::anyhow!("Missing 'max_mem_allowed'"))?
131            .parse()?,
132        min_free_memory_ratio: metadata
133            .get("min_free_memory_ratio")
134            .ok_or_else(|| anyhow::anyhow!("Missing 'min_free_memory_ratio'"))?
135            .parse()?,
136        file_split_block_size: metadata
137            .get("file_split_block_size")
138            .ok_or_else(|| anyhow::anyhow!("Missing 'file_split_block_size'"))?
139            .parse()?,
140        max_chunks: metadata
141            .get("max_chunks")
142            .ok_or_else(|| anyhow::anyhow!("Missing 'max_chunks'"))?
143            .parse()?,
144        compression_level: metadata
145            .get("compression_level")
146            .ok_or_else(|| anyhow::anyhow!("Missing 'compression_level'"))?
147            .parse()?,
148        zstd_output_buffer_size: metadata
149            .get("zstd_output_buffer_size")
150            .ok_or_else(|| anyhow::anyhow!("Missing 'zstd_output_buffer_size'"))?
151            .parse()?,
152    })
153}
154
155/// Build the Arrow metadata index batch from blob positions.
156///
157/// Every row carries its own per-slice BLAKE3 in the `checksum` column
158/// (over the chunk's uncompressed bytes).
159pub fn build_metadata_batch<F>(
160    blobs: &[BlobMeta],
161    path_resolver: F,
162    ext_meta: &[FileExtMeta],
163    ext_fields: &[Field],
164) -> arrow::error::Result<RecordBatch>
165where
166    F: Fn(u64) -> String,
167{
168    let len = blobs.len();
169
170    let mut path_builder = StringBuilder::with_capacity(len, len * 64);
171    let mut seq_builder = UInt32Builder::with_capacity(len);
172    let mut fdata_builder = UInt64Builder::with_capacity(len);
173    let mut compressed_builder = BooleanBuilder::with_capacity(len);
174    let mut size_builder = UInt64Builder::with_capacity(len);
175    let mut blob_offset_builder = UInt64Builder::with_capacity(len);
176    let mut blob_size_builder = UInt64Builder::with_capacity(len);
177    let mut checksum_builder = FixedSizeBinaryBuilder::with_capacity(len, 32);
178
179    for blob in blobs {
180        let m = &blob.chunk_meta;
181        path_builder.append_value(path_resolver(m.file_index));
182        seq_builder.append_value(m.chunk_seq);
183        fdata_builder.append_value(m.fdata_offset);
184        compressed_builder.append_value(m.compressed);
185        size_builder.append_value(m.uncompressed_size);
186        blob_offset_builder.append_value(blob.blob_offset);
187        blob_size_builder.append_value(blob.blob_size);
188        checksum_builder.append_value(m.checksum)?;
189    }
190
191    let mut columns: Vec<ArrayRef> = vec![
192        Arc::new(path_builder.finish()),
193        Arc::new(seq_builder.finish()),
194        Arc::new(fdata_builder.finish()),
195        Arc::new(compressed_builder.finish()),
196        Arc::new(size_builder.finish()),
197        Arc::new(blob_offset_builder.finish()),
198        Arc::new(blob_size_builder.finish()),
199        Arc::new(checksum_builder.finish()),
200    ];
201
202    // Module-contributed columns: a pkg_type discriminator + one column per ext field.
203    if !ext_fields.is_empty() {
204        let mut pkg_type_builder = Int8Builder::with_capacity(len);
205        for blob in blobs {
206            match ext_meta.get(blob.chunk_meta.file_index as usize).and_then(|x| x.as_ref()) {
207                Some((type_id, _)) => pkg_type_builder.append_value(*type_id),
208                None => pkg_type_builder.append_null(),
209            }
210        }
211        columns.push(Arc::new(pkg_type_builder.finish()));
212
213        for field in ext_fields {
214            columns.push(build_ext_column(field, blobs, ext_meta));
215        }
216    }
217
218    RecordBatch::try_new(compose_index_schema(ext_fields), columns)
219}
220
221/// Build one extension column from the per-file `ExtensionRow`, keyed by the field name.
222/// Supports the Arrow types modules declare (Utf8, UInt32, List<Utf8>); other types yield nulls.
223fn build_ext_column(field: &Field, blobs: &[BlobMeta], ext_meta: &[FileExtMeta]) -> ArrayRef {
224    use crate::plugin::ExtensionValue;
225    let len = blobs.len();
226    let value_for = |blob: &BlobMeta| -> Option<&ExtensionValue> {
227        ext_meta
228            .get(blob.chunk_meta.file_index as usize)
229            .and_then(|x| x.as_ref())
230            .and_then(|(_, row)| row.fields.get(field.name()))
231    };
232
233    match field.data_type() {
234        DataType::UInt32 => {
235            let mut b = UInt32Builder::with_capacity(len);
236            for blob in blobs {
237                match value_for(blob) {
238                    Some(ExtensionValue::U32(n)) => b.append_value(*n),
239                    _ => b.append_null(),
240                }
241            }
242            Arc::new(b.finish())
243        }
244        // A list of strings (StrList): cargo `deps`, python `requires_dist`.
245        DataType::List(item) if item.data_type() == &DataType::Utf8 => {
246            let mut b = ListBuilder::new(StringBuilder::new()).with_field(Arc::clone(item));
247            for blob in blobs {
248                match value_for(blob) {
249                    Some(ExtensionValue::StrList(v)) => {
250                        for s in v {
251                            b.values().append_value(s);
252                        }
253                        b.append(true);
254                    }
255                    _ => b.append(false),
256                }
257            }
258            Arc::new(b.finish())
259        }
260        // Default to Utf8 for string-like fields (Str / OptStr).
261        _ => {
262            let mut b = StringBuilder::with_capacity(len, len * 16);
263            for blob in blobs {
264                match value_for(blob) {
265                    Some(ExtensionValue::Str(s)) => b.append_value(s),
266                    Some(ExtensionValue::OptStr(Some(s))) => b.append_value(s),
267                    _ => b.append_null(),
268                }
269            }
270            Arc::new(b.finish())
271        }
272    }
273}
274
275// ─── Multi-index container codec (planned v0.7, see design.md §6) ──────────────
276//
277// A multi-type archive holds several Arrow IPC index streams (one per (pkg_type, repo)
278// sub-znippy, each with its own narrow schema), followed by a manifest stream that points
279// at them, and a footer. The footer layout still *distinguishes* the legacy v0.6 trailer
280// from the v0.7 one, but v0.6 archives are no longer readable — they are detected only to
281// emit a clear "unsupported, re-compress with v0.7" error (see `read_znippy_index_filtered`):
282//
283//   v0.6 single index:  [...index...] [8-byte LE u64 index_offset]
284//   v0.7 multi index:   [...sub-indexes...][manifest] [8-byte MAGIC] [8-byte LE u64 manifest_offset]
285//
286// A reader peeks the 8 bytes preceding the trailing offset: if they equal MAGIC it's a
287// multi-index (v0.7) archive; otherwise it's a legacy v0.6 single index, which the reader
288// rejects rather than parses.
289
290/// Magic preceding the trailing offset that marks a multi-index (v0.7) archive.
291pub const MULTI_INDEX_MAGIC: [u8; 8] = *b"ZNPYMIDX";
292
293/// One entry in the multi-index manifest: a sub-znippy's identity + byte range.
294#[derive(Debug, Clone, PartialEq)]
295pub struct ManifestEntry {
296    pub pkg_type: i8,
297    pub repo: String,
298    pub module_name: String,
299    pub index_offset: u64,
300    pub index_len: u64,
301    pub row_count: u64,
302}
303
304/// Reserved `module_name` for the sorted random-access lookup sub-index.
305/// Its rows are the base index columns re-sorted by `(relative_path, chunk_seq)`,
306/// so external tools can `SELECT … WHERE relative_path = …` in O(log n) and the
307/// native reader can binary-search it. Manifest readers filter this entry out of
308/// the data sub-index set; [`read_znippy_lookup`] reads it explicitly.
309pub const LOOKUP_MODULE: &str = "__znippy_lookup__";
310
311/// Reserved `module_name` for the fst trie blob: an `fst::Map` of
312/// `relative_path → first row index in the lookup sub-index`. Not Arrow IPC —
313/// raw fst bytes — so it must never be parsed as a sub-index.
314pub const TRIE_MODULE: &str = "__znippy_trie__";
315
316/// Reserved `module_name` for the per-artifact detached CMS signatures
317/// (feature `sign`). An Arrow IPC sub-index of `(relative_path, cms)` rows — one
318/// detached CMS `SignedData` per file, over that file's merkle-folded chunk
319/// hashes. Additive: readers that don't know it simply skip it (it is reserved),
320/// so unsigned archives are byte-identical and old readers ignore signed ones.
321pub const SIGN_ARTIFACTS_MODULE: &str = "__znippy_sign_artifacts__";
322
323/// Reserved `module_name` for the per-archive detached CMS signature (feature
324/// `sign`): raw CMS `SignedData` (DER) over the archive root digest.
325pub const SIGN_ARCHIVE_MODULE: &str = "__znippy_sign_archive__";
326
327/// Reserved `module_name` for the **searchable metadata sub-index**: typed
328/// key/value rows, per entry and per archive, sorted by `(key, relative_path)`.
329/// An Arrow IPC sub-index like the lookup — so "which entries carry key `X`" is
330/// answered by one seek to the footer plus one read of this section, at a cost
331/// set by the number of metadata rows and **not** by the payload size.
332///
333/// Reserved, therefore additive: a reader that predates it skips the entry, and
334/// an archive that predates it simply has no such entry — which
335/// [`crate::meta_index::read_archive_meta`] reports as
336/// [`ArchiveMeta::NoMetadata`](crate::meta_index::ArchiveMeta::NoMetadata),
337/// a state distinct from a present-but-empty index.
338pub const META_MODULE: &str = "__znippy_meta__";
339
340/// Reserved `module_name` for the **git oid index** written by the `git` package
341/// format (`znippy-plugin-git`): a raw (non-Arrow) `stree` payload over the first
342/// eight bytes of every object id, mapping to that object's first lookup row.
343/// Raw bytes, never an Arrow IPC stream — it must never be parsed as a sub-index.
344pub const GUNNAR_OID_MODULE: &str = "__gunnar_oid__";
345
346/// Reserved `module_name` for the **git commit graph** written by the `git`
347/// package format: an Arrow IPC sub-index of
348/// `(oid, parents[], tree, committer_time, generation)`. Reserved so ordinary
349/// `list`/`decompress`/iceberg readers skip it; queryable from DuckDB by slicing
350/// its manifest byte range.
351pub const GUNNAR_GRAPH_MODULE: &str = "__gunnar_graph__";
352
353/// Reserved `module_name` for the **git reachability bitmaps** written by the
354/// `git` package format: an Arrow IPC sub-index of `(commit_oid, bitmap)` where
355/// `bitmap` is a serialized roaring bitmap over object ordinals.
356pub const GUNNAR_REACH_MODULE: &str = "__gunnar_reach__";
357
358/// Reserved `module_name` for the **git ref log** written by the `git` package
359/// format: an Arrow IPC sub-index carrying **one RecordBatch per push**.
360///
361/// The batch boundary *is* the transaction boundary. There is no database here
362/// (D18: redb was a database wedged between two archive formats, and it is
363/// gone). A push is durable exactly when its IPC frame is complete on disk; a
364/// frame torn by a crash is not half a push, it is a trailing byte run that no
365/// reader will accept. Recovery is therefore "read forward while frames parse",
366/// never a journal replay.
367pub const GUNNAR_REFS_MODULE: &str = "__gunnar_refs__";
368
369/// Reserved `module_name` for the **git secrets log** written by the `git`
370/// package format: the same one-RecordBatch-per-push shape as
371/// [`GUNNAR_REFS_MODULE`], carrying already-encrypted material only. znippy
372/// never sees plaintext, and ciphertext is never compressed.
373pub const GUNNAR_SECRETS_MODULE: &str = "__gunnar_secrets__";
374
375/// Reserved `module_name` for the **delta map**: which stored chunks are delta
376/// chunks, and what each one's base entry is.
377///
378/// # Why a reserved section and not a column
379///
380/// The obvious place is a `delta_base` column on the data sub-index. It is the
381/// wrong place, and the reason is worth recording because the column is the
382/// first thing anyone reaches for. `base_index_fields()` is the schema of EVERY
383/// archive znippy has ever written; adding a column to it means a
384/// [`ZNIPPY_FORMAT_VERSION`] bump, a new column every existing writer must
385/// supply, and a changed on-disk shape for the iceberg sink and for anyone
386/// pointing DuckDB at an archive — all to describe a property that almost no row
387/// has.
388///
389/// A reserved section is additive by construction: the manifest readers already
390/// skip reserved modules, so an archive without this one is exactly what it is
391/// today and needs no version bump. `ZnippyArchive::open` joins it onto the
392/// chunks it names; absent, every chunk is [`Stored`](crate::archive) and the
393/// read path is byte-for-byte the one the golden digest pins.
394///
395/// The payload is an Arrow IPC stream of `(relative_path, chunk_seq, base_path)`.
396/// It is independent of the objects, so an append must carry it — but it travels
397/// as **decoded rows** rather than raw bytes (like `__meta__`, and unlike
398/// `__gunnar_refs__`), because an append may add rows to it and two sections with
399/// one module name is not a thing the manifest can express. That is why it is
400/// NOT in [`CARRIED_RESERVED_MODULES`].
401pub const ZNIPPY_DELTA_MODULE: &str = "__znippy_delta__";
402
403/// Reserved `module_name` for the **filesystem-shape** section: the entries the
404/// data index structurally cannot hold, because the data index is a table of
405/// *chunks* and a chunk belongs to a file.
406///
407/// # What is missing without it
408///
409/// `base_index_fields()` is `(relative_path, chunk_seq, fdata_offset, compressed,
410/// uncompressed_size, blob_offset, blob_size, checksum)`. Every row is a slice of
411/// a regular file's bytes. Two facts of a real tree have no row shape there at
412/// all, and both were silently dropped on the way in:
413///
414/// * a **directory** — most sharply an EMPTY one, which owns no file and so
415///   contributes no row. Extract only ever ran `create_dir_all` on a file's
416///   parent (`crate::decompress`), so a directory with no files under it did not
417///   come back. A container-registry mirror (`talosctl image cache-create`) has
418///   them, and a git checkout, and a build tree.
419/// * a **unix mode** — the executable bit above all. An extracted file got
420///   `0666 & ~umask` regardless of what went in, so a script round-tripped
421///   non-executable.
422///
423/// # Why a reserved section rather than columns or dir rows
424///
425/// Same argument as [`ZNIPPY_DELTA_MODULE`], and the alternatives are worse here.
426/// A `mode` column on the base schema is a [`ZNIPPY_FORMAT_VERSION`] bump plus a
427/// column every existing writer (the iceberg sink, the append sink, gunnar's
428/// generation-0 seal) must start supplying. Encoding a directory as a zero-byte
429/// data row is worse still: every reader — `list`, `verify`, `get`, the typed
430/// views, DuckDB over the Arrow file — would then see a *file* of length 0 where
431/// a directory is, and extract would create one.
432///
433/// Reserved is additive by construction: `is_reserved_module` already makes every
434/// data reader skip it, so an archive written before this section existed reads
435/// exactly as it did, and one written with it opens unchanged in an older znippy
436/// — minus the empty dirs and modes it cannot know about.
437///
438/// The payload is an Arrow IPC stream of `(relative_path, kind, mode)`; see
439/// [`crate::fsmeta`].
440pub const ZNIPPY_FSMETA_MODULE: &str = "__znippy_fsmeta__";
441
442/// Schema of the [`ZNIPPY_DELTA_MODULE`] section.
443pub fn delta_map_schema() -> Arc<Schema> {
444    Arc::new(Schema::new(vec![
445        Field::new("relative_path", DataType::Utf8, false),
446        Field::new("chunk_seq", DataType::UInt32, false),
447        Field::new("base_path", DataType::Utf8, false),
448    ]))
449}
450
451/// `pkg_type` discriminant carried by reserved (non-data) manifest entries.
452pub const RESERVED_PKG_TYPE: i8 = i8::MIN;
453
454/// Every reserved `module_name` this reader knows, in one place.
455///
456/// Kept as a slice rather than a hand-written `||` chain so that adding a
457/// reserved module is a single edit that both [`is_reserved_module`] and the
458/// guard test see — the failure mode being avoided is a new module name that is
459/// written as reserved by the sink but classified as *data* by the manifest
460/// readers, which silently corrupts `list`, `decompress`, the iceberg sink and
461/// the manifest-count assertions.
462pub const RESERVED_MODULES: &[&str] = &[
463    LOOKUP_MODULE,
464    TRIE_MODULE,
465    SIGN_ARTIFACTS_MODULE,
466    SIGN_ARCHIVE_MODULE,
467    META_MODULE,
468    GUNNAR_OID_MODULE,
469    GUNNAR_GRAPH_MODULE,
470    GUNNAR_REACH_MODULE,
471    GUNNAR_REFS_MODULE,
472    GUNNAR_SECRETS_MODULE,
473    ZNIPPY_DELTA_MODULE,
474    ZNIPPY_FSMETA_MODULE,
475];
476
477/// A reserved manifest entry holds a derived structure (lookup / trie / detached
478/// signatures / the git oid-index, commit-graph and reachability sections), not
479/// file rows. The merge + manifest readers skip these so data consumers are
480/// unaffected — which is exactly what makes those sections additive and
481/// backward-compatible.
482pub fn is_reserved_module(module_name: &str) -> bool {
483    RESERVED_MODULES.contains(&module_name)
484}
485
486/// The reserved sections an **append must carry forward**, because they are
487/// independent logs rather than derivations of the blobs beside them.
488///
489/// MEASURED 2026-08-04, gunnar: an object-carrying push removed
490/// `__gunnar_refs__` from the archive (144 928 -> 146 396 bytes, section gone),
491/// because [`crate::ArrowIpcSinkAppend::open_existing`] truncates the whole
492/// metadata tail and re-supplies no reserved section it was not handed. All
493/// reserved sections were treated alike, and they are not alike:
494///
495/// * **derived** — `__gunnar_oid__`, `__gunnar_graph__`, `__gunnar_reach__`,
496///   plus `__lookup__` / `__trie__` — are functions of the objects in the
497///   archive. When objects change they are *wrong*, and dropping them is
498///   correct; the writer rebuilds them.
499/// * **independent** — `__gunnar_refs__` and `__gunnar_secrets__` — are logs,
500///   one RecordBatch per push. Nothing in a new push's objects reproduces the
501///   ref history that came before it, so dropping them destroys data.
502///
503/// `__meta__` is carried by its own typed path (`open_existing` decodes it into
504/// a `MetaTable`) and so is deliberately not listed here: it would then be
505/// written twice.
506///
507/// This distinction is what the append path could not previously express, and it
508/// blocks serving refs from the archive for most of a repository's life.
509/// [`ZNIPPY_FSMETA_MODULE`] joins the independent half for the same reason: the
510/// tree's shape — its empty directories, its modes — is a fact about the walk
511/// that produced the archive, and NOTHING in a later append's file list
512/// reproduces it. `append_files` is handed `(relative_path, bytes)` pairs, never
513/// a directory; if the section were dropped on append, the first `znippy append`
514/// after a `compress` would silently delete every empty directory from the
515/// archive. Carried verbatim, an append leaves the shape exactly as the compress
516/// that established it recorded it.
517pub const CARRIED_RESERVED_MODULES: &[&str] =
518    &[GUNNAR_REFS_MODULE, GUNNAR_SECRETS_MODULE, ZNIPPY_FSMETA_MODULE];
519
520/// Whether an append must carry this reserved section forward verbatim.
521/// See [`CARRIED_RESERVED_MODULES`].
522pub fn is_carried_reserved_module(module_name: &str) -> bool {
523    CARRIED_RESERVED_MODULES.contains(&module_name)
524}
525
526/// Read the raw bytes of one reserved sub-section by module name, if present.
527/// Public entry point for the signature layer (feature `sign`) and any other
528/// reader that needs a reserved section's bytes without re-implementing the
529/// manifest walk.
530pub fn read_reserved_section_bytes(path: &Path, module_name: &str) -> Result<Option<Vec<u8>>> {
531    read_reserved_section(path, module_name)
532}
533
534/// Schema of the lookup sub-index — identical to the base index columns. The
535/// lookup is the same per-chunk rows, re-sorted by `(relative_path, chunk_seq)`
536/// and stripped of any plugin columns, so one path's chunks are contiguous.
537///
538/// **Carries no schema metadata**, and that is correct for the *lookup*: the
539/// lookup is a RESERVED sub-index, which every reader skips. It is NOT correct
540/// for a **data** sub-index — use [`data_subindex_schema`] there, or the archive
541/// records no format version and [`check_format_version`] has nothing to check.
542pub fn lookup_schema() -> Arc<Schema> {
543    Arc::new(Schema::new(base_index_fields()))
544}
545
546/// Schema for a base-column **data** sub-index: the same columns as
547/// [`lookup_schema`], stamped with the archive metadata that
548/// [`build_arrow_metadata_for_config`] produces — including
549/// [`FORMAT_VERSION_KEY`].
550///
551/// The reader (and holger's independent reader-side pin) determines an archive's
552/// on-disk format version from the Arrow schema metadata of the **first
553/// non-reserved sub-index**. A writer that seals its data sub-index with the bare
554/// [`lookup_schema`] therefore produces an archive that records no version at
555/// all, and the version pin degrades to "undetermined → read as before" for
556/// exactly those archives. Every data sub-index must be sealed with this.
557pub fn data_subindex_schema() -> Arc<Schema> {
558    Arc::new(Schema::new_with_metadata(
559        base_index_fields(),
560        build_arrow_metadata_for_config(&crate::common_config::CONFIG),
561    ))
562}
563
564/// One chunk's location for single-file random access.
565#[derive(Debug, Clone, PartialEq, Eq)]
566pub struct ChunkLoc {
567    pub chunk_seq: u32,
568    pub fdata_offset: u64,
569    pub blob_offset: u64,
570    pub blob_size: u64,
571    pub uncompressed_size: u64,
572    pub compressed: bool,
573    pub checksum: [u8; 32],
574}
575
576/// What the trailing footer of an archive points at.
577#[derive(Debug, Clone, PartialEq)]
578pub enum IndexFooter {
579    /// Legacy v0.6: a single Arrow IPC index began at this offset. Still detected
580    /// so the reader can reject v0.6 archives with a clear "re-compress with v0.7"
581    /// error — it is no longer a live read path.
582    Single { index_offset: u64 },
583    /// v0.7: the manifest stream begins at this offset.
584    Multi { manifest_offset: u64 },
585}
586
587/// Interpret an archive's trailing bytes. `tail` must be the last 16 bytes of the file
588/// (or last 8 for tiny v0.6 files — then it's always Single).
589pub fn interpret_footer(tail: &[u8]) -> IndexFooter {
590    let n = tail.len();
591    let offset = u64::from_le_bytes(tail[n - 8..].try_into().unwrap());
592    if n >= 16 && tail[n - 16..n - 8] == MULTI_INDEX_MAGIC {
593        IndexFooter::Multi { manifest_offset: offset }
594    } else {
595        IndexFooter::Single { index_offset: offset }
596    }
597}
598
599fn manifest_schema() -> Arc<Schema> {
600    Arc::new(Schema::new(vec![
601        Field::new("pkg_type", DataType::Int8, false),
602        Field::new("repo", DataType::Utf8, false),
603        Field::new("module_name", DataType::Utf8, false),
604        Field::new("index_offset", DataType::UInt64, false),
605        Field::new("index_len", DataType::UInt64, false),
606        Field::new("row_count", DataType::UInt64, false),
607    ]))
608}
609
610/// Serialize manifest entries to an Arrow IPC stream (itself DuckDB-readable).
611pub fn write_manifest_bytes(entries: &[ManifestEntry]) -> Result<Vec<u8>> {
612    use arrow::ipc::writer::StreamWriter;
613
614    let len = entries.len();
615    let mut pkg_type = Int8Builder::with_capacity(len);
616    let mut repo = StringBuilder::with_capacity(len, len * 16);
617    let mut module_name = StringBuilder::with_capacity(len, len * 16);
618    let mut index_offset = UInt64Builder::with_capacity(len);
619    let mut index_len = UInt64Builder::with_capacity(len);
620    let mut row_count = UInt64Builder::with_capacity(len);
621    for e in entries {
622        pkg_type.append_value(e.pkg_type);
623        repo.append_value(&e.repo);
624        module_name.append_value(&e.module_name);
625        index_offset.append_value(e.index_offset);
626        index_len.append_value(e.index_len);
627        row_count.append_value(e.row_count);
628    }
629
630    let schema = manifest_schema();
631    let batch = RecordBatch::try_new(
632        schema.clone(),
633        vec![
634            Arc::new(pkg_type.finish()),
635            Arc::new(repo.finish()),
636            Arc::new(module_name.finish()),
637            Arc::new(index_offset.finish()),
638            Arc::new(index_len.finish()),
639            Arc::new(row_count.finish()),
640        ],
641    )?;
642
643    let mut buf = Vec::new();
644    {
645        let mut w = StreamWriter::try_new(&mut buf, &schema)?;
646        w.write(&batch)?;
647        w.finish()?;
648    }
649    Ok(buf)
650}
651
652/// Parse a manifest Arrow IPC stream back into entries.
653pub fn read_manifest_bytes(bytes: &[u8]) -> Result<Vec<ManifestEntry>> {
654    use arrow::array::{Int8Array, StringArray, UInt64Array};
655    use arrow::ipc::reader::StreamReader;
656
657    let reader = StreamReader::try_new(std::io::Cursor::new(bytes), None)?;
658    let mut out = Vec::new();
659    for batch in reader {
660        let batch = batch?;
661        let col = |name: &str| batch.column_by_name(name)
662            .ok_or_else(|| anyhow::anyhow!("manifest missing column {name}"));
663        let pkg_type = col("pkg_type")?.as_any().downcast_ref::<Int8Array>()
664            .ok_or_else(|| anyhow::anyhow!("pkg_type type"))?;
665        let repo = col("repo")?.as_any().downcast_ref::<StringArray>()
666            .ok_or_else(|| anyhow::anyhow!("repo type"))?;
667        let module_name = col("module_name")?.as_any().downcast_ref::<StringArray>()
668            .ok_or_else(|| anyhow::anyhow!("module_name type"))?;
669        let index_offset = col("index_offset")?.as_any().downcast_ref::<UInt64Array>()
670            .ok_or_else(|| anyhow::anyhow!("index_offset type"))?;
671        let index_len = col("index_len")?.as_any().downcast_ref::<UInt64Array>()
672            .ok_or_else(|| anyhow::anyhow!("index_len type"))?;
673        let row_count = col("row_count")?.as_any().downcast_ref::<UInt64Array>()
674            .ok_or_else(|| anyhow::anyhow!("row_count type"))?;
675        for i in 0..batch.num_rows() {
676            out.push(ManifestEntry {
677                pkg_type: pkg_type.value(i),
678                repo: repo.value(i).to_string(),
679                module_name: module_name.value(i).to_string(),
680                index_offset: index_offset.value(i),
681                index_len: index_len.value(i),
682                row_count: row_count.value(i),
683            });
684        }
685    }
686    Ok(out)
687}
688
689/// Read the Arrow IPC index from a v0.7 .znippy file.
690///
691/// Reads the 16-byte footer (8-byte `ZNPYMIDX` magic + 8-byte LE u64 manifest_offset),
692/// parses the manifest, reads every sub-index, and merges all batches into one so callers
693/// need no format-version awareness.
694pub fn read_znippy_index(path: &Path) -> Result<(Arc<Schema>, Vec<RecordBatch>)> {
695    read_znippy_index_filtered(path, &IndexFilter::default())
696}
697
698/// Selective sub-index filter: keep only sub-indexes whose manifest entry
699/// matches. A `None` field matches anything, so `IndexFilter::default()` reads
700/// the whole archive (what [`read_znippy_index`] does). Filtering happens at the
701/// `(pkg_type, repo)` sub-index granularity — whole Arrow IPC streams are
702/// skipped, never read — so a selective extract touches only the matching rows.
703#[derive(Debug, Clone, Default)]
704pub struct IndexFilter {
705    /// Keep only this package-type discriminant (`ManifestEntry::pkg_type`).
706    pub pkg_type: Option<i8>,
707    /// Keep only this repo (`ManifestEntry::repo`).
708    pub repo: Option<String>,
709}
710
711impl IndexFilter {
712    pub fn is_empty(&self) -> bool {
713        self.pkg_type.is_none() && self.repo.is_none()
714    }
715    fn matches(&self, e: &ManifestEntry) -> bool {
716        self.pkg_type.is_none_or(|t| e.pkg_type == t)
717            && self.repo.as_deref().is_none_or(|r| e.repo == r)
718    }
719}
720
721/// Like [`read_znippy_index`] but keeps only sub-indexes matching `filter`.
722pub fn read_znippy_index_filtered(
723    path: &Path,
724    filter: &IndexFilter,
725) -> Result<(Arc<Schema>, Vec<RecordBatch>)> {
726    let mut file = File::open(path)?;
727    let file_len = file.metadata()?.len();
728    anyhow::ensure!(file_len >= 16, "file too small to be a v0.7 znippy archive");
729
730    file.seek(SeekFrom::End(-16))?;
731    let mut tail = [0u8; 16];
732    file.read_exact(&mut tail)?;
733
734    match interpret_footer(&tail) {
735        IndexFooter::Multi { manifest_offset } => {
736            read_multi_index(&mut file, file_len, manifest_offset, filter)
737        }
738        IndexFooter::Single { .. } => {
739            anyhow::bail!("v0.6 archives are not supported; re-compress with v0.7")
740        }
741    }
742}
743
744/// Read all sub-indexes from a v0.7 multi-index archive and concatenate them.
745fn read_multi_index(
746    file: &mut File,
747    file_len: u64,
748    manifest_offset: u64,
749    filter: &IndexFilter,
750) -> Result<(Arc<Schema>, Vec<RecordBatch>)> {
751    use arrow::ipc::reader::StreamReader;
752
753    // manifest lives between manifest_offset and (file_len − 16): 8-byte magic + 8-byte offset
754    let manifest_end = file_len.checked_sub(16)
755        .ok_or_else(|| anyhow::anyhow!("v0.7 archive too small"))?;
756    anyhow::ensure!(manifest_offset <= manifest_end, "corrupt v0.7 manifest_offset");
757    let manifest_len = (manifest_end - manifest_offset) as usize;
758
759    file.seek(SeekFrom::Start(manifest_offset))?;
760    let mut manifest_bytes = vec![0u8; manifest_len];
761    file.read_exact(&mut manifest_bytes)?;
762    let entries = read_manifest_bytes(&manifest_bytes)?;
763
764    let mut all_batches: Vec<RecordBatch> = Vec::new();
765    let mut schema: Option<Arc<Schema>> = None;
766
767    for entry in &entries {
768        // Skip derived structures (lookup sub-index, trie blob) — they are not
769        // file-row data and must not be merged into the index.
770        if is_reserved_module(&entry.module_name) {
771            continue;
772        }
773        // Selective read: skip whole sub-indexes that don't match the filter.
774        if !filter.matches(entry) {
775            continue;
776        }
777        // Bound the attacker-controlled declared length against the real file
778        // size before allocating, so a corrupt manifest can't force a huge alloc.
779        anyhow::ensure!(
780            entry.index_offset.checked_add(entry.index_len)
781                .is_some_and(|end| end <= file_len),
782            "sub-index for module {} out of bounds (offset={}, len={}, file_len={})",
783            entry.module_name, entry.index_offset, entry.index_len, file_len
784        );
785        file.seek(SeekFrom::Start(entry.index_offset))?;
786        let mut sub_bytes = vec![0u8; entry.index_len as usize];
787        file.read_exact(&mut sub_bytes)?;
788        let cursor = std::io::Cursor::new(sub_bytes);
789        let reader = StreamReader::try_new(cursor, None)?;
790        if schema.is_none() {
791            let sub_schema = reader.schema();
792            // Refuse archives written by a newer znippy than this reader supports,
793            // before parsing any sub-index batches we might mis-interpret.
794            check_format_version(sub_schema.metadata())?;
795            schema = Some(sub_schema);
796        }
797        for batch in reader {
798            all_batches.push(batch.map_err(|e| anyhow::anyhow!("sub-index read error: {}", e))?);
799        }
800    }
801
802    let schema = schema.unwrap_or_else(|| Arc::new(Schema::new(base_index_fields())));
803
804    // Merge all sub-index batches into one so callers stay format-agnostic.
805    let merged = if all_batches.len() <= 1 {
806        all_batches
807    } else {
808        let batch = arrow_select::concat::concat_batches(&schema, all_batches.iter())
809            .map_err(|e| anyhow::anyhow!("concat sub-indexes: {}", e))?;
810        vec![batch]
811    };
812
813    Ok((schema, merged))
814}
815
816/// Read the manifest from a v0.7 multi-index archive.
817/// Returns an error if the file is a plain v0.6 single-index archive.
818pub fn read_znippy_manifest(path: &Path) -> Result<Vec<ManifestEntry>> {
819    let mut file = File::open(path)?;
820    let file_len = file.metadata()?.len();
821    anyhow::ensure!(file_len >= 16, "file too small to be a v0.7 archive");
822
823    file.seek(SeekFrom::End(-16))?;
824    let mut tail = [0u8; 16];
825    file.read_exact(&mut tail)?;
826
827    match interpret_footer(&tail) {
828        IndexFooter::Single { .. } => {
829            anyhow::bail!("not a v0.7 multi-index archive (no MULTI_INDEX_MAGIC)")
830        }
831        IndexFooter::Multi { manifest_offset } => {
832            let manifest_end = file_len - 16;
833            anyhow::ensure!(manifest_offset <= manifest_end, "corrupt manifest_offset");
834            let manifest_len = (manifest_end - manifest_offset) as usize;
835            file.seek(SeekFrom::Start(manifest_offset))?;
836            let mut manifest_bytes = vec![0u8; manifest_len];
837            file.read_exact(&mut manifest_bytes)?;
838            let mut entries = read_manifest_bytes(&manifest_bytes)?;
839            // Hide reserved (lookup/trie) entries from data-manifest consumers.
840            entries.retain(|e| !is_reserved_module(&e.module_name));
841            Ok(entries)
842        }
843    }
844}
845
846/// Read **all** manifest entries of a sealed v0.7 archive, *including* the
847/// reserved (lookup/trie) ones, plus the manifest's byte offset.
848///
849/// Public entrypoint used by the append/resume sink
850/// ([`ArrowIpcSinkAppend::open_existing`](crate::ArrowIpcSinkAppend::open_existing)):
851/// it needs the reserved sections (to find the blob-region end and to recover
852/// the sorted lookup) which `read_znippy_manifest` hides. Read-only — does not
853/// touch the original write path.
854pub fn read_znippy_full_manifest(path: &Path) -> Result<(Vec<ManifestEntry>, u64)> {
855    let mut file = File::open(path)?;
856    let file_len = file.metadata()?.len();
857    read_full_manifest(&mut file, file_len)
858}
859
860/// Read all manifest entries, *including* reserved (lookup/trie) ones.
861fn read_full_manifest(file: &mut File, file_len: u64) -> Result<(Vec<ManifestEntry>, u64)> {
862    anyhow::ensure!(file_len >= 16, "file too small to be a v0.7 archive");
863    file.seek(SeekFrom::End(-16))?;
864    let mut tail = [0u8; 16];
865    file.read_exact(&mut tail)?;
866    let manifest_offset = match interpret_footer(&tail) {
867        IndexFooter::Multi { manifest_offset } => manifest_offset,
868        IndexFooter::Single { .. } => anyhow::bail!("not a v0.7 multi-index archive"),
869    };
870    let manifest_end = file_len.checked_sub(16)
871        .ok_or_else(|| anyhow::anyhow!("v0.7 archive too small"))?;
872    anyhow::ensure!(manifest_offset <= manifest_end, "corrupt manifest_offset");
873    let manifest_len = (manifest_end - manifest_offset) as usize;
874    file.seek(SeekFrom::Start(manifest_offset))?;
875    let mut manifest_bytes = vec![0u8; manifest_len];
876    file.read_exact(&mut manifest_bytes)?;
877    Ok((read_manifest_bytes(&manifest_bytes)?, manifest_offset))
878}
879
880/// Read the raw bytes of one reserved sub-section (lookup or trie), if present.
881fn read_reserved_section(path: &Path, module_name: &str) -> Result<Option<Vec<u8>>> {
882    let mut file = File::open(path)?;
883    let file_len = file.metadata()?.len();
884    let (entries, _) = read_full_manifest(&mut file, file_len)?;
885    let Some(entry) = entries.iter().find(|e| e.module_name == module_name) else {
886        return Ok(None);
887    };
888    // Bound the attacker-controlled declared length against the real file size
889    // before allocating, so a corrupt manifest can't force a huge allocation.
890    anyhow::ensure!(
891        entry.index_offset.checked_add(entry.index_len)
892            .is_some_and(|end| end <= file_len),
893        "reserved section {} out of bounds (offset={}, len={}, file_len={})",
894        entry.module_name, entry.index_offset, entry.index_len, file_len
895    );
896    file.seek(SeekFrom::Start(entry.index_offset))?;
897    let mut bytes = vec![0u8; entry.index_len as usize];
898    file.read_exact(&mut bytes)?;
899    Ok(Some(bytes))
900}
901
902/// Decode the lookup sub-index bytes into parallel column vectors (already sorted
903/// by `(relative_path, chunk_seq)` on disk).
904/// Read row `i` of an index/lookup `checksum` column as a 32-byte blake3 digest.
905///
906/// `FixedSizeBinaryArray` is one concrete Arrow type for *every* width, so a
907/// `downcast_ref::<FixedSizeBinaryArray>()` succeeds just as happily for
908/// `FixedSizeBinary(16)` as for `(32)`. Copying such a row straight into a
909/// `[u8; 32]` panics ("source slice length (16) does not match destination slice
910/// length (32)") — a process abort driven by an attacker-supplied schema, which
911/// is exactly what the downcast-or-error style everywhere else in this file
912/// exists to prevent. Check the declared width and return a clean `Err` instead.
913/// (`ZnippyArchive::build_file_index` already guards this with the same test.)
914fn checksum32(col: &arrow::array::FixedSizeBinaryArray, i: usize) -> Result<[u8; 32]> {
915    if col.value_length() != 32 {
916        return Err(anyhow::anyhow!(
917            "checksum column has width {}, expected 32",
918            col.value_length()
919        ));
920    }
921    let mut ck = [0u8; 32];
922    ck.copy_from_slice(col.value(i));
923    Ok(ck)
924}
925
926fn decode_lookup(bytes: &[u8]) -> Result<LookupColumns> {
927    use arrow::array::{BooleanArray, FixedSizeBinaryArray, StringArray, UInt32Array, UInt64Array};
928    use arrow::ipc::reader::StreamReader;
929
930    let reader = StreamReader::try_new(std::io::Cursor::new(bytes), None)?;
931    let mut cols = LookupColumns::default();
932    for batch in reader {
933        let batch = batch?;
934        let get = |n: &str| batch.column_by_name(n)
935            .ok_or_else(|| anyhow::anyhow!("lookup missing column {n}"));
936        let paths = get("relative_path")?.as_any().downcast_ref::<StringArray>()
937            .ok_or_else(|| anyhow::anyhow!("relative_path type"))?;
938        let chunk_seq = get("chunk_seq")?.as_any().downcast_ref::<UInt32Array>()
939            .ok_or_else(|| anyhow::anyhow!("chunk_seq type"))?;
940        let fdata = get("fdata_offset")?.as_any().downcast_ref::<UInt64Array>()
941            .ok_or_else(|| anyhow::anyhow!("fdata_offset type"))?;
942        let compressed = get("compressed")?.as_any().downcast_ref::<BooleanArray>()
943            .ok_or_else(|| anyhow::anyhow!("compressed type"))?;
944        let usize_col = get("uncompressed_size")?.as_any().downcast_ref::<UInt64Array>()
945            .ok_or_else(|| anyhow::anyhow!("uncompressed_size type"))?;
946        let blob_offset = get("blob_offset")?.as_any().downcast_ref::<UInt64Array>()
947            .ok_or_else(|| anyhow::anyhow!("blob_offset type"))?;
948        let blob_size = get("blob_size")?.as_any().downcast_ref::<UInt64Array>()
949            .ok_or_else(|| anyhow::anyhow!("blob_size type"))?;
950        let checksum = get("checksum")?.as_any().downcast_ref::<FixedSizeBinaryArray>()
951            .ok_or_else(|| anyhow::anyhow!("checksum type"))?;
952        for i in 0..batch.num_rows() {
953            cols.paths.push(paths.value(i).to_string());
954            let ck = checksum32(checksum, i)?;
955            cols.locs.push(ChunkLoc {
956                chunk_seq: chunk_seq.value(i),
957                fdata_offset: fdata.value(i),
958                blob_offset: blob_offset.value(i),
959                blob_size: blob_size.value(i),
960                uncompressed_size: usize_col.value(i),
961                compressed: compressed.value(i),
962                checksum: ck,
963            });
964        }
965    }
966    Ok(cols)
967}
968
969#[derive(Default)]
970struct LookupColumns {
971    paths: Vec<String>,
972    locs: Vec<ChunkLoc>,
973}
974
975/// Locate every chunk of `target` for single-file random access.
976///
977/// Fast paths, in order: the fst trie (O(key length)) → binary search of the
978/// sorted lookup sub-index (O(log n)) → linear scan of the merged main index
979/// (O(n), for archives written before the lookup layer existed). Returns the
980/// chunks sorted by `chunk_seq`, or an empty vec if `target` is not in the archive.
981pub fn locate_file(path: &Path, target: &str) -> Result<Vec<ChunkLoc>> {
982    if let Some(lookup_bytes) = read_reserved_section(path, LOOKUP_MODULE)? {
983        let cols = decode_lookup(&lookup_bytes)?;
984        let n = cols.paths.len();
985
986        // Find any row whose path == target.
987        let hit = if let Some(trie_bytes) = read_reserved_section(path, TRIE_MODULE)? {
988            let map = fst::Map::new(trie_bytes)
989                .map_err(|e| anyhow::anyhow!("trie open: {e}"))?;
990            map.get(target.as_bytes()).map(|v| v as usize)
991        } else {
992            // Lookup is sorted by path; binary-search the path column.
993            match cols.paths.binary_search_by(|p| p.as_str().cmp(target)) {
994                Ok(i) => Some(i),
995                Err(_) => None,
996            }
997        };
998
999        let Some(hit) = hit else { return Ok(Vec::new()); };
1000
1001        // The trie value is attacker-controlled: a corrupt/malicious archive can
1002        // map `target` to a row index past the (smaller) lookup table. Treat an
1003        // out-of-range hit as "not found" instead of indexing OOB below — this is
1004        // a remote-DoS guard on the random-access read path.
1005        if hit >= n { return Ok(Vec::new()); }
1006
1007        // Expand to the contiguous run of rows sharing this path.
1008        let mut start = hit;
1009        while start > 0 && cols.paths[start - 1] == target { start -= 1; }
1010        let mut end = hit + 1;
1011        while end < n && cols.paths[end] == target { end += 1; }
1012
1013        let mut out: Vec<ChunkLoc> = cols.locs[start..end].to_vec();
1014        out.sort_by_key(|c| c.chunk_seq);
1015        return Ok(out);
1016    }
1017
1018    // Fallback: no lookup layer — scan the merged main index.
1019    locate_file_via_index(path, target)
1020}
1021
1022/// Slim per-file (per-artifact) metadata for browsing an archive without
1023/// reading file bytes. One row per file, aggregated across its chunks.
1024#[derive(Debug, Clone, PartialEq)]
1025pub struct ArtifactMeta {
1026    pub relative_path: String,
1027    /// Total uncompressed size across all of the file's chunks.
1028    pub uncompressed_size: u64,
1029    pub chunk_count: u32,
1030    /// Whether the file's data was compressed (false on the stored-raw skip path).
1031    pub compressed: bool,
1032}
1033
1034/// Metadata for **every** file in the archive, sorted by `relative_path`.
1035///
1036/// Reads only the lookup sub-index (not the file bytes); falls back to the main
1037/// index for archives written before the lookup layer.
1038pub fn get_all_files_meta(path: &Path) -> Result<Vec<ArtifactMeta>> {
1039    files_meta_impl(path, None)
1040}
1041
1042/// Metadata for the files whose `relative_path` starts with `prefix` — for when
1043/// you don't want all files (e.g. one `group/artifact/` subtree).
1044///
1045/// When the trie is present this jumps straight to the first matching key via the
1046/// fst's ordered range (O(prefix) to seek, then O(matches)); otherwise it binary
1047/// -searches the sorted lookup; otherwise it scans the legacy main index.
1048pub fn get_files_meta_with_prefix(path: &Path, prefix: &str) -> Result<Vec<ArtifactMeta>> {
1049    files_meta_impl(path, Some(prefix))
1050}
1051
1052fn files_meta_impl(path: &Path, prefix: Option<&str>) -> Result<Vec<ArtifactMeta>> {
1053    use fst::{IntoStreamer, Streamer};
1054
1055    if let Some(lookup_bytes) = read_reserved_section(path, LOOKUP_MODULE)? {
1056        let cols = decode_lookup(&lookup_bytes)?; // sorted by (relative_path, chunk_seq)
1057        let n = cols.paths.len();
1058
1059        // Resolve the contiguous row window [lo, hi) to aggregate.
1060        let (lo, hi) = match prefix {
1061            None | Some("") => (0, n),
1062            Some(pre) => {
1063                // Seek the first key >= prefix. Use the trie's ordered range when
1064                // present (the "trie search"); else binary-search the sorted paths.
1065                let lo = if let Some(trie_bytes) = read_reserved_section(path, TRIE_MODULE)? {
1066                    let map = fst::Map::new(trie_bytes)
1067                        .map_err(|e| anyhow::anyhow!("trie open: {e}"))?;
1068                    let mut stream = map.range().ge(pre.as_bytes()).into_stream();
1069                    match stream.next() {
1070                        Some((k, v)) if k.starts_with(pre.as_bytes()) => v as usize,
1071                        _ => return Ok(Vec::new()),
1072                    }
1073                } else {
1074                    cols.paths.partition_point(|p| p.as_str() < pre)
1075                };
1076                if lo >= n || !cols.paths[lo].starts_with(pre) {
1077                    return Ok(Vec::new());
1078                }
1079                let mut hi = lo;
1080                while hi < n && cols.paths[hi].starts_with(pre) {
1081                    hi += 1;
1082                }
1083                (lo, hi)
1084            }
1085        };
1086
1087        // Aggregate contiguous chunk rows per path.
1088        let mut out = Vec::new();
1089        let mut i = lo;
1090        while i < hi {
1091            let p = &cols.paths[i];
1092            let mut total = 0u64;
1093            let mut count = 0u32;
1094            let mut compressed = false;
1095            while i < hi && &cols.paths[i] == p {
1096                total += cols.locs[i].uncompressed_size;
1097                count += 1;
1098                compressed |= cols.locs[i].compressed;
1099                i += 1;
1100            }
1101            out.push(ArtifactMeta {
1102                relative_path: p.clone(),
1103                uncompressed_size: total,
1104                chunk_count: count,
1105                compressed,
1106            });
1107        }
1108        return Ok(out);
1109    }
1110
1111    files_meta_via_index(path, prefix)
1112}
1113
1114/// A cached, open-once random-access reader over a sealed v0.7 archive.
1115///
1116/// The free functions [`locate_file`] and [`get_files_meta_with_prefix`] re-open
1117/// the file and re-read the footer + manifest + both reserved sections (the
1118/// lookup sub-index and the trie) on **every** call, and re-`decode_lookup` every
1119/// row each time — three manifest reads per lookup (analysis §1.5). For a
1120/// long-lived reader (a browsing UI, holger's dynamic side, selective restore)
1121/// that is pure repeated I/O.
1122///
1123/// `ArchiveReader::open` reads the footer, manifest, lookup sub-index and trie
1124/// **once**, decodes the lookup columns once, and builds the fst map once. Every
1125/// subsequent [`locate`](ArchiveReader::locate),
1126/// [`files_meta`](ArchiveReader::files_meta) and
1127/// [`files_meta_with_prefix`](ArchiveReader::files_meta_with_prefix) query is then
1128/// served from memory — an fst `get` (O(key)) or a binary search (O(log n)) over
1129/// the already-decoded columns — with **zero** further I/O and **zero**
1130/// per-call manifest reads. The on-disk format is untouched; results are
1131/// identical to the equivalent free function.
1132pub struct ArchiveReader {
1133    /// Relative paths, sorted by `(relative_path, chunk_seq)` (lookup order).
1134    paths: Vec<String>,
1135    /// Chunk locators, index-aligned with `paths`.
1136    locs: Vec<ChunkLoc>,
1137    /// `path -> first row index` fst over the sorted paths, when the archive
1138    /// carries a trie section (it always does for v0.7 seals). Absent ⇒ fall back
1139    /// to binary search over `paths`.
1140    trie: Option<fst::Map<Vec<u8>>>,
1141    /// The archive handle kept open across calls, so [`read_file`](Self::read_file)
1142    /// serves selective restore with **one** open + cached locate (positioned
1143    /// `pread`s only — never seeks a shared cursor).
1144    file: File,
1145    /// Real archive length, for the same pre-alloc bounds check the free
1146    /// `get_file` does against attacker-controlled blob offsets/sizes.
1147    file_len: u64,
1148}
1149
1150impl ArchiveReader {
1151    /// Open a sealed v0.7 archive and cache its manifest + lookup + trie. Reads
1152    /// the manifest exactly once (vs three re-reads per free-function call).
1153    pub fn open(path: &Path) -> Result<Self> {
1154        let mut file = File::open(path)?;
1155        let file_len = file.metadata()?.len();
1156        let (entries, _) = read_full_manifest(&mut file, file_len)?;
1157
1158        // Read one reserved section by module name from the already-open handle,
1159        // bounding the attacker-controlled declared length against the real file
1160        // size before allocating (same guard as `read_reserved_section`).
1161        let mut read_section = |module: &str| -> Result<Option<Vec<u8>>> {
1162            let Some(entry) = entries.iter().find(|e| e.module_name == module) else {
1163                return Ok(None);
1164            };
1165            anyhow::ensure!(
1166                entry.index_offset.checked_add(entry.index_len)
1167                    .is_some_and(|end| end <= file_len),
1168                "reserved section {} out of bounds (offset={}, len={}, file_len={})",
1169                entry.module_name, entry.index_offset, entry.index_len, file_len
1170            );
1171            file.seek(SeekFrom::Start(entry.index_offset))?;
1172            let mut bytes = vec![0u8; entry.index_len as usize];
1173            file.read_exact(&mut bytes)?;
1174            Ok(Some(bytes))
1175        };
1176
1177        let lookup_bytes = read_section(LOOKUP_MODULE)?.ok_or_else(|| {
1178            anyhow::anyhow!("archive has no lookup sub-index (not a v0.7 sealed archive)")
1179        })?;
1180        let cols = decode_lookup(&lookup_bytes)?;
1181        let trie = match read_section(TRIE_MODULE)? {
1182            Some(tb) => Some(fst::Map::new(tb).map_err(|e| anyhow::anyhow!("trie open: {e}"))?),
1183            None => None,
1184        };
1185
1186        Ok(Self { paths: cols.paths, locs: cols.locs, trie, file, file_len })
1187    }
1188
1189    /// Total number of chunk rows cached (across all files).
1190    pub fn row_count(&self) -> usize {
1191        self.paths.len()
1192    }
1193
1194    /// Read a single file's bytes by relative path — the cached, held-open
1195    /// equivalent of [`get_file`](crate::get_file). Uses the in-memory locate
1196    /// (no manifest re-read, no per-call `decode_lookup`) and the archive handle
1197    /// opened once in [`open`](Self::open), then `pread`s + decompresses +
1198    /// blake3-verifies each chunk. This is the selective-restore entry point:
1199    /// hold one reader and call `read_file` per artifact instead of paying a full
1200    /// index re-parse on every `get_file`.
1201    ///
1202    /// Returns an error if `target` is not present in the archive.
1203    pub fn read_file(&self, target: &str) -> Result<Vec<u8>> {
1204        let chunks = self.locate(target);
1205        anyhow::ensure!(!chunks.is_empty(), "file not found in archive: {target}");
1206        crate::decompress::reassemble_file(&self.file, self.file_len, target, &chunks)
1207    }
1208
1209    /// Locate every chunk of `target`, sorted by `chunk_seq` — the cached
1210    /// equivalent of [`locate_file`], but with no I/O after `open`.
1211    pub fn locate(&self, target: &str) -> Vec<ChunkLoc> {
1212        let n = self.paths.len();
1213        let hit = match &self.trie {
1214            Some(map) => map.get(target.as_bytes()).map(|v| v as usize),
1215            None => self.paths.binary_search_by(|p| p.as_str().cmp(target)).ok(),
1216        };
1217        let Some(hit) = hit else { return Vec::new(); };
1218
1219        // The trie value is attacker-controlled (see `locate_file`): a malicious
1220        // archive can map `target` to a row index past the cached lookup table.
1221        // Treat an out-of-range hit as "not found" instead of panicking below.
1222        if hit >= n { return Vec::new(); }
1223
1224        // Expand to the contiguous run of rows sharing this path.
1225        let mut start = hit;
1226        while start > 0 && self.paths[start - 1] == target { start -= 1; }
1227        let mut end = hit + 1;
1228        while end < n && self.paths[end] == target { end += 1; }
1229
1230        let mut out: Vec<ChunkLoc> = self.locs[start..end].to_vec();
1231        out.sort_by_key(|c| c.chunk_seq);
1232        out
1233    }
1234
1235    /// Per-file metadata for **every** file, sorted by `relative_path` — the
1236    /// cached equivalent of [`get_all_files_meta`].
1237    pub fn files_meta(&self) -> Vec<ArtifactMeta> {
1238        self.aggregate(0, self.paths.len())
1239    }
1240
1241    /// Per-file metadata for files whose path starts with `prefix` — the cached
1242    /// equivalent of [`get_files_meta_with_prefix`].
1243    pub fn files_meta_with_prefix(&self, prefix: &str) -> Vec<ArtifactMeta> {
1244        let (lo, hi) = self.window(prefix);
1245        self.aggregate(lo, hi)
1246    }
1247
1248    /// Resolve the contiguous `[lo, hi)` row window matching `prefix` — trie
1249    /// range-seek when present, else binary search over the sorted paths. Empty
1250    /// prefix selects all rows; a non-matching prefix returns `(0, 0)`.
1251    fn window(&self, prefix: &str) -> (usize, usize) {
1252        use fst::{IntoStreamer, Streamer};
1253        let n = self.paths.len();
1254        if prefix.is_empty() {
1255            return (0, n);
1256        }
1257        let lo = match &self.trie {
1258            Some(map) => {
1259                let mut stream = map.range().ge(prefix.as_bytes()).into_stream();
1260                match stream.next() {
1261                    Some((k, v)) if k.starts_with(prefix.as_bytes()) => v as usize,
1262                    _ => return (0, 0),
1263                }
1264            }
1265            None => self.paths.partition_point(|p| p.as_str() < prefix),
1266        };
1267        if lo >= n || !self.paths[lo].starts_with(prefix) {
1268            return (0, 0);
1269        }
1270        let mut hi = lo;
1271        while hi < n && self.paths[hi].starts_with(prefix) {
1272            hi += 1;
1273        }
1274        (lo, hi)
1275    }
1276
1277    /// Aggregate the contiguous chunk rows in `[lo, hi)` into one `ArtifactMeta`
1278    /// per file (rows are already grouped by path in lookup order).
1279    fn aggregate(&self, lo: usize, hi: usize) -> Vec<ArtifactMeta> {
1280        let mut out = Vec::new();
1281        let mut i = lo;
1282        while i < hi {
1283            let p = &self.paths[i];
1284            let mut total = 0u64;
1285            let mut count = 0u32;
1286            let mut compressed = false;
1287            while i < hi && &self.paths[i] == p {
1288                total += self.locs[i].uncompressed_size;
1289                count += 1;
1290                compressed |= self.locs[i].compressed;
1291                i += 1;
1292            }
1293            out.push(ArtifactMeta {
1294                relative_path: p.clone(),
1295                uncompressed_size: total,
1296                chunk_count: count,
1297                compressed,
1298            });
1299        }
1300        out
1301    }
1302}
1303
1304/// Legacy fallback: aggregate per-file metadata from the merged main index.
1305fn files_meta_via_index(path: &Path, prefix: Option<&str>) -> Result<Vec<ArtifactMeta>> {
1306    use arrow::array::{BooleanArray, StringArray, UInt64Array};
1307
1308    let (schema, batches) = read_znippy_index(path)?;
1309    let batch = match batches.len() {
1310        0 => return Ok(Vec::new()),
1311        1 => batches.into_iter().next().unwrap(),
1312        _ => arrow_select::concat::concat_batches(&schema, batches.iter())?,
1313    };
1314    // Downcast-or-error: the schema is attacker-controlled, so a wrong column
1315    // type must surface as an `Err`, never an `unwrap` panic (DoS on `get`/meta).
1316    // Downcast-or-error: the schema is attacker-controlled, so a wrong column
1317    // type must surface as an `Err`, never an `unwrap` panic (DoS on `get`/meta).
1318    let col = |n: &str| batch.column_by_name(n)
1319        .ok_or_else(|| anyhow::anyhow!("index missing column {n}"));
1320    let paths = col("relative_path")?.as_any().downcast_ref::<StringArray>()
1321        .ok_or_else(|| anyhow::anyhow!("index column relative_path has unexpected type"))?;
1322    let usize_col = col("uncompressed_size")?.as_any().downcast_ref::<UInt64Array>()
1323        .ok_or_else(|| anyhow::anyhow!("index column uncompressed_size has unexpected type"))?;
1324    let compressed = col("compressed")?.as_any().downcast_ref::<BooleanArray>()
1325        .ok_or_else(|| anyhow::anyhow!("index column compressed has unexpected type"))?;
1326
1327    // Main index rows are not path-sorted, so aggregate via a map.
1328    let mut agg: HashMap<&str, (u64, u32, bool)> = HashMap::new();
1329    for i in 0..batch.num_rows() {
1330        let p = paths.value(i);
1331        if let Some(pre) = prefix {
1332            if !p.starts_with(pre) { continue; }
1333        }
1334        let e = agg.entry(p).or_insert((0, 0, false));
1335        e.0 += usize_col.value(i);
1336        e.1 += 1;
1337        e.2 |= compressed.value(i);
1338    }
1339    let mut out: Vec<ArtifactMeta> = agg.into_iter().map(|(p, (sz, c, comp))| ArtifactMeta {
1340        relative_path: p.to_string(),
1341        uncompressed_size: sz,
1342        chunk_count: c,
1343        compressed: comp,
1344    }).collect();
1345    out.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));
1346    Ok(out)
1347}
1348
1349/// O(n) fallback for archives written before the lookup sub-index existed.
1350fn locate_file_via_index(path: &Path, target: &str) -> Result<Vec<ChunkLoc>> {
1351    use arrow::array::{BooleanArray, FixedSizeBinaryArray, StringArray, UInt32Array, UInt64Array};
1352
1353    let (schema, batches) = read_znippy_index(path)?;
1354    let batch = match batches.len() {
1355        0 => return Ok(Vec::new()),
1356        1 => batches.into_iter().next().unwrap(),
1357        _ => arrow_select::concat::concat_batches(&schema, batches.iter())?,
1358    };
1359    // Downcast-or-error: the schema is attacker-controlled, so a wrong column
1360    // type must surface as an `Err`, never an `unwrap` panic (DoS on `get`).
1361    let col = |n: &str| batch.column_by_name(n)
1362        .ok_or_else(|| anyhow::anyhow!("index missing column {n}"));
1363    let paths = col("relative_path")?.as_any().downcast_ref::<StringArray>()
1364        .ok_or_else(|| anyhow::anyhow!("index column relative_path has unexpected type"))?;
1365    let chunk_seq = col("chunk_seq")?.as_any().downcast_ref::<UInt32Array>()
1366        .ok_or_else(|| anyhow::anyhow!("index column chunk_seq has unexpected type"))?;
1367    let fdata = col("fdata_offset")?.as_any().downcast_ref::<UInt64Array>()
1368        .ok_or_else(|| anyhow::anyhow!("index column fdata_offset has unexpected type"))?;
1369    let compressed = col("compressed")?.as_any().downcast_ref::<BooleanArray>()
1370        .ok_or_else(|| anyhow::anyhow!("index column compressed has unexpected type"))?;
1371    let usize_col = col("uncompressed_size")?.as_any().downcast_ref::<UInt64Array>()
1372        .ok_or_else(|| anyhow::anyhow!("index column uncompressed_size has unexpected type"))?;
1373    let blob_offset = col("blob_offset")?.as_any().downcast_ref::<UInt64Array>()
1374        .ok_or_else(|| anyhow::anyhow!("index column blob_offset has unexpected type"))?;
1375    let blob_size = col("blob_size")?.as_any().downcast_ref::<UInt64Array>()
1376        .ok_or_else(|| anyhow::anyhow!("index column blob_size has unexpected type"))?;
1377    let checksum = col("checksum")?.as_any().downcast_ref::<FixedSizeBinaryArray>()
1378        .ok_or_else(|| anyhow::anyhow!("index column checksum has unexpected type"))?;
1379
1380    let mut out = Vec::new();
1381    for i in 0..batch.num_rows() {
1382        if paths.value(i) == target {
1383            let ck = checksum32(checksum, i)?;
1384            out.push(ChunkLoc {
1385                chunk_seq: chunk_seq.value(i),
1386                fdata_offset: fdata.value(i),
1387                blob_offset: blob_offset.value(i),
1388                blob_size: blob_size.value(i),
1389                uncompressed_size: usize_col.value(i),
1390                compressed: compressed.value(i),
1391                checksum: ck,
1392            });
1393        }
1394    }
1395    out.sort_by_key(|c| c.chunk_seq);
1396    Ok(out)
1397}
1398
1399pub fn is_probably_compressed(path: &Path) -> bool {
1400    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
1401        let ext = ext.to_ascii_lowercase();
1402        matches!(
1403            ext.as_str(),
1404            "zip" | "gz" | "bz2" | "xz" | "lz" | "lzma" | "7z" | "rar" | "cab"
1405                | "jar" | "war" | "ear" | "zst" | "sz" | "lz4" | "tgz" | "txz"
1406                | "tbz" | "apk" | "dmg" | "deb" | "rpm" | "arrow" | "mpeg" | "mpg"
1407                | "jpeg" | "jpg" | "gif" | "bmp" | "png" | "crate" | "znippy"
1408                | "zdata" | "parquet" | "webp" | "webm"
1409                // already-compressed / high-entropy payloads (Skidbladnir bundles
1410                // carry these): age = encrypted (never shrinks), iso = squashfs
1411                // (already xz/zstd), pdf = Flate streams. NB: pg_dump plain-SQL
1412                // (`.dump`/`.sql`) is deliberately NOT here — it compresses well.
1413                | "age" | "iso" | "pdf"
1414                // git's pack directory — the bulk of any real `.git`, and of
1415                // gunnar's cold tier. `pack` holds deflated objects; `idx`,
1416                // `midx` and `bitmap` hold object ids (hash output) and EWAH
1417                // bitmaps, all incompressible. NB: `.rev` is deliberately NOT
1418                // here — it is a permutation of 0..N as big-endian u32s, whose
1419                // high bytes are mostly zero, so it genuinely does compress.
1420                | "pack" | "idx" | "midx" | "bitmap"
1421        )
1422    } else {
1423        false
1424    }
1425}
1426
1427/// The name-only decision, kept as the fast path it has always been.
1428///
1429/// This is step 2 of the policy in [`crate::precompressed`] — it sees a path and
1430/// nothing else, so it cannot reach an entry that carries no extension (a git
1431/// loose object is named for its oid) and it believes a name that lies. Callers
1432/// holding real bytes should go through
1433/// [`SkipPolicy`](crate::precompressed::SkipPolicy), which runs this first and
1434/// then refines it with a magic-byte probe.
1435pub fn should_skip_compression(path: &Path) -> bool {
1436    is_probably_compressed(path)
1437}
1438
1439#[cfg(test)]
1440mod skip_compression_tests {
1441    use super::*;
1442
1443    #[test]
1444    fn skips_already_compressed_and_encrypted() {
1445        for p in ["secrets/secrets.age", "infra/talos/talos-1.13.iso", "s3/doc.pdf", "x.PDF"] {
1446            assert!(should_skip_compression(Path::new(p)), "should skip {p}");
1447        }
1448    }
1449
1450    #[test]
1451    fn compresses_plain_dump_and_config() {
1452        // pg_dump plain-SQL + declarative config must still be compressed.
1453        for p in ["dbdump/njord.dump", "njord.sql", "lakespec.json", "njordconf/serverspec.json"] {
1454            assert!(!should_skip_compression(Path::new(p)), "should compress {p}");
1455        }
1456    }
1457
1458    /// A packfile and its index are the two largest things in any real `.git`,
1459    /// and neither was on the extension table.
1460    #[test]
1461    fn skips_git_pack_directory_artefacts() {
1462        for p in [
1463            ".git/objects/pack/pack-9f2c.pack",
1464            ".git/objects/pack/pack-9f2c.idx",
1465            ".git/objects/pack/multi-pack-index.midx",
1466            ".git/objects/pack/pack-9f2c.bitmap",
1467        ] {
1468            assert!(should_skip_compression(Path::new(p)), "should skip {p}");
1469        }
1470    }
1471
1472    /// The pack directory is not uniformly incompressible, and the table must
1473    /// not be widened to the whole directory. `.rev` is a permutation of
1474    /// `0..N` as big-endian u32s — its high bytes are mostly zero.
1475    #[test]
1476    fn still_compresses_the_compressible_git_artefacts() {
1477        for p in [
1478            ".git/objects/pack/pack-9f2c.rev",
1479            ".git/objects/pack/pack-9f2c.promisor",
1480            ".git/COMMIT_EDITMSG",
1481            ".git/config",
1482        ] {
1483            assert!(!should_skip_compression(Path::new(p)), "should compress {p}");
1484        }
1485    }
1486}
1487
1488#[derive(Debug, Default)]
1489pub struct VerifyReport {
1490    pub total_files: usize,
1491    pub verified_files: usize,
1492    pub corrupt_files: usize,
1493    pub total_bytes: u64,
1494    pub verified_bytes: u64,
1495    pub corrupt_bytes: u64,
1496    pub chunks: u64,
1497}
1498
1499pub fn list_archive_contents(path: &Path) -> Result<()> {
1500    let (_schema, batches) = read_znippy_index(path)?;
1501    for batch in &batches {
1502        let paths = batch
1503            .column_by_name("relative_path")
1504            .and_then(|c| c.as_any().downcast_ref::<arrow::array::StringArray>())
1505            .ok_or_else(|| anyhow::anyhow!("missing relative_path column"))?;
1506        let sizes = batch
1507            .column_by_name("uncompressed_size")
1508            .and_then(|c| c.as_any().downcast_ref::<arrow::array::UInt64Array>())
1509            .ok_or_else(|| anyhow::anyhow!("missing uncompressed_size column"))?;
1510        let chunk_seqs = batch
1511            .column_by_name("chunk_seq")
1512            .and_then(|c| c.as_any().downcast_ref::<arrow::array::UInt32Array>());
1513        let group_ids = batch
1514            .column_by_name("group_id")
1515            .and_then(|c| c.as_any().downcast_ref::<arrow::array::StringArray>());
1516        let artifact_ids = batch
1517            .column_by_name("artifact_id")
1518            .and_then(|c| c.as_any().downcast_ref::<arrow::array::StringArray>());
1519        let versions = batch
1520            .column_by_name("version")
1521            .and_then(|c| c.as_any().downcast_ref::<arrow::array::StringArray>());
1522        for i in 0..batch.num_rows() {
1523            // Only print once per file (chunk_seq == 0)
1524            if let Some(seqs) = chunk_seqs {
1525                if seqs.value(i) != 0 {
1526                    continue;
1527                }
1528            }
1529            if let (Some(g), Some(a), Some(v)) = (group_ids, artifact_ids, versions) {
1530                if !g.is_null(i) {
1531                    println!(
1532                        "{}\t{}\t{}:{}:{}",
1533                        paths.value(i),
1534                        sizes.value(i),
1535                        g.value(i),
1536                        a.value(i),
1537                        v.value(i)
1538                    );
1539                    continue;
1540                }
1541            }
1542            println!("{}\t{}", paths.value(i), sizes.value(i));
1543        }
1544    }
1545    Ok(())
1546}
1547
1548pub fn verify_archive_integrity(path: &Path) -> Result<VerifyReport> {
1549    let out_dir = PathBuf::from("/dev/null");
1550    decompress_archive(path, false, &out_dir)
1551}
1552
1553#[cfg(test)]
1554mod version_tests {
1555    use super::*;
1556
1557    #[test]
1558    fn current_and_older_versions_are_accepted() {
1559        let mut m = HashMap::new();
1560        // No version recorded (legacy / pre-version archives): read as before.
1561        check_format_version(&m).unwrap();
1562        // Exactly the supported version.
1563        m.insert(FORMAT_VERSION_KEY.into(), ZNIPPY_FORMAT_VERSION.to_string());
1564        check_format_version(&m).unwrap();
1565        // An older version.
1566        m.insert(FORMAT_VERSION_KEY.into(), (ZNIPPY_FORMAT_VERSION - 1).to_string());
1567        check_format_version(&m).unwrap();
1568    }
1569
1570    #[test]
1571    fn newer_version_is_rejected_clearly() {
1572        let mut m = HashMap::new();
1573        m.insert(FORMAT_VERSION_KEY.into(), (ZNIPPY_FORMAT_VERSION + 1).to_string());
1574        let err = check_format_version(&m).unwrap_err().to_string();
1575        assert!(err.contains("newer than this reader supports"), "got: {err}");
1576        assert!(err.contains("upgrade znippy"), "got: {err}");
1577    }
1578
1579    #[test]
1580    fn writer_records_the_current_version() {
1581        let meta = build_arrow_metadata_for_config(&crate::common_config::CONFIG);
1582        assert_eq!(
1583            meta.get(FORMAT_VERSION_KEY).map(String::as_str),
1584            Some(ZNIPPY_FORMAT_VERSION.to_string().as_str())
1585        );
1586    }
1587}
1588
1589#[cfg(test)]
1590mod checksum_width_tests {
1591    use super::*;
1592    use arrow::array::FixedSizeBinaryArray;
1593
1594    /// `FixedSizeBinaryArray` is ONE concrete Arrow type for every width, so the
1595    /// `downcast_ref::<FixedSizeBinaryArray>()` in `decode_lookup` /
1596    /// `locate_file_via_index` succeeds just as happily for a hostile archive
1597    /// declaring `FixedSizeBinary(16)`. The old code then did
1598    /// `[0u8; 32].copy_from_slice(col.value(i))`, which panics — a process abort
1599    /// driven purely by an attacker-supplied schema, on the `znippy get` path.
1600    #[test]
1601    fn narrow_checksum_column_errors_instead_of_panicking() {
1602        let narrow = FixedSizeBinaryArray::try_from_iter([[0u8; 16]].into_iter()).unwrap();
1603        assert_eq!(narrow.value_length(), 16);
1604        let err = checksum32(&narrow, 0).expect_err("a 16-byte checksum column must be an Err");
1605        assert!(
1606            err.to_string().contains("width 16"),
1607            "error must name the bad width, got: {err}"
1608        );
1609    }
1610
1611    #[test]
1612    fn wide_checksum_column_errors_instead_of_truncating() {
1613        let wide = FixedSizeBinaryArray::try_from_iter([[7u8; 64]].into_iter()).unwrap();
1614        assert!(checksum32(&wide, 0).is_err(), "a 64-byte checksum column must be an Err");
1615    }
1616
1617    #[test]
1618    fn correct_width_checksum_is_read_verbatim() {
1619        let mut digest = [0u8; 32];
1620        for (i, b) in digest.iter_mut().enumerate() {
1621            *b = i as u8;
1622        }
1623        let col = FixedSizeBinaryArray::try_from_iter([digest].into_iter()).unwrap();
1624        assert_eq!(checksum32(&col, 0).unwrap(), digest);
1625    }
1626}
1627
1628#[cfg(test)]
1629mod reserved_module_tests {
1630    use super::*;
1631
1632    /// The regression this guards is specific and it has teeth: a module name
1633    /// that the sink writes with `RESERVED_PKG_TYPE` but that
1634    /// [`is_reserved_module`] does not recognise is classified as a **data**
1635    /// sub-index. `read_multi_index` then merges its rows into the file index and
1636    /// `read_znippy_manifest` reports it as a data section — corrupting `list`,
1637    /// `decompress`, the iceberg sink and every manifest-count assertion.
1638    ///
1639    /// So the assertion is not "the constant exists"; it is "every reserved
1640    /// module name in the catalog is classified reserved, and nothing else is".
1641    #[test]
1642    fn every_reserved_module_is_classified_reserved() {
1643        for m in RESERVED_MODULES {
1644            assert!(
1645                is_reserved_module(m),
1646                "'{m}' is in RESERVED_MODULES but is_reserved_module says it is DATA — \
1647                 its rows would be merged into the file index"
1648            );
1649        }
1650    }
1651
1652    /// The three `git` package-format modules, named literally. If someone
1653    /// removes one from [`RESERVED_MODULES`], the loop above still passes
1654    /// (it iterates whatever is left) — this is the test that goes red.
1655    #[test]
1656    fn the_git_format_modules_are_reserved() {
1657        for m in [
1658            GUNNAR_OID_MODULE,
1659            GUNNAR_GRAPH_MODULE,
1660            GUNNAR_REACH_MODULE,
1661            GUNNAR_REFS_MODULE,
1662            GUNNAR_SECRETS_MODULE,
1663        ] {
1664            assert!(is_reserved_module(m), "git package-format module '{m}' must be reserved");
1665            assert!(
1666                RESERVED_MODULES.contains(&m),
1667                "git package-format module '{m}' must be in the catalog"
1668            );
1669        }
1670        assert_eq!(GUNNAR_OID_MODULE, "__gunnar_oid__");
1671        assert_eq!(GUNNAR_GRAPH_MODULE, "__gunnar_graph__");
1672        assert_eq!(GUNNAR_REACH_MODULE, "__gunnar_reach__");
1673        assert_eq!(GUNNAR_REFS_MODULE, "__gunnar_refs__");
1674        assert_eq!(GUNNAR_SECRETS_MODULE, "__gunnar_secrets__");
1675    }
1676
1677    /// The other direction: a data module must NOT be swept up as reserved, or
1678    /// its file rows would silently vanish from `list` and `decompress`.
1679    #[test]
1680    fn ordinary_module_names_stay_data() {
1681        for m in ["maven", "git", "rust", "", "__gunnar__", "gunnar_oid", "__gunnar_oid", "objects"] {
1682            assert!(!is_reserved_module(m), "'{m}' must be treated as a DATA sub-index");
1683        }
1684    }
1685
1686    /// No duplicates and no accidental empty entry in the catalog.
1687    #[test]
1688    fn the_catalog_is_well_formed() {
1689        let mut seen = std::collections::HashSet::new();
1690        for m in RESERVED_MODULES {
1691            assert!(!m.is_empty(), "empty reserved module name");
1692            assert!(seen.insert(*m), "duplicate reserved module name '{m}'");
1693        }
1694        assert_eq!(seen.len(), RESERVED_MODULES.len());
1695    }
1696}