Skip to content
Merged
38 changes: 34 additions & 4 deletions docs/compat-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,42 @@ Where it applies:
- `RarSession`, through `RarSessionOptions::damaged_input`: the catalogue reads past the damage, its members list and
extract (`extract_selected`, `extract_selected_controlled`, printing) as any other, and
`RarArchiveMetadata::damaged_headers()` says what was read past;
- `Volume::open_damaged` and `Volume::take_damage` for callers walking blocks themselves.

Progressive and persistent-partial sessions still refuse a damaged header whatever `damaged_input` says; listings that
take no `Options` (`extract::list`, `list_detailed`, `inspect*`) refuse it too. The CLI takes `--salvage` and
- `Volume::open_damaged` and `Volume::take_damage` for callers walking blocks themselves;
- progressive sessions (`ProgressiveSession::new`, `from_sealed_memory`), through `ProgressiveOptions::extract`: each
damaged header is a `ProgressiveEvent::HeaderDamaged { volume, damage }`, delivered before anything read from that
header (so just before the `MemberStarted` of a member whose header failed its checksum). See "Damaged headers in a
progressive session" below.

Persistent-partial sessions (`ProgressiveSession::new_partial`) still refuse a damaged header whatever `damaged_input`
says: their journal binds each member to the header bytes it was read from, and a header repaired later would no longer
match. Listings that take no `Options` (`extract::list`, `list_detailed`, `inspect*`) refuse it too. The CLI takes `--salvage` and
`--resync` (for `l`, `t`, `x`, `e`; a listing of anything but RAR stays as it is), prints each damaged header and a warning, and exits 1.
`tests/rar_salvage.rs` checks each point.

### Damaged headers in a progressive session

A progressive session reads bytes as its source delivers them, and a range that is missing now may be filled later
(an article fetched again, PAR2 repair, another copy). So only bytes the source has delivered are ever judged damaged:

- A header the source has not delivered yet (`RangeRead::NotYet`, or a volume not there yet) is waited on, as under
`Refuse`; it is never damage. When it arrives it is read as any other header.
- A header inside a range the source answers as a `Hole` is not damage either: the session stops with `HeaderHole`, as
under `Refuse`, since the bytes may yet be repaired. Run the session again once they are.
- A resync never searches across a `Hole`: the next header may be inside it, so the search stops with `HeaderHole`.
On a source whose end isn't known yet (`GrowingFileVolumes`) a search that has not found a header waits for more
bytes, as reading a header does.
- Only a header whose bytes were delivered and are wrong (a checksum that fails, fields that don't parse, a header or
data running past the volume's known end) is salvaged, exactly as the whole-archive runners do: the same members,
the same bytes and the same `HeaderDamage` records (`volume` is the volume's name as the session asked for it).

Under salvage the session takes the volume's length from the source (`SourceInfo::length`, unless it is
`Advertised(u64::MAX)`), so a header or data running past it is a volume cut short (`UnexpectedEnd`). The rest of a
damaged volume is passed over as in the whole-archive runners: a volume set goes on with its next volume (a sealed set,
or a declared last volume, may end there), anything else ends. A folder whose damaged header claims data is created and
its data passed over. A resync's work is bounded as the whole-archive one's, by 16 times the furthest offset read in
the volume plus 1 MiB, and it charges its window (at most a chunk and one candidate header) to `MetadataBytes`. Bytes
passed over break a solid archive's history: the next solid member stops with `SolidHistoryUnavailable`.
`tests/progressive_salvage.rs` checks each point.
## `unsupported_filters`: a RAR 3.x/4.x member using a VM filter zipfast doesn't run

RAR 3.x/4.x carries filters as programs for its virtual machine. zipfast runs the standard ones (E8, E8E9, ITANIUM,
Expand Down
2 changes: 1 addition & 1 deletion docs/persistent-partials.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This H2 path accepts RAR5 Stored members with known sizes and a Complete source

`PartialReady` follows durable genesis and precedes payload reads. Retain its `MemberKey` and obtain a handle with `partial_file`. Once a member has finished and its handle has been taken with `partial_file`, the session drops its own reference: the member's state pages, descriptors and `open_partials` lease are freed when the caller drops its last handle, so a set's member count is not bounded by `state_pages` or `open_files`. Drop each handle when done with it. The session never verifies or extracts a released member again; `partial_file` returns another handle while one lives, and `BindingMismatch(Member)` after the last is dropped (reopen it with `PartialFile::open`). Damage that `declare_hole` maps to a released member whose handles were all dropped is listed in `DamageReceipt::released` as `(MemberKey, PackedSpan)`, never in `unmapped`: reopen that member with `PartialFile::open` and declare the span there with `PartialFile::declare_hole`. While a handle lives, the damage is applied to it as before. `PartialFile::relative_output_path` returns the sanitized output path relative to the output root (normal components only) and `PartialFile::output_path` the absolute path (the root made absolute joined with it), so a caller need not walk the output tree to find it. Each `Hole` describes an observed output range and exact packed spans; waiting for a provider is never damage. A persistent session lets a source answer one `RangeRead::Hole` longer than the read buffer while it extracts a member's data: up to the end of that member's packed data in the volume and at most 256 MiB, and not past the start of a range declared with `declare_hole`. A longer answer inside the volume is accepted up to that limit and the rest is read again (the source cannot see where the part ends, how much the read needs or what was declared); on the session's other reads (headers, and the walk that resumes an adopted member, such as an AES member's 16-byte previous ciphertext block) a longer answer inside the volume is trimmed to the read in the same way, so a source may answer every hole whole. One past the volume's length is a `SourceContract` failure, as is any answer longer than the buffer outside persistent sessions. Such an answer is recorded as one hole, in one transaction, and delivered as one `Hole` event (an encrypted member also reports the block after it as a `CipherNeighbour` hole when that block's ciphertext is read later), so a dead volume answered once costs one record and one journal transaction per 256 MiB; refill it later in as many `refill_from_packed` pieces as you like. A source that answers one buffer per `Hole` still works: the run arrives as one `Hole` event per buffer, and the state stores it as one record: a hole that continues the last recorded one (same cause, the next output, packed bytes running on in the same volume) extends it, so `ProgressiveLimits::hole_intervals` counts unread ranges per volume, not buffers. Copies of the member's index, hole list and kept ciphertext blocks made while a hole is recorded are charged only while it is recorded, so recording a hole costs the same however much output and however many earlier holes came before it. What stays charged for the session is the record (when the hole starts a new one, a few hundred bytes) and each transaction's journal bookkeeping (about 4 KiB). So a contiguous range is stored as one record, but each of its `Hole` events still adds that bookkeeping: at the default `metadata_bytes` that is a few tens of GiB of unread data per session in 1 MiB holes, and holes scattered between data cost in proportion to their number. `Hole` and `BytesWritten` events arrive in output order: every hole below a `BytesWritten`'s `logical_end` has been delivered before it. A fill cannot target unproduced preallocation, except a tail abandoned with `abandon_tail` (see "Taking over an unfinished member" below). Known overlaps must match before any write. The handle can fill and poll verification while another member progresses, and survives session cancellation/drop. Whole-member `Ok` and archive metadata require the canonical member finish to be durably committed, even when every output byte is already known. A handle paused or closed before that finish returns `Incomplete` (possibly with no holes); reopen preserves this distinction. Deferred part checks appear in `Verdict::Incomplete.parts` as well as `Ok.parts`; a live `PartVerified` event is a mirror.

