Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
feat(updater): add safe manual retry after failure (#4502)
Lizi0f65d982317d
diff --git a/apps/desktop/cindy-updater/README.md b/apps/desktop/cindy-updater/README.mdindex 9fbf65cfa3..13628302d4 100644--- a/apps/desktop/cindy-updater/README.md+++ b/apps/desktop/cindy-updater/README.md@@ -42,6 +42,7 @@ that the Electron main process previously generated in `executeUpdateWindows````cindy-updater.exe \--zip <path-to-downloaded-patch.zip> \+ --zip-sha256 <verified-manifest-sha256> \--app-dir <electron-install-dir> \--exe-name Cindy.exe \--pid <main-process-pid> \@@ -57,7 +58,10 @@ on a light OS who has selected dark mode in Cindy would briefly see alight updater window.The Electron main process owns argument construction; see-`executeUpdateWindows`.+`executeUpdateWindows`. It passes the SHA-256 from the verified update manifest+through `--zip-sha256`. The updater fails closed when that digest is missing or+the archive does not match it, and forwards the same value through elevation+and manual Retry.### Logging@@ -80,6 +84,115 @@ The Electron main process owns argument construction; see5. `done` — `pgrep`-style verification passed; updater self-cleans.6. `failed` — any step bubbled an error; lock file dropped, UI sticks.+### Manual retry after failure++The failure window offers Retry only if the installation was not modified, or+rollback succeeded, and the update zip can be isolated as `retry.zip` in this+attempt's temporary workdir. Moving it out of Electron's staged-patch location+prevents the restored app from automatically applying the same failed archive.+Same-volume isolation uses rename; a cross-volume `EXDEV` / `ERROR_NOT_SAME_DEVICE`+falls back to a digest-checked copy of the still-open source, then deletes the+staged file only after the copy matches. If isolation fails, Retry is unavailable.+A second updater that loses the exclusive `.updating` lock leaves the staged ZIP+in place for the holder; it does not delete the active installation's archive.++Retry is never automatic: Rust rechecks the failure state and readable archive,+then re-runs the installer in the already-loaded process using trusted Rust-owned+arguments. It does not spawn a new updater executable from the `%TEMP%` workdir,+so Windows cannot load a planted `vcruntime140*.dll` beside that path into an+elevated retry. Electron supplies the SHA-256 from the verified update manifest;+Rust verifies it before the first attempt and passes it through elevation and+retry. Extraction hashes and consumes the same protected file handle only after+the digest still matches. A TEMP replacement after failure cannot be installed.+If the isolated archive is later missing, unreadable, or no longer matches, Retry+stays hidden and the failure window only keeps the check-for-updates guidance.+Deterministically malformed or unsupported ZIP formats also do not offer Retry+and the staged archive is deleted so the next Cindy launch downloads a fresh+copy; transient archive I/O failures remain eligible. Only the archive path and+PID change: the retry uses the isolated zip and does not wait on the original,+potentially stale PID. The WebView cannot supply paths or commands.+When the updater is already elevated via `--elevated` after UAC, extract and+backup staging live under the install directory. Classification does not+re-probe writability with that high token: after UAC, Program Files would look+writable and send staging back to the unelevated `%TEMP%` workdir. Inherited+elevation from an elevated Cindy spawn that omitted `--elevated` classifies+with the linked medium-integrity token instead. A writable per-user install+creates `%ProgramData%\cindy-update-{ts}\` with a High integrity DACL at+`CreateDirectoryW` time so a same-login medium process cannot pre-create or+hold a writable handle on that tree before `copy_tree`; a planted directory+or junction is refused. ProgramData is the system known folder, not an+inherited `ProgramData` environment variable. Only that per-attempt private+root uses the High-IL create path; an install directory that happens to live+under ProgramData still uses ordinary `create_dir_all`. A protected Program+Files install still stages next to the app. The archive digest is verified+after the updater window is shown, not before. Stale cleanup under ProgramData+deletes only `cindy-update-{digits}` roots this updater creates; a directory+such as `cindy-update-service` is left alone.+Unelevated per-user installs still probe and stay in TEMP.+Failures before replacement delete those staging directories; only a rollback+that itself failed keeps the backup for manual recovery.+The retry command checks for processes running from the install directory and+asks the user to close them; manual retry never force-terminates those processes.+The first attempt may still request UAC by relaunching this TEMP-copied updater.+The exclusive `.updating` lock is acquired only after that handoff, so the+elevated child can install. Cancelling that prompt, or a Retry that would need a+second `runas`, does not offer Retry and does not relaunch the TEMP executable;+close the window and check for updates again. An already-elevated Retry continues+in-process. Inherited-elevation Retry pins the first medium-integrity+writability of `app_dir` before AppState is cloned, so Retry does not re-probe+a later DACL, and keeps High-IL ProgramData staging even if the install later+looks protected. Windows startup keeps waiting while the `.updating` holder+PID is alive before the 30s timeout, then attempts unlink even if that PID+was recycled. It only keeps waiting after that when unlink fails with a+sharing violation (Retry still owns `FILE_SHARE_READ`); an ACL or third-party+deny-delete error does not hang the pre-window loop. The unelevated parent+forwards `--install-writable true|false` across UAC so the elevated child+does not treat `--elevated` as proof that a per-user install is protected.+A denied write probe still pins the install as writable when the same-login+medium token can modify the directory, an existing `Cindy.exe`, an app-local+DLL, `resources/app.asar`, or an unpacked native addon under+`resources/app.asar.unpacked`, even if Administrators own the tree. Ownership+alone is not enough. A `CreateFile(GENERIC_WRITE)` sharing or lock violation+while Cindy.exe is still mapped is treated as writable, not as a protected+ACL. After Cindy+exits, Retry opens a non-reparse directory handle and writes backup/copy/+rollback through that path, refusing descendant junctions so a swapped+`resources` reparse cannot receive elevated files. Windows startup waits+past 30s on a sharing violation only while the recorded updater PID is+alive; antivirus or other deny-delete handles stop after the timeout.++A successful rollback retains the isolated zip for retry and does not relaunch+Cindy while Retry remains available. Isolation before the outer digest check+is preliminary: if that check later withdraws Retry, the restored Cindy is+relaunched immediately and Close no longer starts a second copy. The exclusive `.updating` lock stays on+disk for that window so a second updater cannot start from `%TEMP%`. Close, or+any other abandoned Retry exit, deletes that file so the next Cindy launch does+not wait 30 seconds, then relaunches the restored Cindy — the concurrency+reason for keeping it closed no longer applies. An elevated updater does not+`CreateProcess` a medium-writable `Cindy.exe` with the high token: Close and+the successful install launch use the linked medium token instead, or skip+relaunch if that token is unavailable. Protected install roots still relaunch+at the current integrity. Retryable failures that never acquired `.updating`+still relaunch if this updater already stopped Cindy. A terminal pre-install+Retry that never rewrote `app_dir` also relaunches on Close even though Retry+is hidden; an inconsistent rollback still does not. A present `.updating`+owned by another process suppresses relaunch — Cindy's 30-second wait can+delete this window's leftover and a later updater may already be replacing+files. A second updater that never owned the lock and never stopped Cindy+does not relaunch or delete it. Close then window+destroy share a one-shot abandon so the restored Cindy is not launched twice.+Clicking Retry hides Close until the worker reports a terminal state, and+Alt+F4 / `CloseRequested` is ignored while that worker is running, so Close+cannot kill an in-flight hash/install. Retry reopens a retained lock only when+the file still names this process.+Failures before UAC on a protected install directory do not offer Retry. A successful update still removes its zip. If rollback fails, Retry+is unavailable and the backup directory is preserved for manual recovery. Stale+temporary directories retain+the existing seven-day cleanup policy.++Only the newly added retry copy has a five-language catalog, selected from the+WebView language with English fallback. Existing updater text remains unchanged.+## Build```@@ -103,6 +216,7 @@ For testing the updater UI without running an actual update, pass dummy args:```cargo run -- \--zip path/to/anything.zip \+ --zip-sha256 <sha256-of-anything.zip> \--app-dir C:\Temp\fake-app \--exe-name notepad.exe \--pid 0 \diff --git a/apps/desktop/cindy-updater/src-tauri/Cargo.lock b/apps/desktop/cindy-updater/src-tauri/Cargo.lockindex 543734a918..2c3cb2d255 100644--- a/apps/desktop/cindy-updater/src-tauri/Cargo.lock+++ b/apps/desktop/cindy-updater/src-tauri/Cargo.lock@@ -396,6 +396,7 @@ dependencies = ["open","serde","serde_json",+ "sha2","sysinfo","tauri","tauri-build",diff --git a/apps/desktop/cindy-updater/src-tauri/Cargo.toml b/apps/desktop/cindy-updater/src-tauri/Cargo.tomlindex 1fc75bc7ce..cf728d2796 100644--- a/apps/desktop/cindy-updater/src-tauri/Cargo.toml+++ b/apps/desktop/cindy-updater/src-tauri/Cargo.toml@@ -24,13 +24,20 @@ chrono = { version = "0.4", default-features = false, features = ["clock"] }anyhow = "1"thiserror = "2"open = "5"+sha2 = "0.10"[target.'cfg(windows)'.dependencies]-# Used by installer::self_elevate to call ShellExecuteExW(verb="runas") and-# trigger the UAC prompt. Minimal feature set — only the three modules whose-# symbols we touch — to keep the release binary lean (we still strip + LTO).+# Used by installer::self_elevate (ShellExecuteExW runas),+# process_is_elevated (OpenProcessToken / TokenElevation), High-IL+# staging ACLs, and CreateProcessWithTokenW de-elevation. Keep the+# feature set to the modules whose symbols we touch.windows-sys = { version = "0.59", features = ["Win32_Foundation",+ "Win32_Security",+ "Win32_Security_Authorization",+ "Win32_Storage_FileSystem",+ "Win32_System_Com",+ "Win32_System_Threading","Win32_UI_Shell","Win32_UI_WindowsAndMessaging",# SHELLEXECUTEINFOW carries an HKEY field (hkeyClass) and so the typediff --git a/apps/desktop/cindy-updater/src-tauri/capabilities/default.json b/apps/desktop/cindy-updater/src-tauri/capabilities/default.jsonindex 8aa464aad1..36f963c6f8 100644--- a/apps/desktop/cindy-updater/src-tauri/capabilities/default.json+++ b/apps/desktop/cindy-updater/src-tauri/capabilities/default.json@@ -1,7 +1,7 @@{"$schema": "../gen/schemas/desktop-schema.json","identifier": "default",- "description": "Allow the updater main window to call our three custom commands.",+ "description": "Allow the updater main window to call its custom commands.","windows": ["main"],"permissions": ["core:default",diff --git a/apps/desktop/cindy-updater/src-tauri/permissions/default.toml b/apps/desktop/cindy-updater/src-tauri/permissions/default.tomlindex 619ffdd8ad..1426c0c7a1 100644--- a/apps/desktop/cindy-updater/src-tauri/permissions/default.toml+++ b/apps/desktop/cindy-updater/src-tauri/permissions/default.toml@@ -15,6 +15,16 @@ identifier = "allow-quit-now"description = "Allow the UI to terminate the updater process."commands.allow = ["quit_now"]+[[permission]]+identifier = "allow-retry-update"+description = "Allow the UI to start a user-requested update retry."+commands.allow = ["retry_update"]+[default]description = "All commands the cindy-updater UI needs."-permissions = ["allow-get-status", "allow-open-log-dir", "allow-quit-now"]+permissions = [+ "allow-get-status",+ "allow-open-log-dir",+ "allow-quit-now",+ "allow-retry-update",+]diff --git a/apps/desktop/cindy-updater/src-tauri/src/args.rs b/apps/desktop/cindy-updater/src-tauri/src/args.rsindex 14d2a1dd2a..ae59687c5d 100644--- a/apps/desktop/cindy-updater/src-tauri/src/args.rs+++ b/apps/desktop/cindy-updater/src-tauri/src/args.rs@@ -58,6 +58,19 @@ pub struct CliArgs {/// only by the updater itself during self-elevation.#[arg(long, default_value_t = false)]pub elevated: bool,++ /// Manifest SHA-256 supplied by the Electron main process. Elevation and+ /// retry pass the same digest so a later replacement of the TEMP file+ /// cannot be extracted by a still-elevated child.+ #[arg(long = "zip-sha256")]+ pub zip_sha256: Option<String>,++ /// First-attempt medium-integrity writability of `app_dir`. Retry must not+ /// re-probe: a same-login process can change the DACL after failure. The+ /// unelevated parent forwards this across UAC so `--elevated` is not treated+ /// as proof that a per-user install is protected.+ #[arg(long = "install-writable", num_args = 0..=1, default_missing_value = "true")]+ pub install_writable: Option<bool>,}#[derive(Copy, Clone, Debug, clap::ValueEnum)]diff --git a/apps/desktop/cindy-updater/src-tauri/src/installer.rs b/apps/desktop/cindy-updater/src-tauri/src/installer.rsindex 7d0ecacade..8b2862de36 100644--- a/apps/desktop/cindy-updater/src-tauri/src/installer.rs+++ b/apps/desktop/cindy-updater/src-tauri/src/installer.rs@@ -1,8 +1,11 @@use std::fs::{self, File};-use std::io;-use std::path::Path;+use std::io::{self, Read, Seek, SeekFrom, Write};+use std::path::{Path, PathBuf};+use std::sync::Mutex;use std::time::{Duration, Instant};+use sha2::{Digest, Sha256};+use serde::Serialize;use sysinfo::System;@@ -32,7 +35,15 @@ pub enum InstallerEvent {Phase(Phase, String),Progress(Phase, String, i32),Done,- Failed(String),+ Failed {+ error: String,+ can_retry: bool,+ install_unmodified: bool,+ install_restored: bool,+ },+ /// Cindy has exited (or this is an in-process Retry that no longer waits).+ /// Abandoning Retry must relaunch even if `.updating` was never acquired.+ AppExited,}const PID_WAIT_TIMEOUT: Duration = Duration::from_secs(60);@@ -50,660 +61,5018 @@ const APPDIR_PROCESS_POLL: Duration = Duration::from_millis(500);const LAUNCH_VERIFY_TIMEOUT: Duration = Duration::from_secs(3);const LAUNCH_VERIFY_POLL: Duration = Duration::from_millis(100);-pub fn run<F: FnMut(InstallerEvent)>(args: CliArgs, mut emit: F) {- match run_inner(&args, &mut emit) {- Ok(()) => emit(InstallerEvent::Done),- Err(err) => {- logger::error(format!("[installer] FAILED: {err}"));- let _ = fs::remove_file(&args.lock);- emit(InstallerEvent::Failed(err.to_string()));- }- }+pub fn run<F: FnMut(InstallerEvent)>(args: CliArgs, emit: F) {+ run_with_lock(args, None, emit);}-fn run_inner<F: FnMut(InstallerEvent)>(- args: &CliArgs,- emit: &mut F,-) -> anyhow::Result<()> {- logger::info(format!(- "[installer] zip={} app_dir={} exe_name={} pid={}",- args.zip.display(),- args.app_dir.display(),- args.exe_name,- args.pid- ));-- // 1. Wait for the main process to exit.- emit(InstallerEvent::Phase(- Phase::Waiting,- format!("等待 PID {} 退出…", args.pid),- ));- let exited = pid_wait::wait_for_exit(args.pid, PID_WAIT_TIMEOUT);- if !exited {- anyhow::bail!("主程序在 60 秒内没有退出,更新中止");+pub(crate) fn run_with_lock<F: FnMut(InstallerEvent)>(+ mut args: CliArgs,+ held_lock: Option<UpdateLock>,+ mut emit: F,+) {+ pin_install_writable(&mut args);+ let lock = held_lock;+ if let Err(error) = bind_zip_sha256(&mut args) {+ logger::error(format!("[installer] FAILED: archive unavailable ({error})"));+ let (extract_dir, backup_dir) = staging_dirs(&args);+ remove_pre_install_staging(&extract_dir, &backup_dir);+ discard_staged_archives(&args);+ if let Some(lock) = lock {+ release_update_lock(lock);+ }+ // Electron already force-quit Cindy before this process started. This+ // path never reaches run_inner's PID wait, so Close would otherwise+ // leave the unmodified install shut down. Always de-elevate: the root+ // is not pinned yet, and a medium-writable parent can swap Cindy.exe.+ relaunch_unmodified_app_after_early_failure(&args);+ emit(InstallerEvent::Failed {+ error: "更新文件已不存在或无法读取,请重新检查更新".into(),+ can_retry: false,+ install_unmodified: false,+ install_restored: false,+ });+ return;}- std::thread::sleep(FS_SETTLE_DELAY);-- // 1.2. Terminate lingering processes that run FROM app_dir. pid_wait only- // covers the one main-process PID, but executables living inside the- // install dir can outlive it and keep image locks on files we're- // about to replace. Real-world case: the bundled Android adb.exe —- // any adb invocation self-forks a persistent `adb server` daemon- // that survives app exit and made both the replace AND the rollback- // fail with os error 32 (sharing violation). Windows never allows- // overwriting a running executable, so these must be gone first.- terminate_appdir_processes(&args.app_dir, emit)?;-- // 1.5. Permission probe → optional self-elevation. Run BEFORE the lock- // write so cancelling UAC leaves zero on-disk state. If app_dir is- // not user-writable (e.g. installed under D:\ or C:\Program Files\- // with admin-only ACL), relaunch ourselves via ShellExecuteExW(runas)- // so the elevated child can replace the files. Default install path- // (%LOCALAPPDATA%\xdt-maker) probes successfully → no UAC, no change- // from prior behavior.- match needs_elevation(&args.app_dir) {- Ok(true) if !args.elevated => {- logger::info(format!(- "[elevate] app_dir {} not user-writable, requesting UAC elevation",- args.app_dir.display()- ));- emit(InstallerEvent::Phase(- Phase::RequestingElevation,- "需要管理员权限,请在弹出的 UAC 提示中点击「是」…".into(),- ));- match self_elevate(args) {- Ok(()) => {- logger::info("[elevate] elevated child spawned, exiting original updater");- // Hard-exit: the elevated child has its own UI and event- // loop. Returning Ok() here would emit Done → flash- // "更新完成" briefly in this (now-stale) window. No lock- // was created, no app_dir state was touched — clean exit.- std::process::exit(0);- }- Err(ElevateError::UserCancelled) => {- anyhow::bail!("用户取消了管理员授权,更新已取消");+ match run_inner(&args, lock, &mut emit) {+ Ok(lock) => {+ release_update_lock(lock);+ emit(InstallerEvent::Done);+ }+ Err(failure) => {+ let (extract_dir, backup_dir) = staging_dirs(&args);+ if !failure.keep_backup {+ remove_pre_install_staging(&extract_dir, &backup_dir);+ }+ let can_retry = if failure.can_retry {+ finalize_retry_state(&args, true)+ } else {+ if failure.discard_archive {+ discard_staged_archives(&args);}- Err(ElevateError::Other(e)) => {- anyhow::bail!("请求管理员权限失败:{}", e);+ false+ };+ logger::error(format!("[installer] FAILED: {}", failure.message));+ if let Some(lock) = failure.lock {+ if can_retry {+ // Keep the `.updating` file so Cindy waits at startup, but close+ // the handle so in-process Retry can reopen it. create_new still+ // rejects a second updater while the file exists.+ retain_update_lock_file(lock);+ } else {+ release_update_lock(lock);}}+ let relaunched_restored = failure.install_restored+ && should_relaunch_after_rollback(can_retry);+ if relaunched_restored {+ relaunch_restored_app(&args);+ }+ emit(InstallerEvent::Failed {+ error: failure.message,+ can_retry,+ install_unmodified: failure.install_unmodified,+ install_restored: failed_event_install_restored(+ failure.install_restored,+ relaunched_restored,+ ),+ });}- Ok(true) => {- // Already elevated and STILL can't write — not a permission- // problem. Most likely an antivirus / EDR holds a file open.- // Bail with a directive error rather than looping UAC.- anyhow::bail!(- "无法写入安装目录 {} (已使用管理员权限)。可能被杀软或其他进程锁定,请将该目录加入杀软白名单后重试",- args.app_dir.display()- );- }- Ok(false) => {- // Writable; continue normal flow.+ }+}++struct InstallerFailure {+ message: String,+ can_retry: bool,+ /// Terminal archive errors must delete Electron's staged ZIP. UAC cancel+ /// leaves a still-valid package in place so the next Cindy launch can+ /// apply it without a TEMP `runas` from this updater.+ discard_archive: bool,+ /// Keep the rollback directory only after a real rollback failure. Extract+ /// and snapshot errors happen before app_dir is rewritten, so their+ /// elevated staging under app_dir must be deleted.+ keep_backup: bool,+ /// True until copy_tree starts rewriting app_dir. Close must relaunch Cindy+ /// after a terminal pre-install Retry even when Retry is no longer offered.+ install_unmodified: bool,+ /// True after rollback restored the previous files. Close must relaunch+ /// Cindy if the later digest check withdraws Retry.+ install_restored: bool,+ lock: Option<UpdateLock>,+}++impl InstallerFailure {+ fn new(message: impl Into<String>, can_retry: bool) -> Self {+ Self {+ message: message.into(),+ can_retry,+ discard_archive: !can_retry,+ keep_backup: false,+ install_unmodified: true,+ install_restored: false,+ lock: None,}- Err(e) => {- // Probe itself errored unexpectedly. Don't block the update —- // fall through to the existing copy_with_retry which has its own- // diagnostics. Logged in needs_elevation already.- logger::warn(format!(- "[elevate] probe inconclusive ({}); proceeding without elevation",- e- ));+ }++ /// Leave Electron's staged ZIP in place. Used when this updater must stop+ /// without taking ownership — UAC cancel, or another updater already holds+ /// `.updating` and may still need the same archive.+ fn keep_archive(message: impl Into<String>) -> Self {+ Self {+ message: message.into(),+ can_retry: elevation_prompt_can_retry(),+ discard_archive: false,+ keep_backup: false,+ install_unmodified: true,+ install_restored: false,+ lock: None,}}- // 2. Mark lock so a racing main-process restart waits.- if let Some(parent) = args.lock.parent() {- let _ = fs::create_dir_all(parent);+ fn with_lock(mut self, lock: UpdateLock) -> Self {+ self.lock = Some(lock);+ self}- fs::write(&args.lock, b"updating")?;+}- // 3. Extract zip into a subdir of workdir. Child dirs preserve the- // parent's `{ts}` suffix so a copy-out for support still carries the- // attempt timestamp regardless of whether the parent context survives.- let ts = workdir_ts(&args.workdir);- let extract_dir = args.workdir.join(format!("cindy-update-extract-{ts}"));- fs::create_dir_all(&extract_dir)?;- logger::info(format!("[installer] extract_dir={}", extract_dir.display()));- extract_zip(&args.zip, &extract_dir, |done, total| {- let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };- emit(InstallerEvent::Progress(- Phase::Extracting,- format!("解压中 {}/{}", done, total),- pct,- ));- })?;+/// Exclusive `.updating` lock. Cindy waits on this file at startup; a second+/// updater must fail closed instead of replacing files concurrently.+pub(crate) struct UpdateLock {+ path: PathBuf,+ _file: File,+}- // 3.5. Selective backup: copy *only* the files in app_dir that the new- // release is about to overwrite. Files that exist in the old version- // but not in the new release are left untouched (we never delete),- // so there's nothing to restore for them. Failure here aborts BEFORE- // app_dir is modified — install is safe to abort with no rollback.- emit(InstallerEvent::Phase(- Phase::BackingUp,- "备份当前版本…".into(),- ));- let backup_dir = args.workdir.join(format!("cindy-update-rollback-{ts}"));- fs::create_dir_all(&backup_dir)?;- logger::info(format!("[installer] backup_dir={}", backup_dir.display()));- snapshot_overwritten_files(&extract_dir, &args.app_dir, &backup_dir, |done, total| {- let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };- emit(InstallerEvent::Progress(- Phase::BackingUp,- format!("备份 {}/{}", done, total),- pct,- ));+pub(crate) fn acquire_update_lock(path: &Path) -> Result<UpdateLock, String> {+ if let Some(parent) = path.parent() {+ let _ = fs::create_dir_all(parent);+ }+ let mut options = fs::OpenOptions::new();+ options.write(true).create_new(true);+ #[cfg(target_os = "windows")]+ {+ use std::os::windows::fs::OpenOptionsExt;+ const FILE_SHARE_READ: u32 = 0x00000001;+ options.share_mode(FILE_SHARE_READ);+ }+ let mut file = options.open(path).map_err(|error| {+ if error.kind() == io::ErrorKind::AlreadyExists {+ "updater_busy".to_string()+ } else {+ error.to_string()+ }})?;+ file.write_all(format!("updating {}\n", std::process::id()).as_bytes())+ .map_err(|error| error.to_string())?;+ file.sync_all().map_err(|error| error.to_string())?;+ Ok(UpdateLock {+ path: path.to_path_buf(),+ _file: file,+ })+}- // 4–6. The risky window: replace files, drop lock, launch, verify.- // Wrapped so any failure triggers rollback before bubbling out.- let install_result: anyhow::Result<()> = (|| {- // 4. Copy new files over app_dir.- copy_tree(&extract_dir, &args.app_dir, |done, total| {- let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };- emit(InstallerEvent::Progress(- Phase::Replacing,- format!("替换 {}/{}", done, total),- pct,- ));- })?;+pub(crate) fn release_update_lock(lock: UpdateLock) {+ drop(lock._file);+ let _ = fs::remove_file(&lock.path);+}- // 5. Drop lock so the new process won't busy-wait on it at startup.- let _ = fs::remove_file(&args.lock);+pub(crate) fn retain_update_lock_file(lock: UpdateLock) {+ drop(lock._file);+}- // 6. Verify exe exists, launch detached, verify it actually came up.- let exe_path = args.app_dir.join(&args.exe_name);- if !exe_path.exists() {- anyhow::bail!(- "新版本主程序缺失:{} 在替换后不存在",- exe_path.display()- );- }- emit(InstallerEvent::Phase(- Phase::Launching,- "启动新版本…".into(),- ));- launch_detached(&exe_path)?;- if !poll_until_process_running(&args.exe_name, LAUNCH_VERIFY_TIMEOUT) {- anyhow::bail!(- "新进程 {} 在启动 {} 秒后未出现,可能被杀软拦截或新可执行文件损坏",- args.exe_name,- LAUNCH_VERIFY_TIMEOUT.as_secs()- );- }- Ok(())- })();+fn lock_file_owned_by_this_process(path: &Path) -> bool {+ fs::read_to_string(path)+ .map(|contents| contents == format!("updating {}\n", std::process::id()))+ .unwrap_or(false)+}- // Staging dirs (extract + zip) are ALWAYS cleaned regardless of outcome —- // they're never useful for recovery. The backup dir survives only if the- // rollback itself failed (last-resort manual recovery).- let cleanup_staging = || {- let _ = fs::remove_dir_all(&extract_dir);- let _ = fs::remove_file(&args.zip);+fn lock_contents_owned_by_this_process(contents: &str) -> bool {+ contents == format!("updating {}\n", std::process::id())+}++/// True when `.updating` exists and does not name this process. Cindy's 30s+/// startup wait can delete this window's leftover; a later updater then owns+/// the same path. Close must not relaunch into that replace window.+pub(crate) fn lock_owned_by_foreign_process(path: &Path) -> bool {+ path.exists() && !lock_file_owned_by_this_process(path)+}++/// Close / abandon Retry must delete a retained `.updating` file. Cindy's+/// startup lock loop cannot tell this leftover from a live updater and would+/// otherwise wait the full 30s timeout. Only delete a lock this process wrote;+/// a second updater that lost acquisition must not remove the first instance's+/// mutex by pathname.+pub(crate) fn release_abandoned_update_lock(path: &Path) {+ if lock_file_owned_by_this_process(path) {+ let _ = fs::remove_file(path);+ }+}++pub(crate) fn should_relaunch_after_abandoning_retry(+ can_retry: bool,+ retry_in_progress: bool,+ install_unmodified: bool,+ install_restored: bool,+) -> bool {+ !retry_in_progress && (can_retry || install_unmodified || install_restored)+}++/// Alt+F4 / 关闭 must not destroy the updater until the install worker has+/// reached Done or Failed. The first attempt never sets `retry_started`, so+/// blocking only on that flag would relaunch Cindy from a half-replaced tree.+pub(crate) fn close_should_be_blocked(phase: Phase, retry_in_progress: bool) -> bool {+ retry_in_progress || !matches!(phase, Phase::Done | Phase::Failed)+}++pub(crate) fn restored_app_relaunch_path(args: &CliArgs) -> Option<PathBuf> {+ let exe = args.app_dir.join(&args.exe_name);+ exe.exists().then_some(exe)+}++/// Launching Cindy.exe from this process inherits the updater token. A+/// medium-writable per-user install can be replaced before Close; do not+/// CreateProcess it while this process is still elevated.+pub(crate) fn may_relaunch_with_current_integrity(+ cli_elevated: bool,+ process_elevated: bool,+ install_writable: bool,+) -> bool {+ !(install_writable && (cli_elevated || process_elevated))+}++fn relaunch_restored_app(args: &CliArgs) {+ let Some(exe) = restored_app_relaunch_path(args) else {+ return;};+ match launch_app_exe(args, &exe) {+ Ok(AppLaunch::Started) => logger::info(format!(+ "[installer] relaunched restored exe after abandoning Retry at {}",+ exe.display()+ )),+ Ok(AppLaunch::Skipped) => {}+ Err(error) => logger::warn(format!(+ "[installer] relaunch of restored exe after abandoning Retry failed: {error}"+ )),+ }+}- match install_result {- Ok(()) => {- logger::info(format!(- "[installer] LAUNCH VERIFIED: {} is running",- args.exe_name+/// Digest failure happens before `run_inner` pins `app_dir`. A medium-writable+/// parent can replace the directory and Cindy.exe after the unelevated parent+/// drops its handle; never inherit this process's high token.+fn relaunch_unmodified_app_after_early_failure(args: &CliArgs) {+ let Some(exe) = restored_app_relaunch_path(args) else {+ return;+ };+ match launch_de_elevated(&exe) {+ Ok(()) => logger::info(format!(+ "[installer] relaunched unmodified exe after archive validation failed at {}",+ exe.display()+ )),+ Err(error) => logger::warn(format!(+ "[installer] skip elevated CreateProcess after archive validation failed {}: {error}",+ exe.display()+ )),+ }+}++enum AppLaunch {+ Started,+ Skipped,+}++fn launch_app_exe(args: &CliArgs, exe: &Path) -> io::Result<AppLaunch> {+ if may_relaunch_with_current_integrity(+ args.elevated,+ process_is_elevated(),+ resolved_install_writable(+ args,+ install_writable_for_staging(+ args.elevated,+ medium_integrity_needs_elevation(&args.app_dir),+ install_is_user_owned_for(&args.app_dir, &args.exe_name),+ ),+ ),+ ) {+ launch_detached(exe)?;+ return Ok(AppLaunch::Started);+ }+ match launch_de_elevated(exe) {+ Ok(()) => Ok(AppLaunch::Started),+ Err(error) => {+ logger::warn(format!(+ "[installer] skip elevated CreateProcess of writable exe {}: {error}",+ exe.display()));- // Best-effort metadata only, outside the file replacement/rollback- // transaction. Reuse existing elevation for HKLM, never request it.- if let Some(key) = installation_version_key(args) {- crate::installation_version::sync(&args.app_dir.join(&args.exe_name), &key);- }- let _ = fs::remove_dir_all(&backup_dir);- cleanup_staging();- Ok(())+ Ok(AppLaunch::Skipped)}- Err(install_err) => {- logger::error(format!("[installer] install failed: {install_err}"));- emit(InstallerEvent::Phase(- Phase::RollingBack,- "更新失败,正在回滚到旧版本…".into(),- ));- match rollback(&backup_dir, &args.app_dir, |done, total| {- let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };- emit(InstallerEvent::Progress(- Phase::RollingBack,- format!("回滚 {}/{}", done, total),- pct,- ));- }) {- Ok(()) => {- logger::info("[installer] rollback succeeded");- let _ = fs::remove_dir_all(&backup_dir);- cleanup_staging();- // Best-effort relaunch of the (now restored) old exe so- // the user isn't left without the app after a failed- // update. If it fails to start, the Failed UI still- // tells them what happened.- let exe_path = args.app_dir.join(&args.exe_name);- if exe_path.exists() {- if let Err(e) = launch_detached(&exe_path) {- logger::warn(format!(- "[installer] relaunch of old exe after rollback failed: {e}"- ));- } else {- logger::info(format!(- "[installer] relaunched old exe at {}",- exe_path.display()- ));- }- }- let _ = fs::remove_file(&args.lock);- anyhow::bail!("{} (已回滚到旧版本)", install_err)- }- Err(rb_err) => {- logger::error(format!(- "[installer] ROLLBACK ALSO FAILED: {rb_err} — appDir is now in an inconsistent state, see {}",- backup_dir.display()- ));- // KEEP backup_dir — user / support may need to manually- // restore. Staging is still cleaned (it's never useful- // for recovery, only the backup is).- cleanup_staging();- anyhow::bail!(- "{} (回滚也失败:{};备份保留在 {} 供手动恢复)",- install_err,- rb_err,- backup_dir.display()- )- }- }+ }+}++pub(crate) fn should_relaunch_restored_app_on_abandon(+ can_retry: bool,+ retry_in_progress: bool,+ owned_lock: bool,+ stopped_app: bool,+ foreign_lock: bool,+ install_unmodified: bool,+ install_restored: bool,+) -> bool {+ should_relaunch_after_abandoning_retry(+ can_retry,+ retry_in_progress,+ install_unmodified,+ install_restored,+ ) && (owned_lock || stopped_app)+ && !foreign_lock+}++static ABANDONED_RETRY_LOCKS: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());++/// First Close / Destroyed for this lock path wins. `quit_now` then+/// `app.exit(0)` also fires `Destroyed`; the second call must not relaunch.+pub(crate) fn begin_abandon_retry(path: &Path) -> bool {+ let mut seen = ABANDONED_RETRY_LOCKS.lock().unwrap_or_else(|e| e.into_inner());+ if seen.iter().any(|p| p == path) {+ return false;+ }+ seen.push(path.to_path_buf());+ true+}++/// User closed the failure window instead of Retry. Drop this process's+/// retained lock, then relaunch the restored Cindy if Retry had kept it closed.+/// Retryable failures can happen before `.updating` exists, so relaunch also+/// when this updater already stopped Cindy. A second updater that never owned+/// the lock and never stopped Cindy must not relaunch.+pub(crate) fn abandon_retry(+ args: &CliArgs,+ can_retry: bool,+ retry_in_progress: bool,+ stopped_app: bool,+ install_unmodified: bool,+ install_restored: bool,+) {+ if !begin_abandon_retry(&args.lock) {+ return;+ }+ let owned = lock_file_owned_by_this_process(&args.lock);+ let foreign = lock_owned_by_foreign_process(&args.lock);+ release_abandoned_update_lock(&args.lock);+ if should_relaunch_restored_app_on_abandon(+ can_retry,+ retry_in_progress,+ owned,+ stopped_app,+ foreign,+ install_unmodified,+ install_restored,+ ) {+ relaunch_restored_app(args);+ }+}++/// Re-open a lock this process already created and kept during the failure+/// window. Missing, unreadable, or foreign-owned files mean another updater is+/// active — do not take over a later process's mutex.+pub(crate) fn reopen_held_update_lock(path: &Path) -> Option<UpdateLock> {+ let mut options = fs::OpenOptions::new();+ options.write(true).read(true);+ #[cfg(target_os = "windows")]+ {+ use std::os::windows::fs::OpenOptionsExt;+ const FILE_SHARE_READ: u32 = 0x00000001;+ options.share_mode(FILE_SHARE_READ);+ }+ let mut file = options.open(path).ok()?;+ let mut contents = String::new();+ file.read_to_string(&mut contents).ok()?;+ if !lock_contents_owned_by_this_process(&contents) {+ return None;+ }+ Some(UpdateLock {+ path: path.to_path_buf(),+ _file: file,+ })+}++pub(crate) fn should_relaunch_after_rollback(can_retry: bool) -> bool {+ !can_retry+}++/// Close must not start a second Cindy after `run_with_lock` already relaunched+/// the restored app because Retry was withdrawn.+pub(crate) fn failed_event_install_restored(+ install_restored: bool,+ already_relaunched: bool,+) -> bool {+ install_restored && !already_relaunched+}++/// Retry cannot request UAC. Failures before the permission probe must not+/// advertise Retry when this install still needs elevation.+pub(crate) fn pre_elevation_failure_can_retry(needs_uac: bool, already_elevated: bool) -> bool {+ already_elevated || !needs_uac+}++pub(crate) fn retry_available(zip: &Path) -> bool {+ File::open(zip)+ .and_then(|file| file.metadata())+ .map(|metadata| metadata.is_file())+ .unwrap_or(false)+}++fn sha256_hex(file: &mut File) -> io::Result<String> {+ file.seek(SeekFrom::Start(0))?;+ let mut hasher = Sha256::new();+ let mut buffer = [0u8; 64 * 1024];+ loop {+ let read = file.read(&mut buffer)?;+ if read == 0 {+ break;}+ hasher.update(&buffer[..read]);}+ file.seek(SeekFrom::Start(0))?;+ Ok(format!("{:x}", hasher.finalize()))}-/// Sweep stale `xdt-update-*` dirs/files from %TEMP% that are older than-/// `MAX_AGE_DAYS`. Last-resort cleanup for backups left behind by failed-/// rollbacks (which we intentionally do NOT auto-delete at end-of-run).-/// Best-effort: any IO failure is ignored — sweeping is purely housekeeping.-pub fn sweep_stale_temp_dirs() {- const MAX_AGE_SECS: u64 = 7 * 24 * 60 * 60; // 7 days- // Broad prefixes catch:- // - cindy-update-{ts}/ current workdir layout (2026-07 rebrand)- // - xdt-update-{ts}/ legacy workdir layout- // - xdt-update-extract-{ts}/ legacy (pre-workdir refactor)- // - xdt-update-rollback-{ts}/ legacy- // - xdt-updater-{ts}.exe legacy standalone updater binary- let prefixes = ["cindy-update", "xdt-update"];- let now = std::time::SystemTime::now();- let temp = std::env::temp_dir();- let entries = match fs::read_dir(&temp) {- Ok(it) => it,- Err(_) => return,- };- let mut swept = 0u32;- for entry in entries.flatten() {- let name = entry.file_name();- let name_str = name.to_string_lossy();- if !prefixes.iter().any(|p| name_str.starts_with(p)) {- continue;+fn open_regular_file(zip: &Path) -> io::Result<File> {+ let mut options = fs::OpenOptions::new();+ options.read(true);+ #[cfg(target_os = "windows")]+ {+ use std::os::windows::fs::OpenOptionsExt;+ // Rust's default Windows share mode permits concurrent writers and+ // deleters. Digest verification and ZipArchive must hold one handle+ // that denies both for the entire verify-and-extract transaction.+ const FILE_SHARE_READ: u32 = 0x00000001;+ options.share_mode(FILE_SHARE_READ);+ }+ let file = options.open(zip)?;+ if !file.metadata()?.is_file() {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("archive path is not a regular file: {}", zip.display()),+ ));+ }+ Ok(file)+}++pub(crate) fn archive_matches_digest(zip: &Path, expected: &str) -> bool {+ open_regular_file(zip)+ .and_then(|mut file| sha256_hex(&mut file))+ .map(|digest| digest.eq_ignore_ascii_case(expected))+ .unwrap_or(false)+}++/// Verify the archive against the manifest digest supplied by Electron. Later+/// elevation and retry reuse that digest so a TEMP replacement cannot be extracted.+pub(crate) fn bind_zip_sha256(args: &mut CliArgs) -> Result<String, String> {+ let expected = args+ .zip_sha256+ .as_deref()+ .filter(|digest| !digest.is_empty())+ .ok_or_else(|| "archive_unavailable".to_string())?;+ let mut file = open_regular_file(&args.zip).map_err(|_| "archive_unavailable".to_string())?;+ let digest = sha256_hex(&mut file).map_err(|_| "archive_unavailable".to_string())?;+ if expected.eq_ignore_ascii_case(&digest) {+ Ok(digest)+ } else {+ Err("archive_unavailable".into())+ }+}++/// Retry is safe only when the failed attempt left the install directory in a+/// known-good state and the original archive still matches the captured digest.+fn retry_allowed(can_retry: bool, zip: &Path, expected_sha256: Option<&str>) -> bool {+ match expected_sha256 {+ Some(digest) => can_retry && archive_matches_digest(zip, digest),+ None => false,+ }+}++pub(crate) fn retry_archive(args: &CliArgs) -> PathBuf {+ args.workdir.join("retry.zip")+}++/// Recheck the persisted retry state. The digest must come from the same+/// `CliArgs` that Retry will spawn, not a copy that never received the bind.+pub(crate) fn retry_request_allowed(+ args: &CliArgs,+ phase: Phase,+ can_retry: bool,+) -> Result<(), String> {+ if phase != Phase::Failed || !can_retry {+ return Err("unavailable".into());+ }+ let digest = args.zip_sha256.as_deref().unwrap_or("");+ if digest.is_empty() || !archive_matches_digest(&retry_archive(args), digest) {+ return Err("archive_unavailable".into());+ }+ Ok(())+}++/// Elevated retries copy into a UAC-protected install dir. Staging under the+/// unelevated `%TEMP%` workdir would let a medium-integrity process replace+/// extracted files before `copy_tree`. Keep extract/backup next to the app+/// instead; unelevated installs stay in the temp workdir.+///+/// Electron's normal spawn omits `--elevated`. The updater is `asInvoker`, so a+/// Cindy that is already running elevated hands this process a high token with+/// `args.elevated == false`. Staging must follow the real token, not the flag.+///+/// Do not ask an already-elevated token whether `app_dir` is writable: after+/// UAC, Program Files looks writable and would send extract/backup back to the+/// unelevated `%TEMP%` workdir. `--elevated` is the pre-UAC classification of a+/// protected install. Inherited elevation must classify with the linked+/// medium-integrity token instead: a per-user install is still writable by the+/// same user's medium processes, so extract/backup stay in TEMP.+pub(crate) fn install_writable_for_staging(+ cli_elevated: bool,+ medium_integrity_needs_elevation: bool,+ user_owned: bool,+) -> bool {+ !cli_elevated && (!medium_integrity_needs_elevation || user_owned)+}++/// True when a same-login medium-integrity process can modify `app_dir`.+/// Ownership alone is not enough: an Administrators-owned directory can still+/// grant the current user modify access. Unknown AccessCheck fails toward+/// High-IL staging instead of treating the tree as protected.+pub(crate) fn install_is_user_owned_for(app_dir: &Path, exe_name: &str) -> bool {+ install_is_medium_writable_from_file_access(+ directory_medium_token_can_modify(app_dir),+ existing_install_files_medium_writable(app_dir, exe_name),+ ) || matches!(directory_owned_by_current_user(app_dir), Some(true))+ || install_root_parent_is_medium_replaceable(app_dir)+}++pub(crate) fn install_is_medium_writable_from_access(+ medium_can_modify: Option<bool>,+ owned_by_current_user: Option<bool>,+) -> bool {+ !matches!(medium_can_modify, Some(false)) || matches!(owned_by_current_user, Some(true))+}++/// Directory FILE_ADD_FILE can be denied while Cindy.exe itself remains+/// writable. That still needs High-IL staging and a de-elevated launch.+pub(crate) fn install_is_medium_writable_from_file_access(+ directory_can_modify: Option<bool>,+ files_can_modify: Option<bool>,+) -> bool {+ !matches!(directory_can_modify, Some(false)) || matches!(files_can_modify, Some(true))+}++fn existing_install_files_medium_writable(app_dir: &Path, exe_name: &str) -> Option<bool> {+ #[cfg(windows)]+ {+ existing_install_files_medium_writable_windows(app_dir, exe_name)+ }+ #[cfg(not(windows))]+ {+ let _ = exe_name;+ runtime_trees_pin_install_writable(app_dir).then_some(true)+ }+}++/// Existing loadable inputs a medium-integrity process can replace to hijack+/// an elevated launch: the main exe, `resources/app.asar`, app-local native+/// binaries, every unpacked file under `resources/app.asar.unpacked`+/// (Forge unpacks JS such as `node-pty` that production later `require`s),+/// extraResource natives under `resources/tools` (the Windows desktop host+/// `.node` is required into the main process), Forge extraResource+/// `resources/drizzle` companions that startup `require()`s, and the+/// `resources/windows-installation-version.ps1` extraResource that packaged+/// Windows startup reads and runs through System32 PowerShell.+pub(crate) fn medium_writable_install_file_candidates(+ app_dir: &Path,+ exe_name: &str,+) -> Vec<PathBuf> {+ let mut candidates = vec![+ app_dir.join(exe_name),+ app_dir.join("resources").join("app.asar"),+ app_dir+ .join("resources")+ .join("windows-installation-version.ps1"),+ ];+ let _ = collect_medium_writable_root_natives(app_dir, &mut candidates);+ let _ = collect_medium_writable_unpacked_files(+ &app_dir.join("resources").join("app.asar.unpacked"),+ &mut candidates,+ );+ let _ = collect_medium_writable_unpacked_files(+ &app_dir.join("resources").join("tools"),+ &mut candidates,+ );+ let _ = collect_medium_writable_unpacked_files(+ &app_dir.join("resources").join("drizzle"),+ &mut candidates,+ );+ candidates+}++fn is_medium_writable_native_file(path: &Path) -> bool {+ path.extension()+ .and_then(|ext| ext.to_str())+ .is_some_and(|ext| {+ ext.eq_ignore_ascii_case("dll")+ || ext.eq_ignore_ascii_case("exe")+ || ext.eq_ignore_ascii_case("node")+ })+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) enum UnpackedWalk {+ Complete,+ Unreadable,+ Reparse,+}++pub(crate) fn unpacked_walk_pins_install_writable(walk: UnpackedWalk) -> bool {+ matches!(walk, UnpackedWalk::Unreadable | UnpackedWalk::Reparse)+}++fn runtime_trees_pin_install_writable(app_dir: &Path) -> bool {+ let mut sink = Vec::new();+ unpacked_walk_pins_install_writable(collect_medium_writable_root_natives(app_dir, &mut sink))+ || unpacked_walk_pins_install_writable(unpacked_tree_walk(app_dir))+ || unpacked_walk_pins_install_writable(extra_resource_tools_walk(app_dir))+ || unpacked_walk_pins_install_writable(extra_resource_drizzle_walk(app_dir))+}++fn extra_resource_tools_walk(app_dir: &Path) -> UnpackedWalk {+ let mut sink = Vec::new();+ collect_medium_writable_unpacked_files(&app_dir.join("resources").join("tools"), &mut sink)+}++fn extra_resource_drizzle_walk(app_dir: &Path) -> UnpackedWalk {+ let mut sink = Vec::new();+ collect_medium_writable_unpacked_files(&app_dir.join("resources").join("drizzle"), &mut sink)+}++pub(crate) fn loadable_input_is_reparse(path: &Path) -> bool {+ path.exists() && is_reparse_point(path)+}++fn unpacked_tree_walk(app_dir: &Path) -> UnpackedWalk {+ let mut sink = Vec::new();+ collect_medium_writable_unpacked_files(+ &app_dir.join("resources").join("app.asar.unpacked"),+ &mut sink,+ )+}++pub(crate) fn collect_medium_writable_root_natives(+ app_dir: &Path,+ out: &mut Vec<PathBuf>,+) -> UnpackedWalk {+ if is_reparse_point(app_dir) {+ return UnpackedWalk::Reparse;+ }+ let entries = match fs::read_dir(app_dir) {+ Ok(entries) => entries,+ Err(error) if error.kind() == io::ErrorKind::NotFound => {+ return UnpackedWalk::Complete;}- let meta = match entry.metadata() {- Ok(m) => m,- Err(_) => continue,- };- let modified = match meta.modified() {- Ok(m) => m,- Err(_) => continue,+ Err(_) => return UnpackedWalk::Unreadable,+ };+ for entry in entries {+ let entry = match entry {+ Ok(entry) => entry,+ Err(_) => return UnpackedWalk::Unreadable,};- let age_secs = now.duration_since(modified).map(|d| d.as_secs()).unwrap_or(0);- if age_secs < MAX_AGE_SECS {+ let path = entry.path();+ if !is_medium_writable_native_file(&path) {continue;}- let path = entry.path();- let removed = if meta.is_dir() {- fs::remove_dir_all(&path).is_ok()- } else {- fs::remove_file(&path).is_ok()- };- if removed {- swept += 1;- logger::info(format!(- "[sweep] removed stale {} (age {}d)",- path.display(),- age_secs / (24 * 60 * 60),- ));+ if is_reparse_point(&path) {+ return UnpackedWalk::Reparse;}+ out.push(path);}- if swept > 0 {- logger::info(format!("[sweep] cleaned {} stale temp entries", swept));+ UnpackedWalk::Complete+}++/// GENERIC_WRITE, FILE_WRITE_DATA / FILE_APPEND_DATA, DELETE, or parent+/// FILE_DELETE_CHILD / FILE_ADD_FILE each lets a medium-integrity process+/// replace a loadable input before Close. GENERIC_WRITE is denied unless every+/// mapped write right is granted, so data/append must be probed on their own.+pub(crate) fn file_is_medium_replaceable_from_access(+ generic_write: Option<bool>,+ delete: Option<bool>,+ parent_delete_child: Option<bool>,+ parent_add_file: Option<bool>,+ write_data: Option<bool>,+ append_data: Option<bool>,+) -> Option<bool> {+ if matches!(generic_write, Some(true))+ || matches!(delete, Some(true))+ || matches!(parent_delete_child, Some(true))+ || matches!(parent_add_file, Some(true))+ || matches!(write_data, Some(true))+ || matches!(append_data, Some(true))+ {+ return Some(true);}+ if matches!(generic_write, Some(false))+ && matches!(delete, Some(false))+ && matches!(parent_delete_child, Some(false))+ && matches!(parent_add_file, Some(false))+ && matches!(write_data, Some(false))+ && matches!(append_data, Some(false))+ {+ return Some(false);+ }+ None}-/// Pull the `{ts}` slice out of a `xdt-update-{ts}` workdir basename so-/// child names can echo the same timestamp. Falls back to a fresh chrono-/// timestamp if the basename doesn't match the convention — the dir still-/// works either way, the suffix is just for human-readable forensics.-fn workdir_ts(workdir: &Path) -> String {- workdir- .file_name()- .and_then(|n| n.to_str())- .and_then(|n| n.strip_prefix("cindy-update-").or_else(|| n.strip_prefix("xdt-update-")))- .map(|s| s.to_string())- .unwrap_or_else(|| chrono::Local::now().timestamp_millis().to_string())+/// WRITE_DAC / WRITE_OWNER are enough to replace Cindy.exe after UAC even when+/// the current DACL denies add/write/delete. Unknown ACL-control fails open+/// toward High-IL staging and a de-elevated launch.+pub(crate) fn acl_control_pins_install_writable(+ write_dac: Option<bool>,+ write_owner: Option<bool>,+) -> bool {+ !matches!(write_dac, Some(false)) || !matches!(write_owner, Some(false))}-fn extract_zip<F: FnMut(u64, u64)>(- zip_path: &Path,- dest: &Path,- mut on_progress: F,-) -> anyhow::Result<()> {- let file = File::open(zip_path)?;- let mut archive = zip::ZipArchive::new(file)?;- let total = archive.len() as u64;- on_progress(0, total);+/// FILE_DELETE_CHILD / FILE_ADD_FILE, directory DELETE / FILE_ADD_SUBDIRECTORY,+/// or ACL-control on a higher ancestor lets a medium process swap a package+/// directory to a junction after classification.+pub(crate) fn ancestor_control_pins_install_writable(+ delete_child: Option<bool>,+ add_file: Option<bool>,+ write_dac: Option<bool>,+ write_owner: Option<bool>,+ delete: Option<bool>,+ add_subdirectory: Option<bool>,+) -> bool {+ matches!(delete_child, Some(true))+ || matches!(add_file, Some(true))+ || matches!(delete, Some(true))+ || matches!(add_subdirectory, Some(true))+ || acl_control_pins_install_writable(write_dac, write_owner)+}- for i in 0..archive.len() {- let mut entry = archive.by_index(i)?;- let outpath = match entry.enclosed_name() {- Some(p) => dest.join(p),- None => continue,- };- if entry.is_dir() {- fs::create_dir_all(&outpath)?;- } else {- if let Some(parent) = outpath.parent() {- fs::create_dir_all(parent)?;- }- let mut out = File::create(&outpath)?;- io::copy(&mut entry, &mut out)?;+/// Parent FILE_DELETE_CHILD / FILE_ADD_SUBDIRECTORY or ACL-control lets a+/// medium user replace a protected-looking `app_dir` across the UAC handoff.+pub(crate) fn install_root_parent_pins_install_writable(+ delete_child: Option<bool>,+ add_subdirectory: Option<bool>,+ write_dac: Option<bool>,+ write_owner: Option<bool>,+) -> bool {+ matches!(delete_child, Some(true))+ || matches!(add_subdirectory, Some(true))+ || acl_control_pins_install_writable(write_dac, write_owner)+}++/// Directories above `app_dir` that can replace the whole install path.+/// Immediate parent is not enough: DELETE on `D:\Games` plus+/// FILE_ADD_SUBDIRECTORY on `D:\` recreates `Games\Cindy` after UAC.+pub(crate) fn install_root_ancestors(app_dir: &Path) -> Vec<PathBuf> {+ let mut ancestors = Vec::new();+ let mut current = match app_dir.parent() {+ Some(parent) => parent,+ None => return ancestors,+ };+ loop {+ ancestors.push(current.to_path_buf());+ match current.parent() {+ Some(parent) if parent != current => current = parent,+ _ => break,}- on_progress((i as u64) + 1, total);}- Ok(())+ ancestors}-/// Walk `extract_dir` and copy every file that ALREADY exists at the-/// equivalent relative path under `app_dir` into `backup_dir`, mirroring-/// the directory structure. Files in app_dir that the new release does-/// NOT overwrite stay where they are — they're still intact post-rollback.-fn snapshot_overwritten_files<F: FnMut(u64, u64)>(- extract_dir: &Path,- app_dir: &Path,- backup_dir: &Path,- mut on_progress: F,-) -> anyhow::Result<()> {- let entries: Vec<_> = walkdir::WalkDir::new(extract_dir)- .into_iter()- .filter_map(Result::ok)- .filter(|e| e.file_type().is_file())- .collect();- let total = entries.len() as u64;- on_progress(0, total);+/// Directories between a loadable input and `app_dir`, excluding `app_dir`.+/// Immediate parent is not enough: `node_modules/node-pty` can be replaced+/// while `lib/` still denies delete.+pub(crate) fn runtime_path_ancestors(path: &Path, app_dir: &Path) -> Vec<PathBuf> {+ let mut ancestors = Vec::new();+ let mut current = match path.parent() {+ Some(parent) => parent,+ None => return ancestors,+ };+ loop {+ if current == app_dir {+ break;+ }+ ancestors.push(current.to_path_buf());+ match current.parent() {+ Some(parent) if parent != current => current = parent,+ _ => break,+ }+ }+ ancestors+}- for (idx, entry) in entries.iter().enumerate() {- let rel = entry.path().strip_prefix(extract_dir)?;- let appfile = app_dir.join(rel);- if appfile.exists() {- let backup_path = backup_dir.join(rel);- if let Some(parent) = backup_path.parent() {- fs::create_dir_all(parent)?;+pub(crate) fn collect_medium_writable_unpacked_files(+ dir: &Path,+ out: &mut Vec<PathBuf>,+) -> UnpackedWalk {+ if is_reparse_point(dir) {+ return UnpackedWalk::Reparse;+ }+ let entries = match fs::read_dir(dir) {+ Ok(entries) => entries,+ Err(error) if error.kind() == io::ErrorKind::NotFound => {+ return UnpackedWalk::Complete;+ }+ Err(_) => return UnpackedWalk::Unreadable,+ };+ for entry in entries {+ let entry = match entry {+ Ok(entry) => entry,+ Err(_) => return UnpackedWalk::Unreadable,+ };+ let path = entry.path();+ if is_reparse_point(&path) {+ return UnpackedWalk::Reparse;+ }+ let Ok(file_type) = entry.file_type() else {+ return UnpackedWalk::Unreadable;+ };+ if file_type.is_dir() {+ let nested = collect_medium_writable_unpacked_files(&path, out);+ if unpacked_walk_pins_install_writable(nested) {+ return nested;}- copy_with_retry(&appfile, &backup_path)?;+ continue;+ }+ if file_type.is_file() {+ out.push(path);}- on_progress((idx as u64) + 1, total);}- Ok(())+ UnpackedWalk::Complete}-/// Reverse of `snapshot_overwritten_files`: copy every file in `backup_dir`-/// back over `app_dir`. Any new files added by the failed install remain as-/// orphans in app_dir — harmless, the next clean install would remove them-/// — but the originals are restored so the old version still works.-fn rollback<F: FnMut(u64, u64)>(- backup_dir: &Path,- app_dir: &Path,- mut on_progress: F,-) -> anyhow::Result<()> {- let entries: Vec<_> = walkdir::WalkDir::new(backup_dir)- .into_iter()- .filter_map(Result::ok)- .filter(|e| e.file_type().is_file())- .collect();- let total = entries.len() as u64;- on_progress(0, total);+/// CreateFile GENERIC_WRITE can fail because the image is mapped, not because+/// the DACL denies write. Sharing/lock is not proof the file is protected.+pub(crate) fn file_write_probe_from_os_error(+ kind: io::ErrorKind,+ raw: Option<i32>,+) -> Option<bool> {+ const ERROR_ACCESS_DENIED: i32 = 5;+ const ERROR_SHARING_VIOLATION: i32 = 32;+ const ERROR_LOCK_VIOLATION: i32 = 33;+ match raw {+ Some(ERROR_SHARING_VIOLATION) | Some(ERROR_LOCK_VIOLATION) => Some(true),+ Some(ERROR_ACCESS_DENIED) => Some(false),+ _ if kind == io::ErrorKind::PermissionDenied => Some(false),+ _ => None,+ }+}- for (idx, entry) in entries.iter().enumerate() {- let rel = entry.path().strip_prefix(backup_dir)?;- let target = app_dir.join(rel);- if let Some(parent) = target.parent() {- fs::create_dir_all(parent)?;- }- copy_with_retry(entry.path(), &target)?;- on_progress((idx as u64) + 1, total);+fn directory_medium_token_can_modify(app_dir: &Path) -> Option<bool> {+ #[cfg(windows)]+ {+ directory_medium_token_can_modify_windows(app_dir)+ }+ #[cfg(not(windows))]+ {+ let _ = app_dir;+ None}- Ok(())}-fn copy_tree<F: FnMut(u64, u64)>(- src: &Path,- dst: &Path,- mut on_progress: F,-) -> anyhow::Result<()> {- let entries: Vec<_> = walkdir::WalkDir::new(src)- .into_iter()- .filter_map(Result::ok)- .collect();- let total = entries.len() as u64;- on_progress(0, total);+fn directory_owned_by_current_user(app_dir: &Path) -> Option<bool> {+ #[cfg(windows)]+ {+ directory_owned_by_current_user_windows(app_dir)+ }+ #[cfg(not(windows))]+ {+ let _ = app_dir;+ None+ }+}++fn install_root_parent_is_medium_replaceable(app_dir: &Path) -> bool {+ #[cfg(windows)]+ {+ install_root_parent_is_medium_replaceable_windows(app_dir)+ }+ #[cfg(not(windows))]+ {+ let _ = app_dir;+ false+ }+}++pub(crate) fn install_is_protected_system_for(app_dir: &Path, roots: &[PathBuf]) -> bool {+ roots.iter().any(|root| windows_path_is_within(app_dir, root))+}++/// Case-insensitive Windows path prefix, used even when this crate is tested+/// on macOS so LocalAppData / Program Files classification stays honest.+fn windows_path_is_within(child: &Path, parent: &Path) -> bool {+ let child = child.to_string_lossy().replace('/', "\\").to_lowercase();+ let parent = parent.to_string_lossy().replace('/', "\\").to_lowercase();+ if parent.is_empty() {+ return false;+ }+ child == parent+ || child.starts_with(&format!("{parent}\\"))+}++pub(crate) fn pin_install_writable(args: &mut CliArgs) {+ if args.install_writable.is_some() {+ return;+ }+ args.install_writable = Some(install_writable_for_staging(+ args.elevated,+ !args.elevated && medium_integrity_needs_elevation(&args.app_dir),+ install_is_user_owned_for(&args.app_dir, &args.exe_name),+ ));+}++fn resolved_install_writable(args: &CliArgs, probed: bool) -> bool {+ args.install_writable.unwrap_or(probed)+}++pub(crate) fn staging_dirs(args: &CliArgs) -> (PathBuf, PathBuf) {+ let process_elevated = process_is_elevated();+ let probed = install_writable_for_staging(+ args.elevated,+ !args.elevated && medium_integrity_needs_elevation(&args.app_dir),+ install_is_user_owned_for(&args.app_dir, &args.exe_name),+ );+ staging_dirs_for(args, process_elevated, resolved_install_writable(args, probed))+}++pub(crate) fn uses_protected_staging(+ cli_elevated: bool,+ process_elevated: bool,+ install_writable: bool,+) -> bool {+ (cli_elevated || process_elevated) && !install_writable+}++/// Elevated + medium-writable: `app_dir` and `%TEMP%` are both plantable.+/// Stage under a High-IL directory the same-login medium token cannot write.+pub(crate) fn uses_elevated_private_staging(+ cli_elevated: bool,+ process_elevated: bool,+ install_writable: bool,+) -> bool {+ (cli_elevated || process_elevated) && install_writable+}++pub(crate) fn elevated_private_staging_root(args: &CliArgs) -> PathBuf {+ protected_staging_base().join(format!("cindy-update-{}", workdir_ts(&args.workdir)))+}++fn protected_staging_base() -> PathBuf {+ trusted_staging_ancestor()+}++fn trusted_staging_ancestor() -> PathBuf {+ #[cfg(windows)]+ {+ program_data_dir()+ }+ #[cfg(not(windows))]+ {+ std::env::temp_dir().join("cindy-update-elevated")+ }+}++/// System ProgramData, independent of an inherited `ProgramData` environment+/// variable a medium-integrity process could override.+#[cfg(windows)]+pub(crate) fn program_data_dir() -> PathBuf {+ known_folder_program_data().unwrap_or_else(|| PathBuf::from(r"C:\ProgramData"))+}++#[cfg(not(windows))]+#[cfg_attr(not(test), allow(dead_code))]+pub(crate) fn program_data_dir() -> PathBuf {+ PathBuf::from(r"C:\ProgramData")+}++#[cfg(windows)]+fn known_folder_program_data() -> Option<PathBuf> {+ known_folder(&windows_sys::Win32::UI::Shell::FOLDERID_ProgramData)+}++#[cfg(windows)]+fn known_folder(folder_id: &windows_sys::core::GUID) -> Option<PathBuf> {+ use std::os::windows::ffi::OsStringExt;+ use windows_sys::Win32::System::Com::CoTaskMemFree;+ use windows_sys::Win32::UI::Shell::SHGetKnownFolderPath;++ let mut path: windows_sys::core::PWSTR = std::ptr::null_mut();+ let hr = unsafe { SHGetKnownFolderPath(folder_id, 0, std::ptr::null_mut(), &mut path) };+ if hr != 0 || path.is_null() {+ return None;+ }+ let wide = unsafe {+ let mut len = 0usize;+ while *path.add(len) != 0 {+ len += 1;+ }+ std::slice::from_raw_parts(path, len)+ };+ let os = std::ffi::OsString::from_wide(wide);+ unsafe {+ CoTaskMemFree(path.cast());+ }+ Some(PathBuf::from(os))+}++pub(crate) fn uses_private_staging_acl(path: &Path, private_root: &Path) -> bool {+ path.starts_with(private_root)+}++pub(crate) fn staging_dirs_for(+ args: &CliArgs,+ process_elevated: bool,+ install_writable: bool,+) -> (PathBuf, PathBuf) {+ let ts = workdir_ts(&args.workdir);+ let install_writable = resolved_install_writable(args, install_writable);+ let private_root;+ let root = if uses_protected_staging(args.elevated, process_elevated, install_writable) {+ &args.app_dir+ } else if uses_elevated_private_staging(args.elevated, process_elevated, install_writable) {+ private_root = elevated_private_staging_root(args);+ &private_root+ } else {+ &args.workdir+ };+ (+ root.join(format!("cindy-update-extract-{ts}")),+ root.join(format!("cindy-update-rollback-{ts}")),+ )+}++/// True when this process already holds an elevated Windows token.+/// `--elevated` is only set by our own `runas` child; inherited elevation from+/// an elevated Cindy spawn does not set that flag.+#[cfg(target_os = "windows")]+fn process_is_elevated() -> bool {+ use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};+ use windows_sys::Win32::Security::{+ GetTokenInformation, TokenElevation, TOKEN_ELEVATION, TOKEN_QUERY,+ };+ use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};++ unsafe {+ let mut token: HANDLE = std::ptr::null_mut();+ if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {+ return false;+ }+ let mut elevation = TOKEN_ELEVATION { TokenIsElevated: 0 };+ let mut returned = 0u32;+ let ok = GetTokenInformation(+ token,+ TokenElevation,+ (&mut elevation as *mut TOKEN_ELEVATION).cast(),+ std::mem::size_of::<TOKEN_ELEVATION>() as u32,+ &mut returned,+ );+ let _ = CloseHandle(token);+ ok != 0 && elevation.TokenIsElevated != 0+ }+}++#[cfg(not(target_os = "windows"))]+fn process_is_elevated() -> bool {+ false+}++fn medium_integrity_needs_elevation(app_dir: &Path) -> bool {+ matches!(probe_medium_integrity_needs_elevation(app_dir), Ok(true))+}++/// Writability as a same-login medium-integrity process would see it.+/// An elevated token makes Program Files look writable; impersonate the+/// linked limited token so inherited elevation does not reclassify a+/// protected install, and so per-user installs are not treated as protected.+fn probe_medium_integrity_needs_elevation(app_dir: &Path) -> io::Result<bool> {+ #[cfg(target_os = "windows")]+ {+ if process_is_elevated() {+ return match with_medium_integrity(|| needs_elevation(app_dir)) {+ Some(result) => result,+ None => {+ logger::warn(+ "[probe] no linked medium token; treating install as writable for staging",+ );+ Ok(false)+ }+ };+ }+ }+ needs_elevation(app_dir)+}++#[cfg(target_os = "windows")]+fn with_medium_integrity<T>(f: impl FnOnce() -> T) -> Option<T> {+ use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};+ use windows_sys::Win32::Security::{+ GetTokenInformation, ImpersonateLoggedOnUser, RevertToSelf, TokenLinkedToken,+ TOKEN_LINKED_TOKEN, TOKEN_QUERY,+ };+ use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};++ struct LinkedImpersonation {+ token: HANDLE,+ }+ impl Drop for LinkedImpersonation {+ fn drop(&mut self) {+ unsafe {+ let _ = RevertToSelf();+ if !self.token.is_null() {+ let _ = CloseHandle(self.token);+ }+ }+ }+ }++ unsafe {+ let mut token: HANDLE = std::ptr::null_mut();+ if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {+ return None;+ }+ let mut linked = TOKEN_LINKED_TOKEN {+ LinkedToken: std::ptr::null_mut(),+ };+ let mut returned = 0u32;+ let ok = GetTokenInformation(+ token,+ TokenLinkedToken,+ (&mut linked as *mut TOKEN_LINKED_TOKEN).cast(),+ std::mem::size_of::<TOKEN_LINKED_TOKEN>() as u32,+ &mut returned,+ );+ let _ = CloseHandle(token);+ if ok == 0 || linked.LinkedToken.is_null() {+ return None;+ }+ if ImpersonateLoggedOnUser(linked.LinkedToken) == 0 {+ let _ = CloseHandle(linked.LinkedToken);+ return None;+ }+ let _impersonation = LinkedImpersonation {+ token: linked.LinkedToken,+ };+ Some(f())+ }+}++#[cfg(windows)]+const CROSS_DEVICE_ERRNO: i32 = 17; // ERROR_NOT_SAME_DEVICE+#[cfg(unix)]+const CROSS_DEVICE_ERRNO: i32 = 18; // EXDEV+#[cfg(not(any(windows, unix)))]+const CROSS_DEVICE_ERRNO: i32 = 18;++fn is_cross_device(error: &io::Error) -> bool {+ error.raw_os_error() == Some(CROSS_DEVICE_ERRNO)+}++fn prepare_retry_archive(args: &CliArgs) -> bool {+ let target = retry_archive(args);+ if args.zip == target || !args.zip.exists() {+ return retry_available(&target);+ }+ isolate_archive_with(+ &args.zip,+ &target,+ args.zip_sha256.as_deref(),+ |src, dst| fs::rename(src, dst),+ )+}++fn isolate_archive_with<F>(+ src: &Path,+ dst: &Path,+ expected_sha256: Option<&str>,+ rename: F,+) -> bool+where+ F: FnOnce(&Path, &Path) -> io::Result<()>,+{+ if dst.exists() {+ return false;+ }+ match rename(src, dst) {+ Ok(()) => retry_available(dst),+ Err(error) if is_cross_device(&error) => copy_verified_archive(src, dst, expected_sha256),+ Err(error) => {+ logger::warn(format!("[retry] could not isolate archive: {error}"));+ false+ }+ }+}++fn copy_verified_archive(src: &Path, dst: &Path, expected_sha256: Option<&str>) -> bool {+ let Some(expected) = expected_sha256.filter(|digest| !digest.is_empty()) else {+ logger::warn("[retry] refusing cross-device copy without a trusted digest");+ return false;+ };+ let mut source = match open_regular_file(src) {+ Ok(file) => file,+ Err(error) => {+ logger::warn(format!("[retry] could not open archive for copy: {error}"));+ return false;+ }+ };+ let digest = match sha256_hex(&mut source) {+ Ok(digest) => digest,+ Err(error) => {+ logger::warn(format!("[retry] could not hash archive before copy: {error}"));+ return false;+ }+ };+ if !digest.eq_ignore_ascii_case(expected) {+ logger::warn("[retry] archive digest changed before cross-device copy");+ return false;+ }+ let copied = (|| -> io::Result<()> {+ let mut dest = File::create(dst)?;+ io::copy(&mut source, &mut dest)?;+ dest.sync_all()?;+ Ok(())+ })();+ if let Err(error) = copied {+ logger::warn(format!("[retry] cross-device copy failed: {error}"));+ let _ = fs::remove_file(dst);+ return false;+ }+ if !archive_matches_digest(dst, expected) {+ logger::warn("[retry] isolated copy did not match the trusted digest");+ let _ = fs::remove_file(dst);+ return false;+ }+ // Windows opened the source with FILE_SHARE_READ only, so deletion is+ // denied until this handle is gone.+ drop(source);+ if let Err(error) = fs::remove_file(src) {+ logger::warn(format!(+ "[retry] isolated copy but could not remove source {}: {error}",+ src.display()+ ));+ let _ = fs::remove_file(dst);+ return false;+ }+ retry_available(dst)+}++/// Retryable failures isolate the archive away from Electron's auto-apply path.+/// Terminal archive failures must delete that staged ZIP (and any isolated copy)+/// so the next Cindy launch downloads a fresh file instead of relaunching the+/// same deterministic extract error until applyAttempts is exhausted.+pub(crate) fn finalize_retry_state(args: &CliArgs, can_retry: bool) -> bool {+ if can_retry+ && prepare_retry_archive(args)+ && retry_allowed(true, &retry_archive(args), args.zip_sha256.as_deref())+ {+ true+ } else {+ discard_staged_archives(args);+ false+ }+}++fn remove_pre_install_staging(extract_dir: &Path, backup_dir: &Path) {+ if extract_dir.exists() {+ match fs::remove_dir_all(extract_dir) {+ Ok(()) => logger::info(format!(+ "[installer] removed pre-install extract staging {}",+ extract_dir.display()+ )),+ Err(error) => logger::warn(format!(+ "[installer] could not remove extract staging {}: {error}",+ extract_dir.display()+ )),+ }+ }+ if backup_dir.exists() {+ match fs::remove_dir_all(backup_dir) {+ Ok(()) => logger::info(format!(+ "[installer] removed pre-install backup staging {}",+ backup_dir.display()+ )),+ Err(error) => logger::warn(format!(+ "[installer] could not remove backup staging {}: {error}",+ backup_dir.display()+ )),+ }+ }+}++fn discard_staged_archives(args: &CliArgs) {+ let retry = retry_archive(args);+ if args.zip != retry {+ if fs::remove_file(&args.zip).is_ok() {+ logger::info(format!(+ "[retry] removed terminal archive from staged path {}",+ args.zip.display()+ ));+ }+ }+ if fs::remove_file(&retry).is_ok() {+ logger::info(format!(+ "[retry] removed terminal archive from {}",+ retry.display()+ ));+ }+}++pub(crate) fn ensure_retry_processes_closed(args: &CliArgs) -> Result<(), String> {+ let mut sys = System::new();+ if collect_appdir_processes(&mut sys, &args.app_dir, std::process::id()).is_empty() {+ Ok(())+ } else {+ Err("processes_running".into())+ }+}++/// Recreate the original updater arguments from trusted Rust state for a user-initiated retry.+/// An elevated updater keeps its internal marker so a retry does not accidentally start an+/// elevated process as if it were unelevated; the original process omits it and follows UAC again.+/// Retry continues in the already-loaded process so Windows does not search `%TEMP%`+/// for `vcruntime140*.dll` beside a freshly spawned updater executable.+pub(crate) fn retry_args(args: &CliArgs) -> CliArgs {+ CliArgs {+ zip: retry_archive(args),+ app_dir: args.app_dir.clone(),+ exe_name: args.exe_name.clone(),+ install_key: args.install_key.clone(),+ pid: 0,+ log: args.log.clone(),+ lock: args.lock.clone(),+ workdir: args.workdir.clone(),+ theme: args.theme,+ elevated: args.elevated,+ zip_sha256: args.zip_sha256.clone(),+ install_writable: args.install_writable,+ }+}++#[cfg(test)]+pub(crate) fn retry_cli_args(args: &CliArgs) -> Vec<std::ffi::OsString> {+ let retry = retry_args(args);+ let theme = match retry.theme {+ ThemeArg::Light => "light",+ ThemeArg::Dark => "dark",+ ThemeArg::Auto => "auto",+ };+ let values = [+ ("--zip", retry.zip.as_os_str()),+ ("--app-dir", retry.app_dir.as_os_str()),+ ("--exe-name", std::ffi::OsStr::new(&retry.exe_name)),+ ("--pid", std::ffi::OsStr::new("0")),+ ("--log", retry.log.as_os_str()),+ ("--lock", retry.lock.as_os_str()),+ ("--workdir", retry.workdir.as_os_str()),+ ("--theme", std::ffi::OsStr::new(theme)),+ ];+ let mut result = values+ .into_iter()+ .flat_map(|(key, value)| [std::ffi::OsString::from(key), value.to_os_string()])+ .collect::<Vec<_>>();+ if let Some(digest) = retry+ .zip_sha256+ .as_deref()+ .filter(|digest| !digest.is_empty())+ {+ result.push(std::ffi::OsString::from("--zip-sha256"));+ result.push(std::ffi::OsString::from(digest));+ }+ if retry.elevated {+ result.push(std::ffi::OsString::from("--elevated"));+ }+ result+}++fn run_inner<F: FnMut(InstallerEvent)>(+ args: &CliArgs,+ held_lock: Option<UpdateLock>,+ emit: &mut F,+) -> Result<UpdateLock, InstallerFailure> {+ logger::info(format!(+ "[installer] zip={} app_dir={} exe_name={} pid={}",+ args.zip.display(),+ args.app_dir.display(),+ args.exe_name,+ args.pid+ ));++ // 1. Wait for the main process to exit.+ emit(InstallerEvent::Phase(+ Phase::Waiting,+ format!("等待 PID {} 退出…", args.pid),+ ));+ let is_retry = args.zip == retry_archive(args);+ let exited = is_retry || pid_wait::wait_for_exit(args.pid, PID_WAIT_TIMEOUT);+ let needs_uac = matches!(needs_elevation(&args.app_dir), Ok(true));+ let process_elevated = args.elevated || process_is_elevated();+ let pre_elevation_retry = pre_elevation_failure_can_retry(needs_uac, process_elevated);+ if !exited {+ return Err(InstallerFailure::new(+ "主程序在 60 秒内没有退出,更新中止",+ pre_elevation_retry,+ ));+ }+ emit(InstallerEvent::AppExited);+ let identity = capture_install_dir_identity(&args.app_dir).map_err(|error| {+ InstallerFailure::new(+ format!("无法钉住安装目录 {}:{error}", args.app_dir.display()),+ false,+ )+ })?;+ if identity.is_reparse {+ return Err(InstallerFailure::new(+ format!("安装目录是重解析点,拒绝继续:{}", args.app_dir.display()),+ false,+ ));+ }+ std::thread::sleep(FS_SETTLE_DELAY);+ ensure_install_dir_unchanged(&identity, &args.app_dir)+ .map_err(|error| InstallerFailure::new(error, false))?;+ let pinned = open_install_dir_handle(&args.app_dir, &identity).map_err(|error| {+ InstallerFailure::new(+ format!("无法钉住安装目录 {}:{error}", args.app_dir.display()),+ false,+ )+ })?;++ // 1.2. Terminate lingering processes that run FROM app_dir. pid_wait only+ // covers the one main-process PID, but executables living inside the+ // install dir can outlive it and keep image locks on files we're+ // about to replace. Real-world case: the bundled Android adb.exe —+ // any adb invocation self-forks a persistent `adb server` daemon+ // that survives app exit and made both the replace AND the rollback+ // fail with os error 32 (sharing violation). Windows never allows+ // overwriting a running executable, so these must be gone first.+ if is_retry {+ ensure_retry_processes_closed(args)+ .map_err(|error| InstallerFailure::new(error, pre_elevation_retry))?;+ } else {+ terminate_appdir_processes(&args.app_dir, emit)+ .map_err(|error| InstallerFailure::new(error.to_string(), pre_elevation_retry))?;+ }++ // 1.5. Permission probe → optional self-elevation. Run BEFORE the lock+ // write so cancelling UAC leaves zero on-disk state. If app_dir is+ // not user-writable (e.g. installed under D:\ or C:\Program Files\+ // with admin-only ACL), relaunch ourselves via ShellExecuteExW(runas)+ // so the elevated child can replace the files. Default install path+ // (%LOCALAPPDATA%\xdt-maker) probes successfully → no UAC, no change+ // from prior behavior.+ match needs_elevation(&args.app_dir) {+ Ok(true) if !process_elevated => {+ logger::info(format!(+ "[elevate] app_dir {} not user-writable, requesting UAC elevation",+ args.app_dir.display()+ ));+ emit(InstallerEvent::Phase(+ Phase::RequestingElevation,+ "需要管理员权限,请在弹出的 UAC 提示中点击「是」…".into(),+ ));+ if !may_self_elevate(is_retry) {+ return Err(InstallerFailure::keep_archive(+ "需要管理员权限,但无法从当前更新器再次请求授权。请关闭此窗口后重新检查更新",+ ));+ }+ match self_elevate(args) {+ Ok(()) => {+ logger::info("[elevate] elevated child spawned, exiting original updater");+ // Hard-exit: the elevated child has its own UI and event+ // loop. Returning Ok() here would emit Done → flash+ // "更新完成" briefly in this (now-stale) window. No lock+ // was created, no app_dir state was touched — clean exit.+ std::process::exit(0);+ }+ Err(ElevateError::UserCancelled) => {+ return Err(InstallerFailure::keep_archive(+ "用户取消了管理员授权,更新已取消",+ ));+ }+ Err(ElevateError::Other(e)) => {+ return Err(InstallerFailure::keep_archive(format!(+ "请求管理员权限失败:{}",+ e+ )));+ }+ }+ }+ Ok(true) => {+ // Already elevated and STILL can't write — not a permission+ // problem. Most likely an antivirus / EDR holds a file open.+ // Bail with a directive error rather than looping UAC.+ return Err(InstallerFailure::new(+ format!(+ "无法写入安装目录 {} (已使用管理员权限)。可能被杀软或其他进程锁定,请将该目录加入杀软白名单后重试",+ args.app_dir.display()+ ),+ true,+ ));+ }+ Ok(false) => {+ // Writable; continue normal flow.+ }+ Err(e) => {+ // Probe itself errored unexpectedly. Don't block the update —+ // fall through to the existing copy_with_retry which has its own+ // diagnostics. Logged in needs_elevation already.+ logger::warn(format!(+ "[elevate] probe inconclusive ({}); proceeding without elevation",+ e+ ));+ }+ }++ // Acquire the exclusive lock only after any UAC handoff. The unelevated+ // parent must not leave `.updating` behind for the elevated child.+ let lock = match held_lock {+ Some(lock) => lock,+ None => acquire_update_lock(&args.lock).map_err(|_| {+ InstallerFailure::keep_archive(+ "另一项更新正在进行,请关闭此窗口后重新检查更新",+ )+ })?,+ };++ let install = (|| -> Result<(), InstallerFailure> {+ let (extract_dir, backup_dir) = staging_dirs(args);+ remove_staging_dir(&extract_dir)+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;+ remove_staging_dir(&backup_dir)+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;+ ensure_staging_directory(&extract_dir, args)+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;+ logger::info(format!("[installer] extract_dir={}", extract_dir.display()));+ extract_zip(&args.zip, &extract_dir, args.zip_sha256.as_deref(), |done, total| {+ let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };+ emit(InstallerEvent::Progress(+ Phase::Extracting,+ format!("解压中 {}/{}", done, total),+ pct,+ ));+ })+ .map_err(|error| {+ let can_retry = extract_error_can_retry(&error);+ InstallerFailure {+ message: error.to_string(),+ can_retry,+ discard_archive: !can_retry,+ keep_backup: false,+ install_unmodified: true,+ install_restored: false,+ lock: None,+ }+ })?;++ // 3.5. Selective backup: copy *only* the files in app_dir that the new+ // release is about to overwrite. Files that exist in the old version+ // but not in the new release are left untouched (we never delete),+ // so there's nothing to restore for them. Failure here aborts BEFORE+ // app_dir is modified — install is safe to abort with no rollback.+ emit(InstallerEvent::Phase(+ Phase::BackingUp,+ "备份当前版本…".into(),+ ));+ pinned+ .ensure()+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;+ ensure_staging_directory(&backup_dir, args)+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;+ logger::info(format!("[installer] backup_dir={}", backup_dir.display()));+ snapshot_overwritten_files(&extract_dir, &pinned, &backup_dir, |done, total| {+ let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };+ emit(InstallerEvent::Progress(+ Phase::BackingUp,+ format!("备份 {}/{}", done, total),+ pct,+ ));+ })+ .map_err(|error| InstallerFailure::new(error.to_string(), true))?;++ // 4–6. The risky window: replace files, drop lock, launch, verify.+ // Wrapped so any failure triggers rollback before bubbling out.+ let install_result: anyhow::Result<()> = (|| {+ // 4. Copy new files over app_dir through the pinned directory handle.+ copy_tree_into_pinned(&extract_dir, &pinned, |done, total| {+ let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };+ emit(InstallerEvent::Progress(+ Phase::Replacing,+ format!("替换 {}/{}", done, total),+ pct,+ ));+ })?;++ // 5. Verify exe exists, launch detached, verify it actually came up.+ // The exclusive lock stays held until `run` finishes so a racing+ // Cindy start waits at the .updating file.+ let exe_path = pinned+ .join(Path::new(&args.exe_name))+ .map_err(|error| anyhow::anyhow!(error))?;+ if !exe_path.exists() {+ anyhow::bail!(+ "新版本主程序缺失:{} 在替换后不存在",+ exe_path.display()+ );+ }+ emit(InstallerEvent::Phase(+ Phase::Launching,+ "启动新版本…".into(),+ ));+ match launch_app_exe(args, &exe_path)? {+ AppLaunch::Started => {+ if !poll_until_process_running(&args.exe_name, LAUNCH_VERIFY_TIMEOUT) {+ anyhow::bail!(+ "新进程 {} 在启动 {} 秒后未出现,可能被杀软拦截或新可执行文件损坏",+ args.exe_name,+ LAUNCH_VERIFY_TIMEOUT.as_secs()+ );+ }+ }+ AppLaunch::Skipped => {+ anyhow::bail!(+ "已替换文件,但无法在不继承管理员权限的情况下启动 Cindy"+ );+ }+ }+ Ok(())+ })();++ // Always clean extracted files. Keep the zip only after a successful+ // rollback so the user can retry; retain the backup if rollback failed.+ let cleanup_staging = |remove_zip: bool| {+ let _ = fs::remove_dir_all(&extract_dir);+ if remove_zip {+ let _ = fs::remove_file(&args.zip);+ }+ };++ match install_result {+ Ok(()) => {+ logger::info(format!(+ "[installer] LAUNCH VERIFIED: {} is running",+ args.exe_name+ ));+ // Best-effort metadata only, outside the file replacement/rollback+ // transaction. Reuse existing elevation for HKLM, never request it.+ if let Some(key) = installation_version_key(args) {+ crate::installation_version::sync(&args.app_dir.join(&args.exe_name), &key);+ }+ let _ = fs::remove_dir_all(&backup_dir);+ cleanup_staging(true);+ Ok(())+ }+ Err(install_err) => {+ logger::error(format!("[installer] install failed: {install_err}"));+ emit(InstallerEvent::Phase(+ Phase::RollingBack,+ "更新失败,正在回滚到旧版本…".into(),+ ));+ if let Err(error) = pinned.ensure() {+ return Err(InstallerFailure::new(error.to_string(), false));+ }+ match rollback_into_pinned(&backup_dir, &pinned, |done, total| {+ let pct = if total == 0 { -1 } else { (done * 100 / total).min(100) as i32 };+ emit(InstallerEvent::Progress(+ Phase::RollingBack,+ format!("回滚 {}/{}", done, total),+ pct,+ ));+ }) {+ Ok(()) => {+ logger::info("[installer] rollback succeeded");+ let _ = fs::remove_dir_all(&backup_dir);+ // Keep the archive so the user can retry after a failed+ // replacement/launch once rollback restored the old app.+ cleanup_staging(false);+ // Remove the archive from Electron's auto-apply path BEFORE+ // restarting the restored app. If isolation fails, retain+ // the original no-retry cleanup behavior.+ let can_retry = prepare_retry_archive(args);+ if !can_retry {+ let _ = fs::remove_file(&args.zip);+ }+ // Keep Cindy closed while Retry is still available. Isolation+ // here is preliminary: finalize_retry_state may still withdraw+ // Retry after a digest check, and only then may we relaunch.+ logger::info(+ "[installer] skipping Cindy relaunch until Retry is finally allowed or withdrawn",+ );+ return Err(InstallerFailure {+ message: format!("{} (已回滚到旧版本)", install_err),+ can_retry,+ discard_archive: !can_retry,+ keep_backup: false,+ install_unmodified: false,+ install_restored: true,+ lock: None,+ });+ }+ Err(rb_err) => {+ logger::error(format!(+ "[installer] ROLLBACK ALSO FAILED: {rb_err} — appDir is now in an inconsistent state, see {}",+ backup_dir.display()+ ));+ // KEEP backup_dir — user / support may need to manually+ // restore. Staging is still cleaned (it's never useful+ // for recovery, only the backup is).+ cleanup_staging(true);+ return Err(InstallerFailure {+ message: format!(+ "{} (回滚也失败:{};备份保留在 {} 供手动恢复)",+ install_err,+ rb_err,+ backup_dir.display()+ ),+ can_retry: false,+ discard_archive: true,+ keep_backup: true,+ install_unmodified: false,+ install_restored: false,+ lock: None,+ });+ }+ }+ }+ }+ })();+ match install {+ Ok(()) => Ok(lock),+ Err(failure) => Err(failure.with_lock(lock)),+ }+}++/// Remove a staging directory before reusing its path. A missing path is+/// already clean; a file at the directory path is an error rather than a+/// silently ignored collision.+fn remove_staging_dir(path: &Path) -> io::Result<()> {+ match fs::symlink_metadata(path) {+ Ok(metadata) if metadata.is_dir() => fs::remove_dir_all(path),+ Ok(_) => Err(io::Error::new(+ io::ErrorKind::AlreadyExists,+ format!("staging path is not a directory: {}", path.display()),+ )),+ Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),+ Err(error) => Err(error),+ }+}++/// Sweep stale `xdt-update-*` dirs/files from %TEMP% that are older than+/// `MAX_AGE_DAYS`. Last-resort cleanup for backups left behind by failed+/// rollbacks (which we intentionally do NOT auto-delete at end-of-run).+/// Best-effort: any IO failure is ignored — sweeping is purely housekeeping.+pub fn sweep_stale_temp_dirs() {+ // TEMP still uses the historical prefixes:+ // - cindy-update-{ts}/ current workdir layout (2026-07 rebrand)+ // - xdt-update-{ts}/ legacy workdir layout+ // - xdt-update-extract-{ts}/ legacy (pre-workdir refactor)+ // - xdt-update-rollback-{ts}/ legacy+ // - xdt-updater-{ts}.exe legacy standalone updater binary+ // ProgramData only deletes this updater's `cindy-update-{millis}` roots.+ let now = std::time::SystemTime::now();+ sweep_stale_under(&std::env::temp_dir(), SweepNameFilter::TempPrefixes, now);+ sweep_stale_under(+ &protected_staging_base(),+ SweepNameFilter::ProgramDataOwned,+ now,+ );+}++#[derive(Clone, Copy)]+enum SweepNameFilter {+ TempPrefixes,+ ProgramDataOwned,+}++/// Exact `cindy-update-{ascii-digits}` used as the High-IL private root.+/// Prefix matches such as `cindy-update-service` are not this updater's.+pub(crate) fn is_owned_program_data_staging_name(name: &str) -> bool {+ let Some(rest) = name.strip_prefix("cindy-update-") else {+ return false;+ };+ !rest.is_empty() && rest.bytes().all(|b| b.is_ascii_digit())+}++fn name_matches_sweep(name: &str, filter: SweepNameFilter) -> bool {+ match filter {+ SweepNameFilter::TempPrefixes => {+ name.starts_with("cindy-update") || name.starts_with("xdt-update")+ }+ SweepNameFilter::ProgramDataOwned => is_owned_program_data_staging_name(name),+ }+}++fn sweep_stale_under(root: &Path, filter: SweepNameFilter, now: std::time::SystemTime) {+ const MAX_AGE_SECS: u64 = 7 * 24 * 60 * 60; // 7 days+ let entries = match fs::read_dir(root) {+ Ok(it) => it,+ Err(_) => return,+ };+ let mut swept = 0u32;+ for entry in entries.flatten() {+ let name = entry.file_name();+ let name_str = name.to_string_lossy();+ if !name_matches_sweep(&name_str, filter) {+ continue;+ }+ let meta = match entry.metadata() {+ Ok(m) => m,+ Err(_) => continue,+ };+ let modified = match meta.modified() {+ Ok(m) => m,+ Err(_) => continue,+ };+ let age_secs = now.duration_since(modified).map(|d| d.as_secs()).unwrap_or(0);+ if age_secs < MAX_AGE_SECS {+ continue;+ }+ let path = entry.path();+ let removed = if meta.is_dir() {+ fs::remove_dir_all(&path).is_ok()+ } else {+ fs::remove_file(&path).is_ok()+ };+ if removed {+ swept += 1;+ logger::info(format!(+ "[sweep] removed stale {} (age {}d)",+ path.display(),+ age_secs / (24 * 60 * 60),+ ));+ }+ }+ if swept > 0 {+ logger::info(format!("[sweep] cleaned {} stale temp entries", swept));+ }+}++/// Pull the `{ts}` slice out of a `xdt-update-{ts}` workdir basename so+/// child names can echo the same timestamp. Falls back to a fresh chrono+/// timestamp if the basename doesn't match the convention — the dir still+/// works either way, the suffix is just for human-readable forensics.+fn workdir_ts(workdir: &Path) -> String {+ workdir+ .file_name()+ .and_then(|n| n.to_str())+ .and_then(|n| n.strip_prefix("cindy-update-").or_else(|| n.strip_prefix("xdt-update-")))+ .map(|s| s.to_string())+ .unwrap_or_else(|| chrono::Local::now().timestamp_millis().to_string())+}++fn extract_error_can_retry(error: &anyhow::Error) -> bool {+ !error.chain().any(|cause| {+ matches!(+ cause.downcast_ref::<zip::result::ZipError>(),+ Some(+ zip::result::ZipError::InvalidArchive(_)+ | zip::result::ZipError::UnsupportedArchive(_)+ )+ )+ })+}++fn extract_zip<F: FnMut(u64, u64)>(+ zip_path: &Path,+ dest: &Path,+ expected_sha256: Option<&str>,+ mut on_progress: F,+) -> anyhow::Result<()> {+ let mut file = open_regular_file(zip_path)?;+ let digest = sha256_hex(&mut file)?;+ match expected_sha256 {+ Some(expected) if expected.eq_ignore_ascii_case(&digest) => {}+ Some(_) => anyhow::bail!("archive digest mismatch"),+ None => anyhow::bail!("archive digest missing"),+ }+ let mut archive = zip::ZipArchive::new(file)?;+ let total = archive.len() as u64;+ on_progress(0, total);++ for i in 0..archive.len() {+ let mut entry = archive.by_index(i)?;+ let outpath = match entry.enclosed_name() {+ Some(p) => dest.join(p),+ None => continue,+ };+ if entry.is_dir() {+ fs::create_dir_all(&outpath)?;+ } else {+ if let Some(parent) = outpath.parent() {+ fs::create_dir_all(parent)?;+ }+ let mut out = File::create(&outpath)?;+ io::copy(&mut entry, &mut out)?;+ }+ on_progress((i as u64) + 1, total);+ }+ Ok(())+}++/// Walk `extract_dir` and copy every file that ALREADY exists at the+/// equivalent relative path under `app_dir` into `backup_dir`, mirroring+/// the directory structure. Files in app_dir that the new release does+/// NOT overwrite stay where they are — they're still intact post-rollback.+fn snapshot_overwritten_files<F: FnMut(u64, u64)>(+ extract_dir: &Path,+ app_dir: &PinnedInstallDir,+ backup_dir: &Path,+ mut on_progress: F,+) -> anyhow::Result<()> {+ let entries: Vec<_> = walkdir::WalkDir::new(extract_dir)+ .into_iter()+ .filter_map(Result::ok)+ .filter(|e| e.file_type().is_file())+ .collect();+ let total = entries.len() as u64;+ on_progress(0, total);++ for (idx, entry) in entries.iter().enumerate() {+ let rel = entry.path().strip_prefix(extract_dir)?;+ let appfile = app_dir.join(rel)?;+ if appfile.exists() {+ let backup_path = backup_dir.join(rel);+ if let Some(parent) = backup_path.parent() {+ fs::create_dir_all(parent)?;+ }+ copy_with_retry(&appfile, &backup_path)?;+ }+ on_progress((idx as u64) + 1, total);+ }+ Ok(())+}++/// Reverse of `snapshot_overwritten_files`: copy every file in `backup_dir`+/// back over `app_dir`. Any new files added by the failed install remain as+/// orphans in app_dir — harmless, the next clean install would remove them+/// — but the originals are restored so the old version still works. Backup+/// walk errors fail this function so callers keep the backup and disable retry.+fn rollback<F: FnMut(u64, u64)>(+ backup_dir: &Path,+ app_dir: &Path,+ mut on_progress: F,+) -> anyhow::Result<()> {+ let mut entries = Vec::new();+ for entry in walkdir::WalkDir::new(backup_dir) {+ let entry = entry?;+ if entry.file_type().is_file() {+ entries.push(entry);+ }+ }+ let total = entries.len() as u64;+ on_progress(0, total);++ for (idx, entry) in entries.iter().enumerate() {+ let rel = entry.path().strip_prefix(backup_dir)?;+ let target = app_dir.join(rel);+ if let Some(parent) = target.parent() {+ fs::create_dir_all(parent)?;+ }+ copy_with_retry(entry.path(), &target)?;+ on_progress((idx as u64) + 1, total);+ }+ Ok(())+}++fn rollback_into_pinned<F: FnMut(u64, u64)>(+ backup_dir: &Path,+ app_dir: &PinnedInstallDir,+ mut on_progress: F,+) -> anyhow::Result<()> {+ let mut entries = Vec::new();+ for entry in walkdir::WalkDir::new(backup_dir) {+ let entry = entry?;+ if entry.file_type().is_file() {+ entries.push(entry);+ }+ }+ let total = entries.len() as u64;+ on_progress(0, total);++ for (idx, entry) in entries.iter().enumerate() {+ let rel = entry.path().strip_prefix(backup_dir)?;+ let target = app_dir.join(rel)?;+ if let Some(parent) = target.parent() {+ fs::create_dir_all(parent)?;+ }+ let target = app_dir.join(rel)?;+ copy_with_retry(entry.path(), &target)?;+ on_progress((idx as u64) + 1, total);+ }+ Ok(())+}++pub(crate) fn copy_tree_into_pinned<F: FnMut(u64, u64)>(+ src: &Path,+ dst: &PinnedInstallDir,+ mut on_progress: F,+) -> anyhow::Result<()> {+ dst.ensure()?;+ let entries: Vec<_> = walkdir::WalkDir::new(src)+ .into_iter()+ .filter_map(Result::ok)+ .collect();+ let total = entries.len() as u64;+ on_progress(0, total);++ for (idx, entry) in entries.iter().enumerate() {+ let rel = entry.path().strip_prefix(src)?;+ let target = dst.join(rel)?;+ if entry.file_type().is_dir() {+ fs::create_dir_all(&target)?;+ dst.join(rel)?;+ } else {+ if let Some(parent) = target.parent() {+ fs::create_dir_all(parent)?;+ }+ let target = dst.join(rel)?;+ copy_with_retry(entry.path(), &target)?;+ }+ on_progress((idx as u64) + 1, total);+ }+ Ok(())+}++/// Copy with a small retry loop. AV scanners or stale handles can briefly+/// hold a file open immediately after the main process exits, and a single+/// `fs::copy` will fail with PermissionDenied if it loses the race.+fn copy_with_retry(src: &Path, dst: &Path) -> io::Result<()> {+ const ATTEMPTS: u32 = 5;+ let mut last_err: Option<io::Error> = None;+ for attempt in 0..ATTEMPTS {+ match fs::copy(src, dst) {+ Ok(_) => return Ok(()),+ Err(e) => {+ logger::warn(format!(+ "[copy] attempt {} failed for {}: {}",+ attempt + 1,+ dst.display(),+ e+ ));+ last_err = Some(e);+ std::thread::sleep(Duration::from_millis(200 * (attempt + 1) as u64));+ }+ }+ }+ Err(last_err.unwrap_or_else(|| io::Error::other("copy_with_retry exhausted")))+}++// io::Error::other has been stable since Rust 1.74; we require >= 1.74 in CI.++fn launch_detached(exe: &Path) -> io::Result<()> {+ use std::process::Command;+ #[cfg(target_os = "windows")]+ {+ use std::os::windows::process::CommandExt;+ // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP — orphan the child so+ // updater can exit immediately without taking the new app down with it.+ const FLAGS: u32 = 0x00000008 | 0x00000200;+ Command::new(exe).creation_flags(FLAGS).spawn()?;+ }+ #[cfg(not(target_os = "windows"))]+ {+ Command::new(exe).spawn()?;+ }+ Ok(())+}++/// Launch Cindy with the linked medium token so a writable per-user install+/// does not inherit this process's high integrity.+fn launch_de_elevated(exe: &Path) -> io::Result<()> {+ #[cfg(target_os = "windows")]+ {+ return launch_with_linked_medium_token(exe);+ }+ #[cfg(not(target_os = "windows"))]+ {+ Err(io::Error::other(format!(+ "refusing to inherit elevation when launching {}",+ exe.display()+ )))+ }+}++fn ensure_staging_directory(path: &Path, args: &CliArgs) -> io::Result<()> {+ let private_root = elevated_private_staging_root(args);+ if uses_private_staging_acl(path, &private_root) {+ create_protected_staging_tree(path, &trusted_staging_ancestor())+ } else {+ fs::create_dir_all(path)+ }+}++static CREATED_PROTECTED_STAGING: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());++fn record_created_protected_dir(path: &Path) {+ let mut seen = CREATED_PROTECTED_STAGING+ .lock()+ .unwrap_or_else(|e| e.into_inner());+ if !seen.iter().any(|p| p == path) {+ seen.push(path.to_path_buf());+ }+}++fn dir_created_by_this_process(path: &Path) -> bool {+ CREATED_PROTECTED_STAGING+ .lock()+ .unwrap_or_else(|e| e.into_inner())+ .iter()+ .any(|p| p == path)+}++/// Create `path` under a verified ancestor. Existing planted directories are+/// refused: applying a DACL after `create_dir_all` does not revoke handles a+/// medium-integrity process already holds. Missing components are born with+/// the High-IL descriptor.+pub(crate) fn create_protected_staging_tree(+ path: &Path,+ trusted_ancestor: &Path,+) -> io::Result<()> {+ if !path.starts_with(trusted_ancestor) {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!(+ "staging path {} is outside {}",+ path.display(),+ trusted_ancestor.display()+ ),+ ));+ }+ verify_trusted_ancestor(trusted_ancestor)?;+ if path == trusted_ancestor {+ return Ok(());+ }+ let rel = path.strip_prefix(trusted_ancestor).unwrap_or(path);+ let mut cur = trusted_ancestor.to_path_buf();+ for component in rel.components() {+ cur.push(component);+ if dir_created_by_this_process(&cur) && cur.exists() && !is_reparse_point(&cur) {+ continue;+ }+ match create_directory_with_high_integrity(&cur) {+ Ok(()) => record_created_protected_dir(&cur),+ Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {+ return Err(io::Error::new(+ io::ErrorKind::AlreadyExists,+ format!(+ "refusing pre-created staging path {}",+ cur.display()+ ),+ ));+ }+ Err(error) => return Err(error),+ }+ }+ Ok(())+}++#[derive(Clone, Debug, PartialEq, Eq)]+pub(crate) struct InstallDirIdentity {+ pub is_reparse: bool,+ device: u64,+ inode: u64,+}++pub(crate) fn capture_install_dir_identity(path: &Path) -> io::Result<InstallDirIdentity> {+ let meta = fs::symlink_metadata(path)?;+ if !meta.is_dir() && !meta.file_type().is_symlink() {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("install path is not a directory: {}", path.display()),+ ));+ }+ Ok(InstallDirIdentity {+ is_reparse: is_reparse_point(path),+ device: file_device(&meta),+ inode: file_inode(&meta),+ })+}++pub(crate) fn install_dir_identity_unchanged(+ expected: &InstallDirIdentity,+ path: &Path,+) -> bool {+ match capture_install_dir_identity(path) {+ Ok(actual) => {+ !actual.is_reparse+ && !expected.is_reparse+ && actual.device == expected.device+ && actual.inode == expected.inode+ }+ Err(_) => false,+ }+}++fn ensure_install_dir_unchanged(+ expected: &InstallDirIdentity,+ path: &Path,+) -> Result<(), String> {+ if install_dir_identity_unchanged(expected, path) {+ Ok(())+ } else {+ Err(format!(+ "安装目录在更新过程中被替换或变成重解析点,拒绝继续:{}",+ path.display()+ ))+ }+}++/// Open handle to `app_dir` that keeps the directory from being replaced for+/// the rest of backup/copy/rollback. Writes go through `join`, which+/// revalidates identity so a swapped junction cannot receive files.+pub(crate) struct PinnedInstallDir {+ path: PathBuf,+ identity: InstallDirIdentity,+ _hold: File,+}++pub(crate) fn open_install_dir_handle(+ path: &Path,+ identity: &InstallDirIdentity,+) -> io::Result<PinnedInstallDir> {+ if identity.is_reparse {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("install path is a reparse point: {}", path.display()),+ ));+ }+ let hold = open_directory_handle(path)?;+ let pinned = PinnedInstallDir {+ path: path.to_path_buf(),+ identity: identity.clone(),+ _hold: hold,+ };+ pinned.ensure()?;+ Ok(pinned)+}++impl PinnedInstallDir {+ fn ensure(&self) -> io::Result<()> {+ ensure_install_dir_unchanged(&self.identity, &self.path)+ .map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))+ }++ pub(crate) fn join(&self, rel: &Path) -> io::Result<PathBuf> {+ self.ensure()?;+ if rel.is_absolute() {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("refusing absolute install path {}", rel.display()),+ ));+ }+ let mut cur = self.path.clone();+ for component in rel.components() {+ match component {+ std::path::Component::Normal(name) => cur.push(name),+ std::path::Component::CurDir => continue,+ _ => {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("refusing install path component {}", rel.display()),+ ));+ }+ }+ if cur.exists() && is_reparse_point(&cur) {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("install path is a reparse point: {}", cur.display()),+ ));+ }+ }+ Ok(cur)+ }+}++fn open_directory_handle(path: &Path) -> io::Result<File> {+ let mut options = fs::OpenOptions::new();+ options.read(true);+ #[cfg(windows)]+ {+ use std::os::windows::fs::OpenOptionsExt;+ const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;+ const FILE_SHARE_READ: u32 = 0x0000_0001;+ const FILE_SHARE_WRITE: u32 = 0x0000_0002;+ options+ .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)+ .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE);+ }+ let file = options.open(path)?;+ if is_reparse_point(path) {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("install path is a reparse point: {}", path.display()),+ ));+ }+ Ok(file)+}++#[cfg(windows)]+fn directory_medium_token_can_modify_windows(app_dir: &Path) -> Option<bool> {+ use windows_sys::Win32::Storage::FileSystem::{+ FILE_FLAG_BACKUP_SEMANTICS, WRITE_DAC, WRITE_OWNER,+ };+ let check = || match directory_grants_add_file(app_dir) {+ Some(true) => Some(true),+ other => {+ if acl_control_pins_install_writable(+ path_grants_access(app_dir, WRITE_DAC, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(app_dir, WRITE_OWNER, FILE_FLAG_BACKUP_SEMANTICS),+ ) {+ Some(true)+ } else {+ other+ }+ }+ };+ if process_is_elevated() {+ with_medium_integrity(check).flatten()+ } else {+ check()+ }+}++#[cfg(windows)]+fn directory_grants_add_file(app_dir: &Path) -> Option<bool> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};+ use windows_sys::Win32::Storage::FileSystem::{+ CreateFileW, FILE_ADD_FILE, FILE_ADD_SUBDIRECTORY, FILE_FLAG_BACKUP_SEMANTICS,+ FILE_SHARE_READ, OPEN_EXISTING,+ };++ let wide: Vec<u16> = app_dir+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let handle = unsafe {+ CreateFileW(+ wide.as_ptr(),+ FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY,+ FILE_SHARE_READ,+ std::ptr::null(),+ OPEN_EXISTING,+ FILE_FLAG_BACKUP_SEMANTICS,+ std::ptr::null_mut(),+ )+ };+ if handle == INVALID_HANDLE_VALUE || handle.is_null() {+ let err = io::Error::last_os_error();+ if err.kind() == io::ErrorKind::PermissionDenied {+ return Some(false);+ }+ return None;+ }+ unsafe {+ let _ = CloseHandle(handle);+ }+ Some(true)+}++#[cfg(windows)]+fn existing_install_files_medium_writable_windows(app_dir: &Path, exe_name: &str) -> Option<bool> {+ let check = || {+ if runtime_trees_pin_install_writable(app_dir) {+ return Some(true);+ }+ let mut saw_existing = false;+ for path in medium_writable_install_file_candidates(app_dir, exe_name) {+ if !path.exists() {+ continue;+ }+ if loadable_input_is_reparse(&path) {+ return Some(true);+ }+ saw_existing = true;+ match file_is_medium_replaceable(&path, app_dir) {+ Some(true) => return Some(true),+ Some(false) | None => {}+ }+ }+ saw_existing.then_some(false)+ };+ if process_is_elevated() {+ with_medium_integrity(check).flatten()+ } else {+ check()+ }+}++#[cfg(windows)]+fn file_is_medium_replaceable(path: &Path, app_dir: &Path) -> Option<bool> {+ use windows_sys::Win32::Storage::FileSystem::{+ DELETE, FILE_ADD_FILE, FILE_ADD_SUBDIRECTORY, FILE_APPEND_DATA, FILE_ATTRIBUTE_NORMAL,+ FILE_DELETE_CHILD, FILE_FLAG_BACKUP_SEMANTICS, FILE_WRITE_DATA, WRITE_DAC, WRITE_OWNER,+ };++ let ancestors = runtime_path_ancestors(path, app_dir);+ let parent = ancestors.first().map(|dir| dir.as_path());+ let replaceable = file_is_medium_replaceable_from_access(+ file_grants_generic_write(path),+ path_grants_access(path, DELETE, FILE_ATTRIBUTE_NORMAL),+ parent.and_then(|dir| {+ path_grants_access(dir, FILE_DELETE_CHILD, FILE_FLAG_BACKUP_SEMANTICS)+ }),+ parent.and_then(|dir| path_grants_access(dir, FILE_ADD_FILE, FILE_FLAG_BACKUP_SEMANTICS)),+ path_grants_access(path, FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL),+ path_grants_access(path, FILE_APPEND_DATA, FILE_ATTRIBUTE_NORMAL),+ );+ if matches!(replaceable, Some(true)) {+ return Some(true);+ }+ if acl_control_pins_install_writable(+ path_grants_access(path, WRITE_DAC, FILE_ATTRIBUTE_NORMAL),+ path_grants_access(path, WRITE_OWNER, FILE_ATTRIBUTE_NORMAL),+ ) {+ return Some(true);+ }+ for dir in &ancestors {+ if loadable_input_is_reparse(dir) {+ return Some(true);+ }+ if ancestor_control_pins_install_writable(+ path_grants_access(dir, FILE_DELETE_CHILD, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(dir, FILE_ADD_FILE, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(dir, WRITE_DAC, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(dir, WRITE_OWNER, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(dir, DELETE, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(dir, FILE_ADD_SUBDIRECTORY, FILE_FLAG_BACKUP_SEMANTICS),+ ) {+ return Some(true);+ }+ }+ replaceable+}++#[cfg(windows)]+fn file_grants_generic_write(path: &Path) -> Option<bool> {+ use windows_sys::Win32::Foundation::GENERIC_WRITE;+ use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_NORMAL;+ path_grants_access(path, GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL)+}++#[cfg(windows)]+fn install_root_parent_is_medium_replaceable_windows(app_dir: &Path) -> bool {+ use windows_sys::Win32::Storage::FileSystem::{+ DELETE, FILE_ADD_SUBDIRECTORY, FILE_DELETE_CHILD, FILE_FLAG_BACKUP_SEMANTICS, WRITE_DAC,+ WRITE_OWNER,+ };+ let check = || {+ for ancestor in install_root_ancestors(app_dir) {+ if loadable_input_is_reparse(&ancestor) {+ return true;+ }+ if ancestor_control_pins_install_writable(+ path_grants_access(&ancestor, FILE_DELETE_CHILD, FILE_FLAG_BACKUP_SEMANTICS),+ None,+ path_grants_access(&ancestor, WRITE_DAC, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(&ancestor, WRITE_OWNER, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(&ancestor, DELETE, FILE_FLAG_BACKUP_SEMANTICS),+ path_grants_access(&ancestor, FILE_ADD_SUBDIRECTORY, FILE_FLAG_BACKUP_SEMANTICS),+ ) {+ return true;+ }+ }+ false+ };+ if process_is_elevated() {+ with_medium_integrity(check).unwrap_or(true)+ } else {+ check()+ }+}++#[cfg(windows)]+fn path_grants_access(path: &Path, desired_access: u32, flags: u32) -> Option<bool> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};+ use windows_sys::Win32::Storage::FileSystem::{CreateFileW, FILE_SHARE_READ, OPEN_EXISTING};++ let wide: Vec<u16> = path+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let handle = unsafe {+ CreateFileW(+ wide.as_ptr(),+ desired_access,+ FILE_SHARE_READ,+ std::ptr::null(),+ OPEN_EXISTING,+ flags,+ std::ptr::null_mut(),+ )+ };+ if handle == INVALID_HANDLE_VALUE || handle.is_null() {+ let err = io::Error::last_os_error();+ return file_write_probe_from_os_error(err.kind(), err.raw_os_error());+ }+ unsafe {+ let _ = CloseHandle(handle);+ }+ Some(true)+}++#[cfg(unix)]+fn file_device(meta: &fs::Metadata) -> u64 {+ use std::os::unix::fs::MetadataExt;+ meta.dev()+}++#[cfg(unix)]+fn file_inode(meta: &fs::Metadata) -> u64 {+ use std::os::unix::fs::MetadataExt;+ meta.ino()+}++#[cfg(windows)]+fn file_device(meta: &fs::Metadata) -> u64 {+ use std::os::windows::fs::MetadataExt;+ meta.volume_serial_number().unwrap_or(0) as u64+}++#[cfg(windows)]+fn file_inode(meta: &fs::Metadata) -> u64 {+ use std::os::windows::fs::MetadataExt;+ meta.file_index().unwrap_or(0)+}++#[cfg(windows)]+fn directory_owned_by_current_user_windows(app_dir: &Path) -> Option<bool> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, LocalFree, ERROR_SUCCESS};+ use windows_sys::Win32::Security::{+ EqualSid, GetTokenInformation, TOKEN_QUERY, TOKEN_USER, TokenUser,+ };+ use windows_sys::Win32::Security::Authorization::{+ GetNamedSecurityInfoW, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION,+ };+ use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};++ let wide: Vec<u16> = app_dir+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let mut owner = std::ptr::null_mut();+ let mut sd = std::ptr::null_mut();+ let status = unsafe {+ GetNamedSecurityInfoW(+ wide.as_ptr(),+ SE_FILE_OBJECT,+ OWNER_SECURITY_INFORMATION,+ &mut owner,+ std::ptr::null_mut(),+ std::ptr::null_mut(),+ std::ptr::null_mut(),+ &mut sd,+ )+ };+ if status != ERROR_SUCCESS || owner.is_null() {+ if !sd.is_null() {+ unsafe {+ let _ = LocalFree(sd);+ }+ }+ return None;+ }+ struct SdGuard(*mut core::ffi::c_void);+ impl Drop for SdGuard {+ fn drop(&mut self) {+ if !self.0.is_null() {+ unsafe {+ let _ = LocalFree(self.0);+ }+ }+ }+ }+ let _sd = SdGuard(sd);++ unsafe {+ let mut token: HANDLE = std::ptr::null_mut();+ if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {+ return None;+ }+ struct TokenGuard(HANDLE);+ impl Drop for TokenGuard {+ fn drop(&mut self) {+ if !self.0.is_null() {+ unsafe {+ let _ = CloseHandle(self.0);+ }+ }+ }+ }+ let token = TokenGuard(token);+ let mut returned = 0u32;+ GetTokenInformation(+ token.0,+ TokenUser,+ std::ptr::null_mut(),+ 0,+ &mut returned,+ );+ if returned == 0 {+ return None;+ }+ let mut buffer = vec![0u8; returned as usize];+ if GetTokenInformation(+ token.0,+ TokenUser,+ buffer.as_mut_ptr().cast(),+ returned,+ &mut returned,+ ) == 0+ {+ return None;+ }+ let user = &*(buffer.as_ptr() as *const TOKEN_USER);+ if user.User.Sid.is_null() {+ return None;+ }+ Some(EqualSid(owner, user.User.Sid) != 0)+ }+}++fn verify_trusted_ancestor(path: &Path) -> io::Result<()> {+ if is_reparse_point(path) {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("trusted ancestor is a reparse point: {}", path.display()),+ ));+ }+ let meta = fs::symlink_metadata(path)?;+ if !meta.is_dir() {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("trusted ancestor is not a directory: {}", path.display()),+ ));+ }+ Ok(())+}++#[cfg(not(target_os = "windows"))]+fn is_reparse_point(path: &Path) -> bool {+ fs::symlink_metadata(path)+ .map(|meta| meta.file_type().is_symlink())+ .unwrap_or(false)+}++#[cfg(not(target_os = "windows"))]+fn create_directory_with_high_integrity(path: &Path) -> io::Result<()> {+ fs::create_dir(path)+}++#[cfg(target_os = "windows")]+fn high_integrity_security_descriptor() -> io::Result<ProtectedSecurityDescriptor> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Security::Authorization::{+ ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,+ };+ use windows_sys::Win32::Security::PSECURITY_DESCRIPTOR;++ // Admins + SYSTEM, protected DACL, High mandatory label with NO_WRITE_UP.+ let sddl: Vec<u16> = std::ffi::OsStr::new(+ "D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)S:(ML;;NW;;;HI)",+ )+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let mut sd: PSECURITY_DESCRIPTOR = std::ptr::null_mut();+ let ok = unsafe {+ ConvertStringSecurityDescriptorToSecurityDescriptorW(+ sddl.as_ptr(),+ SDDL_REVISION_1,+ &mut sd,+ std::ptr::null_mut(),+ )+ };+ if ok == 0 || sd.is_null() {+ return Err(io::Error::last_os_error());+ }+ Ok(ProtectedSecurityDescriptor(sd))+}++#[cfg(target_os = "windows")]+struct ProtectedSecurityDescriptor(windows_sys::Win32::Security::PSECURITY_DESCRIPTOR);++#[cfg(target_os = "windows")]+impl Drop for ProtectedSecurityDescriptor {+ fn drop(&mut self) {+ if !self.0.is_null() {+ unsafe {+ let _ = windows_sys::Win32::Foundation::LocalFree(self.0);+ }+ }+ }+}++#[cfg(target_os = "windows")]+fn is_reparse_point(path: &Path) -> bool {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Storage::FileSystem::{+ GetFileAttributesW, FILE_ATTRIBUTE_REPARSE_POINT, INVALID_FILE_ATTRIBUTES,+ };++ let wide: Vec<u16> = path+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let attrs = unsafe { GetFileAttributesW(wide.as_ptr()) };+ attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0+}++#[cfg(target_os = "windows")]+fn create_directory_with_high_integrity(path: &Path) -> io::Result<()> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;+ use windows_sys::Win32::Storage::FileSystem::CreateDirectoryW;++ if is_reparse_point(path) {+ return Err(io::Error::new(+ io::ErrorKind::InvalidInput,+ format!("staging path is a reparse point: {}", path.display()),+ ));+ }+ let sd = high_integrity_security_descriptor()?;+ let wide: Vec<u16> = path+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let attrs = SECURITY_ATTRIBUTES {+ nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,+ lpSecurityDescriptor: sd.0,+ bInheritHandle: 0,+ };+ let ok = unsafe { CreateDirectoryW(wide.as_ptr(), &attrs) };+ if ok == 0 {+ return Err(io::Error::last_os_error());+ }+ Ok(())+}++#[cfg(target_os = "windows")]+fn launch_with_linked_medium_token(exe: &Path) -> io::Result<()> {+ use std::os::windows::ffi::OsStrExt;+ use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};+ use windows_sys::Win32::Security::{+ DuplicateTokenEx, GetTokenInformation, SecurityImpersonation, TokenLinkedToken,+ TokenPrimary, TOKEN_ASSIGN_PRIMARY, TOKEN_DUPLICATE, TOKEN_LINKED_TOKEN, TOKEN_QUERY,+ TOKEN_ADJUST_DEFAULT, TOKEN_ADJUST_SESSIONID,+ };+ use windows_sys::Win32::System::Threading::{+ CreateProcessWithTokenW, GetCurrentProcess, OpenProcessToken, CREATE_NEW_PROCESS_GROUP,+ DETACHED_PROCESS, PROCESS_INFORMATION, STARTUPINFOW,+ };++ let exe_w: Vec<u16> = exe+ .as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect();+ let dir_w: Vec<u16> = exe+ .parent()+ .map(|dir| {+ dir.as_os_str()+ .encode_wide()+ .chain(std::iter::once(0))+ .collect()+ })+ .unwrap_or_default();++ unsafe {+ let mut token: HANDLE = std::ptr::null_mut();+ if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {+ return Err(io::Error::last_os_error());+ }+ let mut linked = TOKEN_LINKED_TOKEN {+ LinkedToken: std::ptr::null_mut(),+ };+ let mut returned = 0u32;+ let ok = GetTokenInformation(+ token,+ TokenLinkedToken,+ (&mut linked as *mut TOKEN_LINKED_TOKEN).cast(),+ std::mem::size_of::<TOKEN_LINKED_TOKEN>() as u32,+ &mut returned,+ );+ let _ = CloseHandle(token);+ if ok == 0 || linked.LinkedToken.is_null() {+ return Err(io::Error::other("no linked medium token"));+ }+ let mut primary: HANDLE = std::ptr::null_mut();+ let duplicated = DuplicateTokenEx(+ linked.LinkedToken,+ TOKEN_ASSIGN_PRIMARY+ | TOKEN_DUPLICATE+ | TOKEN_QUERY+ | TOKEN_ADJUST_DEFAULT+ | TOKEN_ADJUST_SESSIONID,+ std::ptr::null(),+ SecurityImpersonation,+ TokenPrimary,+ &mut primary,+ );+ let launch_token = if duplicated != 0 && !primary.is_null() {+ let _ = CloseHandle(linked.LinkedToken);+ primary+ } else {+ linked.LinkedToken+ };+ let mut startup: STARTUPINFOW = std::mem::zeroed();+ startup.cb = std::mem::size_of::<STARTUPINFOW>() as u32;+ let mut info: PROCESS_INFORMATION = std::mem::zeroed();+ let created = CreateProcessWithTokenW(+ launch_token,+ 0,+ exe_w.as_ptr(),+ std::ptr::null_mut(),+ DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,+ std::ptr::null(),+ if dir_w.is_empty() {+ std::ptr::null()+ } else {+ dir_w.as_ptr()+ },+ &startup,+ &mut info,+ );+ let _ = CloseHandle(launch_token);+ if created == 0 {+ return Err(io::Error::last_os_error());+ }+ if !info.hThread.is_null() {+ let _ = CloseHandle(info.hThread);+ }+ if !info.hProcess.is_null() {+ let _ = CloseHandle(info.hProcess);+ }+ Ok(())+ }+}++fn is_process_running_by_name(name: &str) -> bool {+ let mut sys = System::new_all();+ sys.refresh_all();+ let target = name.to_lowercase();+ sys.processes().values().any(|p| {+ p.name()+ .to_string_lossy()+ .to_lowercase()+ .contains(&target)+ })+}++/// Poll sysinfo every `LAUNCH_VERIFY_POLL` until the process appears or+/// `timeout` elapses. Lets the happy path exit in ~100-300ms (typical+/// CreateProcess → tasklist register latency on Windows) instead of+/// sleeping a fixed long delay on every successful launch.+fn poll_until_process_running(name: &str, timeout: Duration) -> bool {+ let start = std::time::Instant::now();+ while start.elapsed() < timeout {+ if is_process_running_by_name(name) {+ logger::info(format!(+ "[installer] new process detected after {:?}",+ start.elapsed()+ ));+ return true;+ }+ std::thread::sleep(LAUNCH_VERIFY_POLL);+ }+ false+}++// ─────────────────── Lingering install-dir process sweep ──────────────────++/// Refresh `sys` and return `(pid, name)` of every process whose executable+/// path lives under `app_dir`, excluding ourselves. The updater runs from+/// %TEMP% (updateService copies it out of resources/ precisely so it never+/// locks the install dir), so the self-pid exclusion is just belt-and-braces.+fn collect_appdir_processes(+ sys: &mut System,+ app_dir: &Path,+ self_pid: u32,+) -> Vec<(u32, String)> {+ sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);+ sys.processes()+ .iter()+ .filter_map(|(pid, p)| {+ let pid_u32 = pid.as_u32();+ if pid_u32 == self_pid {+ return None;+ }+ let exe = p.exe()?;+ if path_is_within(exe, app_dir) {+ Some((pid_u32, p.name().to_string_lossy().into_owned()))+ } else {+ None+ }+ })+ .collect()+}++/// Sweep processes still running from `app_dir` after the main process exited:+/// poll for voluntary exit up to APPDIR_PROCESS_GRACE, kill survivors, then+/// wait up to APPDIR_PROCESS_KILL_WAIT for their image locks to drop. If any+/// process survives the full grace+kill cycle, we bail (clean abort — no lock+/// was written, no files were touched yet). The user gets an error message+/// naming the culprit processes and can retry after manually ending them.+fn terminate_appdir_processes<F: FnMut(InstallerEvent)>(app_dir: &Path, emit: &mut F) -> anyhow::Result<()> {+ let self_pid = std::process::id();+ let mut sys = System::new();++ let mut lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);+ if lingering.is_empty() {+ return Ok(());+ }+ logger::warn(format!(+ "[proc-sweep] {} process(es) still running from app_dir: {:?}",+ lingering.len(),+ lingering+ ));+ emit(InstallerEvent::Phase(+ Phase::Waiting,+ "等待残留进程退出…".into(),+ ));++ let grace_start = Instant::now();+ while !lingering.is_empty() && grace_start.elapsed() < APPDIR_PROCESS_GRACE {+ std::thread::sleep(APPDIR_PROCESS_POLL);+ lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);+ }+ if lingering.is_empty() {+ logger::info("[proc-sweep] all lingering processes exited voluntarily");+ return Ok(());+ }++ emit(InstallerEvent::Phase(+ Phase::Waiting,+ "结束残留进程…".into(),+ ));+ for (pid, name) in &lingering {+ if let Some(p) = sys.process(sysinfo::Pid::from_u32(*pid)) {+ let killed = p.kill();+ logger::warn(format!(+ "[proc-sweep] kill {} (pid {}) → {}",+ name, pid, killed+ ));+ }+ }++ let kill_start = Instant::now();+ loop {+ lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);+ if lingering.is_empty() {+ logger::info("[proc-sweep] all lingering processes gone after kill");+ return Ok(());+ }+ if kill_start.elapsed() >= APPDIR_PROCESS_KILL_WAIT {+ break;+ }+ std::thread::sleep(APPDIR_PROCESS_POLL);+ }++ let names: Vec<_> = lingering.iter().map(|(pid, name)| format!("{} (pid {})", name, pid)).collect();+ anyhow::bail!(+ "安装目录下仍有进程无法终止: {}。可能被杀软或系统保护,请手动结束后重试更新",+ names.join(", ")+ );+}++/// Component-wise "is `child` inside `parent`" check. On Windows both sides+/// are lowercased first: NTFS paths are case-insensitive and the two inputs+/// come from different sources (CLI arg vs. sysinfo) that can disagree on+/// casing (e.g. drive letter). Component-wise comparison (Path::starts_with)+/// keeps the boundary safe — `C:\a\xdt-maker2` is NOT within `C:\a\xdt-maker`.+fn path_is_within(child: &Path, parent: &Path) -> bool {+ if parent.as_os_str().is_empty() {+ return false;+ }+ #[cfg(target_os = "windows")]+ {+ let child = std::path::PathBuf::from(child.to_string_lossy().to_lowercase());+ let parent = std::path::PathBuf::from(parent.to_string_lossy().to_lowercase());+ child.starts_with(&parent)+ }+ #[cfg(not(target_os = "windows"))]+ {+ child.starts_with(parent)+ }+}++#[cfg(test)]+mod tests {+ use super::{+ acquire_update_lock, archive_matches_digest, bind_zip_sha256, close_should_be_blocked,+ extract_error_can_retry, create_protected_staging_tree, elevated_private_staging_root,+ finalize_retry_state,+ install_writable_for_staging, is_owned_program_data_staging_name,+ lock_owned_by_foreign_process, may_relaunch_with_current_integrity, may_self_elevate,+ path_is_within, pre_elevation_failure_can_retry, prepare_retry_archive, program_data_dir,+ release_abandoned_update_lock, release_update_lock, remove_staging_dir,+ retain_update_lock_file, retry_allowed, retry_args, retry_available, retry_cli_args,+ retry_request_allowed, rollback, run, should_relaunch_after_abandoning_retry,+ should_relaunch_after_rollback, staging_dirs, staging_dirs_for,+ uses_elevated_private_staging, uses_private_staging_acl, uses_protected_staging, Phase,+ };+ use crate::args::{CliArgs, ThemeArg};+ use clap::Parser;+ use sha2::Digest;+ use std::fs;+ use std::path::{Path, PathBuf};+ use std::sync::atomic::{AtomicU64, Ordering};++ static TEST_DIR_SEQ: AtomicU64 = AtomicU64::new(0);++ fn test_args() -> CliArgs {+ CliArgs {+ zip: PathBuf::from(r"C:\Users\Test User\update.zip"),+ app_dir: PathBuf::from(r"C:\Program Files\Cindy"),+ exe_name: "cindy.exe".into(),+ install_key: None,+ pid: 42,+ log: PathBuf::from(r"C:\Users\Test User\update.log"),+ lock: PathBuf::from(r"C:\Users\Test User\update.lock"),+ workdir: PathBuf::from(r"C:\Users\Test User\update-workdir"),+ theme: ThemeArg::Dark,+ elevated: true,+ zip_sha256: None,+ install_writable: None,+ }+ }++ struct TestDir(PathBuf);++ impl TestDir {+ fn new() -> Self {+ let unique = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos();+ let seq = TEST_DIR_SEQ.fetch_add(1, Ordering::Relaxed);+ let path = std::env::temp_dir().join(format!(+ "cindy-updater-test-{}-{unique}-{seq}",+ std::process::id()+ ));+ fs::create_dir(&path).expect("create isolated test directory");+ Self(path)+ }+ }++ impl Drop for TestDir {+ fn drop(&mut self) {+ let _ = fs::remove_dir_all(&self.0);+ }+ }++ #[test]+ fn remove_staging_dir_is_idempotent_and_rejects_files() {+ let temp = TestDir::new();+ let missing = temp.0.join("missing");+ remove_staging_dir(&missing).expect("missing staging dir is already clean");++ let directory = temp.0.join("directory");+ fs::create_dir(&directory).expect("create staging directory");+ fs::write(directory.join("partial.txt"), b"partial").expect("create partial file");+ remove_staging_dir(&directory).expect("remove partial staging directory");+ assert!(!directory.exists());++ let file = temp.0.join("file");+ fs::write(&file, b"not a directory").expect("create conflicting staging file");+ assert!(remove_staging_dir(&file).is_err());+ assert!(file.exists());+ }++ #[test]+ fn program_data_sweep_keeps_unrelated_cindy_update_prefix_dirs() {+ assert!(is_owned_program_data_staging_name("cindy-update-1710000000000"));+ assert!(+ !is_owned_program_data_staging_name("cindy-update-ts"),+ "workdir leftovers that are not a millisecond timestamp are not ProgramData staging"+ );+ assert!(+ !is_owned_program_data_staging_name("cindy-update-service"),+ "a ProgramData service directory is not this updater's timestamped staging root"+ );+ assert!(!is_owned_program_data_staging_name("cindy-update"));+ assert!(!is_owned_program_data_staging_name("xdt-update-extract-1"));++ let temp = TestDir::new();+ let stale_staging = temp.0.join("cindy-update-1710000000000");+ let service = temp.0.join("cindy-update-service");+ fs::create_dir(&stale_staging).unwrap();+ fs::write(stale_staging.join("keep-marker"), b"staging").unwrap();+ fs::create_dir(&service).unwrap();+ fs::write(service.join("payload"), b"unrelated").unwrap();+ let old = std::time::SystemTime::now() - std::time::Duration::from_secs(8 * 24 * 60 * 60);+ fs::File::open(&stale_staging)+ .unwrap()+ .set_modified(old)+ .unwrap();+ fs::File::open(&service).unwrap().set_modified(old).unwrap();+ super::sweep_stale_under(+ &temp.0,+ super::SweepNameFilter::ProgramDataOwned,+ std::time::SystemTime::now(),+ );+ assert!(+ !stale_staging.exists(),+ "timestamped private staging older than seven days is still eligible"+ );+ assert!(+ service.exists(),+ "ProgramData sweep must not delete cindy-update-service"+ );+ assert_eq!(fs::read(service.join("payload")).unwrap(), b"unrelated");+ }++ #[test]+ fn retry_requires_a_readable_archive() {+ let temp = TestDir::new();+ let zip = temp.0.join("update.zip");+ fs::write(&zip, b"update").expect("create test archive");+ assert!(retry_available(&zip));+ fs::remove_file(&zip).expect("remove test archive");+ assert!(!retry_available(&zip));+ fs::create_dir(&zip).expect("create directory at archive path");+ assert!(!retry_available(&zip));+ }++ #[test]+ fn retry_is_allowed_only_when_failure_is_safe_and_archive_is_readable() {+ let temp = TestDir::new();+ let zip = temp.0.join("update.zip");+ fs::write(&zip, b"update").expect("create test archive");++ let digest = format!("{:x}", sha2::Sha256::digest(b"update"));+ bind_zip_sha256(&mut CliArgs {+ zip: zip.clone(),+ zip_sha256: Some(digest.clone()),+ ..test_args()+ })+ .expect("verify trusted digest");+ assert!(retry_allowed(true, &zip, Some(&digest)));+ assert!(!retry_allowed(false, &zip, Some(&digest)));+ assert!(!retry_allowed(true, &zip, None));++ fs::remove_file(&zip).expect("remove test archive");+ assert!(!retry_allowed(true, &zip, Some(&digest)));+ }++ #[test]+ fn failed_archive_is_removed_from_electron_auto_apply_path() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"archive").unwrap();+ let retry_zip = args.workdir.join("retry.zip");+ assert!(prepare_retry_archive(&args));+ assert!(!args.zip.exists());+ assert_eq!(fs::read(&retry_zip).unwrap(), b"archive");+ assert!(prepare_retry_archive(&args));+ }++ #[test]+ fn archive_isolation_does_not_overwrite_an_existing_retry_file() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.clone();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"original").unwrap();+ fs::write(super::retry_archive(&args), b"existing").unwrap();+ assert!(!prepare_retry_archive(&args));+ assert_eq!(fs::read(&args.zip).unwrap(), b"original");+ assert_eq!(fs::read(super::retry_archive(&args)).unwrap(), b"existing");+ }++ #[test]+ fn rollback_restores_backed_up_files() {+ let temp = TestDir::new();+ let backup_dir = temp.0.join("backup");+ let app_dir = temp.0.join("app");+ fs::create_dir_all(backup_dir.join("nested")).unwrap();+ fs::create_dir_all(app_dir.join("nested")).unwrap();+ fs::write(backup_dir.join("root.txt"), b"old-root").unwrap();+ fs::write(backup_dir.join("nested").join("file.txt"), b"old-nested").unwrap();+ fs::write(app_dir.join("root.txt"), b"new-root").unwrap();+ fs::write(app_dir.join("nested").join("file.txt"), b"new-nested").unwrap();++ rollback(&backup_dir, &app_dir, |_, _| {}).expect("complete backup walk succeeds");+ assert_eq!(fs::read(app_dir.join("root.txt")).unwrap(), b"old-root");+ assert_eq!(+ fs::read(app_dir.join("nested").join("file.txt")).unwrap(),+ b"old-nested"+ );+ }++ #[test]+ fn rollback_fails_when_backup_entries_cannot_be_walked() {+ let temp = TestDir::new();+ let backup_dir = temp.0.join("missing-backup");+ let app_dir = temp.0.join("app");+ fs::create_dir(&app_dir).unwrap();+ fs::write(app_dir.join("restored.txt"), b"new").unwrap();++ let result = rollback(&backup_dir, &app_dir, |_, _| {});+ assert!(+ result.is_err(),+ "incomplete backup walk must fail rollback so retry stays disabled: {result:?}"+ );+ assert_eq!(fs::read(app_dir.join("restored.txt")).unwrap(), b"new");+ }++ #[cfg(target_os = "windows")]+ #[test]+ fn archive_handle_denies_concurrent_writes_and_deletes() {+ use std::os::windows::fs::OpenOptionsExt;++ let temp = TestDir::new();+ let zip = temp.0.join("update.zip");+ fs::write(&zip, b"trusted-archive").unwrap();+ let _reader = super::open_regular_file(&zip).expect("open protected archive handle");++ assert!(fs::OpenOptions::new().write(true).open(&zip).is_err());+ assert!(fs::OpenOptions::new()+ .write(true)+ .share_mode(0x00000001 | 0x00000002 | 0x00000004)+ .open(&zip)+ .is_err());+ assert!(fs::remove_file(&zip).is_err());+ }++ #[test]+ fn deterministic_zip_format_errors_are_not_retryable() {+ let invalid = anyhow::Error::new(zip::result::ZipError::InvalidArchive(+ "invalid central directory",+ ));+ let unsupported = anyhow::Error::new(zip::result::ZipError::UnsupportedArchive(+ "unsupported compression",+ ));++ assert!(!extract_error_can_retry(&invalid));+ assert!(!extract_error_can_retry(&unsupported));+ }++ #[test]+ fn terminal_archive_errors_delete_the_electron_staged_zip() {+ let invalid = anyhow::Error::new(zip::result::ZipError::InvalidArchive(+ "invalid central directory",+ ));+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"not-a-zip").unwrap();+ fs::write(super::retry_archive(&args), b"isolated-copy").unwrap();++ assert!(!extract_error_can_retry(&invalid));+ assert!(!finalize_retry_state(+ &args,+ extract_error_can_retry(&invalid)+ ));+ assert!(+ !args.zip.exists(),+ "terminal ZIP must leave Electron's staged path so the next launch downloads a fresh copy"+ );+ assert!(+ !super::retry_archive(&args).exists(),+ "an isolated retry copy must not keep the same terminal archive around"+ );+ }++ #[test]+ fn retryable_failure_still_isolates_the_archive_for_manual_retry() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"archive").unwrap();+ args.zip_sha256 = Some(format!("{:x}", sha2::Sha256::digest(b"archive")));++ assert!(finalize_retry_state(&args, true));+ assert!(!args.zip.exists());+ assert_eq!(fs::read(super::retry_archive(&args)).unwrap(), b"archive");+ }++ #[test]+ fn retry_args_reuse_trusted_state_without_respawning() {+ let mut source = test_args();+ source.zip_sha256 = Some("abc123".into());+ let retry = retry_args(&source);+ assert_eq!(retry.zip, source.workdir.join("retry.zip"));+ assert_eq!(retry.pid, 0);+ assert_eq!(retry.app_dir, source.app_dir);+ assert_eq!(retry.workdir, source.workdir);+ assert_eq!(retry.zip_sha256.as_deref(), Some("abc123"));+ assert!(retry.elevated);++ source.elevated = false;+ assert!(!retry_args(&source).elevated);+ }++ #[test]+ fn retry_must_not_rerequest_uac_from_the_temp_updater() {+ assert!(+ may_self_elevate(false),+ "the first attempt may still ShellExecute the TEMP updater once"+ );+ assert!(+ !may_self_elevate(true),+ "Retry must not runas current_exe from the Electron-created TEMP workdir"+ );+ }++ #[test]+ fn cancelled_or_failed_uac_prompt_is_not_retryable() {+ assert!(!super::elevation_prompt_can_retry());+ }++ #[test]+ fn retryable_rollback_does_not_relaunch_cindy() {+ assert!(!should_relaunch_after_rollback(true));+ assert!(should_relaunch_after_rollback(false));+ }++ #[test]+ fn install_dir_identity_rejects_a_swapped_reparse_point() {+ let temp = TestDir::new();+ let app_dir = temp.0.join("Cindy");+ fs::create_dir(&app_dir).unwrap();+ let identity = super::capture_install_dir_identity(&app_dir).expect("capture");+ assert!(+ !identity.is_reparse,+ "a normal install directory must not look like a junction"+ );+ assert!(super::install_dir_identity_unchanged(&identity, &app_dir));++ fs::remove_dir(&app_dir).unwrap();+ let planted = temp.0.join("planted");+ fs::create_dir(&planted).unwrap();+ std::os::unix::fs::symlink(&planted, &app_dir).unwrap();+ assert!(+ !super::install_dir_identity_unchanged(&identity, &app_dir),+ "elevated Retry must not copy through a junction swapped in after the first check"+ );+ assert!(+ super::capture_install_dir_identity(&app_dir)+ .expect("capture junction")+ .is_reparse+ );+ }++ #[test]+ fn elevated_retry_revalidates_app_dir_after_the_settle_delay() {+ let source = include_str!("installer.rs");+ let start = source+ .find("fn run_inner")+ .expect("run_inner");+ let end = source[start..]+ .find("copy_tree_into_pinned(&extract_dir, &pinned")+ .expect("copy_tree follows identity checks");+ let body = &source[start..start + end];+ assert!(+ body.contains("capture_install_dir_identity"),+ "pin app_dir before the settle delay, not only as a one-shot reparse check:\n{body}"+ );+ let sleep = body+ .find("FS_SETTLE_DELAY")+ .expect("settle delay");+ let after = &body[sleep..];+ assert!(+ after.contains("ensure_install_dir_unchanged"),+ "revalidate the pinned directory after the two-second window a junction can be planted:\n{after}"+ );+ let copy = source+ .find("copy_tree_into_pinned(&extract_dir, &pinned")+ .expect("copy_tree");+ let before_copy = &source[start..copy];+ assert!(+ before_copy.contains("open_install_dir_handle"),+ "hold a non-reparse app_dir handle before copy_tree, not only a path identity check:\n{before_copy}"+ );+ assert!(+ body.contains("open_install_dir_handle") && source.contains("copy_tree_into_pinned"),+ "copy_tree must write through the pinned non-reparse directory, not reopen app_dir by name:\n{body}"+ );+ }++ #[test]+ fn copy_tree_into_pinned_rejects_a_swapped_destination() {+ let temp = TestDir::new();+ let app_dir = temp.0.join("Cindy");+ let extract = temp.0.join("extract");+ fs::create_dir(&app_dir).unwrap();+ fs::create_dir(&extract).unwrap();+ fs::write(extract.join("Cindy.exe"), b"new").unwrap();+ let identity = super::capture_install_dir_identity(&app_dir).expect("capture");+ let handle = super::open_install_dir_handle(&app_dir, &identity).expect("pin");+ fs::remove_dir(&app_dir).unwrap();+ let planted = temp.0.join("planted");+ fs::create_dir(&planted).unwrap();+ std::os::unix::fs::symlink(&planted, &app_dir).unwrap();+ let error = super::copy_tree_into_pinned(&extract, &handle, |_, _| {}).unwrap_err();+ assert!(+ !planted.join("Cindy.exe").exists(),+ "elevated Retry must not copy through a junction swapped after the identity check: {error}"+ );+ }++ #[test]+ fn pinned_join_rejects_a_descendant_junction() {+ let temp = TestDir::new();+ let app_dir = temp.0.join("Cindy");+ fs::create_dir(&app_dir).unwrap();+ let identity = super::capture_install_dir_identity(&app_dir).expect("capture");+ let handle = super::open_install_dir_handle(&app_dir, &identity).expect("pin");+ let planted = temp.0.join("planted");+ fs::create_dir(&planted).unwrap();+ std::os::unix::fs::symlink(&planted, app_dir.join("resources")).unwrap();+ let error = handle.join(Path::new("resources/app.asar")).unwrap_err();+ assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);+ assert!(+ !planted.join("app.asar").exists(),+ "join must refuse a descendant junction before create_dir_all/fs::copy: {error}"+ );+ }++ #[test]+ fn successful_rollback_relaunches_when_final_retry_validation_fails() {+ assert!(+ should_relaunch_after_abandoning_retry(false, false, false, true),+ "Close must relaunch Cindy after rollback restored the app even if Retry was later withdrawn"+ );+ assert!(+ !should_relaunch_after_abandoning_retry(false, false, false, false),+ "an inconsistent rollback must still not relaunch Cindy"+ );+ assert!(super::should_relaunch_restored_app_on_abandon(+ false, false, true, true, false, false, true+ ));++ let source = include_str!("installer.rs");+ let start = source+ .find("[installer] rollback succeeded")+ .expect("rollback success path");+ let end = source[start..]+ .find("ROLLBACK ALSO FAILED")+ .expect("failed rollback follows success");+ let inner = &source[start..start + end];+ assert!(+ !inner.contains("should_relaunch_after_rollback"),+ "preliminary isolation must not relaunch before finalize_retry_state:\n{inner}"+ );+ let outer_start = source+ .find("let can_retry = if failure.can_retry")+ .expect("outer finalize");+ let outer_end = source[outer_start..]+ .find("struct InstallerFailure")+ .expect("failure struct follows run_with_lock");+ let outer = &source[outer_start..outer_start + outer_end];+ assert!(+ outer.contains("should_relaunch_after_rollback")+ && outer.contains("install_restored"),+ "relaunch after rollback only once Retry is finally allowed or withdrawn:\n{outer}"+ );+ assert!(+ super::failed_event_install_restored(true, true) == false,+ "Close must not relaunch Cindy after run_with_lock already started the restored app"+ );+ assert!(+ super::failed_event_install_restored(true, false),+ "Close still relaunches when rollback restored the app but Retry remains available until Close"+ );+ }++ #[test]+ fn abandoning_retry_relaunches_the_restored_app() {+ assert!(+ should_relaunch_after_abandoning_retry(true, false, false, false),+ "Close after a retryable rollback must relaunch the restored Cindy"+ );+ assert!(+ !should_relaunch_after_abandoning_retry(true, true, true, true),+ "do not relaunch while an in-process Retry is still replacing files"+ );+ assert!(+ !should_relaunch_after_abandoning_retry(false, false, false, false),+ "an inconsistent rollback must not relaunch Cindy"+ );+ assert!(+ should_relaunch_after_abandoning_retry(false, false, true, false),+ "Close after a terminal pre-install Retry must relaunch the unmodified Cindy"+ );++ let temp = TestDir::new();+ let mut args = test_args();+ args.app_dir = temp.0.join("app");+ args.lock = temp.0.join(".updating");+ fs::create_dir(&args.app_dir).unwrap();+ let exe = args.app_dir.join(&args.exe_name);+ fs::write(&exe, b"cindy").unwrap();+ let held = acquire_update_lock(&args.lock).expect("retain lock while Retry is available");+ retain_update_lock_file(held);++ assert_eq!(super::restored_app_relaunch_path(&args).as_ref(), Some(&exe));+ super::abandon_retry(&args, true, false, false, false, false);+ assert!(+ !args.lock.exists(),+ "abandoning Retry still deletes this process's retained .updating"+ );+ assert!(+ !super::begin_abandon_retry(&args.lock),+ "quit_now plus window destroy must not relaunch Cindy a second time"+ );+ }++ #[test]+ fn abandoning_retry_relaunches_even_without_a_retained_lock() {+ assert!(+ should_relaunch_after_abandoning_retry(true, false, false, false),+ "retryable failures that never acquired .updating still left Cindy closed"+ );+ let temp = TestDir::new();+ let mut args = test_args();+ args.app_dir = temp.0.join("app");+ args.lock = temp.0.join(".updating");+ fs::create_dir(&args.app_dir).unwrap();+ fs::write(args.app_dir.join(&args.exe_name), b"cindy").unwrap();+ assert!(!args.lock.exists());+ assert!(super::should_relaunch_restored_app_on_abandon(+ true, false, false, true, false, false, false+ ));+ assert!(+ !super::should_relaunch_restored_app_on_abandon(+ true, false, false, true, true, true, true+ ),+ "Cindy's 30s wait can drop this window's lock; a later updater's .updating must block relaunch"+ );+ assert!(+ super::should_relaunch_restored_app_on_abandon(+ false, false, true, true, false, true, false+ ),+ "a terminal pre-install Retry left the install unmodified and Cindy stopped"+ );+ assert!(+ !super::should_relaunch_restored_app_on_abandon(+ false, false, true, true, false, false, false+ ),+ "a failed rollback must not relaunch an inconsistent Cindy.exe"+ );+ assert!(super::begin_abandon_retry(&args.lock));+ assert!(+ !super::begin_abandon_retry(&args.lock),+ "Destroyed after quit_now must not start a second restored Cindy"+ );+ }++ #[test]+ fn abandoning_retry_deletes_the_retained_lock_file() {+ let temp = TestDir::new();+ let lock = temp.0.join(".updating");+ let held = acquire_update_lock(&lock).expect("first updater holds the lock");+ retain_update_lock_file(held);+ assert!(lock.exists());+ release_abandoned_update_lock(&lock);+ assert!(+ !lock.exists(),+ "Close must delete .updating so Cindy startup does not wait 30s"+ );+ }++ #[test]+ fn abandoning_retry_does_not_relaunch_when_a_foreign_lock_exists() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.app_dir = temp.0.join("app");+ args.lock = temp.0.join(".updating");+ fs::create_dir(&args.app_dir).unwrap();+ fs::write(args.app_dir.join(&args.exe_name), b"cindy").unwrap();+ let other_pid = if std::process::id() == 1 { 2 } else { 1 };+ fs::write(&args.lock, format!("updating {other_pid}\n")).unwrap();+ assert!(lock_owned_by_foreign_process(&args.lock));+ assert!(+ !super::should_relaunch_restored_app_on_abandon(+ true, false, false, true, true, true, true+ ),+ "stopped_app must not relaunch Cindy into another updater's replace window"+ );+ super::abandon_retry(&args, true, false, true, false, false);+ assert!(+ args.lock.exists(),+ "Close must not delete another updater's .updating"+ );+ assert_eq!(+ fs::read_to_string(&args.lock).unwrap(),+ format!("updating {other_pid}\n")+ );+ }++ #[test]+ fn closing_another_updater_does_not_delete_a_retained_lock() {+ let temp = TestDir::new();+ let lock = temp.0.join(".updating");+ let other_pid = if std::process::id() == 1 { 2 } else { 1 };+ fs::write(&lock, format!("updating {other_pid}\n")).unwrap();+ release_abandoned_update_lock(&lock);+ assert!(+ lock.exists(),+ "a second updater that never retained .updating must not delete the first instance's mutex"+ );++ let owned_path = temp.0.join("owned.updating");+ let owned = acquire_update_lock(&owned_path).expect("own lock");+ retain_update_lock_file(owned);+ release_abandoned_update_lock(&owned_path);+ assert!(+ !owned_path.exists(),+ "the updater that retained .updating must still delete it on Close"+ );+ }++ #[test]+ fn pre_elevation_failures_are_not_retryable_when_uac_is_required() {+ assert!(!pre_elevation_failure_can_retry(true, false));+ assert!(pre_elevation_failure_can_retry(false, false));+ assert!(pre_elevation_failure_can_retry(true, true));+ }++ #[test]+ fn first_attempt_does_not_hold_the_lock_across_uac() {+ let source = include_str!("installer.rs");+ let wrapper = source+ .find("pub(crate) fn run_with_lock")+ .expect("run_with_lock");+ let inner = source.find("fn run_inner").expect("run_inner");+ let wrapper_body = &source[wrapper..inner];+ let bind = wrapper_body+ .find("bind_zip_sha256")+ .expect("bind before inner");+ assert!(+ !wrapper_body[..bind].contains("acquire_update_lock"),+ "the unelevated parent must not create_new the lock before UAC handoff"+ );+ let elevate = source[inner..]+ .find("match self_elevate")+ .expect("self_elevate");+ let acquire = source[inner..]+ .find("acquire_update_lock")+ .expect("lock after elevation");+ assert!(+ acquire > elevate,+ "the elevated child must acquire the lock after the UAC handoff"+ );+ let bind_err = wrapper_body+ .find("bind_zip_sha256")+ .expect("bind_zip_sha256 in run_with_lock");+ let inner_call = wrapper_body+ .find("run_inner(")+ .expect("run_inner follows bind");+ let bind_fail = &wrapper_body[bind_err..inner_call];+ assert!(+ bind_fail.contains("relaunch_unmodified_app_after_early_failure"),+ "Electron already force-quit Cindy; a pre-run_inner digest failure must relaunch the unmodified install:\n{bind_fail}"+ );+ assert!(+ !bind_fail.contains("relaunch_restored_app("),+ "digest-fail relaunch must not inherit a high token before the root is pinned:\n{bind_fail}"+ );+ }++ #[test]+ fn update_lock_rejects_a_second_updater() {+ let temp = TestDir::new();+ let lock = temp.0.join(".updating");+ let first = acquire_update_lock(&lock).expect("first updater holds the lock");+ assert!(matches!(+ acquire_update_lock(&lock),+ Err(error) if error == "updater_busy"+ ));+ release_update_lock(first);+ let second = acquire_update_lock(&lock).expect("released lock can be acquired");+ retain_update_lock_file(second);+ assert!(lock.exists());+ assert!(matches!(+ acquire_update_lock(&lock),+ Err(error) if error == "updater_busy"+ ));+ let reopened = super::reopen_held_update_lock(&lock).expect("retry reopens retained lock");+ release_update_lock(reopened);+ assert!(!lock.exists());+ }++ #[test]+ fn reopen_held_update_lock_rejects_a_lock_owned_by_another_process() {+ let temp = TestDir::new();+ let lock = temp.0.join(".updating");+ let other_pid = if std::process::id() == 1 { 2 } else { 1 };+ fs::write(&lock, format!("updating {other_pid}\n")).unwrap();+ assert!(+ super::reopen_held_update_lock(&lock).is_none(),+ "Retry must not take over a later updater's mutex just because the path still exists"+ );+ assert!(lock.exists());+ }++ #[test]+ fn busy_lock_does_not_delete_the_active_updater_archive() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.app_dir = temp.0.join("app");+ args.workdir = temp.0.join("cindy-update-ts");+ args.lock = temp.0.join(".updating");+ args.log = temp.0.join("update.log");+ args.pid = 0;+ args.elevated = true;+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"active-archive").unwrap();+ args.zip_sha256 = Some(format!("{:x}", sha2::Sha256::digest(b"active-archive")));+ let held = acquire_update_lock(&args.lock).expect("first updater holds the lock");++ let mut events = Vec::new();+ run(args.clone(), |event| events.push(event));++ assert!(+ args.zip.exists(),+ "the rejected updater must not delete the ZIP still owned by the lock holder"+ );+ assert_eq!(fs::read(&args.zip).unwrap(), b"active-archive");+ assert!(+ events+ .iter()+ .any(|event| {+ matches!(+ event,+ super::InstallerEvent::Failed {+ can_retry: false,+ ..+ }+ )+ }),+ "lock contention stays non-retryable so this window does not offer Retry"+ );+ release_update_lock(held);+ }++ #[test]+ fn retry_isolation_falls_back_to_copy_when_rename_crosses_devices() {+ let temp = TestDir::new();+ let src = temp.0.join("updates").join("update.zip");+ fs::create_dir_all(src.parent().unwrap()).unwrap();+ let dst = temp.0.join("workdir").join("retry.zip");+ fs::create_dir(dst.parent().unwrap()).unwrap();+ fs::write(&src, b"archive").unwrap();+ let digest = format!("{:x}", sha2::Sha256::digest(b"archive"));++ assert!(super::isolate_archive_with(+ &src,+ &dst,+ Some(&digest),+ |_, _| Err(std::io::Error::from_raw_os_error(super::CROSS_DEVICE_ERRNO)),+ ));+ assert!(+ !src.exists(),+ "source must be removed only after the isolated copy matches the trusted digest"+ );+ assert_eq!(fs::read(&dst).unwrap(), b"archive");+ assert!(archive_matches_digest(&dst, &digest));++ let source = include_str!("installer.rs");+ let start = source+ .find("fn copy_verified_archive")+ .expect("copy_verified_archive");+ let end = source[start..]+ .find("\n/// Retryable failures isolate")+ .expect("finalize_retry_state follows copy");+ let body = &source[start..start + end];+ let drop_at = body.find("drop(source)").expect("close the source handle");+ let remove_at = body+ .find("fs::remove_file(src)")+ .expect("delete the staged source");+ assert!(+ drop_at < remove_at,+ "Windows FILE_SHARE_READ denies delete while the copy handle is still open:\n{body}"+ );+ }++ #[test]+ fn retry_isolation_copy_fallback_keeps_source_when_digest_mismatches() {+ let temp = TestDir::new();+ let src = temp.0.join("update.zip");+ let dst = temp.0.join("retry.zip");+ fs::write(&src, b"tampered").unwrap();++ assert!(!super::isolate_archive_with(+ &src,+ &dst,+ Some(&format!("{:x}", sha2::Sha256::digest(b"trusted"))),+ |_, _| Err(std::io::Error::from_raw_os_error(super::CROSS_DEVICE_ERRNO)),+ ));+ assert!(src.exists(), "untrusted source must stay for diagnosis");+ assert!(!dst.exists(), "failed isolation must not leave a retry.zip");+ }++ #[test]+ fn pre_install_failures_remove_protected_staging_dirs() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.app_dir = temp.0.join("app");+ args.workdir = temp.0.join("cindy-update-ts");+ args.lock = temp.0.join("update.lock");+ args.log = temp.0.join("update.log");+ args.pid = 0;+ args.elevated = true;+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ args.zip = super::retry_archive(&args);+ fs::write(&args.zip, b"not-a-zip").unwrap();+ args.zip_sha256 = Some(format!("{:x}", sha2::Sha256::digest(b"not-a-zip")));+ let (extract_dir, backup_dir) = staging_dirs(&args);+ fs::create_dir_all(extract_dir.join("partial")).unwrap();+ fs::write(extract_dir.join("partial").join("Cindy.exe"), b"partial").unwrap();+ fs::create_dir_all(&backup_dir).unwrap();+ fs::write(backup_dir.join("Cindy.exe"), b"old").unwrap();++ let mut events = Vec::new();+ run(args.clone(), |event| events.push(event));++ assert!(+ !extract_dir.exists(),+ "extract staging under app_dir must not survive a pre-install failure"+ );+ assert!(+ !backup_dir.exists(),+ "backup staging under app_dir must not survive a pre-install failure"+ );+ assert!(+ events+ .iter()+ .any(|event| {+ matches!(+ event,+ super::InstallerEvent::Failed {+ can_retry: false,+ ..+ }+ )+ }),+ "terminal ZIP errors stay non-retryable"+ );+ }++ #[test]+ fn zip_io_errors_remain_retryable() {+ let error = anyhow::Error::new(zip::result::ZipError::Io(std::io::Error::new(+ std::io::ErrorKind::PermissionDenied,+ "temporarily locked",+ )));++ assert!(extract_error_can_retry(&error));+ }++ #[test]+ fn initial_archive_requires_an_externally_trusted_digest() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"trusted-archive").unwrap();++ assert_eq!(bind_zip_sha256(&mut args), Err("archive_unavailable".into()));+ assert!(args.zip_sha256.is_none());+ }++ #[test]+ fn initial_archive_must_match_the_externally_trusted_digest() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ args.zip = temp.0.join("update.zip");+ fs::write(&args.zip, b"trusted-archive").unwrap();+ args.zip_sha256 = Some(format!("{:x}", sha2::Sha256::digest(b"trusted-archive")));++ let digest = bind_zip_sha256(&mut args).expect("verify trusted digest");+ assert!(archive_matches_digest(&args.zip, &digest));+ assert!(retry_allowed(true, &args.zip, args.zip_sha256.as_deref()));++ fs::write(&args.zip, b"replaced-archive").unwrap();+ assert!(!archive_matches_digest(&args.zip, &digest));+ assert!(!retry_allowed(true, &args.zip, args.zip_sha256.as_deref()));+ assert_eq!(+ bind_zip_sha256(&mut args).unwrap_err(),+ "archive_unavailable"+ );+ }++ #[test]+ fn retry_request_uses_the_digest_stored_on_the_same_args() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.zip = temp.0.join("update.zip");+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.workdir).unwrap();+ fs::write(&args.zip, b"trusted-archive").unwrap();+ let unbound = args.clone();+ args.zip_sha256 = Some(format!("{:x}", sha2::Sha256::digest(b"trusted-archive")));+ bind_zip_sha256(&mut args).expect("verify trusted digest");+ assert!(prepare_retry_archive(&args));+ let retry_zip = super::retry_archive(&args);+ assert_eq!(+ retry_request_allowed(&unbound, Phase::Failed, true),+ Err("archive_unavailable".into())+ );+ assert!(retry_request_allowed(&args, Phase::Failed, true).is_ok());+ assert!(archive_matches_digest(+ &retry_zip,+ args.zip_sha256.as_deref().unwrap()+ ));+ }- for (idx, entry) in entries.iter().enumerate() {- let rel = entry.path().strip_prefix(src)?;- let target = dst.join(rel);- if entry.file_type().is_dir() {- fs::create_dir_all(&target)?;- } else {- if let Some(parent) = target.parent() {- fs::create_dir_all(parent)?;- }- // overwrite is the whole point — file may be in use only if it was- // never closed by the previous main process; we already pid-waited.- copy_with_retry(entry.path(), &target)?;- }- on_progress((idx as u64) + 1, total);+ #[test]+ fn elevated_staging_stays_inside_the_install_directory() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = true;+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ let (extract_dir, backup_dir) = staging_dirs_for(&args, false, false);+ assert!(extract_dir.starts_with(&args.app_dir));+ assert!(backup_dir.starts_with(&args.app_dir));+ assert!(!extract_dir.starts_with(&args.workdir));}- Ok(())-}-/// Copy with a small retry loop. AV scanners or stale handles can briefly-/// hold a file open immediately after the main process exits, and a single-/// `fs::copy` will fail with PermissionDenied if it loses the race.-fn copy_with_retry(src: &Path, dst: &Path) -> io::Result<()> {- const ATTEMPTS: u32 = 5;- let mut last_err: Option<io::Error> = None;- for attempt in 0..ATTEMPTS {- match fs::copy(src, dst) {- Ok(_) => return Ok(()),- Err(e) => {- logger::warn(format!(- "[copy] attempt {} failed for {}: {}",- attempt + 1,- dst.display(),- e- ));- last_err = Some(e);- std::thread::sleep(Duration::from_millis(200 * (attempt + 1) as u64));- }- }+ #[test]+ fn unelevated_staging_stays_in_the_temp_workdir() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = false;+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ let (extract_dir, backup_dir) = staging_dirs_for(&args, false, false);+ assert!(extract_dir.starts_with(&args.workdir));+ assert!(backup_dir.starts_with(&args.workdir));}- Err(last_err.unwrap_or_else(|| io::Error::other("copy_with_retry exhausted")))-}-// io::Error::other has been stable since Rust 1.74; we require >= 1.74 in CI.+ #[test]+ fn inherited_elevation_without_cli_flag_uses_protected_staging() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = false;+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ assert!(+ uses_protected_staging(false, true, false),+ "a high-integrity updater spawned without --elevated must not stage in TEMP"+ );+ let (extract_dir, backup_dir) = staging_dirs_for(&args, true, false);+ assert!(extract_dir.starts_with(&args.app_dir));+ assert!(backup_dir.starts_with(&args.app_dir));+ assert!(!extract_dir.starts_with(&args.workdir));-fn launch_detached(exe: &Path) -> io::Result<()> {- use std::process::Command;- #[cfg(target_os = "windows")]- {- use std::os::windows::process::CommandExt;- // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP — orphan the child so- // updater can exit immediately without taking the new app down with it.- const FLAGS: u32 = 0x00000008 | 0x00000200;- Command::new(exe).creation_flags(FLAGS).spawn()?;+ let source = include_str!("installer.rs");+ let start = source+ .find("pub(crate) fn staging_dirs(args: &CliArgs)")+ .expect("staging_dirs");+ let helper = source[start..]+ .find("pub(crate) fn uses_protected_staging")+ .expect("uses_protected_staging");+ let body = &source[start..start + helper];+ assert!(+ body.contains("process_is_elevated()"),+ "staging_dirs must read the process token; --elevated is omitted on inherited elevation"+ );}- #[cfg(not(target_os = "windows"))]- {- Command::new(exe).spawn()?;++ #[test]+ fn inherited_elevation_retry_keeps_pinned_private_staging() {+ assert!(+ uses_elevated_private_staging(false, true, true),+ "inherited elevation of a per-user install must stay on High-IL ProgramData"+ );+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = false;+ args.install_writable = Some(true);+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("cindy-update-1710000000000");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ let private_root = elevated_private_staging_root(&args);+ let (extract_dir, backup_dir) = staging_dirs_for(&args, true, false);+ assert!(+ extract_dir.starts_with(&private_root),+ "a later Administrators-only DACL must not move Retry staging into plantable app_dir"+ );+ assert!(backup_dir.starts_with(&private_root));+ assert!(!extract_dir.starts_with(&args.app_dir));++ let source = include_str!("installer.rs");+ let start = source+ .find("pub(crate) fn staging_dirs(args: &CliArgs)")+ .expect("staging_dirs");+ let helper = source[start..]+ .find("pub(crate) fn uses_protected_staging")+ .expect("uses_protected_staging");+ let body = &source[start..start + helper];+ assert!(+ body.contains("resolved_install_writable"),+ "Retry must pin the first writable classification instead of re-probing app_dir:\n{body}"+ );}- Ok(())-}-fn is_process_running_by_name(name: &str) -> bool {- let mut sys = System::new_all();- sys.refresh_all();- let target = name.to_lowercase();- sys.processes().values().any(|p| {- p.name()- .to_string_lossy()- .to_lowercase()- .contains(&target)- })-}+ #[test]+ fn uac_elevated_staging_ignores_a_writable_probe_of_the_install_directory() {+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = true;+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("workdir");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ let (extract_dir, backup_dir) = staging_dirs(&args);+ assert!(+ extract_dir.starts_with(&args.app_dir),+ "after UAC, do not reclassify a protected install as writable just because the elevated token can write"+ );+ assert!(backup_dir.starts_with(&args.app_dir));+ assert!(!extract_dir.starts_with(&args.workdir));+ }-/// Poll sysinfo every `LAUNCH_VERIFY_POLL` until the process appears or-/// `timeout` elapses. Lets the happy path exit in ~100-300ms (typical-/// CreateProcess → tasklist register latency on Windows) instead of-/// sleeping a fixed long delay on every successful launch.-fn poll_until_process_running(name: &str, timeout: Duration) -> bool {- let start = std::time::Instant::now();- while start.elapsed() < timeout {- if is_process_running_by_name(name) {- logger::info(format!(- "[installer] new process detected after {:?}",- start.elapsed()- ));- return true;+ #[test]+ fn elevated_staging_stays_in_temp_when_the_install_directory_is_writable() {+ assert!(+ !uses_protected_staging(false, true, true),+ "a writable per-user install is not a UAC-protected staging root"+ );+ assert!(uses_elevated_private_staging(false, true, true));+ assert!(uses_protected_staging(true, true, false));+ assert!(!uses_elevated_private_staging(true, true, false));+ assert!(+ install_writable_for_staging(false, false, false),+ "inherited elevation must treat a medium-writable per-user install as writable"+ );+ assert!(+ !install_writable_for_staging(true, false, false),+ "--elevated is the pre-UAC protected classification; do not re-probe with the high token"+ );+ assert!(!install_writable_for_staging(false, true, false));+ assert!(+ install_writable_for_staging(false, true, true),+ "a denied write probe on a user-owned root is not proof the install is protected"+ );+ assert!(+ !super::install_is_protected_system_for(+ Path::new(r"C:\Users\u\AppData\Local\Cindy"),+ &[+ PathBuf::from(r"C:\Program Files"),+ PathBuf::from(r"C:\Windows"),+ ],+ ),+ "per-user LocalAppData installs stay user-owned across a spoofed PermissionDenied probe"+ );+ assert!(+ super::install_is_protected_system_for(+ Path::new(r"C:\Program Files\Cindy"),+ &[+ PathBuf::from(r"C:\Program Files"),+ PathBuf::from(r"C:\Windows"),+ ],+ ),+ "Program Files is a protected system root"+ );+ assert!(+ !super::install_is_protected_system_for(+ Path::new(r"D:\Games\Cindy"),+ &[+ PathBuf::from(r"C:\Program Files"),+ PathBuf::from(r"C:\Windows"),+ ],+ ),+ "a custom same-user-writable install outside the profile stays user-owned"+ );+ assert!(+ super::install_is_protected_system_for(+ Path::new(r"C:\Windows\System32\Cindy"),+ &[+ PathBuf::from(r"C:\Program Files"),+ PathBuf::from(r"C:\Windows"),+ ],+ ),+ "Windows system directories are protected"+ );+ assert!(+ super::install_is_medium_writable_from_access(Some(true), Some(false)),+ "an Administrators-owned directory that the medium token can modify is not protected"+ );+ assert!(+ !super::install_is_medium_writable_from_access(Some(false), Some(false)),+ "an Administrators-owned directory the medium token cannot modify may use protected staging"+ );+ assert!(+ super::install_is_medium_writable_from_access(None, Some(true)),+ "if AccessCheck cannot be read, fail toward High-IL staging instead of app_dir staging"+ );+ assert!(+ super::install_is_medium_writable_from_access(Some(false), Some(true)),+ "current-user ownership still counts as medium-writable"+ );+ assert!(+ super::install_is_medium_writable_from_file_access(Some(false), Some(true)),+ "a medium-writable Cindy.exe is unprotected even when app_dir denies FILE_ADD_FILE"+ );+ assert!(+ !super::install_is_medium_writable_from_file_access(Some(false), Some(false)),+ "directory and executable both denied remain protected"+ );+ let probe = TestDir::new();+ let app = probe.0.join("Cindy");+ fs::create_dir(&app).unwrap();+ fs::write(app.join("Cindy.exe"), b"exe").unwrap();+ fs::create_dir(app.join("resources")).unwrap();+ fs::write(app.join("resources").join("app.asar"), b"asar").unwrap();+ fs::write(app.join("vcruntime140.dll"), b"dll").unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates.iter().any(|path| path.ends_with("Cindy.exe")),+ "the main executable remains a classification input: {candidates:?}"+ );+ assert!(+ candidates.iter().any(|path| path.ends_with("app.asar")),+ "a writable resources/app.asar must pin the install as unprotected: {candidates:?}"+ );+ assert!(+ candidates+ .iter()+ .any(|path| path.ends_with("vcruntime140.dll")),+ "a writable app-local DLL must pin the install as unprotected: {candidates:?}"+ );+ fs::create_dir_all(+ app.join("resources")+ .join("app.asar.unpacked")+ .join("node_modules")+ .join("better-sqlite3")+ .join("build")+ .join("Release"),+ )+ .unwrap();+ fs::write(+ app.join("resources")+ .join("app.asar.unpacked")+ .join("node_modules")+ .join("better-sqlite3")+ .join("build")+ .join("Release")+ .join("better_sqlite3.node"),+ b"node",+ )+ .unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates+ .iter()+ .any(|path| path.ends_with("better_sqlite3.node")),+ "a writable unpacked native addon must pin the install as unprotected: {candidates:?}"+ );+ fs::create_dir_all(+ app.join("resources")+ .join("app.asar.unpacked")+ .join("node_modules")+ .join("node-pty")+ .join("lib"),+ )+ .unwrap();+ fs::write(+ app.join("resources")+ .join("app.asar.unpacked")+ .join("node_modules")+ .join("node-pty")+ .join("lib")+ .join("index.js"),+ b"module.exports = {}",+ )+ .unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates.iter().any(|path| path.ends_with("index.js")),+ "Forge-unpacked JS such as node-pty must pin the install as unprotected: {candidates:?}"+ );+ fs::create_dir_all(+ app.join("resources")+ .join("tools")+ .join("remote-desktop"),+ )+ .unwrap();+ fs::write(+ app.join("resources")+ .join("tools")+ .join("remote-desktop")+ .join("cindy-windows-desktop-host.node"),+ b"node",+ )+ .unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates+ .iter()+ .any(|path| path.ends_with("cindy-windows-desktop-host.node")),+ "Forge extraResource natives under resources/tools must pin the install as unprotected: {candidates:?}"+ );+ fs::create_dir_all(app.join("resources").join("drizzle").join("scripts")).unwrap();+ fs::write(+ app.join("resources")+ .join("drizzle")+ .join("scripts")+ .join("0031_add_recent_workdirs.ts"),+ b"exports.run = () => {}",+ )+ .unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates+ .iter()+ .any(|path| path.ends_with("0031_add_recent_workdirs.ts")),+ "Forge extraResource drizzle companions required at startup must pin writable: {candidates:?}"+ );+ fs::write(+ app.join("resources")+ .join("windows-installation-version.ps1"),+ b"# sync",+ )+ .unwrap();+ let candidates = super::medium_writable_install_file_candidates(&app, "Cindy.exe");+ assert!(+ candidates+ .iter()+ .any(|path| path.ends_with("windows-installation-version.ps1")),+ "startup PowerShell version script must pin writable: {candidates:?}"+ );+ assert_eq!(+ super::file_write_probe_from_os_error(std::io::ErrorKind::PermissionDenied, Some(5)),+ Some(false),+ "ACL deny is the only CreateFile failure that proves the file is protected"+ );+ assert_eq!(+ super::file_write_probe_from_os_error(std::io::ErrorKind::PermissionDenied, Some(32)),+ Some(true),+ "Windows may map ERROR_SHARING_VIOLATION to PermissionDenied; that is not a protected ACL"+ );+ assert_eq!(+ super::file_write_probe_from_os_error(std::io::ErrorKind::ResourceBusy, Some(32)),+ Some(true),+ "ERROR_SHARING_VIOLATION while Cindy.exe is mapped is not a protected ACL"+ );+ assert_eq!(+ super::file_write_probe_from_os_error(std::io::ErrorKind::Other, Some(33)),+ Some(true),+ "ERROR_LOCK_VIOLATION is also a sharing failure, not PermissionDenied"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(true),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ ),+ Some(true),+ "DELETE without GENERIC_WRITE still replaces resources/app.asar"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ Some(false),+ Some(false),+ ),+ Some(true),+ "parent FILE_DELETE_CHILD can recreate a protected-looking asar"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ Some(false),+ ),+ Some(true),+ "parent FILE_ADD_FILE after delete also replaces the loadable input"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ ),+ Some(false),+ "write, delete, and parent replace rights all denied stay protected"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ ),+ Some(true),+ "FILE_WRITE_DATA without GENERIC_WRITE still overwrites unpacked JS"+ );+ assert_eq!(+ super::file_is_medium_replaceable_from_access(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(true),+ ),+ Some(true),+ "FILE_APPEND_DATA without GENERIC_WRITE still edits unpacked JS"+ );+ assert!(+ super::acl_control_pins_install_writable(Some(true), Some(false)),+ "WRITE_DAC after UAC can replace Cindy.exe even when current write/delete is denied"+ );+ assert!(+ super::acl_control_pins_install_writable(Some(false), Some(true)),+ "WRITE_OWNER likewise lets the medium user take the DACL after classification"+ );+ assert!(+ super::acl_control_pins_install_writable(None, Some(false)),+ "unknown ACL-control must de-elevate, not pin the install as protected"+ );+ assert!(+ !super::acl_control_pins_install_writable(Some(false), Some(false)),+ "denied WRITE_DAC and WRITE_OWNER do not by themselves pin writable"+ );+ assert!(+ super::ancestor_control_pins_install_writable(+ Some(true),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ ),+ "FILE_DELETE_CHILD on a non-immediate unpacked ancestor can swap a package to a junction"+ );+ assert!(+ super::ancestor_control_pins_install_writable(+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ Some(false),+ Some(false),+ ),+ "WRITE_DAC on a higher ancestor still replaces unpacked code after UAC"+ );+ assert!(+ super::ancestor_control_pins_install_writable(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ ),+ "DELETE on resources/tools lets a medium user junction-swap the tree after UAC"+ );+ assert!(+ super::ancestor_control_pins_install_writable(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(true),+ ),+ "FILE_ADD_SUBDIRECTORY on a parent of resources/tools recreates the swapped directory"+ );+ assert!(+ !super::ancestor_control_pins_install_writable(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ ),+ "denied ancestor replace and ACL-control rights do not pin writable"+ );+ assert!(+ super::install_root_parent_pins_install_writable(+ Some(true),+ Some(true),+ Some(false),+ Some(false),+ ),+ "parent FILE_DELETE_CHILD and FILE_ADD_SUBDIRECTORY can replace a protected-looking app_dir across UAC"+ );+ assert!(+ super::install_root_parent_pins_install_writable(+ Some(false),+ Some(false),+ Some(true),+ Some(false),+ ),+ "WRITE_DAC on the install-root parent still replaces app_dir after classification"+ );+ assert!(+ !super::install_root_parent_pins_install_writable(+ Some(false),+ Some(false),+ Some(false),+ Some(false),+ ),+ "denied parent replace and ACL-control rights do not pin writable"+ );+ {+ let nested = probe.0.join("Games").join("Vendor").join("Cindy");+ fs::create_dir_all(&nested).unwrap();+ let ancestors = super::install_root_ancestors(&nested);+ assert!(+ ancestors.iter().any(|path| path.ends_with("Vendor")),+ "must check the immediate parent of app_dir: {ancestors:?}"+ );+ assert!(+ ancestors.iter().any(|path| path.ends_with("Games")),+ "DELETE on a non-immediate ancestor can replace the whole install path: {ancestors:?}"+ );+ assert!(+ !ancestors.iter().any(|path| path == &nested),+ "app_dir itself is classified separately: {ancestors:?}"+ );}- std::thread::sleep(LAUNCH_VERIFY_POLL);+ {+ let nested = app+ .join("resources")+ .join("app.asar.unpacked")+ .join("node_modules")+ .join("node-pty")+ .join("lib")+ .join("index.js");+ let ancestors = super::runtime_path_ancestors(&nested, &app);+ assert!(+ ancestors.iter().any(|path| path.ends_with("node-pty")),+ "must check the package directory, not only lib/: {ancestors:?}"+ );+ assert!(+ ancestors.iter().any(|path| path.ends_with("node_modules")),+ "must check node_modules, not only the immediate parent: {ancestors:?}"+ );+ assert!(+ ancestors+ .iter()+ .any(|path| path.ends_with("app.asar.unpacked")),+ "must check the unpacked tree root: {ancestors:?}"+ );+ assert!(+ !ancestors.iter().any(|path| path == &app),+ "app_dir is classified separately: {ancestors:?}"+ );+ }+ assert!(+ super::unpacked_walk_pins_install_writable(super::UnpackedWalk::Unreadable),+ "an unreadable app.asar.unpacked tree must de-elevate, not look protected"+ );+ assert!(!super::unpacked_walk_pins_install_writable(+ super::UnpackedWalk::Complete+ ));+ assert!(+ super::unpacked_walk_pins_install_writable(super::UnpackedWalk::Reparse),+ "a reparse-point unpacked tree must de-elevate, not look protected"+ );+ {+ let not_dir = probe.0.join("not-a-directory");+ fs::write(¬_dir, b"x").unwrap();+ let mut listed = Vec::new();+ assert_eq!(+ super::collect_medium_writable_unpacked_files(¬_dir, &mut listed),+ super::UnpackedWalk::Unreadable,+ "read_dir failure must not become an empty protected subtree: {listed:?}"+ );+ listed.clear();+ assert_eq!(+ super::collect_medium_writable_root_natives(¬_dir, &mut listed),+ super::UnpackedWalk::Unreadable,+ "unlistable install root must not omit ffmpeg.dll and other natives: {listed:?}"+ );+ let missing = probe.0.join("missing-unpacked");+ listed.clear();+ assert_eq!(+ super::collect_medium_writable_unpacked_files(&missing, &mut listed),+ super::UnpackedWalk::Complete,+ "a missing unpacked tree is empty, not unknown"+ );+ listed.clear();+ assert_eq!(+ super::collect_medium_writable_root_natives(&missing, &mut listed),+ super::UnpackedWalk::Complete,+ "a missing install root is empty, not unknown"+ );+ }+ #[cfg(unix)]+ {+ let target = probe.0.join("unpacked-target");+ fs::create_dir_all(&target).unwrap();+ fs::write(target.join("index.js"), b"js").unwrap();+ let link = probe.0.join("unpacked-link");+ std::os::unix::fs::symlink(&target, &link).unwrap();+ let mut listed = Vec::new();+ assert_eq!(+ super::collect_medium_writable_unpacked_files(&link, &mut listed),+ super::UnpackedWalk::Reparse,+ "a junctioned unpacked tree must not look like a complete protected walk: {listed:?}"+ );+ let asar_target = probe.0.join("asar-target");+ fs::write(&asar_target, b"asar").unwrap();+ let asar_link = probe.0.join("app.asar");+ std::os::unix::fs::symlink(&asar_target, &asar_link).unwrap();+ assert!(+ super::loadable_input_is_reparse(&asar_link),+ "a junctioned resources/app.asar must pin writable, not be skipped"+ );+ }+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ let hidden = probe.0.join("unreadable-unpacked");+ fs::create_dir_all(&hidden).unwrap();+ fs::write(hidden.join("index.js"), b"js").unwrap();+ let original = fs::metadata(&hidden).unwrap().permissions();+ let mut denied = original.clone();+ denied.set_mode(0o000);+ fs::set_permissions(&hidden, denied).unwrap();+ let mut listed = Vec::new();+ let walk = super::collect_medium_writable_unpacked_files(&hidden, &mut listed);+ let _ = fs::set_permissions(&hidden, original);+ assert_eq!(+ walk,+ super::UnpackedWalk::Unreadable,+ "listing failure must not become an empty protected subtree: {listed:?}"+ );+ }+ let source = include_str!("installer.rs");+ let start = source+ .find("fn existing_install_files_medium_writable_windows")+ .expect("windows file probe");+ let body = &source[start..start + 600];+ assert!(+ body.contains("medium_writable_install_file_candidates"),+ "do not classify from Cindy.exe alone:\n{body}"+ );+ let start = source+ .find("fn path_grants_access")+ .expect("file access probe");+ let body = &source[start..start + 900];+ assert!(+ body.contains("file_write_probe_from_os_error"),+ "sharing violations must not collapse to protected:\n{body}"+ );+ let start = source+ .find("fn file_is_medium_replaceable(")+ .expect("replaceable file probe");+ let body = &source[start..start + 900];+ assert!(+ body.contains("DELETE") && body.contains("FILE_DELETE_CHILD") && body.contains("FILE_ADD_FILE"),+ "GENERIC_WRITE alone misses delete/recreate of resources/app.asar:\n{body}"+ );+ let start = source+ .find("fn existing_install_files_medium_writable_windows")+ .expect("windows file classification");+ let body = &source[start..start + 700];+ assert!(+ body.contains("runtime_trees_pin_install_writable"),+ "unlistable install roots and unpacked reparse trees must pin writable under the medium token:\n{body}"+ );+ let start = source+ .find("fn collect_medium_writable_root_natives")+ .expect("root native walk");+ let body = &source[start..start + 700];+ assert!(+ body.contains("Unreadable") && !body.contains("if let Ok(entries)"),+ "install-root listing failure must not omit ffmpeg.dll:\n{body}"+ );+ let start = source+ .find("pub(crate) fn collect_medium_writable_unpacked_files")+ .expect("unpacked walk");+ let body = &source[start..start + 500];+ assert!(+ body.contains("UnpackedWalk::Reparse"),+ "an unpacked reparse point must not look like a complete protected walk:\n{body}"+ );+ let start = source+ .find("fn file_is_medium_replaceable(")+ .expect("replaceable file probe");+ let body = &source[start..start + 1800];+ assert!(+ body.contains("WRITE_DAC") && body.contains("WRITE_OWNER"),+ "ACL-control rights must pin writable before Close relaunch:\n{body}"+ );+ assert!(+ body.contains("runtime_path_ancestors"),+ "a non-immediate unpacked ancestor must be checked, not only path.parent():\n{body}"+ );+ assert!(+ body.contains("FILE_WRITE_DATA") && body.contains("FILE_APPEND_DATA"),+ "GENERIC_WRITE misses a DACL that only grants data/append on unpacked JS:\n{body}"+ );+ assert!(+ body.contains("FILE_ADD_SUBDIRECTORY") && body.contains("DELETE"),+ "ancestor directory DELETE / FILE_ADD_SUBDIRECTORY must pin writable:\n{body}"+ );+ let start = source+ .find("pub(crate) fn install_is_user_owned_for")+ .expect("install classification");+ let body = &source[start..start + 500];+ assert!(+ body.contains("install_root_parent_is_medium_replaceable"),+ "a replaceable parent of app_dir must pin writable across UAC:\n{body}"+ );+ let start = source+ .find("fn directory_medium_token_can_modify_windows")+ .expect("directory medium probe");+ let body = &source[start..start + 900];+ assert!(+ body.contains("WRITE_DAC") && body.contains("WRITE_OWNER"),+ "a directory that only grants WRITE_DAC must not look protected:\n{body}"+ );+ let start = source+ .find("fn existing_install_files_medium_writable_windows")+ .expect("windows file classification");+ let body = &source[start..start + 900];+ assert!(+ body.contains("loadable_input_is_reparse")+ || (body.contains("is_reparse_point") && body.contains("Some(true)")),+ "a reparse-point app.asar must pin writable, not be skipped:\n{body}"+ );+ let start = source+ .find("pub(crate) fn medium_writable_install_file_candidates")+ .expect("candidate list");+ let body = &source[start..start + 900];+ assert!(+ body.contains("tools") || body.contains("extra_resource"),+ "resources/tools extraResource natives must be classification inputs:\n{body}"+ );+ assert!(+ body.contains("drizzle"),+ "resources/drizzle migration companions must be classification inputs:\n{body}"+ );+ assert!(+ body.contains("windows-installation-version.ps1"),+ "startup PowerShell version script must be a classification input:\n{body}"+ );+ let start = source+ .find("fn install_root_parent_is_medium_replaceable_windows")+ .expect("install-root parent probe");+ let body = &source[start..start + 900];+ assert!(+ body.contains("install_root_ancestors"),+ "a non-immediate ancestor of app_dir must be checked, not only parent():\n{body}"+ );+ let temp = TestDir::new();+ let mut args = test_args();+ args.elevated = false;+ args.app_dir = temp.0.join("Cindy");+ args.workdir = temp.0.join("cindy-update-ts");+ fs::create_dir(&args.app_dir).unwrap();+ fs::create_dir(&args.workdir).unwrap();+ let private_root = elevated_private_staging_root(&args);+ let (extract_dir, backup_dir) = staging_dirs_for(&args, true, true);+ assert!(+ extract_dir.starts_with(&private_root),+ "elevated writable installs must not stage in TEMP or app_dir"+ );+ assert!(backup_dir.starts_with(&private_root));+ assert!(!extract_dir.starts_with(&args.workdir));+ assert!(!extract_dir.starts_with(&args.app_dir));++ let source = include_str!("installer.rs");+ let start = source+ .find("pub(crate) fn staging_dirs_for")+ .expect("staging_dirs_for");+ let helper = source[start..]+ .find("/// True when this process already holds an elevated Windows token.")+ .expect("process_is_elevated docs");+ let body = &source[start..start + helper];+ assert!(+ body.contains("uses_elevated_private_staging"),+ "elevated+writable must pick a medium-inaccessible staging root, not workdir:\n{body}"+ );}- false-}-// ─────────────────── Lingering install-dir process sweep ──────────────────+ #[test]+ fn protected_staging_tree_rejects_a_precreated_directory() {+ let temp = TestDir::new();+ let ancestor = temp.0.join("ProgramData");+ fs::create_dir(&ancestor).unwrap();+ let planted = ancestor.join("cindy-update-planted");+ fs::create_dir(&planted).unwrap();+ let extract = planted.join("cindy-update-extract-ts");+ let error = create_protected_staging_tree(&extract, &ancestor).unwrap_err();+ assert_eq!(+ error.kind(),+ std::io::ErrorKind::AlreadyExists,+ "a medium-integrity plant of the staging root must not be reused: {error}"+ );+ assert!(!extract.exists());+ }-/// Refresh `sys` and return `(pid, name)` of every process whose executable-/// path lives under `app_dir`, excluding ourselves. The updater runs from-/// %TEMP% (updateService copies it out of resources/ precisely so it never-/// locks the install dir), so the self-pid exclusion is just belt-and-braces.-fn collect_appdir_processes(- sys: &mut System,- app_dir: &Path,- self_pid: u32,-) -> Vec<(u32, String)> {- sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);- sys.processes()- .iter()- .filter_map(|(pid, p)| {- let pid_u32 = pid.as_u32();- if pid_u32 == self_pid {- return None;- }- let exe = p.exe()?;- if path_is_within(exe, app_dir) {- Some((pid_u32, p.name().to_string_lossy().into_owned()))- } else {- None- }- })- .collect()-}+ #[test]+ fn protected_staging_tree_creates_missing_directories() {+ let temp = TestDir::new();+ let ancestor = temp.0.join("ProgramData");+ fs::create_dir(&ancestor).unwrap();+ let extract = ancestor+ .join("cindy-update-nonce")+ .join("cindy-update-extract-ts");+ create_protected_staging_tree(&extract, &ancestor).unwrap();+ assert!(extract.is_dir());+ let backup = ancestor+ .join("cindy-update-nonce")+ .join("cindy-update-rollback-ts");+ create_protected_staging_tree(&backup, &ancestor).unwrap();+ assert!(backup.is_dir());+ }-/// Sweep processes still running from `app_dir` after the main process exited:-/// poll for voluntary exit up to APPDIR_PROCESS_GRACE, kill survivors, then-/// wait up to APPDIR_PROCESS_KILL_WAIT for their image locks to drop. If any-/// process survives the full grace+kill cycle, we bail (clean abort — no lock-/// was written, no files were touched yet). The user gets an error message-/// naming the culprit processes and can retry after manually ending them.-fn terminate_appdir_processes<F: FnMut(InstallerEvent)>(app_dir: &Path, emit: &mut F) -> anyhow::Result<()> {- let self_pid = std::process::id();- let mut sys = System::new();+ #[test]+ fn protected_staging_acl_is_limited_to_the_private_root() {+ let program_data = Path::new(r"C:\ProgramData");+ let private_root = program_data.join("cindy-update-ts");+ let extract = private_root.join("cindy-update-extract-ts");+ assert!(uses_private_staging_acl(&extract, &private_root));+ let app_dir = program_data.join("Cindy");+ let app_extract = app_dir.join("cindy-update-extract-ts");+ assert!(+ !uses_private_staging_acl(&app_extract, &private_root),+ "a Program Files-style install under ProgramData must keep ordinary create_dir_all"+ );- let mut lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);- if lingering.is_empty() {- return Ok(());+ let source = include_str!("installer.rs");+ let start = source+ .find("fn ensure_staging_directory")+ .expect("ensure_staging_directory");+ let body = &source[start..start + 500];+ assert!(+ body.contains("uses_private_staging_acl"),+ "do not treat every ProgramData descendant as private High-IL staging:\n{body}"+ );}- logger::warn(format!(- "[proc-sweep] {} process(es) still running from app_dir: {:?}",- lingering.len(),- lingering- ));- emit(InstallerEvent::Phase(- Phase::Waiting,- "等待残留进程退出…".into(),- ));- let grace_start = Instant::now();- while !lingering.is_empty() && grace_start.elapsed() < APPDIR_PROCESS_GRACE {- std::thread::sleep(APPDIR_PROCESS_POLL);- lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);+ #[test]+ fn program_data_staging_does_not_trust_the_inherited_environment() {+ let source = include_str!("installer.rs");+ let start = source+ .find("fn known_folder_program_data")+ .expect("known_folder_program_data");+ let body = &source[start..start + 1800];+ assert!(+ !body.contains("var_os(\"ProgramData\")") && !body.contains("var_os(\"PROGRAMDATA\")"),+ "an overridden ProgramData env var must not become the High-IL staging ancestor:\n{body}"+ );+ assert!(+ body.contains("SHGetKnownFolderPath") && body.contains("FOLDERID_ProgramData"),+ "resolve the system ProgramData known folder, not the inherited environment:\n{body}"+ );+ let _ = program_data_dir();}- if lingering.is_empty() {- logger::info("[proc-sweep] all lingering processes exited voluntarily");- return Ok(());++ #[test]+ fn protected_staging_is_created_with_the_security_descriptor() {+ let source = include_str!("installer.rs");+ let start = source+ .find("fn create_directory_with_high_integrity")+ .expect("create_directory_with_high_integrity");+ let windows = source[start..]+ .find("#[cfg(target_os = \"windows\")]\nfn create_directory_with_high_integrity")+ .map(|offset| start + offset)+ .unwrap_or(start);+ let body = &source[windows..windows + 900];+ assert!(+ body.contains("CreateDirectoryW") && body.contains("SECURITY_ATTRIBUTES"),+ "High-IL staging must be born with the DACL, not secured after create_dir:\n{body}"+ );+ let tree = source+ .find("pub(crate) fn create_protected_staging_tree")+ .expect("create_protected_staging_tree");+ let tree_body = &source[tree..tree + 900];+ assert!(+ !tree_body.contains("create_dir_all"),+ "create_dir_all accepts a planted tree before the DACL is applied:\n{tree_body}"+ );}- emit(InstallerEvent::Phase(- Phase::Waiting,- "结束残留进程…".into(),- ));- for (pid, name) in &lingering {- if let Some(p) = sys.process(sysinfo::Pid::from_u32(*pid)) {- let killed = p.kill();- logger::warn(format!(- "[proc-sweep] kill {} (pid {}) → {}",- name, pid, killed- ));- }+ #[test]+ fn successful_launch_does_not_createprocess_a_writable_install_elevated() {+ let source = include_str!("installer.rs");+ let start = source+ .find("启动新版本")+ .expect("success launch phase");+ let body = &source[start..start + 800];+ assert!(+ body.contains("may_relaunch_with_current_integrity")+ || body.contains("launch_app_exe"),+ "copy_tree then CreateProcess of app_dir/Cindy.exe must not inherit a high token on a writable install:\n{body}"+ );+ assert!(+ body.contains("anyhow::bail!") && body.contains("AppLaunch::Skipped"),+ "a skipped de-elevated launch after replacement is a terminal failure, not Done:\n{body}"+ );}- let kill_start = Instant::now();- loop {- lingering = collect_appdir_processes(&mut sys, app_dir, self_pid);- if lingering.is_empty() {- logger::info("[proc-sweep] all lingering processes gone after kill");- return Ok(());- }- if kill_start.elapsed() >= APPDIR_PROCESS_KILL_WAIT {- break;- }- std::thread::sleep(APPDIR_PROCESS_POLL);+ #[test]+ fn close_is_blocked_until_the_install_worker_reaches_a_terminal_phase() {+ assert!(+ close_should_be_blocked(Phase::Extracting, false),+ "the first install never sets retry_started; closing during extract would relaunch a mixed tree"+ );+ assert!(close_should_be_blocked(Phase::Replacing, false));+ assert!(close_should_be_blocked(Phase::RollingBack, false));+ assert!(close_should_be_blocked(Phase::Waiting, false));+ assert!(close_should_be_blocked(Phase::Launching, false));+ assert!(+ close_should_be_blocked(Phase::Failed, true),+ "in-process Retry must still block Close while the worker is rewriting files"+ );+ assert!(!close_should_be_blocked(Phase::Failed, false));+ assert!(!close_should_be_blocked(Phase::Done, false));}- let names: Vec<_> = lingering.iter().map(|(pid, name)| format!("{} (pid {})", name, pid)).collect();- anyhow::bail!(- "安装目录下仍有进程无法终止: {}。可能被杀软或系统保护,请手动结束后重试更新",- names.join(", ")- );-}+ #[test]+ fn elevated_abandon_does_not_relaunch_a_writable_install_with_the_high_token() {+ assert!(+ !may_relaunch_with_current_integrity(false, true, true),+ "an elevated updater must not CreateProcess a medium-writable Cindy.exe"+ );+ assert!(!may_relaunch_with_current_integrity(true, true, true));+ assert!(+ may_relaunch_with_current_integrity(true, true, false),+ "a UAC-protected install root is not plantable by a medium-integrity process"+ );+ assert!(may_relaunch_with_current_integrity(false, false, true));-/// Component-wise "is `child` inside `parent`" check. On Windows both sides-/// are lowercased first: NTFS paths are case-insensitive and the two inputs-/// come from different sources (CLI arg vs. sysinfo) that can disagree on-/// casing (e.g. drive letter). Component-wise comparison (Path::starts_with)-/// keeps the boundary safe — `C:\a\xdt-maker2` is NOT within `C:\a\xdt-maker`.-fn path_is_within(child: &Path, parent: &Path) -> bool {- if parent.as_os_str().is_empty() {- return false;+ let source = include_str!("installer.rs");+ let start = source+ .find("fn relaunch_restored_app(args: &CliArgs)")+ .expect("relaunch_restored_app");+ let end = source[start..]+ .find("pub(crate) fn should_relaunch_restored_app_on_abandon")+ .expect("should_relaunch follows relaunch");+ let body = &source[start..start + end];+ assert!(+ body.contains("may_relaunch_with_current_integrity"),+ "Close/Destroyed must not launch a writable Cindy.exe from a still-elevated updater:\n{body}"+ );}- #[cfg(target_os = "windows")]- {- let child = std::path::PathBuf::from(child.to_string_lossy().to_lowercase());- let parent = std::path::PathBuf::from(parent.to_string_lossy().to_lowercase());- child.starts_with(&parent)++ #[test]+ fn retry_rejects_running_processes_without_terminating_them() {+ let temp = TestDir::new();+ let executable = temp.0.join("retry-process-fixture.exe");+ fs::copy(std::env::current_exe().unwrap(), &executable).unwrap();+ struct Child(std::process::Child);+ impl Drop for Child {+ fn drop(&mut self) {+ let _ = self.0.kill();+ let _ = self.0.wait();+ }+ }+ let mut child = Child(std::process::Command::new(&executable)+ .args(["--exact", "installer::tests::retry_process_fixture", "--ignored"])+ .stdout(std::process::Stdio::null())+ .spawn().unwrap());+ let mut args = test_args();+ args.app_dir = temp.0.clone();+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);+ while super::ensure_retry_processes_closed(&args).is_ok() {+ assert!(std::time::Instant::now() < deadline, "fixture did not appear");+ std::thread::sleep(std::time::Duration::from_millis(50));+ }+ assert_eq!(super::ensure_retry_processes_closed(&args), Err("processes_running".into()));+ assert!(child.0.try_wait().unwrap().is_none(), "retry must not kill the process");+ child.0.kill().unwrap();+ child.0.wait().unwrap();+ assert!(super::ensure_retry_processes_closed(&args).is_ok());}- #[cfg(not(target_os = "windows"))]- {- child.starts_with(parent)++ #[test]+ #[ignore = "child process fixture, invoked only by the retry process test"]+ fn retry_process_fixture() {+ std::thread::sleep(std::time::Duration::from_secs(30));}-}-#[cfg(test)]-mod tests {- use super::path_is_within;- use std::path::Path;+ #[test]+ fn retry_cli_args_reuses_trusted_values_and_elevation_marker() {+ let mut source = test_args();+ source.zip_sha256 = Some("abc123".into());+ let args = retry_cli_args(&source);+ let args: Vec<_> = args+ .iter()+ .map(|arg| arg.to_string_lossy().into_owned())+ .collect();+ assert_eq!(+ args,+ vec![+ "--zip",+ &test_args().workdir.join("retry.zip").to_string_lossy(),+ "--app-dir",+ r"C:\Program Files\Cindy",+ "--exe-name",+ "cindy.exe",+ "--pid",+ "0",+ "--log",+ r"C:\Users\Test User\update.log",+ "--lock",+ r"C:\Users\Test User\update.lock",+ "--workdir",+ r"C:\Users\Test User\update-workdir",+ "--theme",+ "dark",+ "--zip-sha256",+ "abc123",+ "--elevated",+ ]+ );++ let mut unelevated = test_args();+ unelevated.elevated = false;+ assert!(!retry_cli_args(&unelevated)+ .iter()+ .any(|arg| arg == "--elevated"));+ }++ #[test]+ fn elevation_forwards_pinned_install_writable() {+ let mut args = test_args();+ args.elevated = false;+ args.install_writable = Some(true);+ let cmdline = super::build_elevation_arg_string(&args);+ assert!(+ cmdline.contains("--install-writable true"),+ "UAC child must keep the unelevated parent's writable pin:\n{cmdline}"+ );+ assert!(cmdline.contains("--elevated"));++ args.install_writable = Some(false);+ let cmdline = super::build_elevation_arg_string(&args);+ assert!(+ cmdline.contains("--install-writable false"),+ "a protected install pin must survive UAC:\n{cmdline}"+ );++ let parsed = CliArgs::try_parse_from([+ "cindy-updater",+ "--zip",+ r"C:\Users\Test User\update.zip",+ "--app-dir",+ r"C:\Users\Test User\AppData\Local\Cindy",+ "--exe-name",+ "Cindy.exe",+ "--pid",+ "1",+ "--log",+ r"C:\Users\Test User\update.log",+ "--lock",+ r"C:\Users\Test User\update.lock",+ "--workdir",+ r"C:\Users\Test User\update-workdir",+ "--install-writable",+ "true",+ "--elevated",+ ])+ .expect("elevated child parses the forwarded pin");+ assert_eq!(parsed.install_writable, Some(true));+ assert!(parsed.elevated);+ }#[cfg(target_os = "windows")]#[test]@@ -803,6 +5172,20 @@ impl std::fmt::Display for ElevateError {}}+/// The first unelevated attempt may `runas` this TEMP-copied updater once.+/// A later Retry must not: the failure window waits long enough for a+/// same-login process to plant `vcruntime140*.dll` beside the executable.+pub(crate) fn may_self_elevate(is_retry: bool) -> bool {+ !is_retry+}++/// Cancelling or failing the UAC prompt leaves a still-valid archive. Offering+/// Retry would `runas` the same TEMP updater; close the window and check for+/// updates again instead.+pub(crate) fn elevation_prompt_can_retry() -> bool {+ false+}+/// Relaunch THIS updater binary with the same args plus `--elevated`, via/// ShellExecuteExW(verb="runas"). Triggers the Windows UAC prompt. On/// success the elevated child is spawned and the caller should exit; on@@ -889,7 +5272,7 @@ fn build_elevation_arg_string(args: &CliArgs) -> String {ThemeArg::Dark => "dark",ThemeArg::Auto => "auto",};- let pairs: [(&str, String); 8] = [+ let mut pairs: Vec<(&str, String)> = vec![("--zip", args.zip.to_string_lossy().into_owned()),("--app-dir", args.app_dir.to_string_lossy().into_owned()),("--exe-name", args.exe_name.clone()),@@ -899,6 +5282,15 @@ fn build_elevation_arg_string(args: &CliArgs) -> String {("--workdir", args.workdir.to_string_lossy().into_owned()),("--theme", theme_str.to_string()),];+ if let Some(digest) = args.zip_sha256.as_deref().filter(|digest| !digest.is_empty()) {+ pairs.push(("--zip-sha256", digest.to_string()));+ }+ if let Some(writable) = args.install_writable {+ pairs.push((+ "--install-writable",+ if writable { "true".into() } else { "false".into() },+ ));+ }let mut out = String::new();for (k, v) in &pairs {if !out.is_empty() {diff --git a/apps/desktop/cindy-updater/src-tauri/src/lib.rs b/apps/desktop/cindy-updater/src-tauri/src/lib.rsindex ce18ea67a4..b486ead1a2 100644--- a/apps/desktop/cindy-updater/src-tauri/src/lib.rs+++ b/apps/desktop/cindy-updater/src-tauri/src/lib.rs@@ -27,12 +27,21 @@ struct StatusPayload {/// 0..=100, only meaningful for `Extracting`/`Replacing`. -1 = indeterminate.progress: i32,error: Option<String>,+ can_retry: bool,+ /// True when app_dir was never rewritten. Close still relaunches Cindy after+ /// a terminal pre-install Retry even though Retry itself is hidden.+ install_unmodified: bool,+ /// True after rollback restored the previous files. Close still relaunches+ /// Cindy if Retry was later withdrawn by the digest check.+ install_restored: bool,log_path: String,}struct AppState {args: CliArgs,last_status: Arc<Mutex<StatusPayload>>,+ retry_started: Arc<Mutex<bool>>,+ stopped_app: Arc<Mutex<bool>>,}#[tauri::command]@@ -53,12 +62,82 @@ fn open_log_dir(state: State<'_, AppState>) -> Result<(), String> {}#[tauri::command]-fn quit_now(app: AppHandle) {+fn quit_now(app: AppHandle, state: State<'_, AppState>) {+ let (phase, can_retry, install_unmodified, install_restored) = {+ let status = state.last_status.lock().unwrap();+ (status.phase, status.can_retry, status.install_unmodified, status.install_restored)+ };+ let retry_in_progress = *state.retry_started.lock().unwrap();+ if installer::close_should_be_blocked(phase, retry_in_progress) {+ return;+ }+ let stopped_app = *state.stopped_app.lock().unwrap();+ installer::abandon_retry(+ &state.args,+ can_retry,+ retry_in_progress,+ stopped_app,+ install_unmodified,+ install_restored,+ );app.exit(0);}+#[tauri::command]+fn retry_update(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {+ {+ let mut started = state.retry_started.lock().unwrap();+ if *started {+ return Err("in_progress".into());+ }+ let (phase, can_retry) = {+ let status = state.last_status.lock().unwrap();+ (status.phase, status.can_retry)+ };+ installer::retry_request_allowed(&state.args, phase, can_retry)?;+ installer::ensure_retry_processes_closed(&state.args)?;+ *started = true;+ }++ // Continue in this already-loaded process. A fresh spawn from the+ // Electron-created `%TEMP%` workdir would search that directory for+ // `vcruntime140*.dll` before System32; fallback-safe staging leaves those+ // DLLs absent, so a medium-integrity plant would load into an elevated retry.+ let args = installer::retry_args(&state.args);+ let held_lock = match installer::reopen_held_update_lock(&args.lock) {+ Some(lock) => Some(lock),+ None if args.lock.exists() => {+ *state.retry_started.lock().unwrap() = false;+ return Err("updater_busy".into());+ }+ None => None,+ };+ let last_status = state.last_status.clone();+ let retry_started = state.retry_started.clone();+ let stopped_app = state.stopped_app.clone();+ let handle = app.clone();+ logger::info("[command] retry_update continuing in-process");+ std::thread::spawn(move || {+ installer::run_with_lock(args, held_lock, |event| {+ if matches!(event, InstallerEvent::AppExited) {+ *stopped_app.lock().unwrap() = true;+ return;+ }+ let payload = event_to_payload(event, &handle);+ *last_status.lock().unwrap() = payload.clone();+ let _ = handle.emit("update-status", payload);+ });+ *retry_started.lock().unwrap() = false;+ let final_phase = last_status.lock().unwrap().phase;+ if final_phase == Phase::Done {+ handle.exit(0);+ }+ });+ Ok(())+}+pub fn run() {- let args = CliArgs::parse();+ let mut args = CliArgs::parse();logger::init(&args.log);logger::info(format!("[cindy-updater] starting, version={}, args={:?}",@@ -68,6 +147,8 @@ pub fn run() {// Best-effort sweep of >7-day-old current and legacy update leftovers in %TEMP%.// Catches backup dirs from prior failed rollbacks that we intentionally// kept around for manual recovery. Bounded so disk doesn't grow forever.+ // Archive hashing runs in the installer worker after this window is shown.+ installer::pin_install_writable(&mut args);installer::sweep_stale_temp_dirs();let initial_status = StatusPayload {@@ -75,17 +156,29 @@ pub fn run() {message: "等待主程序退出…".into(),progress: -1,error: None,+ can_retry: false,+ install_unmodified: true,+ install_restored: false,log_path: args.log.to_string_lossy().into(),};let last_status = Arc::new(Mutex::new(initial_status));+ let retry_started = Arc::new(Mutex::new(false));+ let stopped_app = Arc::new(Mutex::new(false));let state = AppState {args: args.clone(),last_status: last_status.clone(),+ retry_started,+ stopped_app: stopped_app.clone(),};tauri::Builder::default().manage(state)- .invoke_handler(tauri::generate_handler![get_status, open_log_dir, quit_now])+ .invoke_handler(tauri::generate_handler![+ get_status,+ open_log_dir,+ quit_now,+ retry_update+ ]).setup(move |app| {let win = app.get_webview_window("main");@@ -103,6 +196,37 @@ pub fn run() {// when --theme=dark — because both the win32 surface and the// WebView default to white until HTML/CSS lands.if let Some(w) = win.as_ref() {+ let handle = app.handle().clone();+ w.on_window_event(move |event| {+ match event {+ tauri::WindowEvent::CloseRequested { api, .. } => {+ let state = handle.state::<AppState>();+ let phase = state.last_status.lock().unwrap().phase;+ let retry_in_progress = *state.retry_started.lock().unwrap();+ if installer::close_should_be_blocked(phase, retry_in_progress) {+ api.prevent_close();+ }+ }+ tauri::WindowEvent::Destroyed => {+ let state = handle.state::<AppState>();+ let (can_retry, install_unmodified, install_restored) = {+ let status = state.last_status.lock().unwrap();+ (status.can_retry, status.install_unmodified, status.install_restored)+ };+ let retry_in_progress = *state.retry_started.lock().unwrap();+ let stopped_app = *state.stopped_app.lock().unwrap();+ installer::abandon_retry(+ &state.args,+ can_retry,+ retry_in_progress,+ stopped_app,+ install_unmodified,+ install_restored,+ );+ }+ _ => {}+ }+ });let resolved = match app.state::<AppState>().args.theme {ThemeArg::Light => Theme::Light,ThemeArg::Dark => Theme::Dark,@@ -123,8 +247,13 @@ pub fn run() {let handle = app.handle().clone();let args = args.clone();let last_status = last_status.clone();+ let stopped_app = stopped_app.clone();std::thread::spawn(move || {installer::run(args, |event| {+ if matches!(event, InstallerEvent::AppExited) {+ *stopped_app.lock().unwrap() = true;+ return;+ }let payload = event_to_payload(event, &handle);*last_status.lock().unwrap() = payload.clone();let _ = handle.emit("update-status", payload);@@ -145,19 +274,128 @@ pub fn run() {.expect("tauri failed to launch");}+#[cfg(test)]+mod retry_update_contract {+ #[test]+ fn retry_update_reruns_the_installer_in_process() {+ let source = include_str!("lib.rs");+ let start = source+ .find("fn retry_update")+ .expect("retry_update command");+ let end = source[start..]+ .find("\npub fn run")+ .expect("run follows retry_update");+ let body = &source[start..start + end];+ assert!(+ body.contains("installer::run_with_lock") && body.contains("retry_args"),+ "Retry must continue in this already-loaded process:\n{body}"+ );+ }++ #[test]+ fn quit_now_releases_an_abandoned_retry_lock() {+ let source = include_str!("lib.rs");+ let start = source.find("fn quit_now").expect("quit_now");+ let end = source[start..]+ .find("\nfn retry_update")+ .expect("retry_update follows quit_now");+ let body = &source[start..start + end];+ assert!(+ body.contains("abandon_retry"),+ "Close must abandon Retry (release lock and relaunch restored Cindy):\n{body}"+ );+ assert!(+ source.contains("WindowEvent::Destroyed") && source.contains("abandon_retry"),+ "closing the updater window must also abandon Retry"+ );+ assert!(+ !body.contains("current_exe")+ && !body.contains("Command::new")+ && !body.contains("process::exit"),+ "Retry must not respawn the updater from %TEMP%:\n{body}"+ );+ }++ #[test]+ fn close_requested_is_blocked_while_retry_is_in_progress() {+ let source = include_str!("lib.rs");+ let start = source+ .find("w.on_window_event")+ .expect("window event handler");+ let end = source[start..]+ .find("let resolved = match")+ .expect("theme follows window events");+ let body = &source[start..start + end];+ assert!(+ body.contains("WindowEvent::CloseRequested") && body.contains("prevent_close"),+ "Alt+F4 must not destroy the updater while retry_started is hashing or replacing:\n{body}"+ );+ assert!(+ body.contains("close_should_be_blocked"),+ "close interception must wait for Done/Failed, not only retry_started:\n{body}"+ );+ assert!(+ body.contains("WindowEvent::Destroyed") && body.contains("abandon_retry"),+ "closing the updater window must still abandon Retry after a terminal status"+ );++ let quit_start = source.find("fn quit_now").expect("quit_now");+ let quit_end = source[quit_start..]+ .find("\nfn retry_update")+ .expect("retry_update follows quit_now");+ let quit_body = &source[quit_start..quit_start + quit_end];+ assert!(+ quit_body.contains("close_should_be_blocked"),+ "quit_now must not abandon or exit while the first install is still rewriting files:\n{quit_body}"+ );+ }++ #[test]+ fn run_does_not_hash_the_archive_before_showing_the_window() {+ let source = include_str!("lib.rs");+ let start = source.find("pub fn run()").expect("pub fn run");+ let end = source[start..]+ .find("tauri::Builder::default()")+ .expect("window builder follows startup");+ let body = &source[start..start + end];+ assert!(+ !body.contains("bind_zip_sha256"),+ "hashing a large ZIP before the window is shown leaves the user with no UI:\n{body}"+ );+ }++ #[test]+ fn run_pins_install_writable_before_cloning_into_app_state() {+ let source = include_str!("lib.rs");+ let start = source.find("pub fn run()").expect("pub fn run");+ let end = source[start..]+ .find("tauri::Builder::default()")+ .expect("window builder follows startup");+ let body = &source[start..start + end];+ let pin = body+ .find("pin_install_writable")+ .expect("pin before AppState");+ let clone = body.find("args.clone()").expect("AppState clones args");+ assert!(+ pin < clone,+ "Retry reads AppState.args; pin before that clone or Retry re-probes app_dir:\n{body}"+ );+ }+}+fn event_to_payload(event: InstallerEvent, handle: &AppHandle) -> StatusPayload {- let log_path = handle- .state::<AppState>()- .args- .log- .to_string_lossy()- .into();+ let state = handle.state::<AppState>();+ let log_path = state.args.log.to_string_lossy().into();+ let previous = state.last_status.lock().unwrap().clone();match event {InstallerEvent::Phase(phase, message) => StatusPayload {phase,message,progress: -1,error: None,+ can_retry: false,+ install_unmodified: previous.install_unmodified,+ install_restored: previous.install_restored,log_path,},InstallerEvent::Progress(phase, message, progress) => StatusPayload {@@ -165,6 +403,9 @@ fn event_to_payload(event: InstallerEvent, handle: &AppHandle) -> StatusPayloadmessage,progress,error: None,+ can_retry: false,+ install_unmodified: previous.install_unmodified,+ install_restored: previous.install_restored,log_path,},InstallerEvent::Done => StatusPayload {@@ -172,14 +413,26 @@ fn event_to_payload(event: InstallerEvent, handle: &AppHandle) -> StatusPayloadmessage: "更新完成,正在启动新版本…".into(),progress: 100,error: None,+ can_retry: false,+ install_unmodified: false,+ install_restored: false,log_path,},- InstallerEvent::Failed(err) => StatusPayload {+ InstallerEvent::Failed {+ error,+ can_retry,+ install_unmodified,+ install_restored,+ } => StatusPayload {phase: Phase::Failed,message: "更新失败".into(),progress: -1,- error: Some(err),+ error: Some(error),+ can_retry,+ install_unmodified,+ install_restored,log_path,},+ InstallerEvent::AppExited => unreachable!("AppExited is consumed before UI payload mapping"),}}diff --git a/apps/desktop/cindy-updater/ui/app.js b/apps/desktop/cindy-updater/ui/app.jsindex 8f8020e01e..bfdee6b032 100644--- a/apps/desktop/cindy-updater/ui/app.js+++ b/apps/desktop/cindy-updater/ui/app.js@@ -15,6 +15,7 @@ const PHASE_LABEL = {};const INDETERMINATE_PHASES = new Set(["waiting", "launching", "requesting_elevation"]);+const retryCopy = window.retryCopy(navigator.language);const els = {chip: document.getElementById("phase-chip"),@@ -23,10 +24,16 @@ const els = {track: document.getElementById("progress-track"),errorBar: document.getElementById("error-bar"),errorText: document.getElementById("error-text"),+ btnRetry: document.getElementById("btn-retry"),btnLog: document.getElementById("btn-log"),btnQuit: document.getElementById("btn-quit"),};+els.btnRetry.textContent = retryCopy.retry;++let retrying = false;+let canRetry = false;+function applyStatus(payload) {const phase = payload.phase || "waiting";els.chip.textContent = PHASE_LABEL[phase] || phase;@@ -36,7 +43,7 @@ function applyStatus(payload) {if (phase === "failed") {els.track.hidden = true;els.errorBar.hidden = !payload.error;- els.errorText.textContent = payload.error || "";+ els.errorText.textContent = retryCopy[payload.error] || payload.error || "";} else {els.track.hidden = false;els.errorBar.hidden = true;@@ -67,8 +74,41 @@ function applyStatus(payload) {// Close button only appears in terminal states — there's nothing to abort// mid-update without leaving the install dir half-rewritten.els.btnQuit.hidden = phase !== "done" && phase !== "failed";+ // In-process retry keeps this flag set between the click and the next+ // installer event. Any later status, including another Failed, belongs to+ // the new attempt.+ retrying = false;+ canRetry = phase === "failed" && payload.can_retry === true;+ els.btnRetry.hidden = !canRetry;+ els.btnRetry.disabled = false;}+els.btnRetry.addEventListener("click", async () => {+ if (retrying || !canRetry) return;++ retrying = true;+ els.btnRetry.hidden = true;+ els.btnRetry.disabled = true;+ // Hide Close until the worker reports a terminal state. retry_update returns+ // before hashing the ZIP; quitting here would kill an in-flight install.+ els.btnQuit.hidden = true;++ try {+ await invoke("retry_update");+ } catch (err) {+ retrying = false;+ const code = String(err?.message || err);+ if (code === "archive_unavailable" || code === "unavailable") {+ canRetry = false;+ }+ els.btnRetry.hidden = !canRetry;+ els.btnRetry.disabled = false;+ els.btnQuit.hidden = false;+ els.errorBar.hidden = false;+ els.errorText.textContent = retryCopy[code] || retryCopy.spawn_failed;+ }+});+els.btnLog.addEventListener("click", () => {invoke("open_log_dir").catch((err) => {els.errorBar.hidden = false;diff --git a/apps/desktop/cindy-updater/ui/index.html b/apps/desktop/cindy-updater/ui/index.htmlindex 09c55b5d5a..7de26f45b4 100644--- a/apps/desktop/cindy-updater/ui/index.html+++ b/apps/desktop/cindy-updater/ui/index.html@@ -26,11 +26,13 @@</div><footer class="footer">+ <button id="btn-retry" type="button" class="btn" hidden></button><button id="btn-log" type="button" class="btn">打开日志文件夹</button><button id="btn-quit" type="button" class="btn" hidden>关闭</button></footer></main>+ <script src="./retry-copy.js"></script><script type="module" src="./app.js"></script></body></html>diff --git a/apps/desktop/cindy-updater/ui/retry-copy.js b/apps/desktop/cindy-updater/ui/retry-copy.jsnew file mode 100644index 0000000000..518cfea84c--- /dev/null+++ b/apps/desktop/cindy-updater/ui/retry-copy.js@@ -0,0 +1,63 @@+// The standalone updater does not load the Electron renderer's i18next runtime.+const RETRY_COPY = {+ 'zh-CN': {+ retry: '重试',+ processes_running: '请先关闭 Cindy 及从安装目录运行的其他进程,再重试',+ updater_busy: '另一项更新正在进行,请关闭此窗口后重新检查更新',+ in_progress: '更新重试已在进行中',+ unavailable: '当前更新失败不可重试',+ archive_unavailable: '更新文件已不存在或无法读取,请重新检查更新',+ spawn_failed: '无法启动重试更新,请关闭此窗口后重新检查更新',+ },+ 'zh-TW': {+ retry: '重試',+ processes_running: '請先關閉 Cindy 及從安裝目錄執行的其他程序,再重試',+ updater_busy: '另一項更新正在進行,請關閉此視窗後重新檢查更新',+ in_progress: '更新重試已在進行中',+ unavailable: '目前的更新失敗無法重試',+ archive_unavailable: '更新檔案已不存在或無法讀取,請重新檢查更新',+ spawn_failed: '無法啟動更新重試,請關閉此視窗後重新檢查更新',+ },+ en: {+ retry: 'Retry',+ processes_running:+ 'Close Cindy and any processes running from its installation folder, then retry',+ updater_busy:+ 'Another update is already running. Close this window and check for updates again',+ in_progress: 'An update retry is already in progress',+ unavailable: 'This update failure cannot be retried',+ archive_unavailable: 'The update file is missing or unreadable. Please check for updates again',+ spawn_failed: 'Could not restart the updater. Close this window and check for updates again',+ },+ ja: {+ retry: '再試行',+ processes_running:+ 'Cindy とインストール先から実行中の他のプロセスを終了してから、再試行してください',+ updater_busy:+ '別の更新が進行中です。このウィンドウを閉じて、更新を再度確認してください',+ in_progress: '更新の再試行はすでに進行中です',+ unavailable: 'この更新エラーは再試行できません',+ archive_unavailable: '更新ファイルが見つからないか、読み取れません。更新を再度確認してください',+ spawn_failed:+ '更新の再試行を開始できません。このウィンドウを閉じて、更新を再度確認してください',+ },+ ko: {+ retry: '다시 시도',+ processes_running:+ 'Cindy 및 설치 폴더에서 실행 중인 다른 프로세스를 종료한 후 다시 시도해 주세요',+ updater_busy:+ '다른 업데이트가 이미 진행 중입니다. 이 창을 닫고 업데이트를 다시 확인해 주세요',+ in_progress: '업데이트 재시도가 이미 진행 중입니다',+ unavailable: '이 업데이트 오류는 다시 시도할 수 없습니다',+ archive_unavailable: '업데이트 파일이 없거나 읽을 수 없습니다. 업데이트를 다시 확인해 주세요',+ spawn_failed:+ '업데이트 재시도를 시작할 수 없습니다. 이 창을 닫고 업데이트를 다시 확인해 주세요',+ },+};++globalThis.retryCopy = function retryCopy(language = 'en') {+ const locale = language.toLowerCase();+ if (/^zh-(tw|hk|mo|hant)(-|$)/.test(locale)) return RETRY_COPY['zh-TW'];+ if (locale === 'zh' || locale.startsWith('zh-')) return RETRY_COPY['zh-CN'];+ return RETRY_COPY[locale.split('-')[0]] || RETRY_COPY.en;+};diff --git a/apps/desktop/cindy-updater/ui/style.css b/apps/desktop/cindy-updater/ui/style.cssindex a8fbf8b34c..93623f7cb5 100644--- a/apps/desktop/cindy-updater/ui/style.css+++ b/apps/desktop/cindy-updater/ui/style.css@@ -215,22 +215,38 @@ body {}.btn {- padding: 6px 14px;+ display: inline-flex;+ align-items: center;+ justify-content: center;+ height: 32px;+ padding: 0 24px;border-radius: 9999px;background: var(--btn-bg);border: 1px solid var(--btn-border);color: var(--btn-text);font-family: inherit;- font-size: 12px;+ font-size: 13px;font-weight: 500;- line-height: 1.4;+ line-height: 1;cursor: pointer;transition: background 120ms ease;}-.btn:hover {- background: var(--card-border);+.btn:hover:not(:disabled) {+ background: color-mix(in srgb, var(--btn-bg) 92%, var(--text-primary));+}+.btn:active:not(:disabled) {+ background: color-mix(+ in srgb,+ color-mix(in srgb, var(--btn-bg) 92%, var(--text-primary)) 90%,+ var(--text-primary)+ );}.btn:focus-visible {outline: 2px solid rgba(59, 130, 246, 0.5);outline-offset: 1px;}++.btn:disabled {+ cursor: default;+ opacity: 0.6;+}diff --git a/apps/desktop/src/main/__tests__/linuxInstallation.test.ts b/apps/desktop/src/main/__tests__/linuxInstallation.test.tsindex 6ec0471fd1..dac68a5167 100644--- a/apps/desktop/src/main/__tests__/linuxInstallation.test.ts+++ b/apps/desktop/src/main/__tests__/linuxInstallation.test.ts@@ -30,6 +30,8 @@ describe.skipIf(process.platform !== 'linux')('user installer transaction smokelet prefix: string;const installer = path.resolve(__dirname, '../../../resources/linux/install-user.sh');const packages = new Map<string, { file: string; digest: string; size: number }>();+ // Six bsdtar+ar fixtures under a contended Linux unit shard exceed vitest's+ // 10s hookTimeout (CI: 10758ms). Spawn timeouts below are already 15s.beforeAll(() => {root = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-install-test-'));prefix = path.join(root, 'home', "Cindy's space $literal");@@ -54,7 +56,7 @@ describe.skipIf(process.platform !== 'linux')('user installer transaction smokeconst bytes = fs.readFileSync(file);packages.set(version, { file, digest: createHash('sha256').update(bytes).digest('hex'), size: bytes.length });}- });+ }, 30_000);afterAll(() => fs.rmSync(root, { recursive: true, force: true }));function run(version: string, apply = false, overrides: { prefix?: string; digest?: string; version?: string; region?: string; env?: NodeJS.ProcessEnv } = {}) {const pkg = packages.get(version)!;diff --git a/apps/desktop/src/main/__tests__/piSubagentUpdateRelaunchGate.test.ts b/apps/desktop/src/main/__tests__/piSubagentUpdateRelaunchGate.test.tsindex dac4eecc43..3c9fed7170 100644--- a/apps/desktop/src/main/__tests__/piSubagentUpdateRelaunchGate.test.ts+++ b/apps/desktop/src/main/__tests__/piSubagentUpdateRelaunchGate.test.ts@@ -116,7 +116,7 @@ describe('PI Subagent reclaim before an update relaunch', () => {const relaunch = source.slice(source.indexOf('async function executeRelaunch('));const gate = relaunch.indexOf('if (!await reclaimSubagentRunnersForRelaunch())');const attempts = relaunch.indexOf('incrementApplyAttempts();');- const windows = relaunch.indexOf('executeUpdateWindows(readyFilePath, theme);');+ const windows = relaunch.indexOf('executeUpdateWindows(readyFilePath, theme, readyZipSha256);');const mac = relaunch.indexOf('executeUpdateMacOS(readyFilePath);');expect(gate).toBeGreaterThan(-1);expect(attempts).toBeGreaterThan(gate);diff --git a/apps/desktop/src/main/__tests__/updateLockWait.test.ts b/apps/desktop/src/main/__tests__/updateLockWait.test.tsnew file mode 100644index 0000000000..a910b84d04--- /dev/null+++ b/apps/desktop/src/main/__tests__/updateLockWait.test.ts@@ -0,0 +1,107 @@+import fs from 'node:fs';+import { describe, expect, it } from 'vitest';++import {+ isWindowsUpdateLockSharingViolation,+ shouldKeepWaitingForWindowsUpdateLock,+} from '../updateLockWait';++describe('Windows update lock wait', () => {+ it('keeps waiting after 30s when Retry still owns .updating', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs: 30_000,+ maxWaitMs: 30_000,+ holderPid: 4242,+ holderAlive: true,+ unlinkFailed: true,+ sharingViolation: true,+ }),+ ).toBe(true);+ });++ it('attempts unlink after the timeout even if a live PID was recycled', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs: 30_000,+ maxWaitMs: 30_000,+ holderPid: 4242,+ holderAlive: true,+ unlinkFailed: false,+ sharingViolation: false,+ }),+ ).toBe(false);+ });++ it('keeps waiting when unlink fails because the updater still holds the file', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs: 45_000,+ maxWaitMs: 30_000,+ holderPid: 4242,+ holderAlive: true,+ unlinkFailed: true,+ sharingViolation: true,+ }),+ ).toBe(true);+ });++ it('stops waiting after the timeout when a sharing violation is not from a live updater PID', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs: 45_000,+ maxWaitMs: 30_000,+ holderPid: 4242,+ holderAlive: false,+ unlinkFailed: true,+ sharingViolation: true,+ }),+ ).toBe(false);+ });++ it('treats EBUSY as the updater-owned sharing violation', () => {+ expect(isWindowsUpdateLockSharingViolation({ code: 'EBUSY', errno: -4082 })).toBe(true);+ expect(isWindowsUpdateLockSharingViolation({ code: 'EPERM', errno: -1 })).toBe(false);+ });++ it('stops waiting after the timeout when unlink fails for an unrelated ACL error', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs: 45_000,+ maxWaitMs: 30_000,+ holderPid: null,+ holderAlive: false,+ unlinkFailed: true,+ sharingViolation: false,+ }),+ ).toBe(false);+ });++ it('is used by Windows startup instead of a fixed 30s break', () => {+ const source = fs.readFileSync(+ new URL('../bootstrap-electron.ts', import.meta.url),+ 'utf8',+ );+ expect(source).toContain('shouldKeepWaitingForWindowsUpdateLock');+ expect(source).toContain('sharingViolation');+ });++ it('stops waiting once the lock is gone', () => {+ expect(+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: false,+ elapsedMs: 1_000,+ maxWaitMs: 30_000,+ holderPid: null,+ holderAlive: false,+ unlinkFailed: false,+ sharingViolation: false,+ }),+ ).toBe(false);+ });+});diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.tsindex 79c7dc50d3..67f6db556b 100644--- a/apps/desktop/src/main/__tests__/updateService.test.ts+++ b/apps/desktop/src/main/__tests__/updateService.test.ts@@ -1,4 +1,5 @@import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';+import { createHash } from 'node:crypto';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';@@ -442,7 +443,7 @@ function updateManifest(version = '0.0.65', hotfixFile?: string) {version,hotfix: {file: hotfixFile ?? `app/darwin-arm64/xdt-maker-${version}.zip`,- sha256: 'abc',+ sha256: 'a'.repeat(64),size: 123,},},@@ -800,6 +801,289 @@ describe('app update forward-only policy', () => {}});+ it('keeps an offline Windows patch but refuses to apply its persisted digest', async () => {+ vi.useFakeTimers();+ fetchManifest.mockResolvedValue(null);+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const updatesDir = path.join(TEST_USER_DATA, 'updates');+ const patchPath = path.join(updatesDir, 'staged.zip');+ const patchInfoPath = path.join(updatesDir, 'patch-info.json');+ fs.mkdirSync(updatesDir, { recursive: true });+ fs.writeFileSync(patchPath, 'update');+ fs.writeFileSync(+ patchInfoPath,+ JSON.stringify({+ version: '0.0.65',+ fileName: 'staged.zip',+ sha256: 'f'.repeat(64),+ enableBeta: false,+ }),+ );++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ const handler = ipcHandlers.get('update-check-startup');+ await expect(handler?.()).resolves.toMatchObject({+ hasUpdate: false,+ action: 'none',+ error: 'manifest_failed',+ });+ expect(service.getUpdateStatus()).toBe('idle');+ expect(spawnProcess).not.toHaveBeenCalled();+ expect(fs.existsSync(patchPath)).toBe(true);+ expect(fs.existsSync(patchInfoPath)).toBe(true);+ } finally {+ service.stopUpdateService();+ }+ });++ it('hashes staged Windows zips as a stream instead of one main-thread buffer', () => {+ const source = fs.readFileSync(+ path.join(__dirname, '..', 'updateService.ts'),+ 'utf8',+ );+ const start = source.indexOf('function windowsZipFileMatchesDigest');+ expect(start).toBeGreaterThan(-1);+ const body = source.slice(start, start + 900);+ expect(body).toContain('createReadStream');+ expect(body).not.toMatch(/readFileSync\s*\(/);+ expect(body).not.toMatch(/readFile\s*\(/);+ });++ it('lets a later online check re-anchor an offline-ready Windows patch', async () => {+ vi.useFakeTimers();+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const stagedBytes = Buffer.from('update');+ const digest = createHash('sha256').update(stagedBytes).digest('hex');+ const manifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ manifest.app.hotfix.sha256 = digest;+ fetchManifest+ .mockResolvedValueOnce(null)+ .mockResolvedValueOnce(manifest);+ const updatesDir = path.join(TEST_USER_DATA, 'updates');+ fs.mkdirSync(updatesDir, { recursive: true });+ fs.writeFileSync(path.join(updatesDir, 'staged.zip'), stagedBytes);+ fs.writeFileSync(+ path.join(updatesDir, 'patch-info.json'),+ JSON.stringify({+ version: '0.0.65',+ fileName: 'staged.zip',+ sha256: 'f'.repeat(64),+ enableBeta: false,+ }),+ );++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ const startupHandler = ipcHandlers.get('update-check-startup');+ await expect(startupHandler?.()).resolves.toMatchObject({+ hasUpdate: false,+ action: 'none',+ error: 'manifest_failed',+ });++ const checkNowHandler = ipcHandlers.get('update-check-now');+ await expect(checkNowHandler?.()).resolves.toEqual({ result: 'ready' });+ expect(fetchManifest).toHaveBeenCalledTimes(2);+ expect(download).not.toHaveBeenCalled();+ } finally {+ service.stopUpdateService();+ }+ });++ it('redownloads a ready Windows patch when the same version is republished under a new filename', async () => {+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const firstDigest = 'a'.repeat(64);+ const secondDigest = 'b'.repeat(64);+ const firstManifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ firstManifest.app.hotfix.sha256 = firstDigest;+ const secondManifest = updateManifest('0.0.65', 'app/windows-x64/cindy-0.0.65.zip');+ secondManifest.app.hotfix.sha256 = secondDigest;+ download.mockImplementation(async ({ targetPath, sha256 }: { targetPath: string; sha256: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, sha256 === firstDigest ? 'old-bytes' : 'new-bytes');+ return { path: targetPath, size: 123 };+ });++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ await expect(service.checkForUpdate(firstManifest)).resolves.toBe('ready');+ expect(download).toHaveBeenCalledTimes(1);++ await expect(service.checkForUpdate(secondManifest)).resolves.toBe('ready');+ expect(download).toHaveBeenCalledTimes(2);+ expect(download.mock.calls[1]?.[0]).toMatchObject({ sha256: secondDigest });+ expect(fs.readFileSync(path.join(TEST_USER_DATA, 'updates', 'cindy-0.0.65.zip'), 'utf8')).toBe(+ 'new-bytes',+ );+ } finally {+ service.stopUpdateService();+ }+ });++ it('redownloads a ready Windows patch when the same file is republished with a new digest', async () => {+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const firstDigest = 'a'.repeat(64);+ const secondDigest = 'b'.repeat(64);+ const firstManifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ firstManifest.app.hotfix.sha256 = firstDigest;+ const secondManifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ secondManifest.app.hotfix.sha256 = secondDigest;+ download.mockImplementation(async ({ targetPath, sha256 }: { targetPath: string; sha256: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, sha256 === firstDigest ? 'old-bytes' : 'new-bytes');+ return { path: targetPath, size: 123 };+ });++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ await expect(service.checkForUpdate(firstManifest)).resolves.toBe('ready');+ expect(download).toHaveBeenCalledTimes(1);+ const patchPath = path.join(TEST_USER_DATA, 'updates', 'staged.zip');+ expect(fs.readFileSync(patchPath, 'utf8')).toBe('old-bytes');++ await expect(service.checkForUpdate(secondManifest)).resolves.toBe('ready');+ expect(download).toHaveBeenCalledTimes(2);+ expect(download.mock.calls[1]?.[0]).toMatchObject({ sha256: secondDigest });+ expect(fs.readFileSync(patchPath, 'utf8')).toBe('new-bytes');+ } finally {+ service.stopUpdateService();+ }+ });++ it('redownloads a cold-started Windows patch when the same version is republished under a new filename', async () => {+ vi.useFakeTimers();+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const digest = 'd'.repeat(64);+ const manifest = updateManifest('0.0.65', 'app/windows-x64/cindy-0.0.65.zip');+ manifest.app.hotfix.sha256 = digest;+ fetchManifest.mockResolvedValue(manifest);+ const updatesDir = path.join(TEST_USER_DATA, 'updates');+ fs.mkdirSync(updatesDir, { recursive: true });+ fs.writeFileSync(path.join(updatesDir, 'staged.zip'), 'old-bytes');+ fs.writeFileSync(+ path.join(updatesDir, 'patch-info.json'),+ JSON.stringify({+ version: '0.0.65',+ fileName: 'staged.zip',+ sha256: 'f'.repeat(64),+ enableBeta: false,+ }),+ );+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, 'new-bytes');+ return { path: targetPath, size: 123 };+ });++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ const handler = ipcHandlers.get('update-check-startup');+ await expect(handler?.()).resolves.toMatchObject({+ hasUpdate: true,+ version: '0.0.65',+ });+ expect(download).toHaveBeenCalledTimes(1);+ expect(download.mock.calls[0]?.[0]).toMatchObject({ sha256: digest });+ expect(fs.readFileSync(path.join(updatesDir, 'cindy-0.0.65.zip'), 'utf8')).toBe('new-bytes');+ } finally {+ service.stopUpdateService();+ }+ });++ it('redownloads a cold-started Windows patch whose bytes do not match the current digest', async () => {+ vi.useFakeTimers();+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ const digest = 'c'.repeat(64);+ const manifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ manifest.app.hotfix.sha256 = digest;+ fetchManifest.mockResolvedValue(manifest);+ const updatesDir = path.join(TEST_USER_DATA, 'updates');+ fs.mkdirSync(updatesDir, { recursive: true });+ fs.writeFileSync(path.join(updatesDir, 'staged.zip'), 'stale-bytes');+ fs.writeFileSync(+ path.join(updatesDir, 'patch-info.json'),+ JSON.stringify({+ version: '0.0.65',+ fileName: 'staged.zip',+ sha256: 'f'.repeat(64),+ enableBeta: false,+ }),+ );+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, 'fresh-bytes');+ return { path: targetPath, size: 123 };+ });++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ const handler = ipcHandlers.get('update-check-startup');+ await expect(handler?.()).resolves.toMatchObject({+ hasUpdate: true,+ version: '0.0.65',+ });+ expect(download).toHaveBeenCalledTimes(1);+ expect(fs.readFileSync(path.join(updatesDir, 'staged.zip'), 'utf8')).toBe('fresh-bytes');+ } finally {+ service.stopUpdateService();+ }+ });++ it('uses the current matching manifest digest for a staged Windows patch', async () => {+ vi.useFakeTimers();+ readAutoUpdateSettings.mockReturnValue({ autoRelaunchOnIdle: false });+ checkWindowsUpdaterPrerequisites.mockReturnValue({+ satisfied: false,+ missingFiles: ['vcruntime140.dll'],+ });+ const stagedBytes = Buffer.from('update');+ const digest = createHash('sha256').update(stagedBytes).digest('hex');+ const manifest = updateManifest('0.0.65', 'app/windows-x64/staged.zip');+ manifest.app.hotfix.sha256 = digest;+ fetchManifest.mockResolvedValue(manifest);+ const updatesDir = path.join(TEST_USER_DATA, 'updates');+ const patchPath = path.join(updatesDir, 'staged.zip');+ fs.mkdirSync(updatesDir, { recursive: true });+ fs.writeFileSync(patchPath, stagedBytes);+ fs.writeFileSync(+ path.join(updatesDir, 'patch-info.json'),+ JSON.stringify({+ version: '0.0.65',+ fileName: 'staged.zip',+ sha256: 'f'.repeat(64),+ enableBeta: false,+ }),+ );++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ const handler = ipcHandlers.get('update-check-startup');+ await expect(handler?.()).resolves.toMatchObject({+ hasUpdate: true,+ action: 'none',+ version: '0.0.65',+ });+ expect(download).not.toHaveBeenCalled();++ ipcListeners.get('update-relaunch')?.({}, 'dark');++ expect(checkWindowsUpdaterPrerequisites).toHaveBeenCalledTimes(2);+ expect(logError).not.toHaveBeenCalledWith(+ 'Windows update archive is missing its trusted manifest SHA-256',+ );+ } finally {+ service.stopUpdateService();+ }+ });+it('does not apply a lower local patch that matches the online manifest', async () => {vi.useFakeTimers();fetchManifest.mockResolvedValue(updateManifest('0.0.63'));@@ -1835,6 +2119,66 @@ describe('startup update relaunch safety', () => {});describe('Windows updater prerequisites', () => {+ it('passes the verified manifest digest to the Windows updater', async () => {+ const digest = 'a'.repeat(64);+ const manifest = updateManifest('0.0.65');+ manifest.app.hotfix.sha256 = digest;+ const resourcesPath = path.join(TEST_ROOT, 'resources');+ fs.mkdirSync(resourcesPath, { recursive: true });+ fs.writeFileSync(path.join(resourcesPath, 'cindy-updater.exe'), 'updater');+ const resourcesPathDescriptor = Object.getOwnPropertyDescriptor(process, 'resourcesPath');+ Object.defineProperty(process, 'resourcesPath', { value: resourcesPath, configurable: true });+ const tmpdirSpy = vi.spyOn(os, 'tmpdir').mockReturnValue(TEST_ROOT);+ const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);+ spawnProcess.mockImplementationOnce(() => ({+ unref: vi.fn(),+ on: vi.fn((event: string, listener: () => void) => {+ if (event === 'spawn') listener();+ }),+ }));+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, 'update');+ return { path: targetPath, size: 123 };+ });++ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ await expect(service.checkForUpdate(manifest)).resolves.toBe('ready');+ ipcListeners.get('update-relaunch')?.({}, 'dark');+ await vi.waitFor(() => { expect(spawnProcess).toHaveBeenCalledOnce(); });+ const spawnedArgs = (spawnProcess.mock.calls as unknown as Array<[string, string[]]>)[0]?.[1];+ expect(spawnedArgs).toEqual(expect.arrayContaining(['--zip-sha256', digest]));+ } finally {+ service.stopUpdateService();+ tmpdirSpy.mockRestore();+ exitSpy.mockRestore();+ if (resourcesPathDescriptor) Object.defineProperty(process, 'resourcesPath', resourcesPathDescriptor);+ else Reflect.deleteProperty(process, 'resourcesPath');+ }+ });++ it('refuses to spawn the Windows updater when the manifest digest is missing', async () => {+ const manifest = updateManifest('0.0.65');+ manifest.app.hotfix.sha256 = '';+ download.mockImplementation(async ({ targetPath }: { targetPath: string }) => {+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });+ fs.writeFileSync(targetPath, 'update');+ return { path: targetPath, size: 123 };+ });+ const service = await freshUpdateService('win32');+ service.initUpdateService();+ try {+ await expect(service.checkForUpdate(manifest)).resolves.toBe('ready');+ ipcListeners.get('update-relaunch')?.({}, 'dark');+ await vi.waitFor(() => { expect(service.getUpdateStatus()).toBe('error'); });+ expect(spawnProcess).not.toHaveBeenCalled();+ } finally {+ service.stopUpdateService();+ }+ });+it('defers the first startup relaunch and exposes the prerequisite error', async () => {vi.useFakeTimers();checkWindowsUpdaterPrerequisites.mockReturnValue({diff --git a/apps/desktop/src/main/__tests__/updaterUi.test.ts b/apps/desktop/src/main/__tests__/updaterUi.test.tsnew file mode 100644index 0000000000..6603f8d2e4--- /dev/null+++ b/apps/desktop/src/main/__tests__/updaterUi.test.ts@@ -0,0 +1,265 @@+import fs from 'node:fs';+import vm from 'node:vm';+import { describe, expect, it } from 'vitest';++const source = fs.readFileSync(+ new URL('../../../cindy-updater/ui/app.js', import.meta.url),+ 'utf8',+);++function createUi() {+ const elements = new Map<+ string,+ {+ hidden: boolean;+ disabled: boolean;+ textContent: string;+ className: string;+ dataset: Record<string, string>;+ style: Record<string, string>;+ classList: { add(): void; remove(): void };+ addEventListener(event: string, handler: () => Promise<void>): void;+ click?: () => Promise<void>;+ }+ >();+ let statusListener: (event: { payload: Record<string, unknown> }) => void = () => {};+ let rejectRetry: (error: Error) => void = () => {};+ let resolveRetry: () => void = () => {};+ let retryCalls = 0;+ const context = vm.createContext({+ console,+ navigator: { language: 'en-US' },+ document: {+ getElementById(id: string) {+ const element = {+ hidden: false,+ disabled: false,+ textContent: '',+ className: '',+ dataset: {},+ style: {},+ classList: { add() {}, remove() {} },+ addEventListener(_event: string, handler: () => Promise<void>) {+ Object.assign(element, { click: handler });+ },+ };+ elements.set(id, element);+ return element;+ },+ },+ window: {+ __TAURI__: {+ core: {+ invoke(command: string) {+ if (command === 'get_status') return Promise.resolve({ phase: 'waiting' });+ if (command === 'retry_update') {+ retryCalls++;+ return new Promise<void>((resolve, reject) => {+ resolveRetry = () => resolve();+ rejectRetry = reject;+ });+ }+ return Promise.resolve();+ },+ },+ event: {+ listen(_event: string, listener: typeof statusListener) {+ statusListener = listener;+ return Promise.resolve();+ },+ },+ },+ },+ });+ vm.runInContext(+ fs.readFileSync(new URL('../../../cindy-updater/ui/retry-copy.js', import.meta.url), 'utf8'),+ context,+ );+ context.window.retryCopy = context.retryCopy;+ vm.runInContext(source, context);+ return {+ elements,+ status: (payload: Record<string, unknown>) => statusListener({ payload }),+ retryCalls: () => retryCalls,+ resolveRetry: () => resolveRetry(),+ rejectRetry: (message = 'spawn failed') => rejectRetry(new Error(message)),+ };+}++describe('updater failure retry UI', () => {+ it('provides retry copy for all supported languages with English fallback', () => {+ const context = vm.createContext({});+ vm.runInContext(+ fs.readFileSync(new URL('../../../cindy-updater/ui/retry-copy.js', import.meta.url), 'utf8'),+ context,+ );+ for (const [locale, label] of [+ ['zh-CN', '重试'],+ ['zh-Hant-HK', '重試'],+ ['en-US', 'Retry'],+ ['ja-JP', '再試行'],+ ['ko-KR', '다시 시도'],+ ['fr-FR', 'Retry'],+ ]) {+ const copy = context.retryCopy(locale);+ expect(copy.retry).toBe(label);+ expect(Object.values(copy).every((text) => typeof text === 'string' && text.length > 0)).toBe(+ true,+ );+ }+ });+ it('only offers retry for a failed status explicitly marked safe by Rust', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ const button = ui.elements.get('btn-retry')!;+ for (const payload of [+ { phase: 'waiting', can_retry: true },+ { phase: 'replacing', can_retry: true },+ { phase: 'done', can_retry: true },+ { phase: 'failed' },+ { phase: 'failed', can_retry: false },+ { phase: 'failed', can_retry: 'true' },+ ]) {+ ui.status(payload);+ expect(button.hidden).toBe(true);+ }+ expect(ui.retryCalls()).toBe(0);+ ui.status({ phase: 'failed', can_retry: true });+ expect(button.hidden).toBe(false);+ });++ it('keeps retry hidden after the command is accepted until a later failed status', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const button = ui.elements.get('btn-retry')!;+ const pending = button.click!();+ ui.resolveRetry();+ await pending;+ expect(button.hidden).toBe(true);+ expect(button.disabled).toBe(true);+ await button.click!();+ expect(ui.retryCalls()).toBe(1);+ ui.status({ phase: 'waiting', can_retry: false });+ expect(button.hidden).toBe(true);+ ui.status({ phase: 'failed', can_retry: true });+ expect(button.hidden).toBe(false);+ expect(button.disabled).toBe(false);+ });++ it('hides Close as soon as Retry is accepted and restores it only on a later failed status', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const close = ui.elements.get('btn-quit')!;+ expect(close.hidden).toBe(false);+ const pending = ui.elements.get('btn-retry')!.click!();+ expect(close.hidden).toBe(true);+ ui.resolveRetry();+ await pending;+ expect(close.hidden).toBe(true);+ ui.status({ phase: 'waiting', can_retry: false });+ expect(close.hidden).toBe(true);+ ui.status({ phase: 'failed', can_retry: true });+ expect(close.hidden).toBe(false);+ });++ it('restores Close when Retry is rejected before a worker starts', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const close = ui.elements.get('btn-quit')!;+ const pending = ui.elements.get('btn-retry')!.click!();+ expect(close.hidden).toBe(true);+ ui.rejectRetry('processes_running');+ await pending;+ expect(close.hidden).toBe(false);+ });++ it('submits one retry despite repeated clicks and restores the button on spawn failure', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const button = ui.elements.get('btn-retry')!;+ const pending = button.click!();+ await button.click!();+ expect(ui.retryCalls()).toBe(1);+ expect(button.hidden).toBe(true);+ expect(button.disabled).toBe(true);+ ui.rejectRetry();+ await pending;+ expect(button.hidden).toBe(false);+ expect(button.disabled).toBe(false);+ expect(ui.elements.get('error-text')!.textContent).toBe(+ 'Could not restart the updater. Close this window and check for updates again',+ );+ });++ it('asks the user to close Cindy when retry finds running installation processes', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true, error: 'processes_running' });+ const message =+ 'Close Cindy and any processes running from its installation folder, then retry';+ expect(ui.elements.get('error-text')!.textContent).toBe(message);+ const pending = ui.elements.get('btn-retry')!.click!();+ ui.rejectRetry('processes_running');+ await pending;+ expect(ui.elements.get('error-text')!.textContent).toBe(message);+ expect(ui.elements.get('btn-retry')!.hidden).toBe(false);+ });++ it('does not restore retry when the latest status no longer permits it', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const button = ui.elements.get('btn-retry')!;+ const pending = button.click!();+ ui.status({ phase: 'failed', can_retry: false });+ ui.rejectRetry();+ await pending;+ expect(button.hidden).toBe(true);+ });++ it('hides retry after the archive is gone and keeps the check-for-updates guidance', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const button = ui.elements.get('btn-retry')!;+ const pending = button.click!();+ ui.rejectRetry('archive_unavailable');+ await pending;+ expect(button.hidden).toBe(true);+ expect(ui.elements.get('error-text')!.textContent).toBe(+ 'The update file is missing or unreadable. Please check for updates again',+ );+ await button.click!();+ expect(ui.retryCalls()).toBe(1);+ });++ it('hides retry when Rust reports the failure is no longer retryable', async () => {+ const ui = createUi();+ await new Promise<void>((resolve) => setImmediate(resolve));+ ui.status({ phase: 'failed', can_retry: true });+ const button = ui.elements.get('btn-retry')!;+ const pending = button.click!();+ ui.rejectRetry('unavailable');+ await pending;+ expect(button.hidden).toBe(true);+ expect(ui.elements.get('error-text')!.textContent).toBe(+ 'This update failure cannot be retried',+ );+ });++ it('sizes updater actions as standard secondary pills', () => {+ const css = fs.readFileSync(+ new URL('../../../cindy-updater/ui/style.css', import.meta.url),+ 'utf8',+ );+ const btnBlock = css.match(/\.btn\s*\{[^}]+\}/)?.[0] ?? '';+ expect(btnBlock).toContain('height: 32px');+ expect(btnBlock).toContain('padding: 0 24px');+ expect(btnBlock).toContain('font-size: 13px');+ expect(css).toMatch(/\.btn:active:not\(:disabled\)/);+ });+});diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.tsindex b4e30b3d6d..aa295d6687 100644--- a/apps/desktop/src/main/bootstrap-electron.ts+++ b/apps/desktop/src/main/bootstrap-electron.ts@@ -220,6 +220,10 @@ import {notifyUpdateAutoRelaunchBusyStateChanged,setUpdateAutoRelaunchBusyProbe,} from './updateService';+import {+ isWindowsUpdateLockSharingViolation,+ shouldKeepWaitingForWindowsUpdateLock,+} from './updateLockWait';import {createUpdatePresentationRecoveryController,decideUpdateRelaunchBusyTransition,@@ -2653,8 +2657,46 @@ if (started) {Atomics.wait(lockWait, 0, 0, pollMs);continue;}- if (Date.now() - start >= maxWaitMs) break;- Atomics.wait(lockWait, 0, 0, pollMs);+ const holderPid = readLockPid();+ const holderAlive = holderPid !== null && pidAlive(holderPid);+ const elapsedMs = Date.now() - start;+ if (+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: true,+ elapsedMs,+ maxWaitMs,+ holderPid,+ holderAlive,+ unlinkFailed: false,+ sharingViolation: false,+ })+ ) {+ Atomics.wait(lockWait, 0, 0, pollMs);+ continue;+ }+ let unlinkFailed = false;+ let sharingViolation = false;+ try {+ fs.unlinkSync(lockPath);+ } catch (error) {+ unlinkFailed = fs.existsSync(lockPath);+ sharingViolation = unlinkFailed && isWindowsUpdateLockSharingViolation(error);+ }+ if (+ shouldKeepWaitingForWindowsUpdateLock({+ lockExists: fs.existsSync(lockPath),+ elapsedMs,+ maxWaitMs,+ holderPid,+ holderAlive,+ unlinkFailed,+ sharingViolation,+ })+ ) {+ Atomics.wait(lockWait, 0, 0, pollMs);+ continue;+ }+ break;}// If still locked after the wait, proceed anyway (stale lock).// 锁已不存在(更新脚本正常清掉)同样算已清——等锁实例必须走diff --git a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.tsindex 3de5f7eddc..486a3d8e00 100644--- a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts@@ -122,7 +122,7 @@ describe('接管陈旧锁', () => {// 降级(而不是宣称持有):内容写会跳过,清理路径照常执行。expect(status).toEqual({ held: false, reason: 'busy' });// 不该把 waitMs 熬完,也不该反复重试删除。- expect(elapsed).toBeLessThan(1_000);+ expect(elapsed).toBeLessThan(2_000);expect(renameAttempts).toBe(1);// 别人的锁没被动过。expect(fs.existsSync(lock)).toBe(true);diff --git a/apps/desktop/src/main/updateLockWait.ts b/apps/desktop/src/main/updateLockWait.tsnew file mode 100644index 0000000000..e958d4ffea--- /dev/null+++ b/apps/desktop/src/main/updateLockWait.ts@@ -0,0 +1,41 @@+/**+ * Windows startup must not enter the install directory while cindy-updater+ * still owns `.updating`. Retry reopens that handle with FILE_SHARE_READ, so+ * unlinkSync fails with a sharing violation; a fixed 30s timeout is not enough+ * for a large ZIP. Unrelated ACL / deny-delete errors are not proof of Retry+ * and must not block the pre-window startup loop forever.+ */++export type WindowsUpdateLockWaitInput = {+ lockExists: boolean;+ elapsedMs: number;+ maxWaitMs: number;+ holderPid: number | null;+ holderAlive: boolean;+ unlinkFailed: boolean;+ sharingViolation: boolean;+};++export function isWindowsUpdateLockSharingViolation(error: unknown): boolean {+ if (!error || typeof error !== 'object') {+ return false;+ }+ const code = 'code' in error ? error.code : undefined;+ const errno = 'errno' in error ? error.errno : undefined;+ return code === 'EBUSY' || errno === -4082 || errno === 32;+}++export function shouldKeepWaitingForWindowsUpdateLock(+ input: WindowsUpdateLockWaitInput,+): boolean {+ if (!input.lockExists) {+ return false;+ }+ if (input.unlinkFailed && input.sharingViolation && input.holderAlive) {+ return true;+ }+ if (input.holderAlive && input.elapsedMs < input.maxWaitMs) {+ return true;+ }+ return input.elapsedMs < input.maxWaitMs;+}diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.tsindex e39946fb44..8f593d8953 100644--- a/apps/desktop/src/main/updateService.ts+++ b/apps/desktop/src/main/updateService.ts@@ -21,6 +21,7 @@*/import { app, BrowserWindow, ipcMain, powerMonitor } from 'electron';+import { createHash } from 'node:crypto';import path from 'node:path';import fs from 'node:fs';import { spawn } from 'node:child_process';@@ -170,6 +171,8 @@ const STARTUP_MANIFEST_TIMEOUT_MS = 8_000;let currentStatus: UpdateStatus = 'idle';let readyVersion: string | undefined;let readyFilePath: string | undefined;+/** SHA-256 from the verified manifest for the staged Windows archive. */+let readyZipSha256: string | undefined;/** 当前 staged 补丁对应的渠道代际。延迟清理用它区分「同路径上的新旧包」。 */let readyChannelEpoch: number | undefined;/**@@ -180,6 +183,7 @@ let readyChannelEpoch: number | undefined;let updateChannelEpoch = 0;/** 本进程上次看到的有效渠道。别的共库实例改开关后,用这个发现跨进程渠道变化。 */let observedEnableBeta = false;+let firstCheckTimer: ReturnType<typeof setTimeout> | null = null;let pollTimer: ReturnType<typeof setInterval> | null = null;let autoRelaunchPollTimer: ReturnType<typeof setInterval> | null = null;let isRelaunching = false;@@ -697,9 +701,11 @@ function checkExistingPatch(): { action: 'relaunch' | 'check' | 'none'; version?return { action: 'check' };}- // Patch present, awaiting relaunch.+ // Patch present, awaiting relaunch. User-writable patch-info metadata cannot+ // re-establish the Windows archive trust anchor after a cold start.readyVersion = patchInfo.version;readyFilePath = patchFilePath;+ readyZipSha256 = undefined;readyChannelEpoch = updateChannelEpoch;return { action: 'relaunch', version: patchInfo.version };}@@ -851,6 +857,7 @@ function discardStagedPatchFiles(): void {}readyVersion = undefined;readyFilePath = undefined;+ readyZipSha256 = undefined;readyChannelEpoch = undefined;linuxStagedDebSha256 = null;linuxStagedDebSize = null;@@ -879,6 +886,7 @@ function flushDeferredStagedPatchClear(): void {if (readyChannelEpoch === stale.epoch) {readyVersion = undefined;readyFilePath = undefined;+ readyZipSha256 = undefined;readyChannelEpoch = undefined;}return;@@ -1102,6 +1110,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Checonst wasReady = currentStatus === 'ready';const previousReadyVersion = wasReady ? readyVersion : undefined;const previousReadyFilePath = wasReady ? readyFilePath : undefined;+ const previousReadyZipSha256 = wasReady ? readyZipSha256 : undefined;const previousReadyChannelEpoch = wasReady ? readyChannelEpoch : undefined;// 只有非 ready 路径才广播 'checking' — wasReady 路径下广播 checking 会让 banner@@ -1171,9 +1180,58 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che}// wasReady 且 manifest 仍是已下好的同一个版本 → 无事发生,保持 ready。+ // Windows 同文件名但 SHA-256 变了时不能把新摘要绑到旧字节上,必须重下。+ let replacingReadyWindowsDigest = false;if (wasReady && latestVersion === previousReadyVersion) {- log.info('Ready patch v%s still matches latest — no superseding needed', previousReadyVersion);- return 'ready';+ if (process.platform === 'win32') {+ const sameFile = path.basename(asset.file) === path.basename(previousReadyFilePath ?? '');+ const trustedSha256 = normalizeWindowsZipSha256(asset.sha256);+ if (!trustedSha256) {+ log.info('Windows: current manifest cannot re-anchor the ready patch — discarding it');+ discardStagedPatchFiles();+ return 'idle';+ }+ if (!sameFile) {+ log.info('Windows: ready patch filename changed — downloading newly advertised artifact');+ replacingReadyWindowsDigest = true;+ } else if (!windowsZipDigestsEqual(previousReadyZipSha256, trustedSha256)) {+ log.info('Windows: ready patch digest changed — re-downloading current artifact');+ replacingReadyWindowsDigest = true;+ } else {+ readyZipSha256 = trustedSha256;+ log.info('Ready patch v%s still matches latest — no superseding needed', previousReadyVersion);+ return 'ready';+ }+ } else {+ log.info('Ready patch v%s still matches latest — no superseding needed', previousReadyVersion);+ return 'ready';+ }+ }++ // Cold-started Windows patches stay off the relaunch path until a current+ // manifest restores the trust anchor. Reuse that staged file only when its+ // bytes still match the current digest.+ if (!wasReady && process.platform === 'win32') {+ const patchResult = checkExistingPatch();+ if (patchResult.action === 'relaunch' && patchResult.version === latestVersion) {+ const sameFile = path.basename(asset.file) === path.basename(readyFilePath ?? '');+ const trustedSha256 = normalizeWindowsZipSha256(asset.sha256);+ if (!trustedSha256) {+ log.info('Windows: current manifest cannot re-anchor the staged patch — discarding it');+ discardStagedPatchFiles();+ return 'idle';+ }+ if (!sameFile) {+ log.info('Windows: staged patch filename changed — downloading newly advertised artifact');+ } else if (await windowsZipFileMatchesDigest(readyFilePath, trustedSha256)) {+ readyZipSha256 = trustedSha256;+ log.info('Staged patch v%s matches latest — skipping re-download', latestVersion);+ setStatus('ready', { version: latestVersion });+ return 'ready';+ } else {+ log.info('Windows: staged patch digest does not match current manifest — re-downloading');+ }+ }}log.info('Update available: %s → %s (wasReady=%s)', currentVersion, latestVersion, wasReady);@@ -1181,10 +1239,12 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Checonst downloadUrl = `${getBaseUrl()}/${asset.file}`;const fileName = path.basename(asset.file);const destPath = path.join(getUpdatesDir(), fileName);+ const keepPreviousReady = wasReady && !replacingReadyWindowsDigest;// wasReady 路径下,旧的 a.zip 必须保留到 b 通过 SHA 校验之后才能删,否则 b 下载失败// 时用户连旧的 a 都装不上了。非 wasReady 路径保持原行为(下载前清理腾空间)。- if (!wasReady) {+ // 同文件 digest 变更会覆盖 destPath,不能再按 superseding 回滚到旧摘要。+ if (!keepPreviousReady) {cleanOldFiles(fileName);setStatus('downloading', { version: latestVersion, progress: 0 });} else {@@ -1210,7 +1270,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che// 启动态热更包几乎总是队首(入队即开下),这条 0% 就是"真实开始"的信号;// 极少数排在二进制下载之后的场景,renderer 侧会在二进制段活跃期间丢弃它,// 不会产生假进度条。后台轮询场景 renderer 以 status==='passed' 挡掉,不受影响。- if (!wasReady) {+ if (!keepPreviousReady) {broadcastUpdateProgress({ progress: 0, received: 0, total: lastTotal });}@@ -1221,9 +1281,9 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che// AND the legacy `app-update-progress` channel (used by EnvCheckContext// splash flow). Keeping the old channel preserves the current renderer// contract — see app-update-system-frontend.md ADR-2.- // wasReady 路径不广播 update-status:banner 期间维持 superseding,version 不变,+ // keepPreviousReady 路径不广播 update-status:banner 期间维持 superseding,version 不变,// 否则每次 progress 都会把 status 重新设回 downloading,banner 隐藏闪烁。- if (!wasReady) {+ if (!keepPreviousReady) {broadcastStatus({status: 'downloading',version: latestVersion,@@ -1280,8 +1340,8 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Chetotal: result.size,});- // wasReady 路径下,SHA 已经过了,这时候才真正安全地把旧 a.zip 清掉。- if (wasReady) {+ // keepPreviousReady 路径下,SHA 已经过了,这时候才真正安全地把旧 a.zip 清掉。+ if (keepPreviousReady) {cleanOldFiles(fileName);}@@ -1315,6 +1375,9 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che}readyVersion = latestVersion;readyFilePath = result.path;+ readyZipSha256 = process.platform === 'win32'+ ? normalizeWindowsZipSha256(asset.sha256)+ : undefined;readyChannelEpoch = updateChannelEpoch;// 信任锚:manifest 里的 installer 摘要与大小,进本进程内存,不落用户可写盘。linuxStagedDebSha256 = normalizeLinuxDebSha256(asset.sha256 ?? '');@@ -1331,10 +1394,10 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Chelog.error('unexpected download error:', err);}- // wasReady 路径:静默回退到旧的 ready 状态。a.zip / patch-info.json / readyVersion /+ // keepPreviousReady 路径:静默回退到旧的 ready 状态。a.zip / patch-info.json / readyVersion /// readyFilePath 全都没动过,直接重新广播 ready 让 banner 按钮从 loading 恢复成可点。- // 下一次 30min 轮询会再次尝试 b。- if (wasReady) {+ // 下一次 30min 轮询会再次尝试 b。同文件 digest 变更会覆盖 destPath,不能回滚。+ if (keepPreviousReady) {// 下载期间用户切渠道:旧 patch 已被作废,不能从局部快照恢复。// 代际在下载开始前就可能已经推进过,所以还要看旧补丁自己的代际。const staleChannelPatch =@@ -1347,6 +1410,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Che}readyVersion = undefined;readyFilePath = undefined;+ readyZipSha256 = undefined;readyChannelEpoch = undefined;removePatchInfo();setStatus('idle');@@ -1355,6 +1419,7 @@ async function doCheckForUpdate(manifestOverride?: Manifest | null): Promise<Chelog.info('Superseding download failed — rolling back to ready v%s', previousReadyVersion);readyVersion = previousReadyVersion;readyFilePath = previousReadyFilePath;+ readyZipSha256 = previousReadyZipSha256;readyChannelEpoch = previousReadyChannelEpoch;setStatus('ready', { version: previousReadyVersion });return 'ready';@@ -1385,6 +1450,7 @@ function handleApplyFailure(reason: string): void {removePatchInfo();readyVersion = undefined;readyFilePath = undefined;+ readyZipSha256 = undefined;readyChannelEpoch = undefined;isRelaunching = false;autoRelaunchInProgress = false;@@ -1411,8 +1477,70 @@ function handleApplyFailure(reason: string): void {// ── F3: Platform Executors ────────────────────────────────────────────────+function normalizeWindowsZipSha256(value: string | undefined): string | undefined {+ return typeof value === 'string' && /^[a-f0-9]{64}$/i.test(value)+ ? value.toLowerCase()+ : undefined;+}++function windowsZipDigestsEqual(left: string | undefined, right: string | undefined): boolean {+ const a = normalizeWindowsZipSha256(left);+ const b = normalizeWindowsZipSha256(right);+ return Boolean(a && b && a === b);+}++async function windowsZipFileMatchesDigest(+ filePath: string | undefined,+ expected: string,+): Promise<boolean> {+ if (!filePath) return false;+ try {+ const actual = await hashFileSha256(filePath);+ return windowsZipDigestsEqual(actual, expected);+ } catch {+ return false;+ }+}++function hashFileSha256(filePath: string): Promise<string> {+ return new Promise((resolve, reject) => {+ const hash = createHash('sha256');+ const stream = fs.createReadStream(filePath);+ stream.on('error', reject);+ stream.on('data', (chunk: Buffer | string) => {+ hash.update(chunk);+ });+ stream.on('end', () => resolve(hash.digest('hex')));+ });+}++function handleMissingWindowsArchiveDigest(): void {+ cancelStartupBinaryUpdateCheck?.();+ cancelStartupBinaryUpdateCheck = undefined;+ log.error('Windows update archive is missing its trusted manifest SHA-256');+ // Cold-started patches have only user-writable patch-info metadata until a+ // current manifest re-establishes the trust anchor. Refuse this apply, but+ // keep the staged patch so a later online startup/check can reconcile it.+ readyZipSha256 = undefined;+ isRelaunching = false;+ autoRelaunchInProgress = false;+ void clearSubagentLaunchFence().catch((err: unknown) => {+ log.error('clearing the Subagent launch fence after a failed apply failed: %s', String(err));+ });+ setStatus('error', { errorCode: 'updater_spawn_failed' });+}++function executeUpdateWindows(+ zipPath: string,+ theme: 'light' | 'dark',+ expectedSha256: string | undefined,+): void {+ const trustedSha256 = normalizeWindowsZipSha256(expectedSha256);+ if (!trustedSha256) {+ handleMissingWindowsArchiveDigest();+ return;+ }-function executeUpdateWindows(zipPath: string, theme: 'light' | 'dark'): void {const appExePath = app.getPath('exe');const appDir = path.dirname(appExePath);const exeName = path.basename(appExePath);@@ -1496,6 +1624,7 @@ function executeUpdateWindows(zipPath: string, theme: 'light' | 'dark'): void {// an in-app override disagrees with the OS preference.const args = ['--zip', zipPath,+ '--zip-sha256', trustedSha256,'--app-dir', appDir,'--exe-name', exeName,'--pid', String(pid),@@ -1984,7 +2113,7 @@ async function executeRelaunchUnguarded(theme: 'light' | 'dark', checkForBinaryUswitch (process.platform) {case 'win32':- executeUpdateWindows(readyFilePath, theme);+ executeUpdateWindows(readyFilePath, theme, readyZipSha256);break;case 'darwin':// Increment immediately before starting the platform executor so a@@ -2204,7 +2333,11 @@ export function initUpdateService(): void {// 这里复用 'downloading' 结果让前端 toast 文案保持一致。if (currentStatus === 'downloading') return { result: 'downloading' };if (currentStatus === 'superseding') return { result: 'downloading' };- if (currentStatus === 'ready') return { result: 'ready' };+ // A cold-started Windows patch has no process-local trust anchor yet. Let a+ // current manifest re-establish it instead of short-circuiting forever.+ if (currentStatus === 'ready' && (process.platform !== 'win32' || readyZipSha256)) {+ return { result: 'ready' };+ }const result = await checkForUpdate();return { result };});@@ -2236,10 +2369,22 @@ export function initUpdateService(): void {// Network unavailable — fall back to local patch.log.info('Manifest fetch failed, falling back to local patch');const patchResult = checkExistingPatch();- if (patchResult.action === 'relaunch' && process.platform !== 'linux') {+ if (patchResult.action === 'relaunch' && process.platform === 'darwin') {currentStatus = 'ready';return await buildStartupReadyReply(patchResult.version);}+ if (patchResult.action === 'relaunch' && process.platform === 'win32') {+ // Windows 安装的信任锚是当前进程里的 manifest SHA-256。冷启动+ // checkExistingPatch 不会信任用户可写的 patch-info.sha256,断网时+ // 没有可信摘要就不能 relaunch;保留暂存包等下次在线对账。+ log.info(+ 'Windows: manifest unavailable — keeping local patch without relaunch until a trusted digest is restored',+ );+ readyVersion = undefined;+ readyFilePath = undefined;+ readyZipSha256 = undefined;+ readyChannelEpoch = undefined;+ }if (patchResult.action === 'relaunch' && process.platform === 'linux') {// Linux 安装的信任锚是 manifest 里的 installer 摘要;断网拿不到// manifest 时旧补丁没有可信摘要,宁可重下也不装。@@ -2286,7 +2431,37 @@ export function initUpdateService(): void {const patchResult = checkExistingPatch();if (patchResult.action === 'relaunch' && patchResult.version === latestVersion) {log.info('Local patch v%s matches latest, requesting relaunch', patchResult.version);- if (process.platform === 'linux') {+ let reuseStagedPatch = true;+ if (process.platform === 'win32') {+ const hotfix = manifest.app.hotfix;+ const sameFile = Boolean(+ hotfix && path.basename(hotfix.file) === path.basename(readyFilePath ?? ''),+ );+ const trustedSha256 = hotfix ? normalizeWindowsZipSha256(hotfix.sha256) : undefined;+ if (!trustedSha256) {+ log.info('Windows: manifest has no trusted hotfix digest — discarding local patch');+ discardStagedPatchFiles();+ return { hasUpdate: false, action: 'none' as const };+ }+ if (!sameFile) {+ log.info('Windows: local patch filename changed — will download newly advertised artifact');+ readyVersion = undefined;+ readyFilePath = undefined;+ readyZipSha256 = undefined;+ readyChannelEpoch = undefined;+ reuseStagedPatch = false;+ } else if (!(await windowsZipFileMatchesDigest(readyFilePath, trustedSha256))) {+ log.info('Windows: local patch bytes do not match current digest — will re-download');+ readyVersion = undefined;+ readyFilePath = undefined;+ readyZipSha256 = undefined;+ readyChannelEpoch = undefined;+ reuseStagedPatch = false;+ } else {+ readyZipSha256 = trustedSha256;+ }+ }+ if (reuseStagedPatch && process.platform === 'linux') {// 冷启动匹配旧补丁:把这份 CDN manifest 的 installer 摘要与大小// 重新锚进进程内存,让后续 apply 有可信锚可用。const installer = manifest.app.installer;@@ -2302,8 +2477,10 @@ export function initUpdateService(): void {return { hasUpdate: false, action: 'none' as const };}}- currentStatus = 'ready';- return await buildStartupReadyReply(patchResult.version);+ if (reuseStagedPatch) {+ currentStatus = 'ready';+ return await buildStartupReadyReply(patchResult.version);+ }}// Stale local patch — drop refs, fresh download will overwrite.@@ -2314,6 +2491,7 @@ export function initUpdateService(): void {);readyVersion = undefined;readyFilePath = undefined;+ readyZipSha256 = undefined;readyChannelEpoch = undefined;linuxStagedDebSha256 = null;linuxStagedDebSize = null;@@ -2367,7 +2545,8 @@ export function initUpdateService(): void {powerMonitor.on('unlock-screen', handlePowerMonitorActivity);powerMonitor.on('user-did-become-active', handlePowerMonitorActivity);- setTimeout(() => {+ firstCheckTimer = setTimeout(() => {+ firstCheckTimer = null;log.info('First background check fires');checkForUpdate().catch((err) => {log.error('Background check threw:', err);@@ -2422,6 +2601,10 @@ export async function enableUncustomizedBetaChannel(}export function stopUpdateService(): void {+ if (firstCheckTimer) {+ clearTimeout(firstCheckTimer);+ firstCheckTimer = null;+ }if (pollTimer) {clearInterval(pollTimer);pollTimer = null;diff --git a/apps/desktop/src/renderer/components/settings/__tests__/ModelPriceOverrideDialog.test.tsx b/apps/desktop/src/renderer/components/settings/__tests__/ModelPriceOverrideDialog.test.tsxindex ff94acdf4d..19d9d266ac 100644--- a/apps/desktop/src/renderer/components/settings/__tests__/ModelPriceOverrideDialog.test.tsx+++ b/apps/desktop/src/renderer/components/settings/__tests__/ModelPriceOverrideDialog.test.tsx@@ -247,9 +247,10 @@ describe('ModelPriceOverrideDialog', () => {onOpenChange={onOpenChangeA}/>,);- const save = await waitFor(() =>- first.getByRole('button', { name: 'settings.providers.models.priceOverride.save' }),- );+ const save = first.getByRole('button', {+ name: 'settings.providers.models.priceOverride.save',+ });+ await waitFor(() => expect(save.hasAttribute('disabled')).toBe(false));fireEvent.click(save);await waitFor(() => expect(setModelPriceOverride).toHaveBeenCalledOnce());@@ -293,9 +294,10 @@ describe('ModelPriceOverrideDialog', () => {onOpenChange={onOpenChange}/>,);- const save = await waitFor(() =>- getByRole('button', { name: 'settings.providers.models.priceOverride.save' }),- );+ const save = getByRole('button', {+ name: 'settings.providers.models.priceOverride.save',+ });+ await waitFor(() => expect(save.hasAttribute('disabled')).toBe(false));fireEvent.click(save);await waitFor(() => expect(setModelPriceOverride).toHaveBeenCalledOnce());