par2-rs 0.8.0

PAR2 parity verification and repair
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
//! Obfuscated filename recovery and split-file detection.
//!
//! Usenet files are frequently uploaded with randomized filenames to evade
//! takedowns. PAR2 file descriptions contain the original filename and a 16KB
//! MD5 hash, which can be used to identify and rename obfuscated files.

use std::collections::HashMap;
use std::fs::{self, File};
use std::io;
use std::path::{Path, PathBuf};

use tracing::debug;

use crate::checksum;
use crate::packet::header::{self, MAGIC, PacketHeader};
use crate::par2_set::Par2FileSet;
use crate::path::is_generated_par2_artifact_name;
use crate::types::{FileId, RecoverySetId};

/// A suggestion to rename a file to its correct name.
#[derive(Debug, Clone)]
pub struct RenameSuggestion {
    /// Current path on disk.
    pub current_path: PathBuf,
    /// The correct filename from PAR2 metadata.
    pub correct_name: String,
    /// The file ID this matched against.
    pub file_id: FileId,
    /// How the match was determined.
    pub match_type: MatchType,
}

/// How an obfuscated file was identified.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchType {
    /// Matched via MD5 hash of the first 16KB.
    Hash16k,
    /// Identified as a PAR2 file by its packet header recovery set ID.
    Par2File,
}

/// Scan a directory and match files against PAR2 file descriptions.
///
/// Reads the first 16KB of each file, computes its MD5 hash, and compares
/// against the `hash_16k` values from the PAR2 set. Returns rename suggestions
/// for files whose names don't match the PAR2 metadata.
///
/// Does **not** perform any actual renames — the caller decides what to do.
pub fn scan_for_renames(dir: &Path, par2_set: &Par2FileSet) -> io::Result<Vec<RenameSuggestion>> {
    // Build lookup: hash_16k -> (FileId, correct_filename)
    let mut hash_lookup: HashMap<[u8; 16], (FileId, &str)> = HashMap::new();
    for (file_id, desc) in &par2_set.files {
        hash_lookup.insert(desc.hash_16k, (*file_id, &desc.filename));
    }

    // Set of filenames already correctly named (so we don't suggest renaming
    // a file that already has the right name).
    let known_filenames: std::collections::HashSet<&str> = par2_set
        .files
        .values()
        .map(|d| d.filename.as_str())
        .collect();

    let mut suggestions = Vec::new();

    let entries = fs::read_dir(dir)?;
    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        if !path.is_file() {
            continue;
        }

        let file_name = match path.file_name().and_then(|n| n.to_str()) {
            Some(n) => n,
            None => continue,
        };

        if is_generated_par2_artifact_name(file_name) {
            continue;
        }

        // Skip files that already have a known correct name.
        if known_filenames.contains(file_name) {
            continue;
        }

        // Read first 16KB and compute MD5.
        let data = read_first_n_bytes(&path, 16384)?;
        if data.is_empty() {
            continue;
        }
        let hash = checksum::md5(&data);

        if let Some(&(file_id, correct_name)) = hash_lookup.get(&hash)
            && file_name != correct_name
        {
            debug!(
                "rename match: {} -> {} (16k hash match)",
                file_name, correct_name
            );
            suggestions.push(RenameSuggestion {
                current_path: path,
                correct_name: correct_name.to_string(),
                file_id,
                match_type: MatchType::Hash16k,
            });
        }
    }

    Ok(suggestions)
}

/// Identify PAR2 files in a directory that belong to a specific recovery set.
///
/// Reads the first 64 bytes of each file to check for PAR2 magic and extract
/// the recovery set ID. Returns paths of files that match `expected_set_id`
/// but may have obfuscated names.
pub fn identify_par2_files(
    dir: &Path,
    expected_set_id: &RecoverySetId,
) -> io::Result<Vec<PathBuf>> {
    let mut matches = Vec::new();

    let entries = fs::read_dir(dir)?;
    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        if !path.is_file() {
            continue;
        }

        let Some(file_name) = path.file_name().and_then(|name| name.to_str()) else {
            continue;
        };

        if is_generated_par2_artifact_name(file_name) {
            continue;
        }

        let data = read_first_n_bytes(&path, header::HEADER_SIZE)?;
        if data.len() < header::HEADER_SIZE {
            continue;
        }

        // Check for PAR2 magic.
        if &data[0..8] != MAGIC {
            continue;
        }

        // Try to parse the header.
        if let Ok(hdr) = PacketHeader::parse(&data, 0)
            && hdr.recovery_set_id == *expected_set_id
        {
            debug!("identified par2 file: {}", path.display());
            matches.push(path);
        }
    }

    Ok(matches)
}

