relswap 0.0.2

Signed-release install/update engine with durable activation and crash recovery
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
//! Install, update, rollback, and the durable activation pipeline.

use super::journal::{launcher_metadata, validate_install_state, validate_version_state};
use super::*;
use crate::release::{ReleaseMetadata, ReleaseTarget};
use std::io::Read;
use std::process::{Command, Stdio};
use std::thread;
use std::time::{Duration, Instant};

/// Backoff between self-test spawn attempts rejected with `ETXTBSY`.
const SELF_TEST_BUSY_RETRY_DELAY: Duration = Duration::from_millis(25);

impl<D: Downloader> Installation<D> {
    /// Install the exact package version supplied by [`App::version`].
    pub fn install(&self) -> Result<ActivationResult> {
        let version = Version::parse(self.app.version)
            .map_err(|error| InstallError::Invalid(format!("invalid package version: {error}")))?;
        self.install_version(version)
    }

    /// Descriptive alias for [`Self::install`].
    pub fn install_current(&self) -> Result<ActivationResult> {
        self.install()
    }

    /// Fetch and install one exact signed release version.
    pub fn install_version(&self, version: Version) -> Result<ActivationResult> {
        self.recover_if_managed()?;
        let metadata = self.fetch_exact_metadata(&version)?;
        let target = current_target()?;
        let archive = release::download_archive(self.app, &self.downloader, &metadata, target)?;
        self.activate_download(metadata, archive, version)
    }

    /// Explicit-version spelling for callers that expose an `install --version` command.
    pub fn install_exact(&self, version: Version) -> Result<ActivationResult> {
        self.install_version(version)
    }

    /// Fetch only signed latest metadata and report the authoritative current pointer.
    pub fn check_latest(&self) -> Result<CheckResult> {
        let _ = self.recover_if_managed()?;
        let latest = self.fetch_latest_metadata()?.version;
        let current = if lexists(&self.root)?
            && (self.read_install_state()?.is_some() || self.pointer_path_exists()?)
        {
            self.read_pointer_unlocked()?
        } else {
            None
        };
        let managed = current.is_some() && self.install_state_path().exists();
        Ok(CheckResult {
            current,
            latest,
            managed,
        })
    }

    /// Descriptive alias for [`Self::check_latest`].
    pub fn check(&self) -> Result<CheckResult> {
        self.check_latest()
    }

    /// Update a managed installation to the signed latest release.  Unmanaged installations are
    /// rejected before any download or filesystem mutation.
    pub fn update(&self) -> Result<ActivationResult> {
        self.recover_if_managed()?;
        let current = self.require_managed_current()?;
        let metadata = self.fetch_latest_metadata()?;
        if metadata.version < current {
            return Err(InstallError::Downgrade {
                current,
                requested: metadata.version,
            });
        }
        if metadata.version == current {
            return Ok(ActivationResult {
                version: current,
                changed: false,
            });
        }
        let archive =
            release::download_archive(self.app, &self.downloader, &metadata, current_target()?)?;
        let version = metadata.version.clone();
        self.activate_download(metadata, archive, version)
    }

    /// Explicit latest-update spelling for CLI integrations.
    pub fn update_latest(&self) -> Result<ActivationResult> {
        self.update()
    }

    /// Roll back to `install.json.previous` using the same pending/pointer/install sequence as an
    /// update.  The retained target is fully reverified before a pending journal is written.
    pub fn rollback(&self) -> Result<ActivationResult> {
        self.recover_if_managed()?;
        let current = self.require_managed_current()?;
        let install = self.read_install_state()?.ok_or(InstallError::Unmanaged)?;
        let target = install.previous.clone().ok_or_else(|| {
            InstallError::Invalid("managed installation has no previous version".to_string())
        })?;
        if target >= current {
            return Err(InstallError::Invalid(
                "managed installation previous version is not older than active".to_string(),
            ));
        }
        self.activate_existing(target)
    }

    /// Explicit previous-version spelling for CLI integrations.
    pub fn rollback_previous(&self) -> Result<ActivationResult> {
        self.rollback()
    }

    /// Roll back to a specific retained version.  The normal CLI-facing operation is
    /// [`Self::rollback`], which targets the descriptive previous field.
    pub fn rollback_to(&self, target: Version) -> Result<ActivationResult> {
        self.recover_if_managed()?;
        let current = self.require_managed_current()?;
        if target >= current {
            return Err(InstallError::Invalid(
                "rollback target must be older than active version".to_string(),
            ));
        }
        self.activate_existing(target)
    }

