eggup-core 0.1.0

Policy-neutral local mechanics for verified multi-artifact updates (checksum integrity only, no transport or service manager)
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
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use crate::domain::{AbsentPolicy, CommitOwnership, MemberId, Ownership};
use crate::error::{Error, Result};
use crate::integrity::hash_file;
use crate::lock::MutationLock;
use crate::stage::PreparedTransaction;

static NEXT_BACKUP_NONCE: AtomicU64 = AtomicU64::new(0);

/// The terminal disposition of a commit attempt.
///
/// A rolled-back or recovery-required result is never equivalent to success;
/// see `examples/receipts.rs`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionDisposition {
    /// Every member was installed and the live set is coherent.
    Committed,
    /// The attempt failed and all old members were restored and verified.
    RolledBack,
    /// The live state or cleanup could not be safely restored automatically.
    RecoveryRequired,
}

/// How transaction-owned temporary evidence was handled.
///
/// This replaces the former `PostCommitFailurePolicy::{Cleaned,
/// RetainForRecovery}` name, which conflated cleanup disposition with the
/// ADR-0002 post-commit `KeepInstalled | RollBack` policy. The ADR-0002 policy
/// is reserved for a future post-commit failure boundary and is not
/// implemented here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CleanupDisposition {
    /// Owned temporary state was cleaned up.
    Cleaned,
    /// Evidence was retained for an operator because cleanup was unsafe or failed.
    RetainedForRecovery,
}

/// The transaction phase that produced a failure.
///
/// Phases are ordered by first occurrence in a commit attempt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FailurePhase {
    /// Mutation-lock acquisition or inspection.
    Lock,
    /// Destination ownership classification or parent containment.
    Ownership,
    /// Re-reading and re-hashing staged bytes under lock.
    StageRevalidation,
    /// Moving live destinations into the backup set.
    Backup,
    /// Renaming staged members into live destinations.
    Commit,
    /// Restoring backups after a failure.
    Rollback,
    /// Removing the backup set after a successful commit.
    Finalize,
}

impl FailurePhase {
    /// Returns the stable phase name used in receipts and diagnostics.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Lock => "lock",
            Self::Ownership => "ownership",
            Self::StageRevalidation => "stage-revalidation",
            Self::Backup => "backup",
            Self::Commit => "commit",
            Self::Rollback => "rollback",
            Self::Finalize => "finalize",
        }
    }
}

/// A stable machine-readable failure category.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FailureCategory {
    /// Another transaction or ambiguous lock owns the domain.
    LockContention,
    /// A destination is foreign, unknown, or violates containment/parent rules.
    OwnershipConflict,
    /// Staged bytes changed, are unreadable, or lack required integrity evidence.
    Verification,
    /// A live filesystem mutation failed.
    Filesystem,
    /// Caller input or transaction configuration is invalid.
    InvalidInput,
    /// The failure was injected by the test fault harness.
    Injected,
}

impl FailureCategory {
    /// Returns the stable category name.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::LockContention => "lock-contention",
            Self::OwnershipConflict => "ownership-conflict",
            Self::Verification => "verification",
            Self::Filesystem => "filesystem",
            Self::InvalidInput => "invalid-input",
            Self::Injected => "injected",
        }
    }
}

/// A bounded structured report describing one transaction failure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FailureReport {
    phase: FailurePhase,
    member: Option<MemberId>,
    category: FailureCategory,
    detail: String,
}

impl FailureReport {
    pub(crate) fn new(
        phase: FailurePhase,
        member: Option<MemberId>,
        category: FailureCategory,
        detail: impl Into<String>,
    ) -> Self {
        let mut detail = detail.into();
        // Bound human-readable detail; receipts must never embed unbounded output.
        if detail.len() > 512 {
            detail.truncate(512);
        }
        Self {
            phase,
            member,
            category,
            detail,
        }
    }

    /// Returns the phase that produced the failure.
    pub fn phase(&self) -> FailurePhase {
        self.phase
    }