/// A group of split files that together form one logical file.
#[derive(Debug, Clone)]
pub struct SplitFileGroup {
    /// The base name without the numeric extension (e.g., "movie.mkv" for "movie.mkv.001").
    pub base_name: String,
    /// Paths of the split parts, sorted by part number.
    pub parts: Vec<PathBuf>,
    /// The part numbers found (e.g., [1, 2, 3]).
    pub part_numbers: Vec<u32>,
    /// Whether the sequence is contiguous starting from 1 (no gaps).
    pub contiguous: bool,
}

/// Detect split files (`.001`, `.002`, etc.) in a directory.
///
/// Groups files by base name and returns groups with 2 or more parts.
pub fn detect_split_files(dir: &Path) -> io::Result<Vec<SplitFileGroup>> {
    let mut groups: HashMap<String, Vec<(u32, PathBuf)>> = HashMap::new();

    let entries = fs::read_dir(dir)?;
    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        if !path.is_file() {
            continue;
        }

        let file_name = match path.file_name().and_then(|n| n.to_str()) {
            Some(n) => n.to_string(),
            None => continue,
        };

        // Check if the extension is purely numeric.
        if let Some((base, ext)) = file_name.rsplit_once('.')
            && !ext.is_empty()
            && ext.chars().all(|c| c.is_ascii_digit())
            && let Ok(num) = ext.parse::<u32>()
        {
            groups
                .entry(base.to_string())
                .or_default()
                .push((num, path));
        }
    }

    let mut result = Vec::new();
    for (base_name, mut parts) in groups {
        if parts.len() < 2 {
            continue;
        }
        parts.sort_by_key(|(n, _)| *n);

        let part_numbers: Vec<u32> = parts.iter().map(|(n, _)| *n).collect();
        let paths: Vec<PathBuf> = parts.into_iter().map(|(_, p)| p).collect();

        // Check contiguity: starts at 1 and no gaps.
        let contiguous = part_numbers[0] == 1
            && part_numbers
                .windows(2)
                .all(|pair| matches!(pair, [a, b] if *b == *a + 1));

        result.push(SplitFileGroup {
            base_name,
            parts: paths,
            part_numbers,
            contiguous,
        });
    }

    // Sort by base name for deterministic output.
    result.sort_by(|a, b| a.base_name.cmp(&b.base_name));
    Ok(result)
}