    fn fetch_exact_metadata(&self, version: &Version) -> Result<ReleaseMetadata> {
        let repository = self.repository_url()?;
        Ok(match &self.trusted_keys {
            Some(keys) => release::fetch_version_metadata_with_keys(
                self.app,
                &self.downloader,
                &repository,
                version,
                keys,
            )?,
            None => {
                release::fetch_exact_metadata(self.app, &self.downloader, &repository, version)?
            }
        })
    }

    fn fetch_latest_metadata(&self) -> Result<ReleaseMetadata> {
        let repository = self.repository_url()?;
        Ok(match &self.trusted_keys {
            Some(keys) => release::fetch_latest_metadata_with_keys(
                self.app,
                &self.downloader,
                &repository,
                keys,
            )?,
            None => release::fetch_latest_metadata(self.app, &self.downloader, &repository)?,
        })
    }

    fn activate_download(
        &self,
        metadata: ReleaseMetadata,
        archive: release::DownloadedArchive,
        version: Version,
    ) -> Result<ActivationResult> {
        let _ = self.recover_if_managed()?;
        if lexists(&self.command_path)? && !lexists(&self.install_state_path())? {
            #[cfg(windows)]
            let proven = self.prove_retained_launcher_ownership()?;
            #[cfg(not(windows))]
            let proven = false;
            if !proven {
                return Err(InstallError::Invalid(format!(
                    "refusing to replace existing unmanaged command {}",
                    self.command_path.display()
                )));
            }
        }
        let lock = self.lock_for_mutation()?;
        let result = self.activate_download_locked(&lock, metadata, archive, version);
        drop(lock);
        result
    }

    fn activate_download_locked(
        &self,
        _lock: &InstallLock,
        metadata: ReleaseMetadata,
        archive: release::DownloadedArchive,
        version: Version,
    ) -> Result<ActivationResult> {
        let target = current_target()?;
        if archive.target != target {
            return Err(InstallError::Invalid(format!(
                "downloaded archive target {} differs from current target {target}",
                archive.target
            )));
        }
        if metadata.version != version {
            return Err(InstallError::Invalid(
                "downloaded metadata version differs from requested version".to_string(),
            ));
        }
        self.verify_metadata(&metadata, target)?;
        let install = self.read_install_state()?;
        if let Some(install) = &install {
            validate_install_state(install)?;
        }
        let from = self.read_pointer_unlocked()?;
        #[cfg(windows)]
        let retained_launcher = if install.is_none() {
            self.retained_launcher_ownership()?
        } else {
            None
        };
        #[cfg(not(windows))]
        let retained_launcher: Option<LauncherOwnership> = None;
        let managed = install.is_some() && from.is_some();
        if managed {
            let active = from.as_ref().expect("managed pointer is present");
            if version <= *active {
                if version == *active {
                    let state = self.verify_final_version(
                        active,
                        install.as_ref().map(|s| s.installation_id.as_str()),
                    )?;
                    if fs::read(self.version_manifest_path(active))? != metadata.manifest_bytes
                        || fs::read(self.version_signature_path(active))?
                            != metadata.signature_bytes
                    {
                        return Err(InstallError::Invalid(
                            "existing final version has different signed metadata".to_string(),
                        ));
                    }
                    self.ensure_command_owned(Some(active), install.as_ref())?;
                    let _ = state;
                    return Ok(ActivationResult {
                        version: active.clone(),
                        changed: false,
                    });
                }
                return Err(InstallError::Downgrade {
                    current: active.clone(),
                    requested: version.clone(),
                });
            }
        }

        self.ensure_private_layout()?;
        let installation_id = self.choose_installation_id(&version, install.as_ref())?;
        if from.is_some() || !lexists(&self.command_path)? {
            self.cleanup_staging()?;
        }
        #[cfg(windows)]
        if from.is_none() && install.is_none() {
            self.cleanup_staging()?;
        }
        self.ensure_command_owned(from.as_ref(), install.as_ref())?;
        let already_managed = install.is_some() || retained_launcher.is_some();

        let final_dir = self.version_dir(&version);
        let existing = lexists(&final_dir)?;
        let mut transaction = None;
        if existing {
            let _state = self.verify_final_version(&version, Some(&installation_id))?;
            if fs::read(self.version_manifest_path(&version))? != metadata.manifest_bytes
                || fs::read(self.version_signature_path(&version))? != metadata.signature_bytes
            {
                return Err(InstallError::Invalid(
                    "existing final version has different signed metadata".to_string(),
                ));
            }
            if let Some(install) = &install {
                self.verify_installed_launcher(install)?;
            } else {
                #[cfg(windows)]
                {
                    let (launcher_size, launcher_sha256) =
                        if let Some(launcher) = retained_launcher.as_ref() {
                            (launcher.size, launcher.sha256.as_str())
                        } else {
                            let launcher = _state.launcher.as_ref().ok_or_else(|| {
                                InstallError::Invalid(
                                    "Windows version state lacks launcher metadata".into(),
                                )
                            })?;
                            (launcher.size, launcher.sha256.as_str())
                        };
                    executable::ensure_regular_file(&self.command_path)?;
                    verify_file_digest(
                        &self.command_path,
                        launcher_size,
                        launcher_sha256,
                        "installed launcher",
                    )?;
                }
            }
        } else {
            let tx = self.create_transaction()?;
            transaction = Some(tx.clone());
            self.write_downloaded_version(
                &tx,
                &metadata,
                &archive,
                &installation_id,
                target,
                already_managed,
            )?;
            self.fault(FaultPoint::PayloadWritten)?;
            self.verify_staged_version(&tx, &version, &installation_id)?;
            self.fault(FaultPoint::Verified)?;
            self.sync_staging_transaction(&tx)?;
            self.fault(FaultPoint::StagingSynced)?;

            if lexists(&final_dir)? {
                return Err(InstallError::Invalid(format!(
                    "retained version directory appeared during installation: {}",
                    final_dir.display()
                )));
            }
            executable::rename_new(&tx.version_dir, &final_dir)?;
            self.fault(FaultPoint::VersionRenamed)?;
        }

        self.ensure_command_parent()?;
        let prior_install = match (install, retained_launcher) {
            (Some(install), _) => Some(install),
            (None, Some(launcher)) => Some(InstallState {
                schema_version: STATE_SCHEMA_VERSION,
                active: None,
                previous: None,
                installation_id: installation_id.clone(),
                launcher: Some(launcher),
            }),
            (None, None) => None,
        };
        self.activate_pointer_and_state(
            from,
            version.clone(),
            installation_id,
            prior_install,
            transaction
                .as_ref()
                .map(|transaction| transaction.id.clone()),
            transaction.as_ref(),
        )?;
        Ok(ActivationResult {
            version,
            changed: true,
        })
    }

