parmesan-par2 0.6.0

Fast, standalone PAR2 (Reed-Solomon) creation, verification and repair tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use crate::packet;
use crate::worker::Par2Worker;
use crate::SimdPath;
use anyhow::{Context, Result};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};

mod geometry;
pub use geometry::calculate_geometry;

/// High-level PAR2 creation parameters.
#[derive(Debug, Clone)]
pub struct CreateOptions {
    pub slice_size: Option<usize>,
    pub slice_count: Option<usize>,
    pub recovery_count: Option<usize>,
    pub recovery_pct: u8,
    pub memory_limit: usize,
    pub threads: usize,
    pub simd: SimdPath,
    /// Align automatic slice sizes to this unit: every slice is a whole
    /// multiple of it, so slice boundaries fall on unit boundaries within each
    /// file. pesto passes its article size, so a lost article damages exactly
    /// one slice. Small sets get one slice per unit at about the total cost
    /// of a 4 GiB set; larger sets keep about 2000 × `effort` slices. Ignored when `slice_size` or `slice_count` is set, and when
    /// padding small files to the aligned size would inflate the set by more
    /// than 15 %. See [`calculate_geometry`].
    pub slice_unit: Option<usize>,
    /// Scales the automatic slice count, and with it the CPU time to create
    /// and repair the set and the number of recovery blocks: 2.0 aims for
    /// twice as many slices as 1.0 (the default), so a set aligned to
    /// `slice_unit` survives twice as many scattered lost units. Must be a
    /// positive finite number.
    pub effort: f64,
}

impl Default for CreateOptions {
    fn default() -> Self {
        Self {
            slice_size: None,
            slice_count: None,
            recovery_count: None,
            recovery_pct: 10,
            memory_limit: 1024 * 1024 * 1024, // 1 GiB
            threads: 0,                       // auto
            simd: SimdPath::Auto,
            slice_unit: None,
            effort: 1.0,
        }
    }
}

/// How to fit `recovery_count × slice_size` into `memory_limit`.
///
/// Parpar can cut the **slice** into windows (`chunks`) so every recovery
/// block stays in the working set; we used to only cut **recovery** into
/// passes, which re-reads the input once per pass and never overlaps dests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryLayout {
    /// Recovery blocks held at once.
    pub recovery_per_pass: usize,
    /// Bytes of each logical slice processed per encoder (equals `slice_size`
    /// when no windowing).
    pub slice_chunk: usize,
}

/// Byte window of each logical PAR2 slice sent to the encoder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SliceWindow {
    pub offset: usize,
    pub len: usize,
}

/// File hashes and per-slice checksums collected while ingesting.
#[derive(Debug, Default)]
pub struct IngestHashes {
    pub hashes: Vec<crate::encoder::FileHashes>,
    pub checksums: Vec<packet::SliceChecksum>,
}

/// Choose recovery-pass vs slice-chunk so the RS buffers fit `memory_limit`.
///
/// Prefers holding **all** recovery blocks with a 4 KiB-aligned slice window
/// when that window is at least 4 KiB. Otherwise falls back to full-slice
/// recovery passes (the previous behaviour).
pub fn plan_memory_layout(
    slice_size: usize,
    recovery_count: usize,
    memory_limit: usize,
) -> MemoryLayout {
    let slice_size = slice_size.max(32);
    if recovery_count == 0 {
        return MemoryLayout {
            recovery_per_pass: 0,
            slice_chunk: slice_size,
        };
    }
    let limit = memory_limit.max(slice_size);
    if recovery_count.saturating_mul(slice_size) <= limit {
        return MemoryLayout {
            recovery_per_pass: recovery_count,
            slice_chunk: slice_size,
        };
    }
    let raw = (limit / recovery_count).max(32);
    let chunk = (raw / 32) * 32;
    if chunk < slice_size && chunk >= 4096 {
        return MemoryLayout {
            recovery_per_pass: recovery_count,
            slice_chunk: chunk,
        };
    }
    let per = (limit / slice_size).max(1).min(recovery_count);
    MemoryLayout {
        recovery_per_pass: per,
        slice_chunk: slice_size,
    }
}