/// Read up to `n` bytes from the start of a file.
fn read_first_n_bytes(path: &Path, n: usize) -> io::Result<Vec<u8>> {
    let mut file = File::open(path)?;
    let file_len = file.metadata()?.len();
    let mut buf = vec![0u8; n];
    // Fill, don't single-read: a short read here would silently hash fewer
    // bytes than the 16k quick hash is defined over. See `disk::read_filled`.
    let bytes_read = crate::disk::read_filled(&mut file, &mut buf)?;
    crate::file_cache::drop_touched_file_cache(&file, path, file_len, 0, bytes_read as u64);
    buf.truncate(bytes_read);
    Ok(buf)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::checksum::SliceChecksumState;
    use crate::packet::header;
    use crate::types::SliceChecksum;
    use md5::{Digest, Md5};
    use tempfile::TempDir;

    /// Helper to build a complete valid packet (header + body).
    fn make_full_packet(packet_type: &[u8; 16], body: &[u8], recovery_set_id: [u8; 16]) -> Vec<u8> {
        let length = (header::HEADER_SIZE + body.len()) as u64;
        let mut hash_input = Vec::new();
        hash_input.extend_from_slice(&recovery_set_id);
        hash_input.extend_from_slice(packet_type);
        hash_input.extend_from_slice(body);
        let packet_hash: [u8; 16] = Md5::digest(&hash_input).into();

        let mut data = Vec::new();
        data.extend_from_slice(header::MAGIC);
        data.extend_from_slice(&length.to_le_bytes());
        data.extend_from_slice(&packet_hash);
        data.extend_from_slice(&recovery_set_id);
        data.extend_from_slice(packet_type);
        data.extend_from_slice(body);
        data
    }

    /// Build a Par2FileSet for a single file with known content.
    fn setup_par2_set(file_data: &[u8], slice_size: u64, filename: &str) -> (Par2FileSet, FileId) {
        let file_length = file_data.len() as u64;
        let hash_full = checksum::md5(file_data);
        let hash_16k_data = &file_data[..file_data.len().min(16384)];
        let hash_16k = checksum::md5(hash_16k_data);

        let mut id_input = Vec::new();
        id_input.extend_from_slice(&hash_16k);
        id_input.extend_from_slice(&file_length.to_le_bytes());
        id_input.extend_from_slice(filename.as_bytes());
        let file_id_bytes: [u8; 16] = Md5::digest(&id_input).into();
        let file_id = FileId::from_bytes(file_id_bytes);

        let num_slices = if file_length == 0 {
            0
        } else {
            file_length.div_ceil(slice_size) as usize
        };

        let mut checksums = Vec::new();
        for i in 0..num_slices {
            let offset = i as u64 * slice_size;
            let end = ((offset + slice_size) as usize).min(file_data.len());
            let slice_data = &file_data[offset as usize..end];
            let mut state = SliceChecksumState::new();
            state.update(slice_data);
            let pad_to = if (slice_data.len() as u64) < slice_size {
                Some(slice_size)
            } else {
                None
            };
            let (crc, md5) = state.finalize(pad_to);
            checksums.push(SliceChecksum { crc32: crc, md5 });
        }

        let mut main_body = Vec::new();
        main_body.extend_from_slice(&slice_size.to_le_bytes());
        main_body.extend_from_slice(&1u32.to_le_bytes());
        main_body.extend_from_slice(&file_id_bytes);
        let rsid: [u8; 16] = Md5::digest(&main_body).into();

        let mut fd_body = Vec::new();
        fd_body.extend_from_slice(&file_id_bytes);
        fd_body.extend_from_slice(&hash_full);
        fd_body.extend_from_slice(&hash_16k);
        fd_body.extend_from_slice(&file_length.to_le_bytes());
        fd_body.extend_from_slice(filename.as_bytes());
        while fd_body.len() % 4 != 0 {
            fd_body.push(0);
        }

        let mut ifsc_body = Vec::new();
        ifsc_body.extend_from_slice(&file_id_bytes);
        for cs in &checksums {
            ifsc_body.extend_from_slice(&cs.md5);
            ifsc_body.extend_from_slice(&cs.crc32.to_le_bytes());
        }

        let mut stream = Vec::new();
        stream.extend_from_slice(&make_full_packet(header::TYPE_MAIN, &main_body, rsid));
        stream.extend_from_slice(&make_full_packet(header::TYPE_FILE_DESC, &fd_body, rsid));
        stream.extend_from_slice(&make_full_packet(header::TYPE_IFSC, &ifsc_body, rsid));

        let set = Par2FileSet::from_files(&[&stream]).unwrap();
        (set, file_id)
    }

    #[test]
    fn scan_finds_obfuscated_file() {
        let dir = TempDir::new().unwrap();
        let file_data = b"This is the real file content for rename testing!!";
        let correct_name = "movie.rar";

        let (par2_set, file_id) = setup_par2_set(file_data, 1024, correct_name);

        // Write file with obfuscated name
        fs::write(dir.path().join("abc123def456.bin"), file_data).unwrap();

        let suggestions = scan_for_renames(dir.path(), &par2_set).unwrap();
        assert_eq!(suggestions.len(), 1);
        assert_eq!(suggestions[0].correct_name, correct_name);
        assert_eq!(suggestions[0].file_id, file_id);
        assert_eq!(suggestions[0].match_type, MatchType::Hash16k);
    }

    #[test]
    fn scan_skips_correctly_named() {
        let dir = TempDir::new().unwrap();
        let file_data = b"This is the real file content for rename testing!!";
        let correct_name = "movie.rar";

        let (par2_set, _) = setup_par2_set(file_data, 1024, correct_name);

        // Write file with correct name
        fs::write(dir.path().join(correct_name), file_data).unwrap();

        let suggestions = scan_for_renames(dir.path(), &par2_set).unwrap();
        assert!(suggestions.is_empty());
    }

    #[test]
    fn scan_skips_generated_repair_artifacts() {
        let dir = TempDir::new().unwrap();
        let file_data = b"This is the real file content for rename testing!!";
        let correct_name = "movie.rar";

        let (par2_set, _) = setup_par2_set(file_data, 1024, correct_name);

        fs::write(
            dir.path().join("movie.rar.weaver-par2-backup.123"),
            file_data,
        )
        .unwrap();

        let suggestions = scan_for_renames(dir.path(), &par2_set).unwrap();
        assert!(suggestions.is_empty());
    }

    #[test]
    fn scan_empty_dir() {
        let dir = TempDir::new().unwrap();
        let file_data = b"data";
        let (par2_set, _) = setup_par2_set(file_data, 1024, "test.bin");

        let suggestions = scan_for_renames(dir.path(), &par2_set).unwrap();
        assert!(suggestions.is_empty());
    }

    #[test]
    fn identify_par2_files_finds_match() {
        let dir = TempDir::new().unwrap();

        // Build a minimal PAR2 packet
        let rsid = [0x42u8; 16];
        let main_body_data = vec![0u8; 12]; // dummy main body
        let packet_data = make_full_packet(header::TYPE_MAIN, &main_body_data, rsid);

        // Write with obfuscated name
        fs::write(dir.path().join("random_name.bin"), &packet_data).unwrap();

        let expected_id = RecoverySetId::from_bytes(rsid);
        let matches = identify_par2_files(dir.path(), &expected_id).unwrap();
        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn identify_par2_files_ignores_non_par2() {
        let dir = TempDir::new().unwrap();

        fs::write(dir.path().join("regular.txt"), b"not a par2 file").unwrap();

        let expected_id = RecoverySetId::from_bytes([0x42; 16]);
        let matches = identify_par2_files(dir.path(), &expected_id).unwrap();
        assert!(matches.is_empty());
    }

    #[test]
    fn detect_split_files_basic() {
        let dir = TempDir::new().unwrap();

        fs::write(dir.path().join("movie.mkv.001"), b"part1").unwrap();
        fs::write(dir.path().join("movie.mkv.002"), b"part2").unwrap();
        fs::write(dir.path().join("movie.mkv.003"), b"part3").unwrap();
        fs::write(dir.path().join("other.txt"), b"not split").unwrap();

        let groups = detect_split_files(dir.path()).unwrap();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].base_name, "movie.mkv");
        assert_eq!(groups[0].part_numbers, vec![1, 2, 3]);
        assert!(groups[0].contiguous);
    }

    #[test]
    fn detect_split_files_with_gap() {
        let dir = TempDir::new().unwrap();

        fs::write(dir.path().join("data.bin.001"), b"p1").unwrap();
        fs::write(dir.path().join("data.bin.003"), b"p3").unwrap();

        let groups = detect_split_files(dir.path()).unwrap();
        assert_eq!(groups.len(), 1);
        assert!(!groups[0].contiguous);
        assert_eq!(groups[0].part_numbers, vec![1, 3]);
    }

    #[test]
    fn detect_split_files_single_part_ignored() {
        let dir = TempDir::new().unwrap();

        fs::write(dir.path().join("lonely.bin.001"), b"alone").unwrap();

        let groups = detect_split_files(dir.path()).unwrap();
        assert!(groups.is_empty());
    }

    #[test]
    fn detect_split_files_empty_dir() {
        let dir = TempDir::new().unwrap();
        let groups = detect_split_files(dir.path()).unwrap();
        assert!(groups.is_empty());
    }
}