    fn activate_existing(&self, target: Version) -> Result<ActivationResult> {
        let lock = self.lock_for_mutation()?;
        let result = self.activate_existing_locked(&lock, target);
        drop(lock);
        result
    }

    fn activate_existing_locked(
        &self,
        _lock: &InstallLock,
        target: Version,
    ) -> Result<ActivationResult> {
        let install = self.read_install_state()?.ok_or(InstallError::Unmanaged)?;
        let from = self
            .read_pointer_unlocked()?
            .ok_or(InstallError::Unmanaged)?;
        if target >= from {
            return Err(InstallError::Invalid(
                "retained activation target must be older than active pointer".to_string(),
            ));
        }
        self.ensure_command_owned(Some(&from), Some(&install))?;
        self.verify_final_version(&target, Some(&install.installation_id))?;
        self.verify_installed_launcher(&install)?;
        self.fault(FaultPoint::Verified)?;
        self.ensure_command_parent()?;
        self.activate_pointer_and_state(
            Some(from),
            target,
            install.installation_id.clone(),
            Some(install),
            None,
            None,
        )
    }

    pub(super) fn activate_pointer_and_state(
        &self,
        from: Option<Version>,
        to: Version,
        installation_id: String,
        prior_install: Option<InstallState>,
        transaction_id: Option<String>,
        transaction: Option<&Transaction>,
    ) -> Result<ActivationResult> {
        if let Some(config) = self.app.self_test {
            self.run_self_test(&to, config)?;
        }
        self.fault(FaultPoint::SelfTested)?;
        let pending = PendingActivation {
            schema_version: STATE_SCHEMA_VERSION,
            from: from.clone(),
            to: to.clone(),
            transaction_id: transaction_id.unwrap_or(random_id("transaction")?),
        };
        self.write_pending(&pending)?;
        self.fault(FaultPoint::PendingWritten)?;
        self.switch_pointer(&to)?;
        self.fault(FaultPoint::PointerSwitched)?;

        let launcher = self.launcher_ownership_for(&to, prior_install.as_ref())?;
        let install = InstallState {
            schema_version: STATE_SCHEMA_VERSION,
            active: Some(to.clone()),
            previous: from,
            installation_id,
            launcher,
        };
        self.write_install_state(&install)?;
        self.fault(FaultPoint::InstallWritten)?;
        self.remove_pending()?;
        self.fault(FaultPoint::PendingRemoved)?;
        if let Some(transaction) = transaction {
            self.remove_transaction_after_rename(transaction)?;
        }
        self.sync_affected_parents()?;
        self.fault(FaultPoint::ParentsSynced)?;
        Ok(ActivationResult {
            version: to,
            changed: true,
        })
    }

