exarch-core 0.11.0

Memory-safe archive extraction library with security validation
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
//! Which landings the current run created itself.
//!
//! Under [`ExistingFiles::Refuse`](crate::ExistingFiles::Refuse) an occupied
//! landing is refused, except when the run made the occupant and it is a file
//! or a symlink: a second archive entry for the same path is a duplicate and is
//! skipped, as under [`ExistingFiles::Skip`](crate::ExistingFiles::Skip). A
//! directory the run made is never a duplicate, but the record keeps it too, so
//! a refusal can say that the archive itself made the occupant. The record
//! tells an entry the run made from one it did not. It is kept per run, by the
//! pinned destination, and every entry the run creates is recorded whatever
//! `ExistingFiles` says, up to the run's [`RecordBudget`].
//!
//! A staged run's record outlives the extraction, so a rollback can tell what
//! the run made from what it did not make.
//!
//! A landing is keyed by file identity (device and file index of the entry
//! itself, never of a symlink's target), which is exact on a case-insensitive
//! host and needs no path folding. Where the volume gives no identity the key
//! is the landing path as the archive spelled it, so a duplicate that differs
//! only in case is refused: the record fails closed. A failed identity lookup
//! records nothing. [`ArchiveManifest::dispositions_on`] can model the path
//! basis when its caller passes [`VolumeIdentity::LandingPaths`]; the CLI and
//! both bindings probe the destination volume for it, and a `list` without a
//! destination assumes file identities.
//!
//! [`ArchiveManifest::dispositions_on`]: crate::ArchiveManifest::dispositions_on
//! [`VolumeIdentity::LandingPaths`]: crate::VolumeIdentity::LandingPaths
//!
//! [`RunRecord::maker`] is the tri-state form an opt-in refusal of repeated
//! paths reads: a saturated or lossy record, a failed identity lookup and a
//! path spelling the record does not hold are all [`Maker::Unknown`], never
//! "not made here".
//!
//! The record is bounded by [`RECORD_CAP`]. Past it nothing more is recorded
//! and every occupant counts as not created by this run, so the run fails
//! closed.
//!
//! # Races
//!
//! The record can be wrong when another process replaces a file this run made
//! (an identity that matches a file it did not make, or no longer matches one
//! it did). A wrong answer only turns a refusal into a skip or a skip into a
//! refusal. Neither writes: every occupant site routes `Refuse` to "refuse" or
//! "skip" and never to an overwrite, and the creates themselves stay `O_EXCL`.
//!
//! Identities are reused: once a created file is deleted, the filesystem may
//! hand its (device, inode) to the next file. A foreign file that takes over
//! the freed identity of a created one is skipped as a duplicate, never
//! overwritten.

use std::cell::RefCell;
use std::collections::HashSet;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;

use super::file_identity::FileIdentity;
use super::file_identity::IdentityKey;
use super::pinned_dir::Leaf;
use super::pinned_dir::VettedDir;
use crate::ExistingFiles;

/// The most landings a run records (4 Mi keys).
const RECORD_CAP: usize = 4 * 1024 * 1024;

/// The most landings a [`RecordBudget::Diagnostic`] run records.
///
/// Under [`ExistingFiles::Skip`] the record is consulted only to attribute a
/// refusal, never to decide a duplicate, so a large archive does not pay for
/// it: attribution is exact for the first 16 Ki landings of a run and
/// `existing` (maker unknown) afterwards.
const DIAGNOSTIC_RECORD_CAP: usize = 16 * 1024;

/// How much a [`RunRecord`] keeps before it saturates.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RecordBudget {
    /// Up to [`RECORD_CAP`] landings.
    Full,
    /// Up to [`DIAGNOSTIC_RECORD_CAP`] landings.
    Diagnostic,
}

impl RecordBudget {
    const fn cap(self) -> usize {
        match self {
            Self::Full => RECORD_CAP,
            Self::Diagnostic => DIAGNOSTIC_RECORD_CAP,
        }
    }
}

impl ExistingFiles {
    /// The budget of the record of an in-place run under this policy.
    pub(crate) const fn record_budget(self) -> RecordBudget {
        match self {
            Self::Skip => RecordBudget::Diagnostic,
            Self::Refuse | Self::Overwrite => RecordBudget::Full,
        }
    }
}

/// How a landing is recorded: by identity, or by path where the volume gives
/// none (FAT, exFAT, and Windows volumes without `FileIdInfo`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum LandingKey {
    Identity(IdentityKey),
    Path(PathBuf),
}