    /// Returns the affected member, when the failure is member-specific.
    pub fn member(&self) -> Option<&MemberId> {
        self.member.as_ref()
    }

    /// Returns the stable failure category.
    pub fn category(&self) -> FailureCategory {
        self.category
    }

    /// Returns bounded human-readable detail.
    pub fn detail(&self) -> &str {
        &self.detail
    }
}

pub(crate) fn report_for_error(
    phase: FailurePhase,
    member: Option<MemberId>,
    error: &Error,
) -> FailureReport {
    let (category, detail) = match error {
        Error::UpdateInProgress { lock } => (
            FailureCategory::LockContention,
            format!("lock contention at {}", lock.display()),
        ),
        Error::DestinationConflict { destination } => (
            FailureCategory::OwnershipConflict,
            format!("destination conflict at {}", destination.display()),
        ),
        Error::VerificationFailed(m) => (FailureCategory::Verification, m.clone()),
        Error::CandidateExecution(m) => (FailureCategory::Verification, m.clone()),
        Error::InvalidInput(m) => {
            let category = if m.contains("injected") {
                FailureCategory::Injected
            } else {
                FailureCategory::InvalidInput
            };
            (category, m.clone())
        }
        Error::UnknownMember(m) => (FailureCategory::InvalidInput, format!("unknown member {m}")),
        Error::Io { operation, source } => (
            FailureCategory::Filesystem,
            format!("{operation}: {source}"),
        ),
    };
    FailureReport::new(phase, member, category, detail)
}

/// Evidence describing the terminal result of a transaction attempt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionReceipt {
    product: crate::domain::ProductId,
    release: crate::domain::ReleaseId,
    disposition: TransactionDisposition,
    rollback_performed: bool,
    rollback_verified: bool,
    cleanup: CleanupDisposition,
    recovery_path: Option<PathBuf>,
    failure: Option<FailureReport>,
    rollback_failure: Option<FailureReport>,
}

impl TransactionReceipt {
    /// Returns the product identity recorded by the transaction.
    pub fn product(&self) -> &crate::domain::ProductId {
        &self.product
    }

    /// Returns the release identity recorded by the transaction.
    pub fn release(&self) -> &crate::domain::ReleaseId {
        &self.release
    }

    /// Returns the terminal disposition.
    pub fn disposition(&self) -> TransactionDisposition {
        self.disposition
    }

    /// Returns whether rollback was attempted.
    pub fn rollback_performed(&self) -> bool {
        self.rollback_performed
    }

    /// Returns whether every restoration check passed.
    pub fn rollback_verified(&self) -> bool {
        self.rollback_verified
    }

    /// Returns how transaction-owned temporary evidence was handled.
    pub fn cleanup(&self) -> CleanupDisposition {
        self.cleanup
    }

    /// Returns retained backup/lock evidence when manual recovery is required.
    ///
    /// When present, the path always refers to real retained evidence. A
    /// cleanup failure after an otherwise successful commit retains the real
    /// backup root and reports the cleanup problem in [`Self::failure`];
    /// no synthetic non-existent path is ever returned.
    pub fn recovery_path(&self) -> Option<&Path> {
        self.recovery_path.as_deref()
    }

    /// Returns the structured report for the triggering failure, if any.
    ///
    /// A rolled-back transaction remains a first-class terminal result, but
    /// the caller can answer "what failed?" from this report without scraping
    /// logs. Successful commits with cleanup trouble also report the finalize
    /// problem here.
    pub fn failure(&self) -> Option<&FailureReport> {
        self.failure.as_ref()
    }

