use a supplier to get the file path when starting the WinUpdateProcess, fix logging issues

This commit is contained in:
idk
2021-08-18 13:28:08 -04:00
parent e7ea163a64
commit 1b35c8dc8b
2 changed files with 23 additions and 25 deletions

View File

@@ -8,15 +8,14 @@ import java.io.*;
import net.i2p.util.Log; import net.i2p.util.Log;
class WinUpdateProcess implements Runnable { class WinUpdateProcess implements Runnable {
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(WinUpdateProcess.class);
private final RouterContext ctx; private final RouterContext ctx;
private final Supplier<String> versionSupplier; private final Supplier<String> versionSupplier;
private final File file; private final Supplier<File> fileSupplier;
WinUpdateProcess(RouterContext ctx, Supplier<String> versionSupplier, File file) { WinUpdateProcess(RouterContext ctx, Supplier<String> versionSupplier, Supplier<File> fileSupplier) {
this.ctx = ctx; this.ctx = ctx;
this.versionSupplier = versionSupplier; this.versionSupplier = versionSupplier;
this.file = file; this.fileSupplier = fileSupplier;
} }
private File workDir() throws IOException{ private File workDir() throws IOException{
@@ -34,8 +33,11 @@ class WinUpdateProcess implements Runnable {
return null; return null;
} }
private void runUpdateInstaller(File file) throws IOException { private void runUpdateInstaller() throws IOException {
String version = versionSupplier.get(); String version = versionSupplier.get();
File file = fileSupplier.get();
if (file == null)
return;
var workingDir = workDir(); var workingDir = workDir();
var logFile = new File(workingDir, "log-" + version + ".txt"); var logFile = new File(workingDir, "log-" + version + ".txt");
@@ -55,17 +57,16 @@ class WinUpdateProcess implements Runnable {
redirectOutput(logFile). redirectOutput(logFile).
start(); start();
} catch (IOException ex) { } catch (IOException ex) {
if (_log.shouldWarn()) System.out.println("Unable to run update-program in background. Update will fail.");
_log.warn("Unable to run update-program in background. Update will fail.");
} }
} }
@Override @Override
public void run() { public void run() {
try { try {
runUpdateInstaller(file); runUpdateInstaller();
} catch(IOException ioe) { } catch(IOException ioe) {
_log.error("Error running updater, update may fail.", ioe); System.out.println("Error running updater, update may fail." + ioe);
} }
} }
} }

View File

@@ -25,6 +25,7 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
protected static Router i2pRouter = null; protected static Router i2pRouter = null;
private final AtomicBoolean hook = new AtomicBoolean(); private final AtomicBoolean hook = new AtomicBoolean();
private volatile String version; private volatile String version;
private volatile File positionedFile = null;
WindowsUpdatePostProcessor() { WindowsUpdatePostProcessor() {
this.ctx = null; this.ctx = null;
} }
@@ -36,6 +37,10 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
public String getVersion() { public String getVersion() {
return version; return version;
} }
public File getFile() {
return positionedFile;
}
public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException { public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException {
_log.info("Got an update to post-process"); _log.info("Got an update to post-process");
@@ -49,9 +54,13 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
_log.warn("Unsupported file type " + fileType); _log.warn("Unsupported file type " + fileType);
return; return;
} }
File positionedFile = moveUpdateInstaller(file);
try {
this.positionedFile = moveUpdateInstaller(file);
} catch(IOException ioe) {
_log.error("Error positioning update installer", ioe);
return;
}
this.version = version; this.version = version;
if (!hook.compareAndSet(false,true)) { if (!hook.compareAndSet(false,true)) {
@@ -60,11 +69,11 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
} }
_log.info("adding shutdown hook"); _log.info("adding shutdown hook");
ctx.addFinalShutdownTask(new WinUpdateProcess(ctx, this::getVersion, positionedFile)); ctx.addFinalShutdownTask(new WinUpdateProcess(ctx, this::getVersion, this::getFile));
} }
private File moveUpdateInstaller(File file) throws IOException{ private File moveUpdateInstaller(File file) throws IOException {
RouterContext i2pContext = i2pRouter.getContext(); RouterContext i2pContext = i2pRouter.getContext();
if (i2pContext != null) { if (i2pContext != null) {
File appDir = i2pContext.getConfigDir(); File appDir = i2pContext.getConfigDir();
@@ -91,7 +100,6 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
return null; return null;
} }
protected File selectProgramFile() { protected File selectProgramFile() {
if (SystemVersion.isWindows()) { if (SystemVersion.isWindows()) {
File jrehome = new File(System.getProperty("java.home")); File jrehome = new File(System.getProperty("java.home"));
@@ -106,15 +114,4 @@ public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
} }
} }
protected File selectProgramFileExe() {
File pfpath = selectProgramFile();
if (SystemVersion.isWindows()) {
File app = new File(pfpath, "I2P.exe");
return app.getAbsoluteFile();
} else {
File app = new File(pfpath, "bin/I2P");
return app.getAbsoluteFile();
}
}
} }