From a230a49af713a34997680602130f13b4cd5b0645 Mon Sep 17 00:00:00 2001 From: idk Date: Sat, 3 Sep 2022 21:17:44 -0400 Subject: [PATCH] format everything for readability and so the brackets all line up again. All whitespace changes. --- java/net/i2p/router/Elevator.java | 48 ++--- java/net/i2p/router/Shell32X.java | 174 ++++++++++-------- java/net/i2p/router/WinUpdateProcess.java | 142 +++++++------- .../router/WindowsUpdatePostProcessor.java | 155 ++++++++-------- 4 files changed, 270 insertions(+), 249 deletions(-) diff --git a/java/net/i2p/router/Elevator.java b/java/net/i2p/router/Elevator.java index 2715a15..42d6536 100644 --- a/java/net/i2p/router/Elevator.java +++ b/java/net/i2p/router/Elevator.java @@ -5,29 +5,31 @@ import com.sun.jna.platform.win32.Kernel32; import com.sun.jna.platform.win32.Kernel32Util; public class Elevator { - public static void main(String... args) { - executeAsAdministrator("c:\\windows\\system32\\notepad.exe", ""); - } + public static void main(String... args) { + executeAsAdministrator("c:\\windows\\system32\\notepad.exe", ""); + } - 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) - execInfo.lpParameters = new WString(args); - execInfo.nShow = Shell32X.SW_SHOWDEFAULT; - execInfo.fMask = Shell32X.SEE_MASK_NOCLOSEPROCESS; - execInfo.lpVerb = new WString("runas"); - boolean result = Shell32X.INSTANCE.ShellExecuteEx(execInfo); - - if (!result) { - int lastError = Kernel32.INSTANCE.GetLastError(); - String errorMessage = Kernel32Util.formatMessageFromLastErrorCode(lastError); - throw new RuntimeException("Error performing elevation: " + lastError + ": " + errorMessage + " (apperror=" - + execInfo.hInstApp + ")"); - } + 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) + execInfo.lpParameters = new WString(args); + execInfo.nShow = Shell32X.SW_SHOWDEFAULT; + execInfo.fMask = Shell32X.SEE_MASK_NOCLOSEPROCESS; + execInfo.lpVerb = new WString("runas"); + boolean result = Shell32X.INSTANCE.ShellExecuteEx(execInfo); + + if (!result) { + int lastError = Kernel32.INSTANCE.GetLastError(); + String errorMessage = + Kernel32Util.formatMessageFromLastErrorCode(lastError); + throw new RuntimeException("Error performing elevation: " + lastError + + ": " + errorMessage + + " (apperror=" + execInfo.hInstApp + ")"); + } + } } diff --git a/java/net/i2p/router/Shell32X.java b/java/net/i2p/router/Shell32X.java index c7906eb..affe0be 100644 --- a/java/net/i2p/router/Shell32X.java +++ b/java/net/i2p/router/Shell32X.java @@ -1,7 +1,5 @@ package net.i2p.router; -import java.util.*; - import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.Structure; @@ -12,100 +10,114 @@ import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.platform.win32.WinNT.HANDLE; import com.sun.jna.platform.win32.WinReg.HKEY; import com.sun.jna.win32.W32APIOptions; +import java.util.*; public interface Shell32X extends Shell32 { - Shell32X INSTANCE = (Shell32X) Native.loadLibrary("shell32", Shell32X.class, W32APIOptions.UNICODE_OPTIONS); + Shell32X INSTANCE = (Shell32X)Native.loadLibrary( + "shell32", Shell32X.class, W32APIOptions.UNICODE_OPTIONS); - int SW_HIDE = 0; - int SW_MAXIMIZE = 3; - int SW_MINIMIZE = 6; - int SW_RESTORE = 9; - int SW_SHOW = 5; - int SW_SHOWDEFAULT = 10; - int SW_SHOWMAXIMIZED = 3; - int SW_SHOWMINIMIZED = 2; - int SW_SHOWMINNOACTIVE = 7; - int SW_SHOWNA = 8; - int SW_SHOWNOACTIVATE = 4; - int SW_SHOWNORMAL = 1; + int SW_HIDE = 0; + int SW_MAXIMIZE = 3; + int SW_MINIMIZE = 6; + int SW_RESTORE = 9; + int SW_SHOW = 5; + int SW_SHOWDEFAULT = 10; + int SW_SHOWMAXIMIZED = 3; + int SW_SHOWMINIMIZED = 2; + int SW_SHOWMINNOACTIVE = 7; + int SW_SHOWNA = 8; + int SW_SHOWNOACTIVATE = 4; + int SW_SHOWNORMAL = 1; - /** File not found. */ - int SE_ERR_FNF = 2; + /** File not found. */ + int SE_ERR_FNF = 2; - /** Path not found. */ - int SE_ERR_PNF = 3; + /** Path not found. */ + int SE_ERR_PNF = 3; - /** Access denied. */ - int SE_ERR_ACCESSDENIED = 5; + /** Access denied. */ + int SE_ERR_ACCESSDENIED = 5; - /** Out of memory. */ - int SE_ERR_OOM = 8; + /** Out of memory. */ + int SE_ERR_OOM = 8; - /** DLL not found. */ - int SE_ERR_DLLNOTFOUND = 32; + /** DLL not found. */ + int SE_ERR_DLLNOTFOUND = 32; - /** Cannot share an open file. */ - int SE_ERR_SHARE = 26; + /** Cannot share an open file. */ + int SE_ERR_SHARE = 26; - int SEE_MASK_NOCLOSEPROCESS = 0x00000040; + int SEE_MASK_NOCLOSEPROCESS = 0x00000040; - int ShellExecute(int i, String lpVerb, String lpFile, String lpParameters, String lpDirectory, int nShow); + int ShellExecute(int i, String lpVerb, String lpFile, String lpParameters, + String lpDirectory, int nShow); - boolean ShellExecuteEx(SHELLEXECUTEINFO lpExecInfo); + boolean ShellExecuteEx(SHELLEXECUTEINFO lpExecInfo); - public static class SHELLEXECUTEINFO extends Structure { - /* - * DWORD cbSize; - * ULONG fMask; - * HWND hwnd; - * LPCTSTR lpVerb; - * LPCTSTR lpFile; - * LPCTSTR lpParameters; - * LPCTSTR lpDirectory; - * int nShow; - * HINSTANCE hInstApp; - * LPVOID lpIDList; - * LPCTSTR lpClass; - * HKEY hkeyClass; - * DWORD dwHotKey; - * union { - * HANDLE hIcon; - * HANDLE hMonitor; - * } DUMMYUNIONNAME; - * HANDLE hProcess; - */ + public static class SHELLEXECUTEINFO extends Structure { + /* + * DWORD cbSize; + * ULONG fMask; + * HWND hwnd; + * LPCTSTR lpVerb; + * LPCTSTR lpFile; + * LPCTSTR lpParameters; + * LPCTSTR lpDirectory; + * int nShow; + * HINSTANCE hInstApp; + * LPVOID lpIDList; + * LPCTSTR lpClass; + * HKEY hkeyClass; + * DWORD dwHotKey; + * union { + * HANDLE hIcon; + * HANDLE hMonitor; + * } DUMMYUNIONNAME; + * HANDLE hProcess; + */ - public int cbSize = size(); - public int fMask; - public HWND hwnd; - public WString lpVerb; - public WString lpFile; - public WString lpParameters; - public WString lpDirectory; - public int nShow; - public HINSTANCE hInstApp; - public Pointer lpIDList; - public WString lpClass; - public HKEY hKeyClass; - public int dwHotKey; + public int cbSize = size(); + public int fMask; + public HWND hwnd; + public WString lpVerb; + public WString lpFile; + public WString lpParameters; + public WString lpDirectory; + public int nShow; + public HINSTANCE hInstApp; + public Pointer lpIDList; + public WString lpClass; + public HKEY hKeyClass; + public int dwHotKey; - /* - * Actually: - * union { - * HANDLE hIcon; - * HANDLE hMonitor; - * } DUMMYUNIONNAME; - */ - public HANDLE hMonitor; - public HANDLE hProcess; + /* + * Actually: + * union { + * HANDLE hIcon; + * HANDLE hMonitor; + * } DUMMYUNIONNAME; + */ + public HANDLE hMonitor; + public HANDLE hProcess; - protected List getFieldOrder() { - return Arrays.asList(new String[] { - "cbSize", "fMask", "hwnd", "lpVerb", "lpFile", "lpParameters", - "lpDirectory", "nShow", "hInstApp", "lpIDList", "lpClass", - "hKeyClass", "dwHotKey", "hMonitor", "hProcess", - }); - } + protected List getFieldOrder() { + return Arrays.asList(new String[] { + "cbSize", + "fMask", + "hwnd", + "lpVerb", + "lpFile", + "lpParameters", + "lpDirectory", + "nShow", + "hInstApp", + "lpIDList", + "lpClass", + "hKeyClass", + "dwHotKey", + "hMonitor", + "hProcess", + }); } - + } } diff --git a/java/net/i2p/router/WinUpdateProcess.java b/java/net/i2p/router/WinUpdateProcess.java index d92630c..3456609 100644 --- a/java/net/i2p/router/WinUpdateProcess.java +++ b/java/net/i2p/router/WinUpdateProcess.java @@ -1,80 +1,88 @@ package net.i2p.router; -import net.i2p.router.*; -import net.i2p.I2PAppContext; - -import java.util.function.*; import java.io.*; +import java.util.function.*; +import net.i2p.I2PAppContext; +import net.i2p.router.*; import net.i2p.util.Log; class WinUpdateProcess implements Runnable { - private final RouterContext ctx; - private final Supplier versionSupplier; - private final Supplier fileSupplier; - private final Log _log; + private final RouterContext ctx; + private final Supplier versionSupplier; + private final Supplier fileSupplier; + private final Log _log; - WinUpdateProcess(RouterContext ctx, Supplier versionSupplier, Supplier fileSupplier) { - this.ctx = ctx; - this.versionSupplier = versionSupplier; - this.fileSupplier = fileSupplier; - this._log = ctx.logManager().getLog(WinUpdateProcess.class); + WinUpdateProcess(RouterContext ctx, Supplier versionSupplier, + Supplier fileSupplier) { + this.ctx = ctx; + this.versionSupplier = versionSupplier; + this.fileSupplier = fileSupplier; + this._log = ctx.logManager().getLog(WinUpdateProcess.class); + } + + private File workDir() throws IOException { + if (ctx != null) { + File workDir = + new File(ctx.getConfigDir().getAbsolutePath(), "i2p_update_win"); + if (workDir.exists()) { + if (workDir.isFile()) + throw new IOException(workDir + + " exists but is a file, get it out of the way"); + return workDir; + } else { + workDir.mkdirs(); + } + return workDir; } + return null; + } - private File workDir() throws IOException { - if (ctx != null) { - File workDir = new File(ctx.getConfigDir().getAbsolutePath(), "i2p_update_win"); - if (workDir.exists()) { - if (workDir.isFile()) - throw new IOException(workDir + " exists but is a file, get it out of the way"); - return workDir; - } else { - workDir.mkdirs(); - } - return workDir; - } - return null; + private void runUpdateInstaller() throws IOException { + String version = versionSupplier.get(); + File file = fileSupplier.get(); + if (file == null) + return; + + var workingDir = workDir(); + var logFile = new File(workingDir, "log-" + version + ".txt"); + + 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"); + + try { + pb.directory(workingDir) + .redirectErrorStream(true) + .redirectOutput(logFile) + .start(); + } catch (IOException ex) { + _log.error( + "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()); } + } - private void runUpdateInstaller() throws IOException { - String version = versionSupplier.get(); - File file = fileSupplier.get(); - if (file == null) - return; - - var workingDir = workDir(); - var logFile = new File(workingDir, "log-" + version + ".txt"); - - 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"); - - try { - pb.directory(workingDir).redirectErrorStream(true).redirectOutput(logFile).start(); - } catch (IOException ex) { - _log.error("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 - public void run() { - try { - runUpdateInstaller(); - } catch (IOException ioe) { - _log.error("Error running updater, update may fail." + ioe); - } + @Override + public void run() { + try { + runUpdateInstaller(); + } catch (IOException ioe) { + _log.error("Error running updater, update may fail." + ioe); } + } } diff --git a/java/net/i2p/router/WindowsUpdatePostProcessor.java b/java/net/i2p/router/WindowsUpdatePostProcessor.java index badad32..41fa99a 100644 --- a/java/net/i2p/router/WindowsUpdatePostProcessor.java +++ b/java/net/i2p/router/WindowsUpdatePostProcessor.java @@ -1,101 +1,100 @@ package net.i2p.router; +import static net.i2p.update.UpdateType.*; + import java.io.*; +import java.lang.InterruptedException; +import java.lang.Process; +import java.lang.ProcessBuilder; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.*; - -import net.i2p.crypto.*; -import static net.i2p.update.UpdateType.*; import net.i2p.I2PAppContext; -import net.i2p.update.UpdateType; +import net.i2p.crypto.*; import net.i2p.update.UpdatePostProcessor; +import net.i2p.update.UpdateType; import net.i2p.util.Log; import net.i2p.util.SystemVersion; -import java.lang.ProcessBuilder; -import java.lang.Process; -import java.lang.InterruptedException; - public class WindowsUpdatePostProcessor implements UpdatePostProcessor { - private final Log _log; - private final RouterContext ctx; - private final AtomicBoolean hook = new AtomicBoolean(); - private final String fileName = "i2p-jpackage-update.exe"; + private final Log _log; + private final RouterContext ctx; + private final AtomicBoolean hook = new AtomicBoolean(); + private final String fileName = "i2p-jpackage-update.exe"; - private volatile String version; + private volatile String version; - private volatile File positionedFile = null; + private volatile File positionedFile = null; - WindowsUpdatePostProcessor(RouterContext ctx) { - this.ctx = ctx; - this._log = ctx.logManager().getLog(WindowsUpdatePostProcessor.class); + WindowsUpdatePostProcessor(RouterContext ctx) { + this.ctx = ctx; + this._log = ctx.logManager().getLog(WindowsUpdatePostProcessor.class); + } + + public String getVersion() { return version; } + + public File getFile() { return positionedFile; } + + 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 (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)); } + } - public String getVersion() { - return version; - } - - public File getFile() { - return positionedFile; - } - - 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 (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 { - if (this.ctx != null) { - File newFile = new File(workDir(), fileName); - boolean renamedStatus = file.renameTo(newFile); - if (renamedStatus) - return newFile; - else - throw new IOException( - "WindowsUpdatePostProcesssor unable to move file to working directory, update will fail"); - } + private File moveUpdateInstaller(File file) throws IOException { + if (this.ctx != null) { + File newFile = new File(workDir(), fileName); + boolean renamedStatus = file.renameTo(newFile); + if (renamedStatus) + return newFile; + else throw new IOException( - "Router context not available to WindowsUpdatePostProcesssor, unable to find working directory, update will fail"); + "WindowsUpdatePostProcesssor unable to move file to working directory, update will fail"); } + throw new IOException( + "Router context not available to WindowsUpdatePostProcesssor, unable to find working directory, update will fail"); + } - private File workDir() throws IOException { - if (this.ctx != null) { - File workDir = new File(this.ctx.getConfigDir().getAbsolutePath(), "i2p_update_win"); - if (workDir.exists()) { - if (workDir.isFile()) - throw new IOException(workDir + " exists but is a file, get it out of the way"); - return null; - } else { - workDir.mkdirs(); - } - return workDir; - } + private File workDir() throws IOException { + if (this.ctx != null) { + File workDir = + new File(this.ctx.getConfigDir().getAbsolutePath(), "i2p_update_win"); + if (workDir.exists()) { + if (workDir.isFile()) + throw new IOException(workDir + + " exists but is a file, get it out of the way"); return null; + } else { + workDir.mkdirs(); + } + return workDir; } - + return null; + } } \ No newline at end of file