    /// Returns the rollback-phase failure when restoration itself failed.
    ///
    /// When present, [`Self::failure`] still carries the original triggering
    /// cause; this field carries the distinct recovery failure.
    pub fn rollback_failure(&self) -> Option<&FailureReport> {
        self.rollback_failure.as_ref()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) enum CommitFault {
    LockCreation,
    Backup(MemberId),
    BeforeFirstCommit,
    Commit(MemberId),
    CommitThenRollback(MemberId, MemberId),
    Finalize,
}

#[derive(Debug)]
struct BackupEntry {
    member: MemberId,
    destination: PathBuf,
    backup: Option<PathBuf>,
}

impl PreparedTransaction {
    pub(crate) fn commit_inner(
        self,
        ownership: CommitOwnership<'_>,
        verified_digests: &HashMap<MemberId, [u8; 32]>,
        fault: Option<CommitFault>,
    ) -> Result<TransactionReceipt> {
        if fault == Some(CommitFault::LockCreation) {
            return Err(Error::invalid("injected lock creation failure"));
        }
        // Preflight ownership classification before taking the lock so a
        // change between preflight and locked revalidation is detectable.
        let preflight = classify_all(&self, &ownership)?;
        let mut lock = MutationLock::acquire(
            self.plan.installation_root(),
            self.plan.product(),
            self.plan.release(),
        )?;
        // Locked ownership revalidation: must match preflight and authorize
        // the intended mutation.
        if let Err(error) = revalidate_ownership_locked(&self, &ownership, &preflight) {
            let report = report_for_error(FailurePhase::Ownership, None, &error);
            let verified = true;
            return finish_failure_no_mutation(self, lock, verified, report);
        }
        // Verification-to-commit continuity: re-read and re-hash every staged
        // member under lock before backing up any live destination.
        if let Err((member, error)) = revalidate_staged_under_lock(&self, verified_digests) {
            let report = report_for_error(FailurePhase::StageRevalidation, member, &error);
            return finish_failure_no_mutation(self, lock, true, report);
        }
        let backup_root = match create_backup_directory(self.plan.installation_root()) {
            Ok(p) => p,
            Err(error) => {
                let report = report_for_error(FailurePhase::Backup, None, &error);
                return finish_failure_no_mutation(self, lock, true, report);
            }
        };
        let mut entries = Vec::with_capacity(self.plan.artifacts().len());
        if let Err((member, error)) = self.backup_members(&mut entries, &backup_root, fault.clone())
        {
            let phase = FailurePhase::Backup;
            let report = report_for_error(phase, member, &error);
            let (verified, rollback_failure) =
                restore_entries(&entries, &HashSet::new(), fault.clone());
            return finish_failure(self, lock, backup_root, verified, rollback_failure, report);
        }
        if fault == Some(CommitFault::BeforeFirstCommit) {
            let error = Error::invalid("injected pre-commit failure");
            let report = report_for_error(FailurePhase::Commit, None, &error);
            let (verified, rollback_failure) =
                restore_entries(&entries, &HashSet::new(), fault.clone());
            return finish_failure(self, lock, backup_root, verified, rollback_failure, report);
        }

        let mut committed = HashSet::new();
        let members = self.plan.artifacts().iter().cloned().collect::<Vec<_>>();
        for member in members {
            let commit_should_fail = fault.as_ref().is_some_and(|candidate| match candidate {
                CommitFault::Commit(id) | CommitFault::CommitThenRollback(id, _) => {
                    id == member.id()
                }
                _ => false,
            });
            if commit_should_fail {
                let error = Error::invalid(format!("injected commit failure at {}", member.id()));
                let report =
                    report_for_error(FailurePhase::Commit, Some(member.id().clone()), &error);
                let (verified, rollback_failure) =
                    restore_entries(&entries, &committed, fault.clone());
                return finish_failure(self, lock, backup_root, verified, rollback_failure, report);
            }
            let destination = match self.plan.destination(member.id()) {
                Ok(destination) => destination,
                Err(error) => {
                    let report =
                        report_for_error(FailurePhase::Commit, Some(member.id().clone()), &error);
                    let (verified, rollback_failure) =
                        restore_entries(&entries, &committed, fault.clone());
                    return finish_failure(
                        self,
                        lock,
                        backup_root,
                        verified,
                        rollback_failure,
                        report,
                    );
                }
            };
            if let Err(error) = require_ready_parent(&destination, self.plan.installation_root()) {
                let report =
                    report_for_error(FailurePhase::Commit, Some(member.id().clone()), &error);
                let (verified, rollback_failure) =
                    restore_entries(&entries, &committed, fault.clone());
                return finish_failure(self, lock, backup_root, verified, rollback_failure, report);
            }
            let staged = match self.staged_path(member.id()) {
                Ok(staged) => staged,
                Err(error) => {
                    let report =
                        report_for_error(FailurePhase::Commit, Some(member.id().clone()), &error);
                    let (verified, rollback_failure) =
                        restore_entries(&entries, &committed, fault.clone());
                    return finish_failure(
                        self,
                        lock,
                        backup_root,
                        verified,
                        rollback_failure,
                        report,
                    );
                }
            };
            if let Err(source) = fs::rename(&staged, &destination) {
                let error = Error::io("replacing destination from prepared stage", source);
                let report =
                    report_for_error(FailurePhase::Commit, Some(member.id().clone()), &error);
                let (verified, rollback_failure) =
                    restore_entries(&entries, &committed, fault.clone());
                return finish_failure(self, lock, backup_root, verified, rollback_failure, report);
            }
            committed.insert(member.id().clone());
        }

        if fault == Some(CommitFault::Finalize) {
            lock.preserve();
            let report = FailureReport::new(
                FailurePhase::Finalize,
                None,
                FailureCategory::Injected,
                "injected finalize failure",
            );
            return Ok(TransactionReceipt {
                product: self.plan.product().clone(),
                release: self.plan.release().clone(),
                disposition: TransactionDisposition::Committed,
                rollback_performed: false,
                rollback_verified: false,
                cleanup: CleanupDisposition::RetainedForRecovery,
                recovery_path: Some(backup_root),
                failure: Some(report),
                rollback_failure: None,
            });
        }
        if let Err(source) = fs::remove_dir_all(&backup_root) {
            lock.preserve();
            let error = Error::io("removing backup set after commit", source);
            let report = report_for_error(FailurePhase::Finalize, None, &error);
            return Ok(TransactionReceipt {
                product: self.plan.product().clone(),
                release: self.plan.release().clone(),
                disposition: TransactionDisposition::Committed,
                rollback_performed: false,
                rollback_verified: false,
                cleanup: CleanupDisposition::RetainedForRecovery,
                recovery_path: Some(backup_root),
                failure: Some(report),
                rollback_failure: None,
            });
        }
        Ok(TransactionReceipt {
            product: self.plan.product().clone(),
            release: self.plan.release().clone(),
            disposition: TransactionDisposition::Committed,
            rollback_performed: false,
            rollback_verified: false,
            cleanup: CleanupDisposition::Cleaned,
            recovery_path: None,
            failure: None,
            rollback_failure: None,
        })
    }