Damage can also be declared late, for example when an article's yEnc CRC turns out wrong after zipfast consumed it. `ProgressiveSession::declare_hole(volume, range)` takes zero-based volume and archive byte offsets at any time. Output already written from those bytes becomes `DeclaredBad` holes in each affected member; the returned `DamageReceipt` lists them, plus `unmapped` spans (header or padding bytes with no output) and `pending` spans not read yet. Pending bytes are skipped without asking the provider and reported as `Hole` events with cause `DeclaredBad` when reached. A finished member's handle sees the new holes, verifies `Incomplete` until they are filled, and then verifies `Ok` again. After the session ends, `PartialFile::declare_hole` does the same for one member. Declaring damage on a header that has not been read yet is not supported yet: the session stops with `HeaderHole` when it reaches it. Each member's holes are committed one durable run at a time, so if `declare_hole` returns an error part way, earlier runs may already be holes; `PartialFile::remaining` shows the current state and repeating the call reports only what is left. Pending ranges past the end of a volume are never reached and so never reported. Hole causes are kept in the durable state; states written before causes were recorded reopen with every hole as `Missing`, and a build from before causes were recorded cannot open a state that holds declared damage.
Damage can also be declared late, for example when an article's yEnc CRC turns out wrong after zipfast consumed it. `ProgressiveSession::declare_hole(volume, range)` takes zero-based volume and archive byte offsets at any time. Output already written from those bytes becomes `DeclaredBad` holes in each affected member; the returned `DamageReceipt` lists them, plus `unmapped` spans (header or padding bytes with no output) and `pending` spans not read yet. Pending bytes are skipped without asking the provider and reported as `Hole` events with cause `DeclaredBad` when reached. A finished member's handle sees the new holes, verifies `Incomplete` until they are filled, and then verifies `Ok` again. After the session ends, `PartialFile::declare_hole` does the same for one member. Declaring damage on a header that has not been read yet is not supported yet: the session stops with `HeaderHole` when it reaches it. A header whose bytes are present but damaged (failing its checksum or not parsing) stops a persistent session too, whatever `extract::Options::damaged_input` says: the journal binds each member to the header bytes it was read from, so salvaging (which ordinary progressive sessions do, see `docs/compat-options.md`) is not offered here yet. Each member's holes are committed one durable run at a time, so if `declare_hole` returns an error part way, earlier runs may already be holes; `PartialFile::remaining` shows the current state and repeating the call reports only what is left. Pending ranges past the end of a volume are never reached and so never reported. Hole causes are kept in the durable state; states written before causes were recorded reopen with every hole as `Missing`, and a build from before causes were recorded cannot open a state that holds declared damage.

