Apply error handling for elevated process

This commit is contained in:
Caelan Sayler
2025-05-26 21:44:41 +01:00
parent a29e21c874
commit 1bb82d744a
2 changed files with 18 additions and 3 deletions

View File

@@ -51,14 +51,29 @@ pub fn apply_package_impl(old_locator: &VelopackLocator, package: &PathBuf, run_
} else { } else {
info!("Re-launching as administrator to update in {:?}", root_path); info!("Re-launching as administrator to update in {:?}", root_path);
let args: Vec<OsString> = vec!["--norestart".into(), "--package".into(), package.into()]; let args: Vec<OsString> = vec!["--norestart".into(), "--package".into(), package.into(), "--root".into(), root_path.into()];
let exe_path = std::env::current_exe()?; let exe_path = std::env::current_exe()?;
let work_dir: Option<String> = None; // same as this process let work_dir: Option<String> = None; // same as this process
let process_handle = process::run_process_as_admin(&exe_path, args, work_dir, false)?; let process_handle = process::run_process_as_admin(&exe_path, args, work_dir, false)?;
info!("Waiting (up to 10 minutes) for elevated process to exit..."); info!("Waiting (up to 10 minutes) for elevated process to exit...");
let result = process::wait_for_process_to_exit(process_handle, Some(Duration::from_secs(10 * 60)))?; let result = process::wait_for_process_to_exit(process_handle, Some(Duration::from_secs(10 * 60)))?;
info!("Elevated process has exited ({:?}).", result);
match result {
process::WaitResult::WaitTimeout => {
bail!("Elevated process has not exited within 10 minutes. (TIMEOUT)");
}
process::WaitResult::ExitCode(code) => {
if code != 0 {
bail!("Elevated process has exited with ERROR: {}.", code);
} else {
info!("Elevated process has run successfully.");
}
}
process::WaitResult::NoWaitRequired => {
info!("Elevated process has not required waiting.");
}
}
return Ok(new_locator); return Ok(new_locator);
} }
} }

View File

@@ -123,7 +123,7 @@ namespace Velopack.Locators
var tempTargetUpdateExe = Path.Combine(TempAppRootDirectory, "Update.exe"); var tempTargetUpdateExe = Path.Combine(TempAppRootDirectory, "Update.exe");
if (File.Exists(UpdateExePath) && !File.Exists(tempTargetUpdateExe)) { if (File.Exists(UpdateExePath) && !File.Exists(tempTargetUpdateExe)) {
initLog.Warn("Application directory is not writable. Copying Update.exe to temp location: " + tempTargetUpdateExe); initLog.Warn("Application directory is not writable. Copying Update.exe to temp location: " + tempTargetUpdateExe);
Debugger.Launch(); // Debugger.Launch();
Directory.CreateDirectory(TempAppRootDirectory); Directory.CreateDirectory(TempAppRootDirectory);
File.Copy(UpdateExePath, tempTargetUpdateExe); File.Copy(UpdateExePath, tempTargetUpdateExe);
} }