    fn backup_members(
        &self,
        entries: &mut Vec<BackupEntry>,
        backup_root: &Path,
        fault: Option<CommitFault>,
    ) -> std::result::Result<(), (Option<MemberId>, Error)> {
        let members = self.plan.artifacts().iter().cloned().collect::<Vec<_>>();
        for member in members {
            if fault == Some(CommitFault::Backup(member.id().clone())) {
                return Err((
                    Some(member.id().clone()),
                    Error::invalid(format!("injected backup failure at {}", member.id())),
                ));
            }
            let destination = self
                .plan
                .destination(member.id())
                .map_err(|e| (Some(member.id().clone()), e))?;
            revalidate_destination(&destination, self.plan.installation_root())
                .map_err(|e| (Some(member.id().clone()), e))?;
            let backup = match fs::symlink_metadata(&destination) {
                Ok(_) => {
                    let path = backup_root.join(member.destination());
                    if let Some(parent) = path.parent() {
                        fs::create_dir_all(parent).map_err(|source| {
                            (
                                Some(member.id().clone()),
                                Error::io("creating backup directory", source),
                            )
                        })?;
                    }
                    fs::rename(&destination, &path).map_err(|source| {
                        (
                            Some(member.id().clone()),
                            Error::io("backing up existing destination", source),
                        )
                    })?;
                    Some(path)
                }
                Err(source) if source.kind() == std::io::ErrorKind::NotFound => None,
                Err(source) => {
                    return Err((
                        Some(member.id().clone()),
                        Error::io("reading destination before backup", source),
                    ));
                }
            };
            entries.push(BackupEntry {
                member: member.id().clone(),
                destination,
                backup,
            });
        }
        Ok(())
    }
}

fn classify_all(
    txn: &PreparedTransaction,
    ownership: &CommitOwnership<'_>,
) -> Result<HashMap<MemberId, Ownership>> {
    let mut out = HashMap::new();
    for member in txn.artifacts().iter() {
        let destination = txn.destination(member.id())?;
        out.insert(
            member.id().clone(),
            ownership.verifier.verify(member.id(), &destination),
        );
    }
    Ok(out)
}

fn revalidate_ownership_locked(
    txn: &PreparedTransaction,
    ownership: &CommitOwnership<'_>,
    preflight: &HashMap<MemberId, Ownership>,
) -> Result<()> {
    for member in txn.artifacts().iter() {
        let destination = txn.destination(member.id())?;
        let locked = ownership.verifier.verify(member.id(), &destination);
        let first = preflight
            .get(member.id())
            .copied()
            .unwrap_or(Ownership::Unknown);
        // Any change between preflight and locked classification fails: the
        // live deployment moved under us.
        if locked != first {
            return Err(Error::DestinationConflict { destination });
        }
        match locked {
            Ownership::Owned => {}
            Ownership::Absent if ownership.absent == AbsentPolicy::AllowCreate => {}
            Ownership::Absent | Ownership::Foreign | Ownership::Unknown => {
                return Err(Error::DestinationConflict { destination });
            }
        }
        // Parent containment is re-proven under lock; missing parents fail
        // without creating anything.
        require_ready_parent(&destination, txn.plan.installation_root())?;
        revalidate_destination(&destination, txn.plan.installation_root())?;
    }
    Ok(())
}

fn revalidate_staged_under_lock(
    txn: &PreparedTransaction,
    verified_digests: &HashMap<MemberId, [u8; 32]>,
) -> std::result::Result<(), (Option<MemberId>, Error)> {
    for member in txn.artifacts().iter() {
        let expected = verified_digests.get(member.id()).ok_or_else(|| {
            (
                Some(member.id().clone()),
                Error::VerificationFailed(format!(
                    "member {} has no verified digest; commit requires verified integrity",
                    member.id()
                )),
            )
        })?;
        let staged = txn
            .staged_path(member.id())
            .map_err(|e| (Some(member.id().clone()), e))?;
        let metadata = fs::symlink_metadata(&staged).map_err(|source| {
            (
                Some(member.id().clone()),
                Error::io("reading staged candidate", source),
            )
        })?;
        if !metadata.is_file() {
            return Err((
                Some(member.id().clone()),
                Error::VerificationFailed(format!(
                    "staged member {} is not a regular file",
                    member.id()
                )),
            ));
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            if metadata.nlink() != 1 {
                return Err((
                    Some(member.id().clone()),
                    Error::VerificationFailed(format!(
                        "staged member {} is hard-linked",
                        member.id()
                    )),
                ));
            }
        }
        let actual = hash_file(&staged).map_err(|e| (Some(member.id().clone()), e))?;
        if &actual != expected {
            return Err((
                Some(member.id().clone()),
                Error::VerificationFailed(format!(
                    "staged member {} changed after verification",
                    member.id()
                )),
            ));
        }
    }
    Ok(())
}

fn create_backup_directory(root: &Path) -> Result<PathBuf> {
    use std::time::{SystemTime, UNIX_EPOCH};
    for _ in 0..32 {
        let nonce = NEXT_BACKUP_NONCE.fetch_add(1, Ordering::Relaxed);
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        let path = root.join(format!(
            ".eggup-backup-{}-{nonce}-{nanos}",
            std::process::id()
        ));
        match fs::create_dir(&path) {
            Ok(()) => {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let _ = fs::set_permissions(&path, fs::Permissions::from_mode(0o700));
                }
                return Ok(path);
            }
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(source) => return Err(Error::io("creating backup set", source)),
        }
    }
    Err(Error::invalid("could not create a unique backup directory"))
}

