2021-08-14 10:17:56 -04:00
|
|
|
package net.i2p.router;
|
|
|
|
|
|
|
|
import net.i2p.router.*;
|
|
|
|
import net.i2p.I2PAppContext;
|
|
|
|
|
|
|
|
import java.util.function.*;
|
|
|
|
import java.io.*;
|
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
|
|
|
class WinUpdateProcess implements Runnable {
|
|
|
|
private final RouterContext ctx;
|
|
|
|
private final Supplier<String> versionSupplier;
|
2021-08-18 13:28:08 -04:00
|
|
|
private final Supplier<File> fileSupplier;
|
2022-02-09 21:43:30 -05:00
|
|
|
|
2021-08-18 13:28:08 -04:00
|
|
|
WinUpdateProcess(RouterContext ctx, Supplier<String> versionSupplier, Supplier<File> fileSupplier) {
|
2021-08-14 10:17:56 -04:00
|
|
|
this.ctx = ctx;
|
|
|
|
this.versionSupplier = versionSupplier;
|
2021-08-18 13:28:08 -04:00
|
|
|
this.fileSupplier = fileSupplier;
|
2021-08-14 10:17:56 -04:00
|
|
|
}
|
2022-02-09 21:43:30 -05:00
|
|
|
|
|
|
|
private File workDir() throws IOException {
|
2021-08-15 18:41:53 -04:00
|
|
|
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");
|
2022-02-09 21:43:30 -05:00
|
|
|
return null;
|
2021-08-15 18:41:53 -04:00
|
|
|
} else {
|
|
|
|
workDir.mkdirs();
|
|
|
|
}
|
|
|
|
return workDir;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-14 10:17:56 -04:00
|
|
|
|
2021-08-18 13:28:08 -04:00
|
|
|
private void runUpdateInstaller() throws IOException {
|
2021-08-15 14:42:02 -04:00
|
|
|
String version = versionSupplier.get();
|
2022-02-09 21:43:30 -05:00
|
|
|
File file = fileSupplier.get();
|
|
|
|
if (file == null)
|
|
|
|
return;
|
2021-08-15 14:42:02 -04:00
|
|
|
|
2021-08-15 18:41:53 -04:00
|
|
|
var workingDir = workDir();
|
2021-08-15 14:42:02 -04:00
|
|
|
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");
|
|
|
|
|
|
|
|
int exitCode = ctx.router().scheduledGracefulExitCode();
|
|
|
|
if (exitCode == Router.EXIT_HARD_RESTART || exitCode == Router.EXIT_GRACEFUL_RESTART)
|
|
|
|
env.put("RESTART_I2P", "true");
|
|
|
|
|
2021-08-14 10:17:56 -04:00
|
|
|
try {
|
2022-02-09 21:43:30 -05:00
|
|
|
pb.directory(workingDir).redirectErrorStream(true).redirectOutput(logFile).start();
|
2021-08-14 10:17:56 -04:00
|
|
|
} catch (IOException ex) {
|
2022-02-09 21:43:30 -05:00
|
|
|
System.out.println("Unable to run update-program in background. Update will fail.");
|
2021-08-14 10:17:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-08-15 18:41:53 -04:00
|
|
|
try {
|
2021-08-18 13:28:08 -04:00
|
|
|
runUpdateInstaller();
|
2022-02-09 21:43:30 -05:00
|
|
|
} catch (IOException ioe) {
|
2021-08-18 13:28:08 -04:00
|
|
|
System.out.println("Error running updater, update may fail." + ioe);
|
2021-08-15 18:41:53 -04:00
|
|
|
}
|
2021-08-14 10:17:56 -04:00
|
|
|
}
|
|
|
|
}
|