    /// Run the configured probe against the staged payload for `version`.
    ///
    /// The expected output is derived from `version` - the release about to be activated - so the
    /// check is correct for updates, where the staged payload differs from the running binary.
    fn run_self_test(&self, version: &Version, config: crate::SelfTest) -> Result<()> {
        let started = Instant::now();
        // ETXTBSY is a race, not a verdict on the payload.  A `fork` in any thread of the host
        // process briefly inherits every open descriptor, so a payload we have only just finished
        // writing can be reported busy because an unrelated thread spawned a child in that window -
        // routine in a threaded application, which is exactly what this crate is embedded in.
        // Retry within the caller's timeout budget.
        //
        // Every other spawn failure is reported with its cause: the probe exists to diagnose a
        // payload that cannot run, and a bare "self-test failed" would hide precisely the
        // ENOENT/EACCES/loader errors it was added to surface.
        let mut child = loop {
            match Command::new(self.payload_path(version))
                .args(config.args)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()
            {
                Ok(child) => break child,
                Err(error)
                    if error.kind() == io::ErrorKind::ExecutableFileBusy
                        && started.elapsed() + SELF_TEST_BUSY_RETRY_DELAY < config.timeout =>
                {
                    thread::sleep(SELF_TEST_BUSY_RETRY_DELAY);
                }
                Err(error) => {
                    return Err(InstallError::Invalid(format!(
                        "self-test could not run staged payload: {error}"
                    )));
                }
            }
        };
        let mut stdout = child
            .stdout
            .take()
            .ok_or_else(|| InstallError::Invalid("self-test failed".into()))?;
        let mut stderr = child
            .stderr
            .take()
            .ok_or_else(|| InstallError::Invalid("self-test failed".into()))?;
        let stdout_reader = thread::spawn(move || {
            let mut bytes = Vec::new();
            stdout.read_to_end(&mut bytes).map(|_| bytes)
        });
        let stderr_reader = thread::spawn(move || {
            let mut bytes = Vec::new();
            stderr.read_to_end(&mut bytes).map(|_| bytes)
        });
        // `started` is deliberately not reset: `config.timeout` bounds the whole probe, spawn
        // retries included, so a payload that is busy for the full budget cannot also get a fresh
        // budget to run in.
        let status = loop {
            if let Some(status) = child
                .try_wait()
                .map_err(|_| InstallError::Invalid("self-test failed".into()))?
            {
                break status;
            }
            if started.elapsed() >= config.timeout {
                let _ = child.kill();
                let _ = child.wait();
                let _ = stdout_reader.join();
                let _ = stderr_reader.join();
                return Err(InstallError::Invalid("self-test timed out".into()));
            }
            thread::sleep(std::time::Duration::from_millis(10));
        };
        let mut output = stdout_reader
            .join()
            .map_err(|_| InstallError::Invalid("self-test failed".into()))?
            .map_err(|_| InstallError::Invalid("self-test failed".into()))?;
        output.extend(
            stderr_reader
                .join()
                .map_err(|_| InstallError::Invalid("self-test failed".into()))?
                .map_err(|_| InstallError::Invalid("self-test failed".into()))?,
        );
        if !status.success() {
            return Err(InstallError::Invalid("self-test failed".into()));
        }
        if !String::from_utf8_lossy(&output).contains(&version.to_string()) {
            return Err(InstallError::Invalid("self-test output mismatch".into()));
        }
        Ok(())
    }

