2021-07-01 16:55:29 -04:00
|
|
|
package net.i2p.router;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
2021-07-01 19:52:21 -04:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2021-07-01 16:55:29 -04:00
|
|
|
|
|
|
|
import static net.i2p.update.UpdateType.*;
|
|
|
|
import net.i2p.update.UpdateType;
|
2021-07-01 19:52:21 -04:00
|
|
|
import net.i2p.update.UpdatePostProcessor;
|
2021-07-01 16:55:29 -04:00
|
|
|
import net.i2p.util.SystemVersion;
|
|
|
|
|
|
|
|
import java.lang.ProcessBuilder;
|
|
|
|
import java.lang.Process;
|
|
|
|
import java.lang.InterruptedException;
|
|
|
|
|
2021-07-01 19:52:21 -04:00
|
|
|
public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
|
|
|
|
protected Router i2pRouter = null;
|
2021-07-01 16:55:29 -04:00
|
|
|
public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException {
|
|
|
|
if (fileType == 6) {
|
|
|
|
if (runUpdate(file)) {
|
2021-07-01 19:52:21 -04:00
|
|
|
try {
|
2021-07-01 20:22:37 -04:00
|
|
|
if (!shutdownGracefullyAndRerun()) {
|
|
|
|
i2pRouter.cancelGracefulShutdown();
|
2021-07-01 19:52:21 -04:00
|
|
|
}
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
i2pRouter.cancelGracefulShutdown();
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-01 18:20:16 -04:00
|
|
|
|
2021-07-01 16:55:29 -04:00
|
|
|
private boolean runUpdate(File file){
|
2021-07-01 18:20:16 -04:00
|
|
|
Process updateProcess = null;
|
2021-07-01 20:18:39 -04:00
|
|
|
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", file.getAbsolutePath(), "/S");
|
2021-07-01 18:20:16 -04:00
|
|
|
try {
|
|
|
|
updateProcess = pb.start();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
// At this point a failure is harmless, but it's also not at all important to
|
|
|
|
// restart the router. Return false.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
updateProcess.waitFor();
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
// if the NSIS installer process got interrupted here, it's possible that the
|
|
|
|
// install was left in a broken state. I think we should direct the uses to
|
|
|
|
// re-run the installer if this happens. TODO: java dialog boxes. That should be
|
|
|
|
// easy.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
2021-07-01 18:20:16 -04:00
|
|
|
|
2021-07-01 19:52:21 -04:00
|
|
|
private boolean shutdownGracefullyAndRerun() throws InterruptedException {
|
2021-07-01 16:55:29 -04:00
|
|
|
i2pRouter.shutdownGracefully();
|
2021-07-01 20:32:09 -04:00
|
|
|
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", selectProgramFileExe().getAbsolutePath());
|
2021-07-01 19:52:21 -04:00
|
|
|
while (i2pRouter.gracefulShutdownInProgress()) {
|
|
|
|
TimeUnit.MILLISECONDS.sleep(125);
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
2021-07-01 18:20:16 -04:00
|
|
|
if (i2pRouter.isFinalShutdownInProgress()) {
|
2021-07-01 16:55:29 -04:00
|
|
|
try {
|
|
|
|
Process restartProcess = pb.start();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
//
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:52:21 -04:00
|
|
|
protected File selectProgramFile() {
|
2021-07-01 16:55:29 -04:00
|
|
|
if (SystemVersion.isWindows()) {
|
|
|
|
File jrehome = new File(System.getProperty("java.home"));
|
|
|
|
File programs = jrehome.getParentFile();
|
|
|
|
return programs.getAbsoluteFile();
|
|
|
|
} else {
|
|
|
|
File jrehome = new File(System.getProperty("java.home"));
|
|
|
|
File programs = new File(jrehome.getParentFile().getParentFile(), "i2p");
|
|
|
|
return programs.getAbsoluteFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:52:21 -04:00
|
|
|
protected File selectProgramFileExe() {
|
2021-07-03 22:39:54 -04:00
|
|
|
File pfpath = selectProgramFile();
|
2021-07-01 18:20:16 -04:00
|
|
|
if (SystemVersion.isWindows()) {
|
|
|
|
File app = new File(pfpath, "I2P.exe");
|
|
|
|
return app.getAbsoluteFile();
|
|
|
|
} else {
|
|
|
|
File app = new File(pfpath, "bin/I2P");
|
|
|
|
return app.getAbsoluteFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|