fn key_for(identity: FileIdentity, rel: &Path) -> LandingKey {
    #[cfg(test)]
    if key_basis::current() == key_basis::KeyBasis::Path {
        return LandingKey::Path(rel.to_path_buf());
    }
    identity
        .key()
        .map_or_else(|| LandingKey::Path(rel.to_path_buf()), LandingKey::Identity)
}

/// A test-only switch that makes this thread key every landing by its spelled
/// path, as a volume without file identities does.
#[cfg(test)]
pub(crate) mod key_basis {
    use std::cell::Cell;

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(crate) enum KeyBasis {
        Identity,
        Path,
    }

    thread_local! {
        static BASIS: Cell<KeyBasis> = const { Cell::new(KeyBasis::Identity) };
    }

    pub(super) fn current() -> KeyBasis {
        BASIS.with(Cell::get)
    }

    /// Runs `body` with this thread keying landings by `basis`.
    pub(crate) fn with<T>(basis: KeyBasis, body: impl FnOnce() -> T) -> T {
        let _restore =
            std::mem::DropGuard::new(BASIS.with(|cell| cell.replace(basis)), |previous| {
                BASIS.with(|cell| cell.set(previous));
            });
        body()
    }
}

/// The key of the occupant of `leaf`, `None` when it is absent or the lookup
/// fails, so the landing is not recorded and a later duplicate is refused.
fn key_of(dir: &VettedDir, leaf: Leaf<'_>, rel: &Path) -> Option<LandingKey> {
    let identity = dir.identity(leaf).ok().flatten()?;
    Some(key_for(identity, rel))
}

/// How much of what the run created the record still holds.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
enum Completeness {
    #[default]
    Complete,
    /// A landing the run created could not be keyed, so the record may be
    /// missing an entry it made.
    Lossy,
    /// The budget was reached; absorbing, nothing more is recorded.
    Saturated,
}

impl Completeness {
    const fn lossy(self) -> Self {
        match self {
            Self::Complete => Self::Lossy,
            other => other,
        }
    }
}

#[derive(Debug, Default)]
struct Created {
    keys: HashSet<LandingKey>,
    completeness: Completeness,
}

/// Who made the occupant of a landing, as far as the record can prove.
///
/// Refusing a repeat is fail-closed: only [`Maker::Run`] is a repeat and only
/// [`Maker::Foreign`] is left to the existing-files policy; everything the
/// record cannot decide is [`Maker::Unknown`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Maker {
    /// The record holds the occupant's key.
    Run,
    /// The key is readable, the record is complete and does not hold it, or
    /// nothing occupies the landing.
    Foreign,
    /// The record is saturated or lossy, the key cannot be read, or it is a
    /// path spelling the record does not hold.
    Unknown,
}

/// The landings a run created, consulted when one is found occupied.
#[derive(Debug)]
pub(crate) struct RunRecord {
    budget: RecordBudget,
    created: RefCell<Created>,
}

impl RunRecord {
    /// An empty record that saturates at `budget`.
    pub(crate) fn new(budget: RecordBudget) -> Self {
        Self {
            budget,
            created: RefCell::default(),
        }
    }

    /// Records that the run just created `leaf` in `dir`.
    ///
    /// A key that cannot be read is not recorded, so a later duplicate is
    /// refused.
    ///
    /// A directory is recorded the same way: a duplicate check never skips
    /// one, but a refusal asks whether the run made the occupant.
    pub(crate) fn note(&self, dir: &VettedDir, leaf: Leaf<'_>, rel: &Path) {
        if !self.is_saturated() {
            self.insert(key_of(dir, leaf, rel));
        }
    }

    /// Like [`note`](Self::note) for the file `leaf` the run just created and
    /// still holds open: keyed by its handle, so no lookup by name unless the
    /// handle gives no identity, which the lookup by name then classifies.
    pub(crate) fn note_file(&self, file: &File, dir: &VettedDir, leaf: Leaf<'_>, rel: &Path) {
        if self.is_saturated() {
            return;
        }
        match FileIdentity::of_file(file) {
            FileIdentity::Unknown => self.insert(key_of(dir, leaf, rel)),
            known @ FileIdentity::Known(_) => self.insert(Some(key_for(known, rel))),
        }
    }

    fn is_saturated(&self) -> bool {
        self.created.borrow().completeness == Completeness::Saturated
    }