    fn switch_pointer(&self, version: &Version) -> Result<()> {
        let payload = self.payload_path(version);
        executable::ensure_regular_file(&payload)?;
        #[cfg(unix)]
        {
            executable::atomic_switch_symlink(&self.command_path, &payload)?;
        }
        #[cfg(windows)]
        {
            executable::atomic_replace_file(&self.active_path(), version.to_string().as_bytes())?;
        }
        Ok(())
    }

    fn create_transaction(&self) -> Result<Transaction> {
        self.ensure_private_layout()?;
        let transaction_id = random_id("transaction")?;
        let transaction_dir = self.staging_dir().join(&transaction_id);
        fs_security::ensure_private_dir(&transaction_dir)?;
        let version_dir = transaction_dir.join("version");
        fs_security::ensure_private_dir(&version_dir)?;
        self.fault(FaultPoint::StagingCreated)?;
        Ok(Transaction {
            id: transaction_id,
            dir: transaction_dir,
            version_dir,
        })
    }

    fn write_downloaded_version(
        &self,
        transaction: &Transaction,
        metadata: &ReleaseMetadata,
        archive: &release::DownloadedArchive,
        installation_id: &str,
        target: ReleaseTarget,
        already_managed: bool,
    ) -> Result<()> {
        #[cfg(not(windows))]
        let _ = already_managed;
        let selected = self.verify_metadata(metadata, target)?;
        let archive_path = transaction.dir.join(&selected.asset.archive);
        executable::create_new_file(&archive_path, &archive.bytes, Some(0o600))?;
        release::extract_archive_file(
            self.app,
            &archive_path,
            selected.asset,
            &transaction.version_dir,
        )?;
        executable::atomic_replace_file_with_mode(
            &transaction.version_dir.join(MANIFEST_FILE),
            &metadata.manifest_bytes,
            Some(0o600),
        )?;
        executable::atomic_replace_file_with_mode(
            &transaction.version_dir.join(SIGNATURE_FILE),
            &metadata.signature_bytes,
            Some(0o600),
        )?;
        let payload = transaction.version_dir.join(target.payload_name(self.app));
        executable::set_executable(&payload)?;
        let state = VersionState {
            schema_version: STATE_SCHEMA_VERSION,
            version: metadata.version.clone(),
            target,
            binary_sha256: selected.asset.payload.sha256.clone(),
            size: selected.asset.payload.size,
            installation_id: installation_id.to_string(),
            launcher: selected.launcher().map(launcher_metadata),
        };
        self.write_version_state(&transaction.version_dir, &state)?;

        #[cfg(windows)]
        {
            let launcher = selected.launcher().ok_or_else(|| {
                InstallError::Invalid("Windows release is missing signed launcher metadata".into())
            })?;
            if launcher.protocol != 1 {
                return Err(InstallError::Invalid(
                    "managed Windows installations require launcher protocol 1".into(),
                ));
            }
            let launcher_name = self.app.launcher_name().ok_or_else(|| {
                InstallError::Invalid(
                    "managed Windows installations require a configured launcher name".into(),
                )
            })?;
            let staged_launcher = transaction.version_dir.join(launcher_name);
            executable::ensure_regular_file(&staged_launcher)?;
            verify_file_digest(
                &staged_launcher,
                launcher.size,
                &launcher.sha256,
                "launcher",
            )?;
            if !lexists(&self.command_path)? {
                if already_managed {
                    return Err(InstallError::Invalid(
                        "managed Windows launcher is missing and cannot be recreated by an update"
                            .into(),
                    ));
                }
                self.ensure_command_parent()?;
                executable::atomic_replace_file_with_mode(
                    &transaction.dir.join(LAUNCHER_CREATED_MARKER),
                    launcher.sha256.as_bytes(),
                    Some(0o600),
                )?;
                executable::create_new_file(
                    &self.command_path,
                    &fs::read(&staged_launcher)?,
                    None,
                )?;
            } else if !already_managed {
                return Err(InstallError::Invalid(
                    "existing Windows launcher requires a proven managed owner".into(),
                ));
            } else if let Some(install) = self.read_install_state()? {
                self.verify_installed_launcher(&install)?;
            } else if !self.prove_retained_launcher_ownership()? {
                return Err(InstallError::Invalid(
                    "existing Windows launcher ownership could not be re-established".into(),
                ));
            }
            fs::remove_file(staged_launcher)?;
        }
        Ok(())
    }