fn revalidate_destination(destination: &Path, root: &Path) -> Result<()> {
    let root = fs::canonicalize(root)
        .map_err(|source| Error::io("canonicalizing install root", source))?;
    let mut ancestor = destination;
    while !ancestor.exists() {
        ancestor = ancestor
            .parent()
            .ok_or_else(|| Error::invalid("destination has no existing ancestor"))?;
    }
    let canonical_ancestor = fs::canonicalize(ancestor)
        .map_err(|source| Error::io("canonicalizing destination ancestor", source))?;
    if !canonical_ancestor.starts_with(&root) {
        return Err(Error::DestinationConflict {
            destination: destination.to_path_buf(),
        });
    }
    match fs::symlink_metadata(destination) {
        Ok(metadata) => {
            if !metadata.is_file() {
                return Err(Error::DestinationConflict {
                    destination: destination.to_path_buf(),
                });
            }
            #[cfg(unix)]
            {
                use std::os::unix::fs::MetadataExt;
                if metadata.nlink() != 1 {
                    return Err(Error::DestinationConflict {
                        destination: destination.to_path_buf(),
                    });
                }
            }
        }
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => {}
        Err(source) => return Err(Error::io("reading destination during revalidation", source)),
    }
    Ok(())
}

/// Requires that a live destination's parent already exists, is a real
/// directory beneath the installation root, and contains no symlink ambiguity.
///
/// Unlike the pre-corrective engine, this never calls `create_dir_all` on an
/// unchecked live path. Missing parents are an actionable error; symlink or
/// otherwise ambiguous parents fail closed.
fn require_ready_parent(destination: &Path, root: &Path) -> Result<()> {
    let parent = destination
        .parent()
        .ok_or_else(|| Error::invalid("destination has no parent"))?;
    let parent_meta = fs::symlink_metadata(parent).map_err(|source| {
        if source.kind() == std::io::ErrorKind::NotFound {
            Error::invalid(format!(
                "destination parent does not exist: {}",
                parent.display()
            ))
        } else {
            Error::io("reading destination parent", source)
        }
    })?;
    if parent_meta.file_type().is_symlink() {
        return Err(Error::DestinationConflict {
            destination: destination.to_path_buf(),
        });
    }
    if !parent_meta.is_dir() {
        return Err(Error::DestinationConflict {
            destination: destination.to_path_buf(),
        });
    }
    let canonical_root = fs::canonicalize(root)
        .map_err(|source| Error::io("canonicalizing install root", source))?;
    let canonical_parent = fs::canonicalize(parent)
        .map_err(|source| Error::io("canonicalizing destination parent", source))?;
    if !canonical_parent.starts_with(&canonical_root) {
        return Err(Error::DestinationConflict {
            destination: destination.to_path_buf(),
        });
    }
    // Walk ancestors of the parent to reject any symlink component, even when
    // the canonical target remains beneath the root.
    let mut cursor = Some(parent);
    while let Some(p) = cursor {
        if p == root {
            break;
        }
        let meta = fs::symlink_metadata(p)
            .map_err(|source| Error::io("reading destination ancestor", source))?;
        if meta.file_type().is_symlink() {
            return Err(Error::DestinationConflict {
                destination: destination.to_path_buf(),
            });
        }
        cursor = p.parent();
        if let Some(next) = cursor {
            if next.as_os_str().is_empty() {
                break;
            }
        } else {
            break;
        }
    }
    Ok(())
}

