thunderstore-engine 0.1.0

Game-agnostic Thunderstore engine: API client, caching, dependency resolution, zip extraction, and schema-driven mod-loader install rules
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
//! Generic, CRC-verified zip extraction primitives.
//!
//! These are the reusable archive operations shared by every Thunderstore mod
//! manager: extracting a package zip with per-entry CRC32 verification and
//! path-traversal protection, deciding whether a downloaded version differs
//! from what is already on disk, and parsing the `Owner-Name-Version.zip`
//! filename convention. Where the extracted files ultimately land is install
//! policy and lives in the caller (see [`crate::install`]).

#![deny(missing_docs)]

use crate::error::{Error, Result};
use crate::manifest::Manifest;
use std::fs;
use std::io::{Read, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use tracing::{debug, error, warn};

/// Size of the reusable read buffer used to stream each archive entry to disk.
/// This bounds extraction memory to a constant regardless of entry size; it is an
/// IO buffer, not a cap on legitimate file size.
const EXTRACT_CHUNK_SIZE: usize = 64 * 1024;

/// Resource limits enforced while extracting an archive, to defend against
/// decompression bombs (an archive that is small on disk but expands to an
/// enormous number of entries or bytes).
///
/// These bound *disk* usage and entry count; extraction memory is already a
/// fixed `EXTRACT_CHUNK_SIZE` buffer regardless of these values. The defaults
/// are generous enough for real mod and texture packs while still rejecting
/// pathological archives; tune them with [`extract_zip_with_limits`] when a
/// caller needs different bounds.
#[derive(Debug, Clone, Copy)]
pub struct ExtractLimits {
  /// Maximum number of entries (files + directories) an archive may contain.
  pub max_entries: u64,
  /// Maximum uncompressed size of any single entry, in bytes.
  pub max_file_bytes: u64,
  /// Maximum total uncompressed size across all entries, in bytes.
  pub max_total_bytes: u64,
}

impl Default for ExtractLimits {
  fn default() -> Self {
    Self {
      max_entries: 100_000,
      max_file_bytes: 1 << 30,  // 1 GiB
      max_total_bytes: 4 << 30, // 4 GiB
    }
  }
}

/// Computes a unique sibling temp path next to `path` so a partially-extracted
/// entry can be written, CRC-verified, then atomically renamed into place — an
/// interrupted or corrupt entry never leaves a usable file behind.
fn temp_sibling_path(path: &Path) -> PathBuf {
  static COUNTER: AtomicU64 = AtomicU64::new(0);

  let seq = COUNTER.fetch_add(1, Ordering::Relaxed);
  let pid = std::process::id();
  let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("entry");
  let tmp_name = format!(".{name}.{pid}.{seq}.partial");

  match path.parent() {
    Some(parent) => parent.join(tmp_name),
    None => PathBuf::from(tmp_name),
  }
}

/// Streams a single archive entry to `tmp_path`, holding only a fixed-size buffer
/// in memory, and verifies its CRC32 against `expected_crc32`.
///
/// Enforces the byte limits in `limits` against the *actual* number of bytes
/// streamed (not the archive's declared sizes, which an attacker controls):
/// `max_file_bytes` for this entry and `max_total_bytes` for the cumulative
/// `total_written` across the whole archive. Exceeding either returns
/// [`Error::ExtractLimitExceeded`].
///
/// Returns [`Error::ChecksumMismatch`] when the computed CRC differs from the one
/// recorded in the archive. The caller is responsible for renaming `tmp_path` into
/// place on success and removing it on any error.
fn write_entry<R: Read>(
  entry: &mut R,
  buffer: &mut [u8],
  tmp_path: &Path,
  expected_crc32: u32,
  dest: &Path,
  limits: &ExtractLimits,
  total_written: &mut u64,
) -> Result<()> {
  let mut outfile = fs::File::create(tmp_path)?;
  let mut hasher = crc32fast::Hasher::new();
  let mut file_written: u64 = 0;

  loop {
    let read = entry.read(&mut buffer[..])?;

    if read == 0 {
      break;
    }

    file_written += read as u64;
    *total_written += read as u64;

    check_extract_size(dest, file_written, *total_written, limits)?;

    hasher.update(&buffer[..read]);
    outfile.write_all(&buffer[..read])?;
  }

  let actual_crc32 = hasher.finalize();

  if actual_crc32 != expected_crc32 {
    error!(
      "CRC32 verification failed for {}: expected {:08x}, got {:08x}",
      dest.display(),
      expected_crc32,
      actual_crc32
    );

    return Err(Error::ChecksumMismatch {
      path: dest.display().to_string(),
      expected: expected_crc32,
      actual: actual_crc32,
    });
  }

  outfile.flush()?;

  Ok(())
}

/// Enforces the streamed-byte limits for one entry: the per-entry
/// `max_file_bytes` and the cumulative `max_total_bytes` across the archive.
///
/// Checked against the *actual* bytes streamed (which an attacker cannot forge,
/// unlike an archive's declared sizes). Pure — no I/O — so the decompression-bomb
/// guard is unit-testable. Returns [`Error::ExtractLimitExceeded`] on breach; this
/// stays distinct from the network `SizeCap`/[`Error::SizeLimitExceeded`] path
/// because archive limits also bound a non-byte quantity (entry count).
fn check_extract_size(
  dest: &Path,
  file_written: u64,
  total_written: u64,
  limits: &ExtractLimits,
) -> Result<()> {
  if file_written > limits.max_file_bytes {
    return Err(Error::ExtractLimitExceeded(format!(
      "entry {} exceeds max file size of {} bytes",
      dest.display(),
      limits.max_file_bytes
    )));
  }

  if total_written > limits.max_total_bytes {
    return Err(Error::ExtractLimitExceeded(format!(
      "archive exceeds max total uncompressed size of {} bytes",
      limits.max_total_bytes
    )));
  }

  Ok(())
}

/// Joins a validated archive entry path onto `dest_dir`, or returns `None` if the
/// entry would escape the destination.
///
/// `entry` is the result of [`zip::read::ZipFile::enclosed_name`] (already `None`
/// for a rejected path). As defense in depth on top of that, this rejects any
/// absolute, root, drive-prefix, or `..` component itself, so the traversal guard
/// is a single pure function testable against crafted paths without a real
/// archive.
fn safe_join(dest_dir: &Path, entry: Option<&Path>) -> Option<PathBuf> {
  let entry = entry?;

  for component in entry.components() {
    match component {
      Component::Normal(_) | Component::CurDir => {}
      Component::ParentDir | Component::RootDir | Component::Prefix(_) => return None,
    }
  }

  Some(dest_dir.join(entry))
}

/// The permissions to force on an extracted entry, or `None` to keep whatever
/// [`fs::File::create`] and the process umask already produced.
///
/// Only the archive's *execute* bits are consulted, and only to widen the file to
/// `0o755`. A loader package ships `run_bepinex.sh` / `start_game_bepinex.sh`
/// marked executable and the generated Steam wrapper `exec`s whichever one it
/// finds, so an entry that loses that bit turns a modded launch into a bare
/// `Permission denied`, which under Steam surfaces only as the game silently
/// never starting.
///
/// The recorded mode is deliberately **not** reproduced wholesale. A downloaded
/// package is untrusted input: applying it verbatim would honour `setuid`,
/// `setgid`, and sticky bits, and would let an entry declare itself
/// world-writable. Widening to a fixed `0o755` can do neither. An entry with no
/// recorded mode at all, which is every archive built on Windows, is left alone
/// rather than guessed at.
#[cfg(unix)]
fn entry_permissions(unix_mode: Option<u32>) -> Option<fs::Permissions> {
  use std::os::unix::fs::PermissionsExt;

  let mode = unix_mode?;

  if mode & 0o111 == 0 {
    return None;
  }

  Some(fs::Permissions::from_mode(0o755))
}

/// Applies an archive entry's recorded execute bit to the file just extracted to
/// `path`. See [`entry_permissions`] for why only that bit is honoured.
///
/// A no-op off Unix, where a zip entry's Unix mode has nothing to map onto.
fn apply_entry_mode(path: &Path, unix_mode: Option<u32>) -> Result<()> {
  #[cfg(unix)]
  if let Some(permissions) = entry_permissions(unix_mode) {
    fs::set_permissions(path, permissions)?;
  }

  #[cfg(not(unix))]
  let _ = (path, unix_mode);

  Ok(())
}

/// Extracts a zip archive to `dest_dir` with per-entry CRC32 verification and
/// the default [`ExtractLimits`].
///
/// Entry paths are validated with [`zip::read::ZipFile::enclosed_name`] so that
/// archive entries containing `..` or absolute paths cannot escape `dest_dir`.
///
/// # Errors
///
/// Returns an error if the file is not a valid zip archive, if IO fails, if
/// CRC32 verification fails for any extracted entry ([`Error::ChecksumMismatch`]),
/// or if the archive exceeds a default extraction limit
/// ([`Error::ExtractLimitExceeded`]).
pub fn extract_zip(zip_path: &Path, dest_dir: &Path) -> Result<()> {
  extract_zip_with_limits(zip_path, dest_dir, &ExtractLimits::default())
}

/// Extracts a zip archive to `dest_dir` with per-entry CRC32 verification,
/// enforcing the supplied [`ExtractLimits`] to defend against decompression
/// bombs.
///
/// Entry paths are validated with [`zip::read::ZipFile::enclosed_name`] so that
/// archive entries containing `..` or absolute paths cannot escape `dest_dir`.
/// The entry count and the actual streamed byte totals are checked against
/// `limits`; CRC32 is an integrity check only and does not authenticate content.
///
/// # Errors
///
/// Returns an error if the file is not a valid zip archive, if IO fails, if
/// CRC32 verification fails for any extracted entry ([`Error::ChecksumMismatch`]),
/// or if the archive exceeds a limit in `limits`
/// ([`Error::ExtractLimitExceeded`]).
pub fn extract_zip_with_limits(
  zip_path: &Path,
  dest_dir: &Path,
  limits: &ExtractLimits,
) -> Result<()> {
  let file = fs::File::open(zip_path)?;
  let mut archive = zip::ZipArchive::new(file)?;

  if archive.len() as u64 > limits.max_entries {
    return Err(Error::ExtractLimitExceeded(format!(
      "archive has {} entries, exceeding the limit of {}",
      archive.len(),
      limits.max_entries
    )));
  }

  fs::create_dir_all(dest_dir)?;

  let mut total_written: u64 = 0;
  let mut buffer = vec![0u8; EXTRACT_CHUNK_SIZE];

  for i in 0..archive.len() {
    let mut file_in_archive = archive.by_index(i)?;
    let enclosed = file_in_archive.enclosed_name();
    let outpath = match safe_join(dest_dir, enclosed.as_deref()) {
      Some(path) => path,
      None => {
        warn!("Invalid file path in zip: {:?}", file_in_archive.name());
        continue;
      }
    };

    let comment = file_in_archive.comment();

    if !comment.is_empty() {
      debug!("File {} comment: {:?}", i, comment);
    }

    if file_in_archive.name().ends_with('/') {
      debug!("Creating directory {}", outpath.display());
      fs::create_dir_all(&outpath)?;
    } else {
      debug!("Extracting file {} to {}", i, outpath.display());

      if let Some(parent) = outpath.parent()
        && !parent.exists()
      {
        fs::create_dir_all(parent)?;
      }

      let expected_crc32 = file_in_archive.crc32();
      // Read before streaming: `write_entry` borrows the entry mutably, and the
      // mode is needed after the file is renamed into place.
      let unix_mode = file_in_archive.unix_mode();
      let tmp_path = temp_sibling_path(&outpath);

      // Stream the entry to a temp file, verifying the CRC as we go. Any failure
      // (IO, a short/corrupt entry the `zip` reader rejects, or a CRC mismatch)
      // must remove the temp file so a corrupt entry never lands at `outpath`.
      match write_entry(
        &mut file_in_archive,
        &mut buffer,
        &tmp_path,
        expected_crc32,
        &outpath,
        limits,
        &mut total_written,
      ) {
        Ok(()) => {
          // Applied to the temp file rather than to `outpath`, so the entry
          // becomes visible at its final path already carrying its final mode
          // instead of appearing briefly as non-executable. Any failure here is
          // treated exactly like a failed rename: remove the temp file so a
          // half-finished entry never lands.
          if let Err(err) = apply_entry_mode(&tmp_path, unix_mode) {
            let _ = fs::remove_file(&tmp_path);

            return Err(err);
          }

          // A failed rename must also remove the temp file, so a CRC-verified
          // entry that cannot be moved into place leaves no orphan behind.
          if let Err(err) = fs::rename(&tmp_path, &outpath) {
            let _ = fs::remove_file(&tmp_path);

            return Err(err.into());
          }

          debug!("CRC32 verification successful for {}", outpath.display());
        }
        Err(err) => {
          let _ = fs::remove_file(&tmp_path);

          return Err(err);
        }
      }
    }
  }

  Ok(())
}

/// Calculates the CRC32 checksum for a byte buffer.
#[cfg(test)]
fn calculate_crc32(data: &[u8]) -> u32 {
  let mut hasher = crc32fast::Hasher::new();
  hasher.update(data);

  hasher.finalize()
}

/// Determines whether a mod should be (re)extracted based on version comparison.
///
/// Returns `Ok(true)` if `mod_dir` has no `manifest.json`, if its manifest has a
/// different `version_number` than `version`, or if its manifest cannot be
/// parsed. Returns `Ok(false)` only when an existing manifest already records
/// the same version.
pub fn should_extract(mod_dir: &Path, version: &str) -> Result<bool> {
  let manifest_path = mod_dir.join("manifest.json");

  let existing_version = if manifest_path.exists() {
    match Manifest::from_file(&manifest_path) {
      Ok(manifest) => Some(manifest.version_number),
      Err(err) => {
        warn!(
          "Failed to parse manifest in {}: {}; re-extracting",
          mod_dir.display(),
          err
        );

        None
      }
    }
  } else {
    debug!(
      "No manifest in {}, extracting version {}",
      mod_dir.display(),
      version
    );

    None
  };

  let reextract = should_reextract(existing_version.as_deref(), version);

  if let Some(existing) = &existing_version {
    if reextract {
      debug!(
        "{} version changed from {} to {}, extracting",
        mod_dir.display(),
        existing,
        version
      );
    } else {
      debug!(
        "{} already has version {}, skipping",
        mod_dir.display(),
        version
      );
    }
  }

  Ok(reextract)
}

/// The pure re-extract decision, given the version an existing on-disk manifest
/// records (`None` when there is no manifest, or it could not be parsed) and the
/// `requested` version.
///
/// Re-extract unless an existing manifest already records exactly the requested
/// version. Extracted from [`should_extract`] so the decision is testable without
/// writing manifest files to disk.
fn should_reextract(existing_version: Option<&str>, requested: &str) -> bool {
  match existing_version {
    Some(existing) => existing != requested,
    None => true,
  }
}

/// Parses a Thunderstore package zip filename of the form
/// `Owner-Name-Version.zip`.
///
/// Returns the package full name (`Owner-Name`) and the version string, or
/// `None` if the filename does not contain at least three `-`-separated parts.
///
/// Only the first two `-` delimiters are significant: the owner and name never
/// contain `-`, but everything after the second delimiter is taken verbatim as
/// the version, so a version that itself contains `-` (e.g. a `1.0.0-beta`
/// prerelease) is preserved rather than truncated.
pub fn parse_package_filename(file_name: &str) -> Option<(String, String)> {
  let stem = file_name.strip_suffix(".zip").unwrap_or(file_name);
  let parts: Vec<&str> = stem.splitn(3, '-').collect();

  if parts.len() < 3 {
    return None;
  }

  let full_name = format!("{}-{}", parts[0], parts[1]);
  let version = parts[2].to_string();

  Some((full_name, version))
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::fs::File;
  use std::io::Write;
  use tempfile::tempdir;
  use zip::write::{FileOptions, ZipWriter};

  #[test]
  fn test_calculate_crc32() {
    let data = b"test data for crc32 calculation";
    let crc = calculate_crc32(data);

    let expected_crc: u32 = 1707861357;

    assert_eq!(crc, expected_crc);
  }

  #[test]
  fn safe_join_rejects_traversal_and_absolute() {
    let base = Path::new("/dest");

    assert_eq!(safe_join(base, None), None);
    assert_eq!(safe_join(base, Some(Path::new("../escape"))), None);
    assert_eq!(safe_join(base, Some(Path::new("a/../../b"))), None);
    assert_eq!(safe_join(base, Some(Path::new("/etc/passwd"))), None);

    assert_eq!(
      safe_join(base, Some(Path::new("plugins/mod.dll"))),
      Some(PathBuf::from("/dest/plugins/mod.dll"))
    );
    assert!(safe_join(base, Some(Path::new("./ok.txt"))).is_some());
  }

  #[test]
  fn should_reextract_truth_table() {
    // No existing manifest (or an unparseable one) => re-extract.
    assert!(should_reextract(None, "1.0.0"));
    // Same version already present => skip.
    assert!(!should_reextract(Some("1.0.0"), "1.0.0"));
    // Different version present => re-extract.
    assert!(should_reextract(Some("1.0.0"), "1.0.1"));
  }

  #[test]
  fn check_extract_size_enforces_both_caps() {
    let limits = ExtractLimits {
      max_entries: 1,
      max_file_bytes: 10,
      max_total_bytes: 20,
    };

    // At the caps is allowed.
    assert!(check_extract_size(Path::new("f"), 10, 20, &limits).is_ok());

    // Over the per-file cap.
    assert!(matches!(
      check_extract_size(Path::new("f"), 11, 11, &limits).unwrap_err(),
      Error::ExtractLimitExceeded(_)
    ));

    // Over the whole-archive cap.
    assert!(matches!(
      check_extract_size(Path::new("f"), 5, 21, &limits).unwrap_err(),
      Error::ExtractLimitExceeded(_)
    ));
  }

  #[test]
  fn temp_sibling_path_is_unique_and_sibling() {
    let base = Path::new("/tmp/x/file.dll");

    let a = temp_sibling_path(base);
    let b = temp_sibling_path(base);

    assert_ne!(a, b);
    assert_eq!(a.parent(), Some(Path::new("/tmp/x")));
  }

  #[test]
  fn write_entry_streams_verifies_crc_and_enforces_cap() {
    let dir = tempdir().unwrap();
    let data = b"hello world, this is a streamed entry";
    let crc = calculate_crc32(data);
    let limits = ExtractLimits::default();

    // Success: a small buffer forces multiple read iterations; the file lands and
    // the running total reflects every byte.
    let tmp = dir.path().join("ok.partial");
    let mut reader = std::io::Cursor::new(data.to_vec());
    let mut buffer = vec![0u8; 4];
    let mut total = 0u64;

    write_entry(
      &mut reader,
      &mut buffer,
      &tmp,
      crc,
      Path::new("ok"),
      &limits,
      &mut total,
    )
    .unwrap();

    assert_eq!(std::fs::read(&tmp).unwrap(), data);
    assert_eq!(total, data.len() as u64);

    // CRC mismatch is reported.
    let tmp2 = dir.path().join("bad.partial");
    let mut reader = std::io::Cursor::new(data.to_vec());
    let mut buffer = vec![0u8; 64];
    let mut total = 0u64;

    let err = write_entry(
      &mut reader,
      &mut buffer,
      &tmp2,
      0xDEAD_BEEF,
      Path::new("bad"),
      &limits,
      &mut total,
    )
    .unwrap_err();

    assert!(matches!(err, Error::ChecksumMismatch { .. }));

    // The per-file byte cap is enforced against the actual streamed bytes.
    let tmp3 = dir.path().join("big.partial");
    let tight = ExtractLimits {
      max_file_bytes: 4,
      ..ExtractLimits::default()
    };
    let mut reader = std::io::Cursor::new(vec![0u8; 10]);
    let mut buffer = vec![0u8; 64];
    let mut total = 0u64;

    let err = write_entry(
      &mut reader,
      &mut buffer,
      &tmp3,
      0,
      Path::new("big"),
      &tight,
      &mut total,
    )
    .unwrap_err();

    assert!(matches!(err, Error::ExtractLimitExceeded(_)));
  }

  #[test]
  fn test_parse_package_filename() {
    assert_eq!(
      parse_package_filename("Owner-ModName-1.0.0.zip"),
      Some(("Owner-ModName".to_string(), "1.0.0".to_string()))
    );

    assert_eq!(parse_package_filename("InvalidFile.zip"), None);
  }

  #[test]
  fn test_parse_package_filename_preserves_hyphenated_version() {
    assert_eq!(
      parse_package_filename("Owner-ModName-1.0.0-beta.1.zip"),
      Some(("Owner-ModName".to_string(), "1.0.0-beta.1".to_string()))
    );
  }

  #[test]
  fn test_extract_zip_with_valid_paths() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");
    fs::create_dir_all(&output_dir).unwrap();

    let zip_path = temp_dir.path().join("test_invalid_paths.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    zip.start_file("valid_file.txt", options).unwrap();
    zip.write_all(b"Valid file content").unwrap();

    zip.finish().unwrap();

    let result = extract_zip(&zip_path, &output_dir);

    assert!(result.is_ok());

    let valid_file = output_dir.join("valid_file.txt");
    assert!(valid_file.exists());
  }

  /// A loader package ships `start_game_bepinex.sh` / `run_bepinex.sh` marked
  /// executable in the archive, and the generated Steam wrapper `exec`s whichever
  /// one it finds. Dropping the archive's execute bit makes that `exec` fail with
  /// `Permission denied`, which under Steam surfaces only as the game silently
  /// never starting, so the bit has to survive extraction.
  #[test]
  #[cfg(unix)]
  fn test_extract_zip_preserves_the_executable_bit() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_modes.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let executable: FileOptions<'_, ()> = FileOptions::default()
      .compression_method(zip::CompressionMethod::Stored)
      .unix_permissions(0o755);
    let plain: FileOptions<'_, ()> = FileOptions::default()
      .compression_method(zip::CompressionMethod::Stored)
      .unix_permissions(0o644);

    zip.start_file("start_game_bepinex.sh", executable).unwrap();
    zip.write_all(b"#!/bin/sh\n").unwrap();

    zip.start_file("mods.yml", plain).unwrap();
    zip.write_all(b"[]").unwrap();

    zip.finish().unwrap();

    extract_zip(&zip_path, &output_dir).unwrap();

    let script_mode = fs::metadata(output_dir.join("start_game_bepinex.sh"))
      .unwrap()
      .permissions()
      .mode();

    assert!(
      script_mode & 0o111 != 0,
      "executable entry extracted without an execute bit: {script_mode:o}"
    );

    // A non-executable entry must not be widened just because a sibling was
    // executable: only the archive's own execute bits are honoured.
    let plain_mode = fs::metadata(output_dir.join("mods.yml"))
      .unwrap()
      .permissions()
      .mode();

    assert!(
      plain_mode & 0o111 == 0,
      "plain entry gained an execute bit: {plain_mode:o}"
    );
  }

  /// An archive built on Windows records no Unix mode at all. Those entries must
  /// still extract, keeping the default permissions rather than erroring or being
  /// made executable.
  #[test]
  #[cfg(unix)]
  fn test_extract_zip_handles_entries_with_no_unix_mode() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_no_mode.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    zip.start_file("plugin.dll", options).unwrap();
    zip.write_all(b"bytes").unwrap();

    zip.finish().unwrap();

    extract_zip(&zip_path, &output_dir).unwrap();

    let mode = fs::metadata(output_dir.join("plugin.dll"))
      .unwrap()
      .permissions()
      .mode();

    assert!(
      mode & 0o111 == 0,
      "entry with no mode made executable: {mode:o}"
    );
  }

  /// `setuid`/`setgid`/sticky bits in an archive are never reproduced on disk: a
  /// downloaded package must not be able to plant a setuid file.
  #[test]
  #[cfg(unix)]
  fn test_extract_zip_drops_setuid_bits() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_setuid.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> = FileOptions::default()
      .compression_method(zip::CompressionMethod::Stored)
      .unix_permissions(0o4755);

    zip.start_file("evil.sh", options).unwrap();
    zip.write_all(b"#!/bin/sh\n").unwrap();

    zip.finish().unwrap();

    extract_zip(&zip_path, &output_dir).unwrap();

    let mode = fs::metadata(output_dir.join("evil.sh"))
      .unwrap()
      .permissions()
      .mode();

    assert!(
      mode & 0o7000 == 0,
      "setuid/setgid/sticky survived: {mode:o}"
    );
    assert!(mode & 0o111 != 0, "execute bit lost: {mode:o}");
  }

  #[test]
  fn test_extract_zip_with_directory() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");
    fs::create_dir_all(&output_dir).unwrap();

    let zip_path = temp_dir.path().join("test_with_dir.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    zip.add_directory("test_dir/", options).unwrap();

    zip.start_file("test_dir/file_in_dir.txt", options).unwrap();
    zip.write_all(b"File in directory").unwrap();

    zip.finish().unwrap();

    let result = extract_zip(&zip_path, &output_dir);

    assert!(result.is_ok());

    let dir_path = output_dir.join("test_dir");
    assert!(dir_path.exists());
    assert!(dir_path.is_dir());

    let file_in_dir = dir_path.join("file_in_dir.txt");
    assert!(file_in_dir.exists());

    let content = fs::read_to_string(&file_in_dir).unwrap();
    assert_eq!(content, "File in directory");
  }

  #[test]
  fn test_extract_zip_with_nested_directories() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_nested.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    zip
      .start_file("deep/nested/path/file.txt", options)
      .unwrap();
    zip.write_all(b"Deep nested file").unwrap();

    zip.finish().unwrap();

    let result = extract_zip(&zip_path, &output_dir);

    assert!(result.is_ok());

    let nested_file = output_dir.join("deep/nested/path/file.txt");
    assert!(nested_file.exists());

    let content = fs::read_to_string(&nested_file).unwrap();
    assert_eq!(content, "Deep nested file");
  }

  #[test]
  fn test_extract_zip_streams_large_file() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_large.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    // Larger than EXTRACT_CHUNK_SIZE so the streaming loop runs many iterations.
    let payload: Vec<u8> = (0..(64 * 1024 * 3 + 17)).map(|i| (i % 251) as u8).collect();

    zip.start_file("big.bin", options).unwrap();
    zip.write_all(&payload).unwrap();

    zip.finish().unwrap();

    let result = extract_zip(&zip_path, &output_dir);

    assert!(result.is_ok());

    let extracted = output_dir.join("big.bin");
    let content = fs::read(&extracted).unwrap();

    assert_eq!(content, payload);

    // No leftover temp/partial files from the atomic write.
    let leftover = fs::read_dir(&output_dir)
      .unwrap()
      .filter_map(|e| e.ok())
      .any(|e| e.file_name().to_string_lossy().contains(".partial"));

    assert!(!leftover, "no .partial temp files should remain");
  }

  #[test]
  fn test_extract_zip_reuses_buffer_across_entries() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    // A large entry followed by a smaller one: the read buffer is reused across
    // entries, so the smaller entry must not inherit stale bytes from the larger.
    let big: Vec<u8> = (0..(64 * 1024 * 2 + 5)).map(|i| (i % 251) as u8).collect();
    let small = b"tiny".to_vec();
    let zip_path = temp_dir.path().join("multi.zip");
    write_stored_zip(&zip_path, &[("big.bin", &big), ("small.txt", &small)]);

    let result = extract_zip(&zip_path, &output_dir);

    assert!(result.is_ok());

    let big_out = fs::read(output_dir.join("big.bin")).unwrap();
    let small_out = fs::read(output_dir.join("small.txt")).unwrap();

    assert_eq!(big_out, big);
    assert_eq!(small_out, small);
    assert!(no_partial_files(&output_dir));
  }

  #[test]
  fn test_extract_zip_leaves_no_file_on_crc_mismatch() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("test_corrupt.zip");
    let file = File::create(&zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    // Stored compression keeps the payload as raw bytes in the local entry, so
    // flipping one payload byte makes the actual CRC diverge from the stored one.
    let marker = b"UNIQUEMARKERPAYLOAD";
    let mut payload = marker.to_vec();
    payload.extend_from_slice(&[7u8; 64]);

    zip.start_file("data.bin", options).unwrap();
    zip.write_all(&payload).unwrap();

    zip.finish().unwrap();

    let mut bytes = fs::read(&zip_path).unwrap();
    let pos = bytes
      .windows(marker.len())
      .position(|w| w == marker)
      .expect("payload marker present in archive");

    // Corrupt a byte inside the payload (after the marker).
    bytes[pos + marker.len()] ^= 0xFF;
    fs::write(&zip_path, &bytes).unwrap();

    let result = extract_zip(&zip_path, &output_dir);

    // A corrupted entry must fail extraction. Depending on compression, the
    // mismatch is caught either by our CRC check (`ChecksumMismatch`) or by the
    // `zip` reader's own validation (surfaced as an IO/ZIP error) — either way
    // extraction errors and nothing is left behind.
    assert!(result.is_err());

    // The corrupt entry must not leave a usable file (nor a .partial temp).
    assert!(!output_dir.join("data.bin").exists());

    let leftover = fs::read_dir(&output_dir)
      .unwrap()
      .filter_map(|e| e.ok())
      .any(|e| e.file_name().to_string_lossy().contains(".partial"));

    assert!(!leftover, "no .partial temp files should remain");
  }

  fn write_stored_zip(zip_path: &Path, entries: &[(&str, &[u8])]) {
    let file = File::create(zip_path).unwrap();
    let mut zip = ZipWriter::new(file);

    let options: FileOptions<'_, ()> =
      FileOptions::default().compression_method(zip::CompressionMethod::Stored);

    for (name, contents) in entries {
      zip.start_file(*name, options).unwrap();
      zip.write_all(contents).unwrap();
    }

    zip.finish().unwrap();
  }

  fn no_partial_files(dir: &Path) -> bool {
    !fs::read_dir(dir)
      .unwrap()
      .filter_map(|e| e.ok())
      .any(|e| e.file_name().to_string_lossy().contains(".partial"))
  }

  #[test]
  fn test_extract_zip_rejects_too_many_entries() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("many_entries.zip");
    write_stored_zip(
      &zip_path,
      &[("a.txt", b"a"), ("b.txt", b"b"), ("c.txt", b"c")],
    );

    let limits = ExtractLimits {
      max_entries: 2,
      ..Default::default()
    };

    let result = extract_zip_with_limits(&zip_path, &output_dir, &limits);

    assert!(matches!(result, Err(Error::ExtractLimitExceeded(_))));
  }

  #[test]
  fn test_extract_zip_rejects_oversized_entry() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("big_entry.zip");
    let payload = vec![0u8; 100];
    write_stored_zip(&zip_path, &[("big.bin", &payload)]);

    let limits = ExtractLimits {
      max_file_bytes: 8,
      ..Default::default()
    };

    let result = extract_zip_with_limits(&zip_path, &output_dir, &limits);

    assert!(matches!(result, Err(Error::ExtractLimitExceeded(_))));
    assert!(!output_dir.join("big.bin").exists());
    assert!(no_partial_files(&output_dir));
  }

  #[test]
  fn test_extract_zip_rejects_exceeding_total() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("total.zip");
    let chunk = vec![7u8; 80];
    write_stored_zip(&zip_path, &[("one.bin", &chunk), ("two.bin", &chunk)]);

    // Each entry fits under max_file_bytes, but together they exceed the total.
    let limits = ExtractLimits {
      max_file_bytes: 1_000,
      max_total_bytes: 100,
      ..Default::default()
    };

    let result = extract_zip_with_limits(&zip_path, &output_dir, &limits);

    assert!(matches!(result, Err(Error::ExtractLimitExceeded(_))));
    assert!(no_partial_files(&output_dir));
  }

  #[test]
  fn test_extract_zip_within_default_limits_succeeds() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");

    let zip_path = temp_dir.path().join("normal.zip");
    write_stored_zip(&zip_path, &[("plugin.dll", b"content")]);

    let result = extract_zip_with_limits(&zip_path, &output_dir, &ExtractLimits::default());

    assert!(result.is_ok());
    assert!(output_dir.join("plugin.dll").exists());
  }

  #[test]
  fn test_extract_zip_rejects_path_traversal() {
    let temp_dir = tempdir().unwrap();
    let output_dir = temp_dir.path().join("output");
    fs::create_dir_all(&output_dir).unwrap();

    // A crafted archive whose entry tries to climb out of the extraction root,
    // alongside a benign entry.
    let zip_path = temp_dir.path().join("traversal.zip");
    write_stored_zip(
      &zip_path,
      &[("../escaped.txt", b"malicious"), ("safe.txt", b"benign")],
    );

    let result = extract_zip(&zip_path, &output_dir);

    // Extraction succeeds by skipping the unsafe entry, the benign file lands
    // inside the root, and nothing is written to the parent (outside the root).
    assert!(result.is_ok());
    assert!(output_dir.join("safe.txt").exists());
    assert!(!temp_dir.path().join("escaped.txt").exists());
    assert!(no_partial_files(&output_dir));
  }

  #[test]
  fn test_should_extract() {
    let temp_dir = tempdir().unwrap();
    let mod_dir = temp_dir.path().join("Owner-ModName");
    fs::create_dir_all(&mod_dir).unwrap();

    let result = should_extract(&mod_dir, "1.0.0");
    assert!(result.unwrap());

    let manifest_content = r#"{
      "version_number": "1.0.0",
      "name": "TestMod",
      "description": "Test mod description"
    }"#;

    let manifest_path = mod_dir.join("manifest.json");
    let mut file = File::create(&manifest_path).unwrap();
    file.write_all(manifest_content.as_bytes()).unwrap();

    let result = should_extract(&mod_dir, "1.0.0");
    assert!(!result.unwrap());

    let result = should_extract(&mod_dir, "2.0.0");
    assert!(result.unwrap());

    let mut file = File::create(&manifest_path).unwrap();
    file.write_all(b"invalid json content").unwrap();

    let result = should_extract(&mod_dir, "1.0.0");
    assert!(result.unwrap());
  }
}