    fn verify_metadata<'a>(
        &self,
        metadata: &'a ReleaseMetadata,
        target: ReleaseTarget,
    ) -> Result<release::SelectedAsset<'a>> {
        if metadata.version != metadata.manifest.version {
            return Err(InstallError::Invalid(
                "release metadata version does not match its manifest".to_string(),
            ));
        }
        if metadata.manifest.version.to_string() != metadata.version.to_string() {
            return Err(InstallError::Invalid(
                "release metadata version is not canonical".to_string(),
            ));
        }
        let verified = match &self.trusted_keys {
            Some(keys) => release::verify_manifest_with_keys(
                &metadata.manifest_bytes,
                &metadata.signature_bytes,
                keys,
            )?,
            None => release::verify_manifest(
                self.app,
                &metadata.manifest_bytes,
                &metadata.signature_bytes,
            )?,
        };
        if verified.key_id != metadata.verified_signature.key_id {
            return Err(InstallError::Invalid(
                "release signature result differs from downloaded metadata".into(),
            ));
        }
        let parsed = release::ReleaseManifest::from_bytes(self.app, &metadata.manifest_bytes)?;
        parsed.ensure_not_expired(chrono::Utc::now())?;
        if parsed != metadata.manifest {
            return Err(InstallError::Invalid(
                "downloaded release metadata changed after verification".into(),
            ));
        }
        Ok(metadata.manifest.asset_for(self.app, target)?)
    }

    fn verify_staged_version(
        &self,
        transaction: &Transaction,
        version: &Version,
        installation_id: &str,
    ) -> Result<VersionState> {
        let state =
            self.verify_version_dir_inner(&transaction.version_dir, Some(installation_id))?;
        if &state.version != version {
            return Err(InstallError::Invalid(
                "staged version state differs from transaction version".to_string(),
            ));
        }
        Ok(state)
    }

    pub(super) fn verify_final_version(
        &self,
        version: &Version,
        installation_id: Option<&str>,
    ) -> Result<VersionState> {
        let state = self.verify_version_dir_inner(&self.version_dir(version), installation_id)?;
        if state.version != *version {
            return Err(InstallError::Invalid(
                "version.json does not match its immutable directory name".into(),
            ));
        }
        Ok(state)
    }

    fn verify_version_dir_inner(
        &self,
        dir: &Path,
        installation_id: Option<&str>,
    ) -> Result<VersionState> {
        fs_security::ensure_private_dir(dir)?;
        let state = super::journal::read_json::<VersionState>(&dir.join(VERSION_FILE))?
            .ok_or_else(|| InstallError::Invalid("version.json is missing".into()))?;
        validate_version_state(self.app, &state)?;
        if let Some(expected) = installation_id
            && state.installation_id != expected
        {
            return Err(InstallError::Invalid(
                "version installation id differs from managed installation".into(),
            ));
        }
        let target = current_target()?;
        if state.target != target {
            return Err(InstallError::Invalid(
                "version target differs from current host target".into(),
            ));
        }
        let manifest_bytes =
            read_regular_limited(&dir.join(MANIFEST_FILE), release::MAX_METADATA_SIZE)?;
        let signature_bytes =
            read_regular_limited(&dir.join(SIGNATURE_FILE), release::MAX_METADATA_SIZE)?;
        let verified = match &self.trusted_keys {
            Some(keys) => {
                release::verify_manifest_with_keys(&manifest_bytes, &signature_bytes, keys)?
            }
            None => release::verify_manifest(self.app, &manifest_bytes, &signature_bytes)?,
        };
        let manifest = release::ReleaseManifest::from_bytes(self.app, &manifest_bytes)?;
        if manifest.version != state.version {
            return Err(InstallError::Invalid(
                "version state does not match signed release version".into(),
            ));
        }
        let selected = manifest.asset_for(self.app, target)?;
        if selected.asset.payload.path
            != format!(
                "{}/{}",
                target.root_name(self.app, &state.version),
                target.payload_name(self.app)
            )
        {
            return Err(InstallError::Invalid(
                "signed payload path is not canonical".into(),
            ));
        }
        if verified.key_id.is_empty() {
            return Err(InstallError::Invalid(
                "release signature key id is empty".into(),
            ));
        }
        if state.binary_sha256 != selected.asset.payload.sha256
            || state.size != selected.asset.payload.size
        {
            return Err(InstallError::Invalid(
                "version state payload digest does not match signed manifest".into(),
            ));
        }
        let payload = dir.join(target.payload_name(self.app));
        executable::ensure_regular_file(&payload)?;
        verify_file_digest(&payload, state.size, &state.binary_sha256, "payload")?;
        if target.is_windows() {
            let launcher = selected.launcher().ok_or_else(|| {
                InstallError::Invalid("Windows release is missing launcher metadata".into())
            })?;
            super::journal::validate_signed_launcher(self.app, launcher)?;
            if state.launcher.as_ref() != Some(&launcher_metadata(launcher)) {
                return Err(InstallError::Invalid(
                    "version state launcher metadata does not match signed manifest".into(),
                ));
            }
        } else if state.launcher.is_some() || selected.launcher().is_some() {
            return Err(InstallError::Invalid(
                "Unix version contains Windows launcher metadata".into(),
            ));
        }
        validate_exact_version_members(self.app, dir, target)?;
        Ok(state)
    }

    fn sync_staging_transaction(&self, transaction: &Transaction) -> Result<()> {
        sync_regular_files(&transaction.version_dir)?;
        sync_regular_files(&transaction.dir)?;
        executable::sync_dir(&self.staging_dir())?;
        Ok(())
    }

    fn remove_transaction_after_rename(&self, transaction: &Transaction) -> Result<()> {
        if lexists(&transaction.dir)? {
            fs::remove_dir_all(&transaction.dir)?;
            executable::sync_dir(&self.staging_dir())?;
        }
        Ok(())
    }

    fn require_managed_current(&self) -> Result<Version> {
        let current = self
            .read_pointer_unlocked()?
            .ok_or(InstallError::Unmanaged)?;
        let install = self.read_install_state()?.ok_or(InstallError::Unmanaged)?;
        validate_install_state(&install)?;
        if install.active.as_ref() != Some(&current) {
            return Err(InstallError::Invalid(
                "managed install metadata does not describe the authoritative pointer".into(),
            ));
        }
        self.verify_final_version(&current, Some(&install.installation_id))?;
        self.ensure_command_owned(Some(&current), Some(&install))?;
        Ok(current)
    }

    fn choose_installation_id(
        &self,
        version: &Version,
        install: Option<&InstallState>,
    ) -> Result<String> {
        if let Some(install) = install {
            validate_install_state(install)?;
            return Ok(install.installation_id.clone());
        }
        let final_dir = self.version_dir(version);
        if lexists(&final_dir)? {
            return Ok(self.verify_final_version(version, None)?.installation_id);
        }
        if lexists(&self.versions_dir())? {
            let mut existing_id = None;
            for entry in fs::read_dir(self.versions_dir())? {
                let path = entry?.path();
                let metadata = fs::symlink_metadata(&path)?;
                if metadata.file_type().is_symlink() || !metadata.is_dir() {
                    return Err(InstallError::Invalid(
                        "versions contains a non-directory entry".into(),
                    ));
                }
                let name = path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .ok_or_else(|| {
                        InstallError::Invalid("version directory is not UTF-8".into())
                    })?;
                let retained = parse_canonical_version(name)?;
                let state = self.verify_final_version(&retained, None)?;
                if let Some(existing) = &existing_id {
                    if existing != &state.installation_id {
                        return Err(InstallError::Invalid(
                            "retained versions belong to different installations".into(),
                        ));
                    }
                } else {
                    existing_id = Some(state.installation_id);
                }
            }
            if let Some(existing_id) = existing_id {
                return Ok(existing_id);
            }
        }
        random_id("installation")
    }

    pub(super) fn ensure_command_owned(
        &self,
        active: Option<&Version>,
        install: Option<&InstallState>,
    ) -> Result<()> {
        let exists = lexists(&self.command_path)?;
        if !exists {
            #[cfg(windows)]
            if active.is_some() && install.is_some() {
                return Err(InstallError::Invalid(
                    "managed Windows launcher is missing and cannot be recreated by an update"
                        .into(),
                ));
            }
            return Ok(());
        }
        let Some(active) = active else {
            #[cfg(windows)]
            if install.is_none() && self.prove_retained_launcher_ownership()? {
                // A first install can create the stable launcher before its version directory is
                // renamed into place.  If the process dies at that boundary, the marker and the
                // retained version prove that this launcher is ours even though install.json and
                // active do not exist yet.
                return Ok(());
            }
            return Err(InstallError::Invalid(format!(
                "refusing to replace existing unmanaged command {}",
                self.command_path.display()
            )));
        };
        let Some(install) = install else {
            return Err(InstallError::Invalid(
                "existing command has no installation ownership record".into(),
            ));
        };
        if install.active.as_ref() != Some(active) {
            return Err(InstallError::Invalid(
                "existing command ownership does not match the active pointer".into(),
            ));
        }
        let version_state = self.verify_final_version(active, Some(&install.installation_id))?;
        #[cfg(unix)]
        {
            let Some(pointer) = executable::read_symlink(&self.command_path)? else {
                return Err(InstallError::Invalid(
                    "managed Unix command is missing its symlink".into(),
                ));
            };
            if !pointer.is_absolute() || !same_path(&pointer, &self.payload_path(active)) {
                return Err(InstallError::Invalid(
                    "existing command symlink is not owned by this installation".into(),
                ));
            }
            let _ = version_state;
        }
        #[cfg(windows)]
        {
            self.verify_installed_launcher(install)?;
            let _ = version_state;
        }
        Ok(())
    }

    pub(super) fn launcher_ownership_for(
        &self,
        version: &Version,
        prior: Option<&InstallState>,
    ) -> Result<Option<LauncherOwnership>> {
        #[cfg(unix)]
        {
            let _ = (version, prior);
            Ok(None)
        }
        #[cfg(windows)]
        {
            if let Some(prior) = prior {
                self.verify_installed_launcher(prior)?;
                return Ok(prior.launcher.clone());
            }
            let state = self.verify_final_version(version, None)?;
            let launcher = state.launcher.ok_or_else(|| {
                InstallError::Invalid("Windows version has no signed launcher metadata".into())
            })?;
            let ownership = LauncherOwnership {
                owned: true,
                path: self.command_path.to_string_lossy().into_owned(),
                sha256: launcher.sha256,
                size: launcher.size,
                protocol: launcher.protocol,
            };
            self.verify_installed_launcher_record(&ownership)?;
            Ok(Some(ownership))
        }
    }

    pub(super) fn verify_installed_launcher(&self, install: &InstallState) -> Result<()> {
        #[cfg(unix)]
        {
            let _ = install;
            Ok(())
        }
        #[cfg(windows)]
        {
            let launcher = install.launcher.as_ref().ok_or_else(|| {
                InstallError::Invalid(
                    "managed Windows installation lacks launcher ownership".into(),
                )
            })?;
            self.verify_installed_launcher_record(launcher)
        }
    }

    #[cfg(windows)]
    pub(super) fn verify_installed_launcher_record(
        &self,
        launcher: &LauncherOwnership,
    ) -> Result<()> {
        if !launcher.owned
            || launcher.protocol != 1
            || launcher.path != self.command_path.to_string_lossy()
        {
            return Err(InstallError::Invalid(
                "Windows launcher ownership/protocol metadata is invalid".into(),
            ));
        }
        executable::ensure_regular_file(&self.command_path)?;
        verify_file_digest(
            &self.command_path,
            launcher.size,
            &launcher.sha256,
            "installed launcher",
        )
    }
}

