From 99f8503a43e14dc8c9246b9761f54b670b5fd2dd Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:51:32 -0300 Subject: [PATCH 1/6] Keep the old program files when putting them back fails, and say where they are --- .github/workflows/build.yml | 2 +- installer/core/src/fsops.rs | 66 +++++++++++++++++++++++++++++++++++-- installer/core/src/ops.rs | 17 ++++++++-- installer/ui/i18n.js | 2 ++ 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2e0eabb..baf49fe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -213,7 +213,7 @@ jobs: env: IIW_EXPECT_NATIVE_ARCH: x64 run: | - build/setup-tests/iiw-setup-core-tests.exe arch:: release:: package:: manifest:: version:: appinstaller:: ops:: win:: + build/setup-tests/iiw-setup-core-tests.exe arch:: release:: package:: manifest:: version:: appinstaller:: ops:: win:: fsops:: - name: Setup UI tests run: node --test installer/ui-tests/request-coordinator.test.js diff --git a/installer/core/src/fsops.rs b/installer/core/src/fsops.rs index ea622f4..f104b5a 100644 --- a/installer/core/src/fsops.rs +++ b/installer/core/src/fsops.rs @@ -170,8 +170,12 @@ impl Swap { match result { Ok(()) => Ok(swap), Err(e) => { - swap.rollback(); - Err(e) + let problems = swap.rollback(); + if problems.is_empty() { + Err(e) + } else { + Err(io::Error::new(e.kind(), format!("{e}; undoing it failed: {}", problems.join("; ")))) + } } } } @@ -191,7 +195,11 @@ impl Swap { problems.push(format!("{}: {e}", self.target.join(&name).display())); } } - let _ = remove_any(&self.previous); + if problems.is_empty() { + let _ = remove_any(&self.previous); + } else { + problems.push(format!("the old files that didn't go back are in {}", self.previous.display())); + } problems } @@ -255,6 +263,58 @@ mod tests { remove_any(&d).unwrap(); } + #[test] + fn rollback_keeps_previous_when_a_file_cant_go_back() { + let d = tmp("rollback-kept"); + let target = d.join("ii-windows"); + let previous = d.join("previous"); + write(&target.join("qsw.exe"), "old"); + write(&target.join("stale").join("old.dll"), "old"); + let staging = d.join("staging"); + write(&staging.join("qsw.exe"), "new"); + + let mut swap = Swap::run(&staging, &target, &previous, &[]).unwrap(); + write(&target.join("stale").join("in-the-way.txt"), "occupied"); + let problems = swap.rollback(); + assert!(problems.iter().any(|p| p.contains(&previous.display().to_string())), "{problems:?}"); + assert_eq!(std::fs::read_to_string(previous.join("stale").join("old.dll")).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(target.join("qsw.exe")).unwrap(), "old"); + remove_any(&d).unwrap(); + } + + #[cfg(windows)] + #[test] + fn locked_file_during_swap_keeps_everything() { + use std::os::windows::fs::OpenOptionsExt; + let d = tmp("swap-locked"); + let target = d.join("ii-windows"); + let previous = d.join("previous"); + write(&target.join("a.txt"), "old a"); + write(&target.join("qsw.exe"), "old"); + write(&target.join("z").join("locked.dll"), "old z"); + let staging = d.join("staging"); + write(&staging.join("a.txt"), "new a"); + write(&staging.join("qsw.exe"), "new"); + write(&staging.join("z").join("locked.dll"), "new z"); + + let lock = + std::fs::OpenOptions::new().read(true).share_mode(0).open(target.join("z").join("locked.dll")).unwrap(); + assert!(Swap::run(&staging, &target, &previous, &[]).is_err()); + drop(lock); + assert_eq!(std::fs::read_to_string(target.join("a.txt")).unwrap(), "old a"); + assert_eq!(std::fs::read_to_string(target.join("qsw.exe")).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(target.join("z").join("locked.dll")).unwrap(), "old z"); + + let mut swap = Swap::run(&staging, &target, &previous, &[]).unwrap(); + let lock = + std::fs::OpenOptions::new().read(true).share_mode(0).open(target.join("z").join("locked.dll")).unwrap(); + let problems = swap.rollback(); + drop(lock); + assert!(problems.iter().any(|p| p.contains(&previous.display().to_string())), "{problems:?}"); + assert_eq!(std::fs::read_to_string(previous.join("z").join("locked.dll")).unwrap(), "old z"); + remove_any(&d).unwrap(); + } + #[test] fn mirror_keeps_listed_names() { let d = tmp("mirror"); diff --git a/installer/core/src/ops.rs b/installer/core/src/ops.rs index 0ef169c..43339ca 100644 --- a/installer/core/src/ops.rs +++ b/installer/core/src/ops.rs @@ -1872,8 +1872,21 @@ fn swap_in(ctx: &mut Ctx, pkg: &PackageInfo, fresh: bool) -> R<()> { Ok(()) } Err(e) => { - for p in swap.rollback() { - ctx.info(format!(" rollback problem: {p}")); + let problems = swap.rollback(); + if !problems.is_empty() { + let text = problems.join("; "); + ctx.info(format!(" rollback problems: {text}")); + let previous = ctx.paths.previous().display().to_string(); + ctx.note( + Msg::new( + "rollback_kept", + format!( + "Some of the previous program files couldn't be put back; they're in {previous} ({text})" + ), + ) + .with("path", previous) + .with("error", text), + ); } if fresh { let _ = fsops::remove_any(&ctx.paths.install_dir); diff --git a/installer/ui/i18n.js b/installer/ui/i18n.js index 96b5bf0..a7263b3 100644 --- a/installer/ui/i18n.js +++ b/installer/ui/i18n.js @@ -281,6 +281,7 @@ window.I18N = { m_cancelled: "Cancelled. Nothing was changed.", m_files_in_use: "Some program files are in use: {error}", m_files_locked: "Close {apps} and try again.", + m_rollback_kept: "Some of the previous program files couldn't be put back. They're in {path}; copy them back by hand before running the setup again. Details: {error}", m_internal: "Internal error: {error}" }, pt: { @@ -565,6 +566,7 @@ window.I18N = { m_cancelled: "Cancelado. Nada foi mudado.", m_files_in_use: "Alguns arquivos do programa estão em uso: {error}", m_files_locked: "Feche {apps} e tente de novo.", + m_rollback_kept: "Alguns arquivos anteriores do programa não voltaram para o lugar. Eles estão em {path}; copie de volta à mão antes de rodar o instalador de novo. Detalhes: {error}", m_internal: "Erro interno: {error}" } }; From 0454ecaa118f8e4c7cc15aa37e10dd61263ab3da Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:54:17 -0300 Subject: [PATCH 2/6] Stop on an unreadable install manifest and only delete the folder on failure if it started empty --- installer/core/src/fsops.rs | 18 +++++ installer/core/src/ops.rs | 141 ++++++++++++++++++++++++++++-------- installer/ui/i18n.js | 2 + 3 files changed, 130 insertions(+), 31 deletions(-) diff --git a/installer/core/src/fsops.rs b/installer/core/src/fsops.rs index f104b5a..3beca71 100644 --- a/installer/core/src/fsops.rs +++ b/installer/core/src/fsops.rs @@ -113,6 +113,13 @@ pub fn mirror_dir(src: &Path, dst: &Path, keep: &[&str]) -> io::Result<()> { Ok(()) } +pub fn missing_or_empty(dir: &Path) -> bool { + match std::fs::read_dir(dir) { + Ok(mut rd) => rd.next().is_none(), + Err(e) => e.kind() == io::ErrorKind::NotFound, + } +} + pub fn dir_size(path: &Path) -> u64 { let mut total = 0; if let Ok(rd) = std::fs::read_dir(path) { @@ -315,6 +322,17 @@ mod tests { remove_any(&d).unwrap(); } + #[test] + fn missing_or_empty_only_for_folders_without_files() { + let d = tmp("missing-or-empty"); + assert!(missing_or_empty(&d.join("nothing"))); + std::fs::create_dir_all(d.join("empty")).unwrap(); + assert!(missing_or_empty(&d.join("empty"))); + write(&d.join("used").join("restore").join("wall.jpg"), "x"); + assert!(!missing_or_empty(&d.join("used"))); + remove_any(&d).unwrap(); + } + #[test] fn mirror_keeps_listed_names() { let d = tmp("mirror"); diff --git a/installer/core/src/ops.rs b/installer/core/src/ops.rs index 43339ca..81b8712 100644 --- a/installer/core/src/ops.rs +++ b/installer/core/src/ops.rs @@ -277,6 +277,16 @@ fn io_msg(key: &str, what: impl std::fmt::Display, e: impl std::fmt::Display) -> Msg::new(key, format!("{what}: {e}")).with("error", e.to_string()).with("path", what.to_string()) } +fn manifest_unreadable(paths: &Paths, e: &std::io::Error) -> Msg { + let path = paths.manifest().display().to_string(); + Msg::new( + "manifest_unreadable", + format!("Couldn't read {path} ({e}); the setup stopped so it doesn't treat the existing install as a new one"), + ) + .with("path", path) + .with("error", e.to_string()) +} + fn known_folder_checks() -> Vec { let mut out = Vec::new(); for folder in KnownFolder::ALL { @@ -421,32 +431,34 @@ pub fn preflight(paths: &Paths, action: Action, needed: u64, terminal: bool, ffm } } - let manifest = Manifest::load(&paths.manifest()).ok().flatten(); - match (&manifest, action) { - (Some(m), _) => checks.push(check( - "existing", - "info", - Msg::new("installed_version", format!("ii-windows {} is installed", m.version)) - .with("version", m.version.clone()), - )), - (None, Action::Install) if paths.install_dir.join("qsw.exe").exists() => checks.push(check( - "existing", - "warn", - Msg::new( - "unmanaged_install", - format!( - "{} has files from an install this setup didn't make; they will be replaced", - paths.install_dir.display() - ), - ) - .with("path", paths.install_dir.display().to_string()), - )), - (None, Action::Uninstall | Action::Repair | Action::Update) => checks.push(check( - "existing", - "error", - Msg::new("not_installed", "ii-windows isn't installed by this setup (no install manifest)"), - )), - _ => {} + match Manifest::load(&paths.manifest()) { + Err(e) => checks.push(check("existing", "error", manifest_unreadable(paths, &e))), + Ok(manifest) => match (&manifest, action) { + (Some(m), _) => checks.push(check( + "existing", + "info", + Msg::new("installed_version", format!("ii-windows {} is installed", m.version)) + .with("version", m.version.clone()), + )), + (None, Action::Install) if paths.install_dir.join("qsw.exe").exists() => checks.push(check( + "existing", + "warn", + Msg::new( + "unmanaged_install", + format!( + "{} has files from an install this setup didn't make; they will be replaced", + paths.install_dir.display() + ), + ) + .with("path", paths.install_dir.display().to_string()), + )), + (None, Action::Uninstall | Action::Repair | Action::Update) => checks.push(check( + "existing", + "error", + Msg::new("not_installed", "ii-windows isn't installed by this setup (no install manifest)"), + )), + _ => {} + }, } let (mine, others) = ii_processes(paths); @@ -1853,11 +1865,11 @@ pub fn launch_ii_checked(paths: &Paths) -> Result { if ii_shell_running(paths) { Ok(pid) } else { Err(launch_exited_msg(paths)) } } -fn swap_in(ctx: &mut Ctx, pkg: &PackageInfo, fresh: bool) -> R<()> { +fn swap_in(ctx: &mut Ctx, pkg: &PackageInfo, wipe_on_failure: bool) -> R<()> { let mut swap = match install_files(ctx, pkg) { Ok(s) => s, Err(e) => { - if fresh { + if wipe_on_failure { let _ = fsops::remove_any(&ctx.paths.install_dir); } return Err(e); @@ -1888,7 +1900,7 @@ fn swap_in(ctx: &mut Ctx, pkg: &PackageInfo, fresh: bool) -> R<()> { .with("error", text), ); } - if fresh { + if wipe_on_failure { let _ = fsops::remove_any(&ctx.paths.install_dir); } Err(e) @@ -1990,8 +2002,15 @@ fn source_steps(source: &Source) -> Vec<&'static str> { } pub fn install(ctx: &mut Ctx, source: &Source, run: &RunOptions) -> bool { - let existing = Manifest::load(&ctx.paths.manifest()).ok().flatten(); + let existing = match Manifest::load(&ctx.paths.manifest()) { + Ok(m) => m, + Err(e) => { + let m = manifest_unreadable(&ctx.paths, &e); + return ctx.finish(Err(m), false); + } + }; let fresh = existing.is_none(); + let wipe_on_failure = fsops::missing_or_empty(&ctx.paths.install_dir); let previous_opts = existing.as_ref().map(|m| m.options.clone()).unwrap_or_default(); let had_policy = existing.as_ref().and_then(|m| m.items.exec_policy.as_ref()).is_some_and(|c| c.changed); let opts = run.options.clone(); @@ -2018,7 +2037,7 @@ pub fn install(ctx: &mut Ctx, source: &Source, run: &RunOptions) -> bool { if m.pre_install.is_none() { m.pre_install = Some(record_pre_install(ctx)); } - swap_in(ctx, &pkg, fresh)?; + swap_in(ctx, &pkg, wipe_on_failure)?; ctx.log.open(&ctx.paths.log()); m.version = pkg.version.clone(); m.setup_version = ctx.setup_version.clone(); @@ -2646,4 +2665,64 @@ mod tests { record_winget_install(&mut m, "CONTOSO.TOOL", &outcome_installed()); assert_eq!(m.items.winget, vec![original]); } + + struct CancelledReporter; + + impl Reporter for CancelledReporter { + fn emit(&self, _: Event) {} + fn cancelled(&self) -> bool { + true + } + } + + fn tmp(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("iiw-ops-{}-{name}", std::process::id())); + let _ = fsops::remove_any(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + fn write(p: &Path, s: &str) { + std::fs::create_dir_all(p.parent().unwrap()).unwrap(); + std::fs::write(p, s).unwrap(); + } + + fn package(root: &Path) -> PackageInfo { + use std::io::Write; + let path = root.join("package.zip"); + let mut w = zip::ZipWriter::new(std::fs::File::create(&path).unwrap()); + w.start_file("qsw.exe", zip::write::SimpleFileOptions::default()).unwrap(); + w.write_all(b"new").unwrap(); + w.finish().unwrap(); + PackageInfo { + path, + version: "0.0.0".into(), + file_size: 0, + unpacked_size: 3, + files: 1, + prefix: String::new(), + has_setup_exe: false, + arch: Arch::X64, + } + } + + #[test] + fn failed_install_keeps_a_folder_that_had_files() { + let root = tmp("keep-existing"); + let paths = + Paths::from_roots(&root.join("local"), &root.join("roaming"), &root.join("temp"), &root.join("start")); + write(&paths.install_dir.join("qsw.exe"), "old"); + write(&paths.restore_dir().join("wallpaper-0.jpg"), "backup"); + let pkg = package(&root); + let log = Log::new(Box::new(|_| {})); + let reporter = CancelledReporter; + let mut ctx = Ctx::new(paths.clone(), &reporter, &log, root.join("scratch"), root.join("setup.exe"), "test"); + + let wipe = fsops::missing_or_empty(&paths.install_dir); + assert!(!wipe); + assert!(swap_in(&mut ctx, &pkg, wipe).is_err()); + assert_eq!(std::fs::read_to_string(paths.install_dir.join("qsw.exe")).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(paths.restore_dir().join("wallpaper-0.jpg")).unwrap(), "backup"); + fsops::remove_any(&root).unwrap(); + } } diff --git a/installer/ui/i18n.js b/installer/ui/i18n.js index a7263b3..901f61b 100644 --- a/installer/ui/i18n.js +++ b/installer/ui/i18n.js @@ -234,6 +234,7 @@ window.I18N = { m_winget_installed: "winget (App Installer) was installed", m_installed_version: "ii-windows {version} is installed", m_unmanaged_install: "{path} has files from an install this setup didn't make; they will be replaced.", + m_manifest_unreadable: "Couldn't read {path} ({error}). The setup stopped so it doesn't treat your install as a new one and delete it on a failure. Fix or move that file, then run the setup again.", m_not_installed: "ii-windows isn't installed by this setup.", m_running_will_stop: "illogical-impulse is running and will be closed first.", m_running_elsewhere: "Another copy of Quickshell is running from {path}. Close it first: while ii runs, the taskbar settings the setup records are ii's, not yours.", @@ -519,6 +520,7 @@ window.I18N = { m_winget_installed: "O winget (Instalador de Aplicativo) foi instalado", m_installed_version: "O ii-windows {version} está instalado", m_unmanaged_install: "{path} tem arquivos de uma instalação que este instalador não fez; eles serão substituídos.", + m_manifest_unreadable: "Não foi possível ler {path} ({error}). O instalador parou para não tratar a sua instalação como nova e apagá-la se algo falhar. Corrija ou mova esse arquivo e rode o instalador de novo.", m_not_installed: "O ii-windows não foi instalado por este instalador.", m_running_will_stop: "O illogical-impulse está aberto e será fechado antes.", m_running_elsewhere: "Outra cópia do Quickshell está rodando a partir de {path}. Feche-a antes: enquanto o ii roda, as configurações da barra de tarefas que o instalador anota são as do ii, não as suas.", From 13e2b81290d74e09d302206426a08bf6edc224c9 Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:55:50 -0300 Subject: [PATCH 3/6] Put the settings back when a repair fails after resetting them --- installer/core/src/fsops.rs | 85 +++++++++++++++++++++++++++++++++++++ installer/core/src/ops.rs | 69 +++++++++++++++++------------- installer/ui/i18n.js | 2 + 3 files changed, 127 insertions(+), 29 deletions(-) diff --git a/installer/core/src/fsops.rs b/installer/core/src/fsops.rs index 3beca71..f251cf5 100644 --- a/installer/core/src/fsops.rs +++ b/installer/core/src/fsops.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::ffi::OsString; use std::io; use std::path::{Path, PathBuf}; use std::time::Duration; @@ -113,6 +114,34 @@ pub fn mirror_dir(src: &Path, dst: &Path, keep: &[&str]) -> io::Result<()> { Ok(()) } +pub fn move_json_files(from: &Path, to: &Path, moved: &mut Vec) -> Result<(), (PathBuf, io::Error)> { + let Ok(rd) = std::fs::read_dir(from) else { + return Ok(()); + }; + for e in rd.flatten() { + let p = e.path(); + if !p.is_file() || !p.extension().is_some_and(|x| x.eq_ignore_ascii_case("json")) { + continue; + } + std::fs::create_dir_all(to) + .and_then(|_| std::fs::copy(&p, to.join(e.file_name()))) + .and_then(|_| std::fs::remove_file(&p)) + .map_err(|err| (p.clone(), err))?; + moved.push(e.file_name()); + } + Ok(()) +} + +pub fn copy_back(from: &Path, to: &Path, names: &[OsString]) -> io::Result<()> { + let mut first = None; + for name in names { + if let Err(e) = std::fs::copy(from.join(name), to.join(name)) { + first.get_or_insert(e); + } + } + first.map_or(Ok(()), Err) +} + pub fn missing_or_empty(dir: &Path) -> bool { match std::fs::read_dir(dir) { Ok(mut rd) => rd.next().is_none(), @@ -322,6 +351,62 @@ mod tests { remove_any(&d).unwrap(); } + fn read(p: &Path) -> String { + std::fs::read_to_string(p).unwrap() + } + + #[test] + fn settings_move_out_and_come_back() { + let d = tmp("settings-back"); + let settings = d.join("illogical-impulse"); + let backup = settings.join("backups").join("repair-1"); + write(&settings.join("config.json"), "config"); + write(&settings.join("todo.json"), "todo"); + write(&settings.join("notes.txt"), "notes"); + let mut moved = Vec::new(); + move_json_files(&settings, &backup, &mut moved).unwrap(); + assert_eq!(moved.len(), 2); + assert!(!settings.join("config.json").exists()); + assert_eq!(read(&settings.join("notes.txt")), "notes"); + copy_back(&backup, &settings, &moved).unwrap(); + assert_eq!(read(&settings.join("config.json")), "config"); + assert_eq!(read(&settings.join("todo.json")), "todo"); + remove_any(&d).unwrap(); + } + + #[test] + fn settings_failure_in_the_middle_puts_back_what_moved() { + let d = tmp("settings-middle"); + let settings = d.join("illogical-impulse"); + let backup = settings.join("backups").join("repair-1"); + for name in ["a.json", "b.json", "c.json"] { + write(&settings.join(name), name); + } + std::fs::create_dir_all(backup.join("b.json")).unwrap(); + let mut moved = Vec::new(); + let (failed, _) = move_json_files(&settings, &backup, &mut moved).unwrap_err(); + assert_eq!(failed, settings.join("b.json")); + copy_back(&backup, &settings, &moved).unwrap(); + for name in ["a.json", "b.json", "c.json"] { + assert_eq!(read(&settings.join(name)), name); + } + remove_any(&d).unwrap(); + } + + #[test] + fn settings_copy_back_reports_a_failure() { + let d = tmp("settings-stuck"); + let settings = d.join("illogical-impulse"); + let backup = settings.join("backups").join("repair-1"); + write(&settings.join("config.json"), "config"); + let mut moved = Vec::new(); + move_json_files(&settings, &backup, &mut moved).unwrap(); + std::fs::create_dir_all(settings.join("config.json")).unwrap(); + assert!(copy_back(&backup, &settings, &moved).is_err()); + assert_eq!(read(&backup.join("config.json")), "config"); + remove_any(&d).unwrap(); + } + #[test] fn missing_or_empty_only_for_folders_without_files() { let d = tmp("missing-or-empty"); diff --git a/installer/core/src/ops.rs b/installer/core/src/ops.rs index 81b8712..1105982 100644 --- a/installer/core/src/ops.rs +++ b/installer/core/src/ops.rs @@ -2151,37 +2151,22 @@ pub fn repair(ctx: &mut Ctx, source: &Source, run: &RunOptions) -> bool { ctx.begin("settings"); let backup = ctx.paths.settings.join("backups").join(format!("repair-{}", log::stamp())); - let mut moved = 0; - if let Ok(rd) = std::fs::read_dir(&ctx.paths.settings) { - for e in rd.flatten() { - let p = e.path(); - let is_json = p.is_file() && p.extension().is_some_and(|x| x.eq_ignore_ascii_case("json")); - if !is_json { - continue; - } - let r = std::fs::create_dir_all(&backup) - .and_then(|_| std::fs::copy(&p, backup.join(e.file_name()))) - .and_then(|_| std::fs::remove_file(&p)); - match r { - Ok(()) => moved += 1, - Err(err) => { - let m = io_msg("settings_failed", p.display(), err); - ctx.fail("settings", &m); - return Err(m); - } - } - } + let mut moved = Vec::new(); + if let Err((p, err)) = fsops::move_json_files(&ctx.paths.settings, &backup, &mut moved) { + let m = io_msg("settings_failed", p.display(), err); + put_settings_back(ctx, &backup, &moved); + ctx.fail("settings", &m); + return Err(m); } - if moved > 0 { + if !moved.is_empty() { ctx.done_with( "settings", - Msg::new("settings_backed_up", format!("{moved} settings file(s) moved to {}", backup.display())) - .with("count", moved.to_string()) - .with("path", backup.display().to_string()), - ); - ctx.note( - Msg::new("repair_backup", format!("Your previous settings are in {}", backup.display())) - .with("path", backup.display().to_string()), + Msg::new( + "settings_backed_up", + format!("{} settings file(s) moved to {}", moved.len(), backup.display()), + ) + .with("count", moved.len().to_string()) + .with("path", backup.display().to_string()), ); } else { ctx.skip("settings", Msg::new("no_settings", "no settings files to reset")); @@ -2192,7 +2177,16 @@ pub fn repair(ctx: &mut Ctx, source: &Source, run: &RunOptions) -> bool { ctx.log.close(); let r = swap_in(ctx, &pkg, false); ctx.log.open(&ctx.paths.log()); - r?; + if let Err(e) = r { + put_settings_back(ctx, &backup, &moved); + return Err(e); + } + if !moved.is_empty() { + ctx.note( + Msg::new("repair_backup", format!("Your previous settings are in {}", backup.display())) + .with("path", backup.display().to_string()), + ); + } m.version = pkg.version.clone(); m.setup_version = ctx.setup_version.clone(); m.arch = Some(pkg.arch); @@ -2217,6 +2211,23 @@ pub fn repair(ctx: &mut Ctx, source: &Source, run: &RunOptions) -> bool { ctx.finish(result, !run.launch) } +fn put_settings_back(ctx: &mut Ctx, backup: &Path, names: &[std::ffi::OsString]) { + if names.is_empty() { + return; + } + match fsops::copy_back(backup, &ctx.paths.settings, names) { + Ok(()) => ctx.info(format!(" settings put back from {}", backup.display())), + Err(e) => ctx.note( + Msg::new( + "settings_not_restored", + format!("Couldn't put your settings back ({e}); they're in {}", backup.display()), + ) + .with("path", backup.display().to_string()) + .with("error", e.to_string()), + ), + } +} + fn schedule_leftovers(ctx: &mut Ctx, files: &[PathBuf]) { if files.is_empty() { return; diff --git a/installer/ui/i18n.js b/installer/ui/i18n.js index 901f61b..b2df590 100644 --- a/installer/ui/i18n.js +++ b/installer/ui/i18n.js @@ -273,6 +273,7 @@ window.I18N = { m_fonts_in_use: "{count} font file(s) were in use; they're deleted after this window closes", m_restart_terminal: "Open a new terminal window to see the new prompt and colors.", m_repair_backup: "Your previous settings are in {path}", + m_settings_not_restored: "Couldn't put your settings back ({error}). They're in {path}; copy the files from there to the folder above it.", m_profile_backup: "Copies of your PowerShell profiles from before the change: {path}", m_uninstall_log: "The uninstall log is {path}", m_setup_log: "The log of this run is {path}", @@ -559,6 +560,7 @@ window.I18N = { m_fonts_in_use: "{count} arquivo(s) de fonte estavam em uso; eles são apagados depois que esta janela fechar", m_restart_terminal: "Abra uma nova janela de terminal para ver o novo prompt e as cores.", m_repair_backup: "As suas configurações anteriores estão em {path}", + m_settings_not_restored: "Não foi possível devolver as suas configurações ({error}). Elas estão em {path}; copie os arquivos de lá para a pasta acima dela.", m_profile_backup: "Cópias dos seus perfis do PowerShell de antes da mudança: {path}", m_uninstall_log: "O log da desinstalação é {path}", m_setup_log: "O log desta execução é {path}", From 63f0f25814f693ded8ed08b7475268f6fa231338 Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:56:07 -0300 Subject: [PATCH 4/6] Look for the terminal tools and FFmpeg on the PATH before asking winget --- installer/core/src/ops.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/installer/core/src/ops.rs b/installer/core/src/ops.rs index 1105982..e7d9a46 100644 --- a/installer/core/src/ops.rs +++ b/installer/core/src/ops.rs @@ -1448,8 +1448,8 @@ fn install_tools(ctx: &mut Ctx, m: &mut Manifest) { let total = crate::TERMINAL_TOOLS.len(); for (i, (id, cmd)) in crate::TERMINAL_TOOLS.iter().enumerate() { ctx.progress("tools", i as f64 / total as f64, Some((*id).to_string())); - let listed = win::winget_installed(&winget, id); let on_path = win::find_on_path(&format!("{cmd}.exe")); + let listed = if on_path.is_some() { None } else { win::winget_installed(&winget, id) }; if listed == Some(true) || on_path.is_some() { ctx.info(format!( " {id}: already installed ({})", @@ -1546,8 +1546,8 @@ fn install_ffmpeg(ctx: &mut Ctx, m: &mut Manifest) { ); return; }; - let listed = win::winget_installed(&winget, crate::FFMPEG_WINGET_ID); let on_path = win::find_on_path(&format!("{}.exe", crate::FFMPEG_CMD)); + let listed = if on_path.is_some() { None } else { win::winget_installed(&winget, crate::FFMPEG_WINGET_ID) }; if listed == Some(true) || on_path.is_some() { ctx.skip( "ffmpeg", From 895cd8e09191455c5b9371722e880ec6488ceec0 Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:56:46 -0300 Subject: [PATCH 5/6] Don't wait forever for the output of a program that left a child holding its pipes --- installer/core/src/win.rs | 46 +++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/installer/core/src/win.rs b/installer/core/src/win.rs index abd7366..5dd004b 100644 --- a/installer/core/src/win.rs +++ b/installer/core/src/win.rs @@ -266,18 +266,8 @@ pub fn run(exe: &Path, args: &[&str], timeout: Duration) -> std::io::Result std::io::Result std::sync::mpsc::Receiver> { + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let mut v = Vec::new(); + let _ = pipe.read_to_end(&mut v); + let _ = tx.send(v); + }); + rx +} + pub fn fresh_path_dirs() -> Vec { let mut dirs = Vec::new(); let expand = |s: String| -> String { @@ -1165,6 +1167,22 @@ mod tests { assert_eq!(super::ps_quote("C:\\Users\\O'Brien\\file.exe"), "'C:\\Users\\O''Brien\\file.exe'"); } + #[test] + fn run_returns_when_a_grandchild_keeps_the_pipe_open() { + let cmd = std::path::PathBuf::from( + std::env::var("ComSpec").unwrap_or_else(|_| r"C:\Windows\System32\cmd.exe".into()), + ); + let start = std::time::Instant::now(); + let out = super::run( + &cmd, + &["/c", "echo", "ready", "&", "start", "/b", "ping", "-n", "30", "127.0.0.1"], + std::time::Duration::from_secs(10), + ) + .unwrap(); + assert!(start.elapsed() < std::time::Duration::from_secs(15), "took {:?}", start.elapsed()); + assert!(!out.timed_out); + } + fn output(code: i32) -> super::Output { super::Output { code: Some(code), stdout: String::new(), stderr: String::new(), timed_out: false } } From 173bd81ebb375e37f851cb6c313dabe6e09dc315 Mon Sep 17 00:00:00 2001 From: nunreasonable Date: Sun, 8 Nov 2026 19:57:20 -0300 Subject: [PATCH 6/6] Give downloads a time limit for the body that still fits slow connections --- installer/core/src/release.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/installer/core/src/release.rs b/installer/core/src/release.rs index fda6103..04cbf9d 100644 --- a/installer/core/src/release.rs +++ b/installer/core/src/release.rs @@ -62,6 +62,7 @@ fn agent() -> ureq::Agent { ureq::Agent::config_builder() .timeout_connect(Some(Duration::from_secs(15))) .timeout_recv_response(Some(Duration::from_secs(30))) + .timeout_recv_body(Some(Duration::from_secs(120))) .user_agent(concat!("ii-windows-setup/", env!("CARGO_PKG_VERSION"))) .http_status_as_error(false) .build() @@ -241,6 +242,11 @@ pub fn sha256_file(path: &Path) -> std::io::Result { Ok(hex(&h.finalize())) } +fn body_budget(expected_size: u64) -> Duration { + let size = if expected_size > 0 { expected_size } else { 1 << 30 }; + Duration::from_secs(10 * 60 + size / (16 << 10)) +} + fn hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() } @@ -261,7 +267,13 @@ pub fn download( progress: &mut dyn FnMut(u64, u64), cancel: &dyn Fn() -> bool, ) -> Result { - let mut resp = agent().get(url).call().map_err(|e| Error::Network(e.to_string()))?; + let mut resp = agent() + .get(url) + .config() + .timeout_recv_body(Some(body_budget(expected_size))) + .build() + .call() + .map_err(|e| Error::Network(e.to_string()))?; let status = resp.status().as_u16(); if status != 200 { return Err(Error::Network(format!("HTTP {status} downloading {url}"))); @@ -486,4 +498,13 @@ mod tests { assert_eq!(sha256_file(&p).unwrap(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); std::fs::remove_file(&p).unwrap(); } + + #[test] + fn body_budget_allows_slow_downloads() { + let package = 79 << 20; + assert!(body_budget(package) >= Duration::from_secs(package / (16 << 10))); + assert!(body_budget(package) > Duration::from_secs(90 * 60)); + assert!(body_budget(250 << 20) > body_budget(package)); + assert_eq!(body_budget(0), body_budget(1 << 30)); + } }