**A part that fails its own check.** Some damage no transport check sees: an article whose yEnc CRC passed but whose bytes are wrong (corrupted before encoding, or a posting of a damaged volume). The session finds it at the end of the part, when the part's packed CRC32 or BLAKE2sp (RAR5) or header CRC (RAR4) fails. For a plain stored member it no longer ends the session there: a part read whole, with no byte missing, that fails its check holds a wrong byte somewhere in its output, so the session declares the part's packed bytes damaged on the member, as `declare_hole` would: the part's output becomes one `DeclaredBad` hole, delivered as a `Hole` event (after the `BytesWritten` events of that output, whose bytes it now calls unknown), with the part's packed span. The member is not judged on the bad bytes: it finishes `PersistentPartial`, and every member after it is extracted as usual, each packed byte read once. The final part has no check of its own (its header holds the member's checks), so a member whose final checks fail while every part before the last passed a check of its own has the wrong byte in its last part, which becomes the `DeclaredBad` hole the same way. Refill the hole with `refill_from_packed` once the volume is repaired (par2 or another copy): it reads that part again, which the part's check cannot narrow, and nothing else. Before, the session ended with `Rar(Integrity { reason: PackedPiece | FinalChecks })` and a caller extracted the set again from its first volume. A part or member that cannot be placed keeps the earlier behaviour: an encrypted member's part (its checks cover ciphertext and its CBC chain runs across parts; it still ends the session), a part with a hole in it (judged by `poll_verify` once filled, `PartBad`), and a member in one part whose checks fail (the session ends, as before), and a final check that fails after a part before the last went unchecked (`Bad`). For those, declare the damaged volume's range with `PartialFile::declare_hole` after `PartBad` or `Bad` and refill it after the repair; only this member's output in it becomes a hole, so `refill_from_packed` over the whole volume reads only those bytes.

Expand Down
9 changes: 9 additions & 0 deletions docs/progressive-archives.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ the complete extractor; this entrypoint creates no links. Stored recovery is
available for the admitted RAR4 and RAR5 subsets below. Full original
compatibility remains open.

## Damaged headers

`ProgressiveOptions::extract.damaged_input` (`Salvage`, `Resync`) reads past damaged RAR headers as the whole-archive
runners do, and reports each as `ProgressiveEvent::HeaderDamaged { volume, damage }`. The default `Refuse` fails as
before. The rule with holes: only delivered bytes are judged. A header the source has not delivered yet is waited on,
one inside a `Hole` answer stops the session with `HeaderHole` (repair may still fill it), and a resync never searches
across a hole. See `docs/compat-options.md` ("Damaged headers in a progressive session"). Persistent-partial sessions
keep refusing damaged headers.

## RAR4 capability boundary

| Progressive RAR4 case | Current admission |
Expand Down
9 changes: 6 additions & 3 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,12 @@ pub struct Options {
/// 7-Zip would ("Corrupt header is found", "Headers Error").
///
/// Honoured by the whole-archive RAR runners ([`run`], [`run_with_password`], [`run_rar_with_outcomes`], the
/// checked dispatchers), by [`list_detailed_with_options`], and by [`crate::rar5::session::RarSession`] through
/// [`crate::rar5::session::RarSessionOptions::damaged_input`]. Progressive and persistent-partial sessions refuse a
/// damaged header whatever this says. 7z and ZIP ignore it.
/// checked dispatchers), by [`list_detailed_with_options`], by [`crate::rar5::session::RarSession`] through
/// [`crate::rar5::session::RarSessionOptions::damaged_input`], and by progressive sessions through
/// [`crate::progressive::ProgressiveOptions::extract`] (reported as
/// [`crate::progressive::ProgressiveEvent::HeaderDamaged`]; only bytes the source delivered are judged, a header
/// still to come is waited on and one in a hole is `HeaderHole`). Persistent-partial sessions refuse a damaged
/// header whatever this says. 7z and ZIP ignore it.
pub damaged_input: DamagedInput,
}

Expand Down
Loading
Loading