UPP implementation

This commit is contained in:
Zlatin Balevsky
2021-08-01 19:12:30 +01:00
parent d8bff1c272
commit 3444784468
4 changed files with 121 additions and 2 deletions

View File

@ -42,10 +42,10 @@ I2P_PKG=$HERE/../i2p.i2p/pkg-temp
mkdir build
echo "compiling custom launcher"
echo "compiling custom launcher and update processor"
cp $I2P_JARS/*.jar build
cd java
javac -d ../build -classpath ../build/i2p.jar:../build/router.jar net/i2p/router/MacLauncher.java
javac -d ../build -classpath ../build/i2p.jar:../build/router.jar net/i2p/router/MacLauncher.java net/i2p/update/*.java
cd ..
echo "copying mac-update.sh"

View File

@ -28,6 +28,7 @@ public class MacLauncher {
System.setProperty("i2p.dir.base", resources.getAbsolutePath());
System.setProperty("mac.bundle.location", bundleLocation.getAbsolutePath());
System.setProperty("router.pid", String.valueOf(ProcessHandle.current().pid()));
// TODO: find a clean way to set the update url
try {
System.load(resources.getAbsolutePath() + "/libMacLauncher.jnilib");

View File

@ -0,0 +1,42 @@
package net.i2p.update;
import net.i2p.router.*;
import java.util.function.*;
import java.io.*;
class MacUpdateProcess implements Runnable {
private final RouterContext ctx;
private final Supplier<String> versionSupplier;
MacUpdateProcess(RouterContext ctx, Supplier<String> versionSupplier) {
this.ctx = ctx;
this.versionSupplier = versionSupplier;
}
@Override
public void run() {
String version = versionSupplier.get();
var workingDir = new File(ctx.getConfigDir(), "mac_updates");
var logFile = new File(workingDir, "log-" + version + ".txt");
var pb = new ProcessBuilder("./mac-update.sh");
var env = pb.environment();
env.put("I2P_PID", System.getProperty("router.pid"));
env.put("I2P_VERSION", version);
env.put("BUNDLE_HOME", System.getProperty("mac.bundle.location"));
try {
var process = pb.
directory(workingDir).
redirectErrorStream(true).
redirectOutput(logFile).
start();
System.out.println("Started update process with PID:" + process.toHandle().pid());
} catch (IOException bad) {
bad.printStackTrace();
}
}
}

View File

@ -0,0 +1,76 @@
package net.i2p.update;
import net.i2p.router.*;
import net.i2p.util.*;
import net.i2p.crypto.*;
import java.io.*;
import java.util.concurrent.atomic.*;
import java.nio.file.*;
public class MacUpdateProcessor implements UpdatePostProcessor {
private final RouterContext ctx;
private final Log log;
private final AtomicBoolean hook = new AtomicBoolean();
private volatile String version;
public MacUpdateProcessor(RouterContext ctx) {
this.ctx = ctx;
log = ctx.logManager().getLog(MacUpdateProcessor.class);
}
public String getVersion() {
return version;
}
@Override
public void updateDownloadedandVerified(UpdateType type, int fileType, String version, File file) throws IOException {
log.info("Got an update to post-process");
if (type != UpdateType.ROUTER_SIGNED_SU3) {
log.warn("Unsupported update type " + type);
return;
}
if (fileType != SU3File.TYPE_DMG) {
log.warn("Unsupported file type " + fileType);
return;
}
// good to go
// first check if working directory is fine
var workDir = new File(ctx.getConfigDir(), "mac_updates");
if (workDir.exists()) {
if (workDir.isFile())
throw new IOException(workDir + " exists but is a file, get it out of the way");
} else
workDir.mkdirs();
var dmg = new File(workDir, "I2P-" + version + ".dmg");
if (!FileUtil.copy(file,dmg,true,false))
throw new IOException("Couldn't copy extracted update");
this.version = version;
if (!hook.compareAndSet(false,true)) {
log.info("shutdown hook was already set");
return;
}
try(InputStream scriptStream = MacUpdateProcessor.class.getClassLoader().getResourceAsStream("mac-update.sh")) {
var scriptFile = new File(workDir,"mac-update.sh");
Files.copy(scriptStream, scriptFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (!scriptFile.setExecutable(true))
throw new IOException("couldn't mark script file executable");
}
log.info("adding shutdown hook");
ctx.addFinalShutdownTask(new MacUpdateProcess(ctx, this::getVersion));
}
}