/// Metadata for an input file to be protected by PAR2.
#[derive(Debug, Clone)]
pub struct InputFile {
    pub path: PathBuf,
    pub display_name: String,
    pub size: u64,
}

/// Reorder `files` into the canonical order the PAR2 spec requires for
/// Reed-Solomon block indices: ascending numeric order of File ID.
///
/// Per the Parity Volume Set Specification, the Main packet lists File IDs
/// sorted as 16-byte unsigned integers, and "the first source block from the
/// first file in this sorted list receives the first valid constant, the
/// second block receives the second constant, and so on" — i.e. this order,
/// not the order files were passed on the command line, determines which
/// Reed-Solomon coefficient each input slice gets. Any reader that follows
/// the spec (par2cmdline included) computes coefficients this way, so the
/// encoder must feed slices in this order for multi-file recovery sets to be
/// repairable by anything other than this exact build of `parmesan`.
///
/// File ID only needs the first 16 KiB of each file (`compute_file_id`
/// hashes the 16k head, not the whole file), so this is a cheap pre-pass —
/// full-file hashing still happens once, later, during encoding.
pub fn sort_files_by_file_id(files: &mut Vec<InputFile>) -> Result<()> {
    use std::io::Read;

    let mut keyed: Vec<([u8; 16], InputFile)> = Vec::with_capacity(files.len());
    for f in files.drain(..) {
        let mut file = std::fs::File::open(&f.path)
            .with_context(|| format!("opening `{}` to compute its File ID", f.path.display()))?;
        let mut head = vec![0u8; 16 * 1024];
        let mut read = 0usize;
        while read < head.len() {
            match file.read(&mut head[read..])? {
                0 => break,
                n => read += n,
            }
        }
        head.truncate(read);
        let md5_16k = packet::md5(&head);
        let file_id = packet::compute_file_id(&md5_16k, f.size, &f.display_name);
        keyed.push((file_id, f));
    }
    keyed.sort_by(|(a, _), (b, _)| packet::cmp_file_id(a, b));
    *files = keyed.into_iter().map(|(_, f)| f).collect();
    Ok(())
}

/// Chunk size for the streaming reader below, and the cutoff under which a
/// file is read as a single blocking call instead — see `ingest_files`.
const CHUNK_SIZE: usize = 8 * 1024 * 1024; // 8 MiB

/// Split `chunk` into `slice_size` pieces, handing each completed-and-since-
/// superseded slice to `worker` (holding the newest one back — see
/// `ingest_files`'s doc comment for why). Pure and synchronous: callers doing
/// this from async context are responsible for wrapping the call in
/// `tokio::task::block_in_place`, since `Par2Worker::send_slice` blocks.
struct HashSink {
    out: IngestHashes,
    current: crate::encoder::FileHasher,
}

fn emit_logical_slice(
    worker: &Par2Worker,
    logical: Vec<u8>,
    actual_len: usize,
    is_last: bool,
    window: Option<SliceWindow>,
    hash: Option<&mut HashSink>,
) {
    if let Some(h) = hash {
        let checksum = h.current.update_and_hash_slice(&logical, actual_len);
        h.out.checksums.push(checksum);
        if is_last {
            let finished = std::mem::take(&mut h.current);
            h.out.hashes.push(finished.finish());
        }
    }
    if let Some(w) = window {
        let mut part = worker.take_buffer(w.len);
        part.clear();
        if w.offset < logical.len() {
            let end = (w.offset + w.len).min(logical.len());
            part.extend_from_slice(&logical[w.offset..end]);
        }
        part.resize(w.len, 0);
        let win_actual = actual_len.saturating_sub(w.offset).min(w.len);
        worker.send_slice(part, win_actual, is_last);
    } else {
        worker.send_slice(logical, actual_len, is_last);
    }
}