fn restore_entries(
    entries: &[BackupEntry],
    committed: &HashSet<MemberId>,
    fault: Option<CommitFault>,
) -> (bool, Option<FailureReport>) {
    let mut verified = true;
    let mut rollback_failure: Option<FailureReport> = None;
    for entry in entries.iter().rev() {
        let rollback_target = match fault.as_ref() {
            Some(CommitFault::CommitThenRollback(_, id)) => Some(id),
            _ => None,
        };
        if rollback_target == Some(&entry.member) {
            verified = false;
            rollback_failure = Some(FailureReport::new(
                FailurePhase::Rollback,
                Some(entry.member.clone()),
                FailureCategory::Injected,
                format!("injected rollback failure at {}", entry.member),
            ));
            continue;
        }
        if entry.backup.is_some() || committed.contains(&entry.member) {
            if let Ok(metadata) = fs::symlink_metadata(&entry.destination) {
                if !metadata.is_file() || fs::remove_file(&entry.destination).is_err() {
                    verified = false;
                    if rollback_failure.is_none() {
                        rollback_failure = Some(FailureReport::new(
                            FailurePhase::Rollback,
                            Some(entry.member.clone()),
                            FailureCategory::Filesystem,
                            format!("could not remove partial member {}", entry.member),
                        ));
                    }
                    continue;
                }
            }
        }
        if let Some(backup) = &entry.backup {
            if fs::rename(backup, &entry.destination).is_err() {
                verified = false;
                if rollback_failure.is_none() {
                    rollback_failure = Some(FailureReport::new(
                        FailurePhase::Rollback,
                        Some(entry.member.clone()),
                        FailureCategory::Filesystem,
                        format!("could not restore backup for {}", entry.member),
                    ));
                }
            }
        }
    }
    for entry in entries {
        let restored = match &entry.backup {
            Some(_) => fs::symlink_metadata(&entry.destination).is_ok(),
            None => {
                !committed.contains(&entry.member)
                    || fs::symlink_metadata(&entry.destination).is_err()
            }
        };
        if !restored {
            verified = false;
        }
    }
    (verified, rollback_failure)
}

