From 2edeb775ab2945d74f1f7c3177d2554340622c5e Mon Sep 17 00:00:00 2001 From: fix2it Date: Tue, 17 Nov 2026 19:30:19 +0200 Subject: [PATCH 1/2] fix(updater): never delete the only copy of the installed exe replace_target_binary removed the backup at the start of every attempt. When the copy failed and restoring the backup failed too, the backup was the only copy of the program, and the next attempt deleted it. Drop the backup only while the target still exists, stop with an error that names the backup path when it does not, and return at once when the restore fails. Retries and delay are parameters so tests run fast. Co-Authored-By: Claude Opus 5.5 --- src/updater.rs | 39 +++++++++++++++++++++++++++++++++------ src/updater/tests.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/updater.rs b/src/updater.rs index 97c166dd..c17af6fa 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -283,18 +283,38 @@ fn build_agent() -> Result { } fn replace_target_binary(target: &Path, source: &Path) -> Result<(), String> { + replace_target_binary_with_retries(target, source, 60, Duration::from_millis(500)) +} + +fn replace_target_binary_with_retries( + target: &Path, + source: &Path, + attempts: u32, + delay: Duration, +) -> Result<(), String> { let backup_path = backup_path_for(target); let mut last_error = None; - for _ in 0..60 { - let _ = std::fs::remove_file(&backup_path); + for _ in 0..attempts { + // The backup may be the only copy of the installed exe (a failed + // restore, or an earlier interrupted update); drop it only while the + // target itself is still in place. + if target.exists() { + let _ = std::fs::remove_file(&backup_path); + } else if backup_path.exists() { + return Err(format!( + "Unable to replace {}. The previous version is kept at {}.", + target.display(), + backup_path.display() + )); + } let renamed_existing = match std::fs::rename(target, &backup_path) { Ok(()) => true, Err(error) if error.kind() == io::ErrorKind::NotFound => false, Err(error) => { last_error = Some(error); - std::thread::sleep(Duration::from_millis(500)); + std::thread::sleep(delay); continue; } }; @@ -305,15 +325,22 @@ fn replace_target_binary(target: &Path, source: &Path) -> Result<(), String> { return Ok(()); } Err(error) => { - last_error = Some(error); let _ = std::fs::remove_file(target); if renamed_existing { - let _ = std::fs::rename(&backup_path, target); + if let Err(restore_error) = std::fs::rename(&backup_path, target) { + return Err(format!( + "Unable to replace {}: {error}. Restoring the previous version \ + failed ({restore_error}); it is kept at {}.", + target.display(), + backup_path.display() + )); + } } + last_error = Some(error); } } - std::thread::sleep(Duration::from_millis(500)); + std::thread::sleep(delay); } Err(format!( diff --git a/src/updater/tests.rs b/src/updater/tests.rs index a62d2502..104b7f70 100644 --- a/src/updater/tests.rs +++ b/src/updater/tests.rs @@ -90,6 +90,35 @@ fn verified_source_can_replace_the_installed_binary() { assert!(!backup_path_for(&target).exists()); } +#[test] +fn failed_replacement_never_deletes_the_only_copy_in_the_backup() { + let dir = tempfile::tempdir().unwrap(); + let source = dir.path().join("missing-download.exe"); + let target = dir.path().join("app.exe"); + let backup = backup_path_for(&target); + // A previous restore failed: the installed exe survives only as the backup. + std::fs::write(&backup, b"original").unwrap(); + + let error = + replace_target_binary_with_retries(&target, &source, 3, Duration::ZERO).unwrap_err(); + + assert!(error.contains(&backup.display().to_string()), "{error}"); + assert_eq!(std::fs::read(&backup).unwrap(), b"original"); +} + +#[test] +fn failed_copy_restores_the_installed_binary() { + let dir = tempfile::tempdir().unwrap(); + let source = dir.path().join("missing-download.exe"); + let target = dir.path().join("app.exe"); + std::fs::write(&target, b"original").unwrap(); + + replace_target_binary_with_retries(&target, &source, 2, Duration::ZERO).unwrap_err(); + + assert_eq!(std::fs::read(&target).unwrap(), b"original"); + assert!(!backup_path_for(&target).exists()); +} + #[test] fn updater_agent_rejects_plain_http() { let error = build_agent() From 72e3630355da801854965620ba3bff2625b3d943 Mon Sep 17 00:00:00 2001 From: fix2it Date: Tue, 17 Nov 2026 19:30:19 +0200 Subject: [PATCH 2/2] fix(window): release the taskbar lock when a locked widget is dropped on the desktop With "Lock in taskbar" on, dropping the widget outside the taskbar re-embedded the drag popup into the taskbar. The window fell to the bottom of the z-order and disappeared behind other windows. A drop on the desktop now turns the lock off (only when it applies to the current placement) and takes the normal floating path, so the widget stays visible and the setting and menu check follow. Co-Authored-By: Claude Opus 5.5 --- src/window.rs | 12 ++++++++ src/window/message_loop.rs | 39 +++++------------------- src/window/placement_regression_tests.rs | 20 ++++++++++++ 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/window.rs b/src/window.rs index 9099d1a1..33a1ae4e 100644 --- a/src/window.rs +++ b/src/window.rs @@ -3506,6 +3506,18 @@ fn taskbar_lock_applies(state: &AppState) -> bool { } } +/// A locked taskbar widget dragged off the taskbar turns the lock off, so the +/// drop lands as an ordinary floating widget. A widget whose placement the +/// lock does not cover (already floating) keeps the user's preference. +/// Returns whether the lock was released. +fn release_taskbar_lock_for_desktop_drop(state: &mut AppState) -> bool { + if !taskbar_lock_applies(state) { + return false; + } + state.lock_taskbar = false; + true +} + fn clear_locked_auto_ejection(state: &mut AppState) -> bool { if !taskbar_lock_applies(state) || !state.auto_ejected { return false; diff --git a/src/window/message_loop.rs b/src/window/message_loop.rs index d49015dd..31cdc731 100644 --- a/src/window/message_loop.rs +++ b/src/window/message_loop.rs @@ -541,37 +541,14 @@ pub(super) unsafe extern "system" fn wnd_proc( position_at_taskbar(); render_layered(); } else { - // Locked widgets cannot be dropped as floating: return them - // to the bar so they can only be moved along it. - let locked = lock_state().as_ref().is_some_and(taskbar_lock_applies); - if locked { - let taskbar = { - let mut state = lock_state(); - state.as_mut().and_then(|s| { - s.embedded = true; - s.is_switching_window_style = true; - s.is_snapped = false; - s.auto_ejected = false; - s.auto_ejected_for_capacity = false; - s.auto_ejected_origin = None; - s.auto_ejected_host = None; - // The drag leaves the configured taskbar host - // and saved dock placement unchanged. - s.taskbar_hwnd.map(|h| h.to_hwnd()) - }) - }; - // SetParent/style changes can synchronously re-enter - // wnd_proc. Release STATE before calling Win32. - if let Some(taskbar) = taskbar { - native_interop::embed_as_child(hwnd, taskbar); - } - if let Some(s) = lock_state().as_mut() { - s.is_switching_window_style = false; - } - save_state_settings(); - position_at_taskbar(); - render_layered(); - return LRESULT(0); + // Dropping a locked widget onto the desktop releases the lock: + // re-embedding it from the drag popup left it at the bottom of + // the z-order, hidden behind other windows and the desktop. + let unlocked = lock_state() + .as_mut() + .is_some_and(release_taskbar_lock_for_desktop_drop); + if unlocked { + diagnose::log("taskbar lock released by drop on desktop"); } let displays = native_interop::find_monitors(); let (monitor_idx, display) = positioning::monitor_for_point(&displays, pt); diff --git a/src/window/placement_regression_tests.rs b/src/window/placement_regression_tests.rs index 5e7dec5d..cb6c3370 100644 --- a/src/window/placement_regression_tests.rs +++ b/src/window/placement_regression_tests.rs @@ -1084,3 +1084,23 @@ fn collision_fixture( reserved: tray.into_iter().collect(), } } + +#[test] +fn desktop_drop_releases_taskbar_lock() { + let mut state = state_for(ThemeDocument::starter(), placement("taskbar")); + state.lock_taskbar = true; + assert!(taskbar_lock_applies(&state)); + assert!(release_taskbar_lock_for_desktop_drop(&mut state)); + assert!(!state.lock_taskbar); + assert!(!taskbar_lock_applies(&state)); + // An unlocked drop leaves the setting alone. + assert!(!release_taskbar_lock_for_desktop_drop(&mut state)); + assert!(!state.lock_taskbar); + + // Moving an already-floating widget keeps the lock preference for a + // later dock. + let mut floating = state_for(ThemeDocument::starter(), placement("floating")); + floating.lock_taskbar = true; + assert!(!release_taskbar_lock_for_desktop_drop(&mut floating)); + assert!(floating.lock_taskbar); +}