fn feed_chunk(
    chunk: &[u8],
    worker: &Par2Worker,
    slice_size: usize,
    slice_accum: &mut Vec<u8>,
    held: &mut Option<Vec<u8>>,
    window: Option<SliceWindow>,
    mut hash: Option<&mut HashSink>,
) {
    let mut chunk_pos = 0;
    while chunk_pos < chunk.len() {
        let space = slice_size - slice_accum.len();
        let take = space.min(chunk.len() - chunk_pos);
        slice_accum.extend_from_slice(&chunk[chunk_pos..chunk_pos + take]);
        chunk_pos += take;

        if slice_accum.len() >= slice_size {
            let next = worker.take_buffer(slice_size);
            let padded = std::mem::replace(slice_accum, next);
            if let Some(previous) = held.replace(padded) {
                emit_logical_slice(
                    worker,
                    previous,
                    slice_size,
                    false,
                    window,
                    hash.as_deref_mut(),
                );
            }
        }
    }
}

/// Ingests files into a PAR2 worker.
///
/// Exactly one slice per file is flagged `is_last_of_file`, and it must be the
/// file's genuine final slice: [`Par2Worker`]'s hasher thread finalizes and
/// pushes a file's MD5/CRC only on that flag. Miss it and the hash bleeds into
/// the next file — or, for the last file in the set, the worker returns fewer
/// hashes than there are files and the caller indexes past the end.
///
/// Getting that right is why the last slice is held back rather than sent as
/// soon as it is full: only once the reader has hit EOF is it known whether
/// the slice just filled was the last one. Flagging only a *partial* trailing
/// slice — the obvious shortcut — silently breaks every file whose size is an
/// exact multiple of the slice size, which is not an edge case at all once a
/// caller passes an explicit `--slice-size` (see the regression test in
/// `tests/exact_multiple_slice_size.rs`, and the same bug's earlier sighting
/// in `pesto`'s poster, `crates/pesto/tests/par2_exact_multiple_of_slice_size.rs`).
///
/// See [`ingest_files_with`] for cancellation and per-file hooks.
pub async fn ingest_files(
    files: &[InputFile],
    worker: &Par2Worker,
    slice_size: usize,
) -> Result<()> {
    ingest_files_with(files, worker, slice_size, None, |_| Ok(())).await
}

/// [`ingest_files`] with optional cancel and a per-file hook so callers such
/// as `pesto` can drive progress without reimplementing the reader.
pub async fn ingest_files_with<F>(
    files: &[InputFile],
    worker: &Par2Worker,
    slice_size: usize,
    cancelled: Option<&AtomicBool>,
    after_file: F,
) -> Result<()>
where
    F: FnMut(&InputFile) -> Result<()>,
{
    ingest_files_ex(files, worker, slice_size, cancelled, after_file, None, None).await
}

/// [`ingest_files_with`] plus a callback after every input chunk is read.
///
/// The callback is intentionally byte-based: it stays responsive even when a
/// single logical PAR2 slice is very large.
pub async fn ingest_files_with_progress<F, P>(
    files: &[InputFile],
    worker: &Par2Worker,
    slice_size: usize,
    cancelled: Option<&AtomicBool>,
    after_file: F,
    on_read: P,
) -> Result<()>
where
    F: FnMut(&InputFile) -> Result<()>,
    P: FnMut(usize) -> Result<()>,
{
    ingest_files_ex_impl(
        files, worker, slice_size, cancelled, after_file, None, None, on_read,
    )
    .await
}

/// Like [`ingest_files_with`], optionally sending only a window of each
/// logical slice (P1b) and/or collecting file hashes + slice checksums in
/// the reader so the encoder can run on chunk-sized buffers.
pub async fn ingest_files_ex<F>(
    files: &[InputFile],
    worker: &Par2Worker,
    slice_size: usize,
    cancelled: Option<&AtomicBool>,
    after_file: F,
    window: Option<SliceWindow>,
    hash_out: Option<&mut IngestHashes>,
) -> Result<()>
where
    F: FnMut(&InputFile) -> Result<()>,
{
    ingest_files_ex_impl(
        files,
        worker,
        slice_size,
        cancelled,
        after_file,
        window,
        hash_out,
        |_| Ok(()),
    )
    .await
}