    /// Keeps `key`; reaching the budget saturates the record, so every occupant
    /// counts as foreign (a refusal then says `existing`) and nothing more is
    /// recorded.
    fn insert(&self, key: Option<LandingKey>) {
        let mut created = self.created.borrow_mut();
        let Some(key) = key else {
            created.completeness = created.completeness.lossy();
            return;
        };
        if created.keys.len() >= self.budget.cap() {
            created.completeness = Completeness::Saturated;
        } else {
            created.keys.insert(key);
        }
    }

    /// Whether the occupant of `leaf` in `dir` is a landing this run created.
    ///
    /// `false` when the key cannot be read or the record is saturated.
    pub(crate) fn created_here(&self, dir: &VettedDir, leaf: Leaf<'_>, rel: &Path) -> bool {
        self.maker(dir, leaf, rel) == Maker::Run
    }

    /// Who made the occupant of `leaf` in `dir`.
    ///
    /// The tri-state form of [`created_here`](Self::created_here) that an
    /// opt-in refusal of repeats needs: a record that cannot vouch either way
    /// answers [`Maker::Unknown`] instead of "not made here". `Maker::Run` is
    /// true exactly when `created_here` is.
    pub(crate) fn maker(&self, dir: &VettedDir, leaf: Leaf<'_>, rel: &Path) -> Maker {
        let created = self.created.borrow();
        if created.completeness == Completeness::Saturated {
            return Maker::Unknown;
        }
        // A held key proves the run made the occupant even in a lossy record,
        // so `Run` is exactly `created_here`; lossiness only withdraws
        // "foreign".
        match dir.identity(leaf) {
            Err(_) => Maker::Unknown,
            Ok(None) => Maker::Foreign,
            Ok(Some(identity)) => match key_for(identity, rel) {
                key if created.keys.contains(&key) => Maker::Run,
                _ if created.completeness == Completeness::Lossy => Maker::Unknown,
                LandingKey::Identity(_) => Maker::Foreign,
                LandingKey::Path(_) => Maker::Unknown,
            },
        }
    }

    /// Whether the run created an entry with this identity.
    ///
    /// `false` for a saturated record, and for an entry recorded by path
    /// because its volume gave no identity: neither can vouch for an entry.
    #[cfg(any(target_os = "macos", all(unix, test)))]
    pub(crate) fn created_with(&self, key: IdentityKey) -> bool {
        let created = self.created.borrow();
        created.completeness != Completeness::Saturated
            && created.keys.contains(&LandingKey::Identity(key))
    }

    #[cfg(all(test, unix))]
    pub(crate) fn insert_identity_for_test(&self, identity: super::file_identity::FileIdentity) {
        if let Some(key) = identity.key() {
            self.created
                .borrow_mut()
                .keys
                .insert(LandingKey::Identity(key));
        }
    }

    #[cfg(all(test, unix))]
    pub(crate) fn saturate(&self) {
        self.created.borrow_mut().completeness = Completeness::Saturated;
    }
}

#[cfg(test)]
mod key_tests {
    use std::assert_matches;

    use super::RecordBudget;

    use super::LandingKey;
    use super::RunRecord;
    use super::key_for;
    use crate::formats::file_identity::FileIdentity;

    #[test]
    fn an_unknown_identity_is_keyed_by_the_path_as_spelled() {
        let record = RunRecord::new(RecordBudget::Full);
        record.insert(Some(key_for(FileIdentity::Unknown, "dir/a".as_ref())));

        let created = |rel: &str| {
            record
                .created
                .borrow()
                .keys
                .contains(&key_for(FileIdentity::Unknown, rel.as_ref()))
        };
        assert!(created("dir/a"));
        assert!(!created("dir/A"));
        assert_matches!(
            key_for(FileIdentity::Unknown, "x".as_ref()),
            LandingKey::Path(_)
        );
    }
}

#[cfg(test)]
mod completeness_tests {
    use super::Completeness;
    use super::RecordBudget;
    use super::RunRecord;

    #[test]
    fn lossy_only_degrades_a_complete_record_and_saturation_absorbs() {
        assert_eq!(Completeness::Complete.lossy(), Completeness::Lossy);
        assert_eq!(Completeness::Lossy.lossy(), Completeness::Lossy);
        assert_eq!(Completeness::Saturated.lossy(), Completeness::Saturated);
    }

    #[test]
    fn an_unkeyed_landing_makes_the_record_lossy_and_never_undoes_saturation() {
        let record = RunRecord::new(RecordBudget::Full);
        assert_eq!(record.created.borrow().completeness, Completeness::Complete);

        record.insert(None);
        assert_eq!(record.created.borrow().completeness, Completeness::Lossy);

        record.created.borrow_mut().completeness = Completeness::Saturated;
        record.insert(None);
        assert_eq!(
            record.created.borrow().completeness,
            Completeness::Saturated
        );
    }
}

