isothrum_machine/install/exit_codes.rs
1//! What a Windows install's exit codes mean: winget's own result codes and the codes an installer
2//! it ran exits with (a declined UAC prompt, an MSI its user backed out of), as codes a UI words.
3//! The raw codes stay in the log.
4
5/// winget's "the installer failed" code: what went wrong is the installer's own exit code, which
6/// winget prints ("Installer failed with exit code: …").
7const WINGET_INSTALLER_FAILED: u32 = 0x8A15_0006;
8/// winget's "no applicable update": the package is installed already, at its latest version.
9const WINGET_ALREADY_INSTALLED: u32 = 0x8A15_002B;
10/// winget's "a restart is required to finish": the install (or removal) itself went through.
11const WINGET_RESTART_TO_FINISH: u32 = 0x8A15_0109;
12
13/// What a failed Windows step's codes say happened.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum Known {
17 /// Its user said no: to the UAC prompt, or to the installer.
18 Cancelled,
19 /// Another installation is already running.
20 AnotherInstall,
21 /// There isn't enough disk space.
22 NoDiskSpace,
23 /// It needs an internet connection.
24 NoInternet,
25 /// Windows must restart first: nothing was installed.
26 RestartFirst,
27 /// A policy on this computer blocks it.
28 Policy,
29 /// winget asks its user to accept the package's agreement, or its source's, which nothing
30 /// accepts for them: they run the same `winget install` themselves, read it and accept it
31 /// there, then check again. Nothing was installed.
32 AgreementToAccept,
33 /// The installer reported an error: the details are in its output.
34 InstallerError,
35}
36
37/// A winget result that isn't a failure although winget exits non-zero.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub enum Done {
41 /// Installed already, at its latest version: nothing to do.
42 AlreadyInstalled,
43 /// Done once Windows restarts.
44 RestartToFinish,
45}
46
47/// What winget's own result codes mean (`APPINSTALLER_CLI_ERROR_*`, documented in winget-cli's
48/// `doc/windows/package-manager/winget/returnCodes.md`).
49fn winget_summary(code: u32) -> Option<Known> {
50 Some(match code {
51 0x8A15_010C => Known::Cancelled,
52 0x8A15_0102 => Known::AnotherInstall,
53 0x8A15_0105 => Known::NoDiskSpace,
54 0x8A15_0107 => Known::NoInternet,
55 0x8A15_010A => Known::RestartFirst,
56 0x8A15_010F => Known::Policy,
57 // APPINSTALLER_CLI_ERROR_PACKAGE_AGREEMENTS_NOT_ACCEPTED, …_SOURCE_AGREEMENTS_NOT_ACCEPTED.
58 0x8A15_0041 | 0x8A15_0046 => Known::AgreementToAccept,
59 WINGET_INSTALLER_FAILED => Known::InstallerError,
60 _ => return None,
61 })
62}
63
64/// winget's results that are done although winget exits non-zero (a restart needed *before*
65/// installing, `0x8A15010A`, is a failure: nothing was installed).
66pub(crate) fn winget_done(code: Option<i32>) -> Option<Done> {
67 match code?.cast_unsigned() {
68 WINGET_ALREADY_INSTALLED => Some(Done::AlreadyInstalled),
69 WINGET_RESTART_TO_FINISH => Some(Done::RestartToFinish),
70 _ => None,
71 }
72}
73
74/// Installer exit codes that mean its user said no (to the UAC prompt or the installer).
75fn installer_cancelled(code: u32) -> bool {
76 matches!(
77 code,
78 1223 // ERROR_CANCELLED: the UAC prompt was declined
79 | 1602 // ERROR_INSTALL_USEREXIT: an MSI cancelled
80 | 0x8007_04C7 // ERROR_CANCELLED as an HRESULT
81 | 0xFFFF_FFFB // Docker Desktop's installer when its UAC prompt is declined
82 )
83}
84
85/// The installer's exit code in a winget output line ("Installer failed with exit code: 1602").
86/// Only winget's English output says it this way; in other languages winget's own code is what is
87/// known.
88pub(crate) fn installer_exit_code(line: &str) -> Option<u32> {
89 let rest = line.trim().strip_prefix("Installer failed with exit code:")?.trim();
90 match rest.strip_prefix("0x").or_else(|| rest.strip_prefix("0X")) {
91 Some(hex) => u32::from_str_radix(hex, 16).ok(),
92 None => rest.parse::<u32>().ok().or_else(|| rest.parse::<i32>().ok().map(i32::cast_unsigned)),
93 }
94}
95
96/// What a failed winget run's codes mean, when they are known.
97pub(crate) fn winget_failure(code: Option<i32>, installer_code: Option<u32>) -> Option<Known> {
98 let code = code?.cast_unsigned();
99 match installer_code {
100 Some(c) if code == WINGET_INSTALLER_FAILED && installer_cancelled(c) => Some(Known::Cancelled),
101 _ => winget_summary(code),
102 }
103}
104
105/// What a failed installer run through PowerShell means, from its own exit code: only a cancel is
106/// known.
107pub(crate) fn installer_failure(code: Option<i32>) -> Option<Known> {
108 installer_cancelled(code?.cast_unsigned()).then_some(Known::Cancelled)
109}
110
111#[cfg(test)]
112mod tests;