#[allow(clippy::too_many_arguments)]
async fn ingest_files_ex_impl<F, P>(
    files: &[InputFile],
    worker: &Par2Worker,
    slice_size: usize,
    cancelled: Option<&AtomicBool>,
    mut after_file: F,
    window: Option<SliceWindow>,
    mut hash_out: Option<&mut IngestHashes>,
    mut on_read: P,
) -> Result<()>
where
    F: FnMut(&InputFile) -> Result<()>,
    P: FnMut(usize) -> Result<()>,
{
    let mut hash = hash_out.as_mut().map(|_| HashSink {
        out: IngestHashes::default(),
        current: crate::encoder::FileHasher::new(),
    });
    for file_info in files {
        if cancelled.is_some_and(|c| c.load(Ordering::Relaxed)) {
            return Ok(());
        }
        // Empty files contribute no slices; the hasher only finalizes on
        // `is_last_of_file`, and callers insert the known empty-file hash.
        if file_info.size == 0 {
            continue;
        }
        let mut slice_accum = worker.take_buffer(slice_size);
        slice_accum.clear();
        // The most recently filled slice, not yet handed to the worker: it
        // cannot be sent until we know whether another slice follows it.
        let mut held: Option<Vec<u8>> = None;

        // A file that fits in one chunk has nothing for the double-buffered
        // path below to overlap — reading it is one syscall either way — so
        // route it through a single blocking read instead of paying a
        // spawn_blocking task and an mpsc channel for that one syscall.
        // Skipping this matters at scale: a corpus of thousands of
        // sub-slice files (see `bench/FINDINGS.md` §5, issue #131) pays that
        // task-spawn/channel-handoff cost once per file regardless of size,
        // and profiling showed it dominating wall time — most cores sitting
        // idle between the rare flushes big enough to need them — even
        // though the per-file work is tiny.
        if (file_info.size as usize) <= CHUNK_SIZE {
            let path = file_info.path.clone();
            tokio::task::block_in_place(|| -> Result<()> {
                let buf = std::fs::read(&path)
                    .with_context(|| format!("reading `{}`", path.display()))?;
                on_read(buf.len())?;
                feed_chunk(
                    &buf,
                    worker,
                    slice_size,
                    &mut slice_accum,
                    &mut held,
                    window,
                    hash.as_mut(),
                );
                Ok(())
            })?;
        } else {
            let (tx, mut rx) = tokio::sync::mpsc::channel::<Vec<u8>>(4);
            let path = file_info.path.clone();

            // Double-buffered reader task: fetch data while we process previous chunks.
            let reader_handle = tokio::task::spawn_blocking(move || {
                use std::fs::File;
                use std::io::Read;
                let mut file = File::open(&path)?;
                loop {
                    let mut buf = vec![0u8; CHUNK_SIZE];
                    let n = file.read(&mut buf)?;
                    if n == 0 {
                        break;
                    }
                    buf.truncate(n);
                    if tx.blocking_send(buf).is_err() {
                        break;
                    }
                }
                Ok::<_, anyhow::Error>(())
            });

            while let Some(chunk) = rx.recv().await {
                if cancelled.is_some_and(|c| c.load(Ordering::Relaxed)) {
                    drop(rx);
                    let _ = reader_handle.await;
                    return Ok(());
                }
                on_read(chunk.len())?;
                tokio::task::block_in_place(|| {
                    feed_chunk(
                        &chunk,
                        worker,
                        slice_size,
                        &mut slice_accum,
                        &mut held,
                        window,
                        hash.as_mut(),
                    );
                });
            }

            reader_handle.await??;
        }

        if !slice_accum.is_empty() {
            // A partial trailing slice: it is the last one, and anything held
            // back is not.
            if let Some(previous) = held.take() {
                tokio::task::block_in_place(|| {
                    emit_logical_slice(worker, previous, slice_size, false, window, hash.as_mut());
                });
            }
            let actual_len = slice_accum.len();
            slice_accum.resize(slice_size, 0);
            tokio::task::block_in_place(|| {
                emit_logical_slice(worker, slice_accum, actual_len, true, window, hash.as_mut());
            });
        } else if let Some(last) = held.take() {
            tokio::task::block_in_place(|| {
                emit_logical_slice(worker, last, slice_size, true, window, hash.as_mut());
            });
        }

        after_file(file_info)?;
    }

    if let (Some(dst), Some(src)) = (hash_out.as_mut(), hash) {
        **dst = src.out;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn memory_layout_fits_in_one_pass() {
        let plan = plan_memory_layout(1 << 20, 200, 1 << 30);
        assert_eq!(plan.recovery_per_pass, 200);
        assert_eq!(plan.slice_chunk, 1 << 20);
    }

    #[test]
    fn memory_layout_prefers_slice_chunk_when_all_dests_fit() {
        // 1000 × 6 MiB = 6 GiB; 1 GiB limit → 1 MiB windows, all dests.
        let slice = 6 * 1024 * 1024;
        let plan = plan_memory_layout(slice, 1000, 1024 * 1024 * 1024);
        assert_eq!(plan.recovery_per_pass, 1000);
        assert!(plan.slice_chunk < slice);
        assert!(plan.slice_chunk >= 4096);
        assert!(plan.slice_chunk.is_multiple_of(32));
        assert!(1000 * plan.slice_chunk <= 1024 * 1024 * 1024);
    }

    #[test]
    fn memory_layout_falls_back_to_recovery_passes_when_chunk_tiny() {
        // Tiny limit: cannot hold 4 KiB × all dests.
        let plan = plan_memory_layout(1 << 20, 10_000, 8 * 1024);
        assert_eq!(plan.slice_chunk, 1 << 20);
        assert!(plan.recovery_per_pass < 10_000);
        assert!(plan.recovery_per_pass >= 1);
    }

    #[test]
    fn sort_files_by_file_id_orders_by_ascending_file_id_not_input_order() {
        let dir = std::env::temp_dir().join(format!(
            "parmesan-ops-test-{}-{:?}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();

        // Content (and therefore File ID) is unrelated to file name or the
        // order files are listed in — that's exactly the case this fix
        // matters for.
        let names = ["zzz.bin", "aaa.bin", "mmm.bin"];
        let mut files = Vec::new();
        for (i, name) in names.iter().enumerate() {
            let path = dir.join(name);
            let data = vec![i as u8; 100 + i * 37];
            std::fs::write(&path, &data).unwrap();
            files.push(InputFile {
                path,
                display_name: (*name).to_string(),
                size: data.len() as u64,
            });
        }

        // Compute the expected order independently of the function under test.
        let mut expected_ids = Vec::new();
        for f in &files {
            let bytes = std::fs::read(&f.path).unwrap();
            let md5_16k = crate::packet::md5(&bytes);
            expected_ids.push(crate::packet::compute_file_id(
                &md5_16k,
                f.size,
                &f.display_name,
            ));
        }
        expected_ids.sort_by(crate::packet::cmp_file_id);

        sort_files_by_file_id(&mut files).unwrap();

        let got_ids: Vec<[u8; 16]> = files
            .iter()
            .map(|f| {
                let bytes = std::fs::read(&f.path).unwrap();
                let md5_16k = crate::packet::md5(&bytes);
                crate::packet::compute_file_id(&md5_16k, f.size, &f.display_name)
            })
            .collect();
        assert_eq!(got_ids, expected_ids);
        assert!(got_ids
            .windows(2)
            .all(|w| crate::packet::cmp_file_id(&w[0], &w[1]).is_le()));

        std::fs::remove_dir_all(&dir).ok();
    }
}