#[cfg(all(test, unix))]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::Maker;
    use super::RecordBudget;
    use super::RunRecord;
    use super::key_basis;
    use crate::formats::pinned_dir::support::leaf;
    use crate::formats::pinned_dir::support::pinned_at;

    fn made_and_foreign() -> (tempfile::TempDir, crate::formats::pinned_dir::VettedDir) {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("made"), b"x").unwrap();
        std::fs::write(temp.path().join("foreign"), b"y").unwrap();
        (temp, dir)
    }

    #[test]
    fn maker_tells_the_run_from_a_foreign_occupant_and_an_absent_one() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("made"), "made".as_ref());

        assert_eq!(
            record.maker(&dir, leaf("made"), "made".as_ref()),
            Maker::Run
        );
        assert_eq!(
            record.maker(&dir, leaf("foreign"), "foreign".as_ref()),
            Maker::Foreign
        );
        assert_eq!(
            record.maker(&dir, leaf("absent"), "absent".as_ref()),
            Maker::Foreign
        );
    }

    #[test]
    fn a_saturated_record_cannot_vouch_either_way() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("made"), "made".as_ref());

        record.saturate();

        for name in ["made", "foreign"] {
            assert_eq!(
                record.maker(&dir, leaf(name), name.as_ref()),
                Maker::Unknown,
                "{name}"
            );
        }
    }

    #[test]
    fn a_record_that_filled_its_budget_saturates_into_unknown() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Diagnostic);
        let filler = |index: usize| Some(super::LandingKey::Path(format!("p{index}").into()));
        for index in 0..=super::DIAGNOSTIC_RECORD_CAP {
            record.insert(filler(index));
        }

        assert_eq!(
            record.maker(&dir, leaf("foreign"), "foreign".as_ref()),
            Maker::Unknown
        );
    }

    #[test]
    fn a_key_that_could_not_be_read_when_noted_makes_the_record_lossy() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("made"), "made".as_ref());

        record.note(&dir, leaf("absent"), "absent".as_ref());

        assert_eq!(
            record.maker(&dir, leaf("made"), "made".as_ref()),
            Maker::Run
        );
        assert_eq!(
            record.maker(&dir, leaf("foreign"), "foreign".as_ref()),
            Maker::Unknown,
            "a lossy record cannot call an unseen occupant foreign"
        );
    }

    #[test]
    fn run_is_exactly_created_here_whatever_the_state_of_the_record() {
        let (_temp, dir) = made_and_foreign();
        let lossy = RunRecord::new(RecordBudget::Full);
        lossy.note(&dir, leaf("made"), "made".as_ref());
        lossy.note(&dir, leaf("absent"), "absent".as_ref());
        let saturated = RunRecord::new(RecordBudget::Full);
        saturated.note(&dir, leaf("made"), "made".as_ref());
        saturated.saturate();
        let too_long = "x".repeat(300);

        for record in [&lossy, &saturated] {
            for name in ["made", "foreign", "absent", too_long.as_str()] {
                assert_eq!(
                    record.maker(&dir, leaf(name), name.as_ref()) == Maker::Run,
                    record.created_here(&dir, leaf(name), name.as_ref()),
                    "{name}"
                );
            }
        }
    }

    #[test]
    fn a_failed_identity_lookup_is_unknown_not_foreign() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Full);
        let too_long = "x".repeat(300);

        assert_eq!(
            record.maker(&dir, leaf(&too_long), too_long.as_ref()),
            Maker::Unknown
        );
    }

    #[test]
    fn a_path_keyed_record_knows_its_spelling_and_nothing_else() {
        let (_temp, dir) = made_and_foreign();
        let record = RunRecord::new(RecordBudget::Full);

        key_basis::with(key_basis::KeyBasis::Path, || {
            record.note(&dir, leaf("made"), "made".as_ref());

            assert_eq!(
                record.maker(&dir, leaf("made"), "made".as_ref()),
                Maker::Run
            );
            assert_eq!(
                record.maker(&dir, leaf("made"), "MADE".as_ref()),
                Maker::Unknown
            );
            assert_eq!(
                record.maker(&dir, leaf("foreign"), "foreign".as_ref()),
                Maker::Unknown
            );
        });
    }

    #[test]
    fn only_what_was_noted_counts_as_created() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("made"), b"x").unwrap();
        std::fs::write(temp.path().join("foreign"), b"y").unwrap();
        let record = RunRecord::new(RecordBudget::Full);

        record.note(&dir, leaf("made"), "made".as_ref());

        assert!(record.created_here(&dir, leaf("made"), "made".as_ref()));
        assert!(!record.created_here(&dir, leaf("foreign"), "foreign".as_ref()));
        assert!(!record.created_here(&dir, leaf("absent"), "absent".as_ref()));
    }

    #[test]
    fn a_file_noted_by_its_handle_counts_as_created() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        let made = std::fs::File::create(temp.path().join("made")).unwrap();
        std::fs::write(temp.path().join("foreign"), b"y").unwrap();
        let record = RunRecord::new(RecordBudget::Full);

        record.note_file(&made, &dir, leaf("made"), "made".as_ref());

        assert!(record.created_here(&dir, leaf("made"), "made".as_ref()));
        assert!(!record.created_here(&dir, leaf("foreign"), "foreign".as_ref()));
    }

    #[test]
    fn a_saturated_record_treats_every_occupant_as_foreign() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("made"), b"x").unwrap();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("made"), "made".as_ref());

        record.saturate();

        assert!(!record.created_here(&dir, leaf("made"), "made".as_ref()));
    }

    #[test]
    fn a_symlink_is_keyed_by_itself_not_by_its_target() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("target"), b"x").unwrap();
        std::os::unix::fs::symlink("target", temp.path().join("link")).unwrap();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("link"), "link".as_ref());

        assert!(record.created_here(&dir, leaf("link"), "link".as_ref()));
        assert!(!record.created_here(&dir, leaf("target"), "target".as_ref()));
    }

    #[test]
    fn files_and_directories_are_both_recorded_by_identity() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("made"), b"x").unwrap();
        std::fs::create_dir(temp.path().join("sub")).unwrap();
        let key = |name: &str| {
            let stat = rustix::fs::stat(temp.path().join(name)).unwrap();
            crate::formats::file_identity::IdentityKey::of_stat(&stat).unwrap()
        };
        let record = RunRecord::new(RecordBudget::Full);

        record.note(&dir, leaf("made"), "made".as_ref());
        record.note(&dir, leaf("sub"), "sub".as_ref());

        assert!(record.created_with(key("made")));
        assert!(record.created_with(key("sub")));
    }

    #[test]
    fn a_saturated_record_vouches_for_no_identity() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("made"), b"x").unwrap();
        let stat = rustix::fs::stat(temp.path().join("made")).unwrap();
        let key = crate::formats::file_identity::IdentityKey::of_stat(&stat).unwrap();
        let record = RunRecord::new(RecordBudget::Full);
        record.note(&dir, leaf("made"), "made".as_ref());
        assert!(record.created_with(key));

        record.saturate();

        assert!(!record.created_with(key));
    }

    #[test]
    fn a_diagnostic_record_keeps_exactly_its_cap_and_saturates_on_the_next_landing() {
        let temp = tempfile::tempdir().unwrap();
        let dir = pinned_at(temp.path());
        std::fs::write(temp.path().join("first"), b"x").unwrap();
        let diagnostic = RunRecord::new(RecordBudget::Diagnostic);
        let full = RunRecord::new(RecordBudget::Full);
        diagnostic.note(&dir, leaf("first"), "first".as_ref());
        full.note(&dir, leaf("first"), "first".as_ref());
        let filler = |index: usize| Some(super::LandingKey::Path(format!("p{index}").into()));
        assert_eq!(super::DIAGNOSTIC_RECORD_CAP, 16384);

        for index in 1..super::DIAGNOSTIC_RECORD_CAP {
            diagnostic.insert(filler(index));
        }
        assert!(diagnostic.created_here(&dir, leaf("first"), "first".as_ref()));

        diagnostic.insert(filler(super::DIAGNOSTIC_RECORD_CAP));
        for index in 1..=super::DIAGNOSTIC_RECORD_CAP {
            full.insert(filler(index));
        }

        assert!(!diagnostic.created_here(&dir, leaf("first"), "first".as_ref()));
        assert!(full.created_here(&dir, leaf("first"), "first".as_ref()));
    }

    #[test]
    fn skip_is_diagnostic_and_the_other_policies_are_full() {
        use crate::ExistingFiles;
        assert_eq!(
            ExistingFiles::Skip.record_budget(),
            RecordBudget::Diagnostic
        );
        assert_eq!(ExistingFiles::Refuse.record_budget(), RecordBudget::Full);
        assert_eq!(ExistingFiles::Overwrite.record_budget(), RecordBudget::Full);
    }
}