fn finish_failure_no_mutation(
    transaction: PreparedTransaction,
    lock: MutationLock,
    nothing_mutated: bool,
    report: FailureReport,
) -> Result<TransactionReceipt> {
    debug_assert!(nothing_mutated);
    drop(lock);
    Ok(TransactionReceipt {
        product: transaction.plan.product().clone(),
        release: transaction.plan.release().clone(),
        disposition: TransactionDisposition::RolledBack,
        rollback_performed: false,
        rollback_verified: true,
        cleanup: CleanupDisposition::Cleaned,
        recovery_path: None,
        failure: Some(report),
        rollback_failure: None,
    })
}

fn finish_failure(
    transaction: PreparedTransaction,
    mut lock: MutationLock,
    backup_root: PathBuf,
    rollback_verified: bool,
    rollback_failure: Option<FailureReport>,
    report: FailureReport,
) -> Result<TransactionReceipt> {
    if rollback_verified && rollback_failure.is_none() && fs::remove_dir_all(&backup_root).is_ok() {
        return Ok(TransactionReceipt {
            product: transaction.plan.product().clone(),
            release: transaction.plan.release().clone(),
            disposition: TransactionDisposition::RolledBack,
            rollback_performed: true,
            rollback_verified: true,
            cleanup: CleanupDisposition::Cleaned,
            recovery_path: None,
            failure: Some(report),
            rollback_failure: None,
        });
    }
    lock.preserve();
    Ok(TransactionReceipt {
        product: transaction.plan.product().clone(),
        release: transaction.plan.release().clone(),
        disposition: TransactionDisposition::RecoveryRequired,
        rollback_performed: true,
        rollback_verified: false,
        cleanup: CleanupDisposition::RetainedForRecovery,
        recovery_path: Some(backup_root),
        failure: Some(report),
        rollback_failure,
    })
}