/// An immutable version directory contains exactly the signed payload, its signed metadata, and
/// the local version state.
fn validate_exact_version_members(app: &App, dir: &Path, target: ReleaseTarget) -> Result<()> {
    let payload_unix = app.payload_name(false);
    let payload_windows = app.payload_name(true);
    let metadata_members = [MANIFEST_FILE, SIGNATURE_FILE, VERSION_FILE];
    for entry in fs::read_dir(dir)? {
        let path = entry?.path();
        let name = path
            .file_name()
            .and_then(|name| name.to_str())
            .ok_or_else(|| InstallError::Invalid("version member is not UTF-8".into()))?;
        let allowed = if target.is_windows() {
            name == payload_windows || metadata_members.contains(&name)
        } else {
            name == payload_unix || metadata_members.contains(&name)
        };
        if !allowed {
            return Err(InstallError::Invalid(format!(
                "unexpected immutable version member: {name}"
            )));
        }
    }
    let payload = if target.is_windows() {
        payload_windows.as_str()
    } else {
        payload_unix.as_str()
    };
    for name in [payload, MANIFEST_FILE, SIGNATURE_FILE, VERSION_FILE] {
        if !lexists(&dir.join(name))? {
            return Err(InstallError::Invalid(format!(
                "missing immutable version member: {name}"
            )));
        }
    }
    Ok(())
}