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-08-14 02:08:45 -04:00
|
|
|
import java.util.concurrent.atomic.*;
|
2021-07-01 16:55:29 -04:00
|
|
|
|
2021-08-14 02:08:45 -04:00
|
|
|
|
|
|
|
import net.i2p.crypto.*;
|
2021-07-01 16:55:29 -04:00
|
|
|
import static net.i2p.update.UpdateType.*;
|
2021-07-15 15:48:34 -04:00
|
|
|
import net.i2p.I2PAppContext;
|
2021-07-01 16:55:29 -04:00
|
|
|
import net.i2p.update.UpdateType;
|
2021-07-01 19:52:21 -04:00
|
|
|
import net.i2p.update.UpdatePostProcessor;
|
2021-07-15 15:48:34 -04:00
|
|
|
import net.i2p.util.Log;
|
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-15 15:48:34 -04:00
|
|
|
|
2021-07-01 19:52:21 -04:00
|
|
|
public class WindowsUpdatePostProcessor implements UpdatePostProcessor {
|
2021-07-13 21:49:02 -04:00
|
|
|
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(WindowsUpdatePostProcessor.class);
|
2021-08-14 02:08:45 -04:00
|
|
|
private final RouterContext ctx;
|
2021-08-02 14:17:02 -04:00
|
|
|
protected static Router i2pRouter = null;
|
2021-08-14 02:08:45 -04:00
|
|
|
private final AtomicBoolean hook = new AtomicBoolean();
|
|
|
|
private volatile String version;
|
2021-08-18 13:28:08 -04:00
|
|
|
private volatile File positionedFile = null;
|
2021-08-14 02:08:45 -04:00
|
|
|
WindowsUpdatePostProcessor() {
|
|
|
|
this.ctx = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowsUpdatePostProcessor(RouterContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getVersion() {
|
|
|
|
return version;
|
|
|
|
}
|
2021-08-18 13:28:08 -04:00
|
|
|
|
|
|
|
public File getFile() {
|
|
|
|
return positionedFile;
|
|
|
|
}
|
2021-07-13 21:49:02 -04:00
|
|
|
|
2021-07-01 16:55:29 -04:00
|
|
|
public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException {
|
2021-08-14 02:08:45 -04:00
|
|
|
_log.info("Got an update to post-process");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:28:08 -04:00
|
|
|
try {
|
|
|
|
this.positionedFile = moveUpdateInstaller(file);
|
|
|
|
} catch(IOException ioe) {
|
|
|
|
_log.error("Error positioning update installer", ioe);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-14 02:08:45 -04:00
|
|
|
this.version = version;
|
|
|
|
|
|
|
|
if (!hook.compareAndSet(false,true)) {
|
|
|
|
_log.info("shutdown hook was already set");
|
|
|
|
return;
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
2021-07-01 18:20:16 -04:00
|
|
|
|
2021-08-14 02:08:45 -04:00
|
|
|
_log.info("adding shutdown hook");
|
2021-08-18 13:28:08 -04:00
|
|
|
ctx.addFinalShutdownTask(new WinUpdateProcess(ctx, this::getVersion, this::getFile));
|
2021-08-14 02:08:45 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:28:08 -04:00
|
|
|
private File moveUpdateInstaller(File file) throws IOException {
|
2021-07-15 15:48:34 -04:00
|
|
|
RouterContext i2pContext = i2pRouter.getContext();
|
2021-07-13 21:49:02 -04:00
|
|
|
if (i2pContext != null) {
|
2021-08-15 18:41:53 -04:00
|
|
|
File appDir = i2pContext.getConfigDir();
|
2021-08-14 02:08:45 -04:00
|
|
|
File newFile = new File(workDir(), file.getName());
|
2021-07-13 21:49:02 -04:00
|
|
|
file.renameTo(newFile);
|
|
|
|
return newFile;
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
2021-07-13 21:49:02 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-14 02:08:45 -04:00
|
|
|
private File workDir() throws IOException{
|
|
|
|
RouterContext i2pContext = i2pRouter.getContext();
|
|
|
|
if (i2pContext != null) {
|
2021-08-15 18:41:53 -04:00
|
|
|
File workDir = new File(i2pContext.getConfigDir().getAbsolutePath(), "i2p_update_win");
|
2021-08-14 02:08:45 -04:00
|
|
|
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;
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
2021-08-14 02:08:45 -04:00
|
|
|
return null;
|
2021-07-01 16:55:29 -04:00
|
|
|
}
|
|
|
|
|
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();
|
2021-08-15 18:41:53 -04:00
|
|
|
System.out.println("Windows portable jpackage wrapper found, using: " + programs + " as working config");
|
2021-07-01 16:55:29 -04:00
|
|
|
return programs.getAbsoluteFile();
|
|
|
|
} else {
|
|
|
|
File jrehome = new File(System.getProperty("java.home"));
|
|
|
|
File programs = new File(jrehome.getParentFile().getParentFile(), "i2p");
|
2021-08-15 18:41:53 -04:00
|
|
|
System.out.println("Linux portable jpackage wrapper found, using: " + programs + " as working config");
|
2021-07-01 16:55:29 -04:00
|
|
|
return programs.getAbsoluteFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|