From 9da43b39610c05734f9c49b0cc272125e9dd7830 Mon Sep 17 00:00:00 2001 From: idk Date: Mon, 21 Mar 2022 11:04:27 -0400 Subject: [PATCH] Try a way of determining required rights for the installer --- java/net/i2p/router/Elevator.java | 4 ++ java/net/i2p/router/WinUpdateProcess.java | 31 +++++++++----- .../router/WindowsUpdatePostProcessor.java | 42 ++++++++++--------- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/java/net/i2p/router/Elevator.java b/java/net/i2p/router/Elevator.java index 3f69327..2715a15 100644 --- a/java/net/i2p/router/Elevator.java +++ b/java/net/i2p/router/Elevator.java @@ -10,6 +10,10 @@ public class Elevator { } public static void executeAsAdministrator(String command, String args) { + if (command == "" || command == null) { + System.out.println("No command specified"); + return; + } Shell32X.SHELLEXECUTEINFO execInfo = new Shell32X.SHELLEXECUTEINFO(); execInfo.lpFile = new WString(command); if (args != null) diff --git a/java/net/i2p/router/WinUpdateProcess.java b/java/net/i2p/router/WinUpdateProcess.java index 46dadcc..9dc6071 100644 --- a/java/net/i2p/router/WinUpdateProcess.java +++ b/java/net/i2p/router/WinUpdateProcess.java @@ -42,20 +42,29 @@ class WinUpdateProcess implements Runnable { var workingDir = workDir(); var logFile = new File(workingDir, "log-" + version + ".txt"); - ProcessBuilder pb = new ProcessBuilder(file.getAbsolutePath()); - var env = pb.environment(); - env.put("OLD_I2P_VERSION", version); - env.remove("RESTART_I2P"); + if (logFile.canWrite()) { + // check if we can write to the log file. If we can, use the ProcessBuilder to + // run the installer. + ProcessBuilder pb = new ProcessBuilder(file.getAbsolutePath(), "/S", "/D=" + workingDir.getAbsolutePath()); + var env = pb.environment(); + env.put("OLD_I2P_VERSION", version); + env.remove("RESTART_I2P"); - int exitCode = ctx.router().scheduledGracefulExitCode(); - if (exitCode == Router.EXIT_HARD_RESTART || exitCode == Router.EXIT_GRACEFUL_RESTART) - env.put("RESTART_I2P", "true"); + int exitCode = ctx.router().scheduledGracefulExitCode(); + if (exitCode == Router.EXIT_HARD_RESTART || exitCode == Router.EXIT_GRACEFUL_RESTART) + env.put("RESTART_I2P", "true"); - try { - pb.directory(workingDir).redirectErrorStream(true).redirectOutput(logFile).start(); - } catch (IOException ex) { - System.out.println("Unable to run update-program in background. Update will fail."); + try { + pb.directory(workingDir).redirectErrorStream(true).redirectOutput(logFile).start(); + } catch (IOException ex) { + System.out.println("Unable to run update-program in background. Update will fail."); + } + } else { + // If we cant write to the log file and we're on Windows, use the elevator to + // execute the installer instead of the ProcessBuilder. + Elevator.executeAsAdministrator(file.getAbsolutePath(), " /S /D=" + workingDir.getAbsolutePath()); } + } @Override diff --git a/java/net/i2p/router/WindowsUpdatePostProcessor.java b/java/net/i2p/router/WindowsUpdatePostProcessor.java index 45dc450..badad32 100644 --- a/java/net/i2p/router/WindowsUpdatePostProcessor.java +++ b/java/net/i2p/router/WindowsUpdatePostProcessor.java @@ -43,28 +43,30 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor { public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException { _log.info("Got an update to post-process"); + if (SystemVersion.isWindows()) { - if (type != UpdateType.ROUTER_SIGNED_SU3 && type != UpdateType.ROUTER_DEV_SU3) { - _log.warn("Unsupported update type " + type); - return; + if (type != UpdateType.ROUTER_SIGNED_SU3 && type != UpdateType.ROUTER_DEV_SU3) { + _log.warn("Unsupported update type " + type); + return; + } + + if (fileType != SU3File.TYPE_EXE) { + _log.warn("Unsupported file type " + fileType); + return; + } + + this.positionedFile = moveUpdateInstaller(file); + this.version = version; + + if (!hook.compareAndSet(false, true)) { + _log.info("shutdown hook was already set"); + return; + } + + _log.info("adding shutdown hook"); + + ctx.addFinalShutdownTask(new WinUpdateProcess(ctx, this::getVersion, this::getFile)); } - - if (fileType != SU3File.TYPE_EXE) { - _log.warn("Unsupported file type " + fileType); - return; - } - - this.positionedFile = moveUpdateInstaller(file); - this.version = version; - - if (!hook.compareAndSet(false, true)) { - _log.info("shutdown hook was already set"); - return; - } - - _log.info("adding shutdown hook"); - ctx.addFinalShutdownTask(new WinUpdateProcess(ctx, this::getVersion, this::getFile)); - } private File moveUpdateInstaller(File file) throws IOException {