if NSIS hasn't put files in place already, get them from the config dir.

This commit is contained in:
idk
2022-09-12 18:53:20 -04:00
parent 0ae1609f0f
commit 5171164167
2 changed files with 228 additions and 176 deletions

View File

@ -5,40 +5,75 @@ import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class CopyConfigDir { public class CopyConfigDir {
static Logger logger = Logger.getLogger("configlog");
static WindowsUpdatePostProcessor wupp = null;
static FileHandler fh;
public CopyConfigDir() {
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(logFile().toString());
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean copyDirectory(String baseDir, String workDir) { public static boolean copyDirectory(String baseDir, String workDir) {
File baseFile = new File(baseDir); File baseFile = new File(baseDir);
File workFile = new File(workDir); File workFile = new File(workDir);
return copyDirectory(baseFile, workFile); return copyDirectory(baseFile, workFile);
} }
public static boolean copyDirectory(File baseDir, File workDir) { public static boolean copyDirectory(File baseDir, File workDir) {
for (File file : baseDir.listFiles()) { for (File file : baseDir.listFiles()) {
System.out.println(file.getAbsolutePath()); String fPath = file.getAbsolutePath().replace(
file.getParentFile().getAbsolutePath(), "");
String newPath = workDir.toString() + fPath;
if (file.isDirectory()) if (file.isDirectory())
copyDirectory(file, new File(workDir, file.toString())); if (copyDirectory(file, new File(newPath)))
return false;
if (file.isFile()) if (file.isFile())
copyFileNeverOverwrite(file, new File(workDir, file.toString())); if (!copyFile(file, new File(newPath), true))
return false;
} }
return true; return true;
} }
public static boolean copyConfigDirectory(File baseDir, File workDir) { public static boolean copyConfigDirectory(File baseDir, File workDir) {
for (File file : baseDir.listFiles()) { for (File file : baseDir.listFiles()) {
System.out.println(file.getAbsolutePath()); // System.out.println(file.getAbsolutePath());
String fPath = file.getAbsolutePath().replace(
file.getParentFile().getAbsolutePath(), "");
String newPath = workDir.toString() + fPath;
if (file.isDirectory()) if (file.isDirectory())
copyDirectory(file, new File(workDir, file.toString())); if (!copyConfigDirectory(file, new File(newPath)))
return false;
if (file.isFile()) if (file.isFile())
copyFileNeverOverwrite(file, new File(workDir, file.toString())); if (!copyFileNeverOverwrite(
file,
new File(newPath))) // new File(workDir, file.toString())))
return false;
} }
return true; return true;
} }
public static boolean copyFileNeverOverwrite(String basePath, String workPath) { public static boolean copyFileNeverOverwrite(String basePath,
String workPath) {
File baseFile = new File(basePath); File baseFile = new File(basePath);
File workFile = new File(workPath); File workFile = new File(workPath);
return copyFileNeverOverwrite(baseFile, workFile); return copyFileNeverOverwrite(baseFile, workFile);
@ -48,12 +83,22 @@ public class CopyConfigDir {
return copyFile(basePath, workPath, false); return copyFile(basePath, workPath, false);
} }
public static boolean copyFile(File basePath, File workPath, boolean overWrite) { public static boolean copyFile(File basePath, File workPath,
boolean overWrite) {
if (!basePath.exists()) { if (!basePath.exists()) {
logger.info(basePath.getAbsolutePath() + " doesn't exist, not copying");
return false; return false;
} }
if (!overWrite && workPath.exists()) { if (!overWrite && workPath.exists()) {
return false; logger.info(workPath.getAbsolutePath() +
" already exists, not overwriting");
return true;
}
File workDir = workPath.getParentFile();
if (!workDir.exists()) {
workDir.mkdirs();
} }
try (InputStream in = try (InputStream in =
new BufferedInputStream(new FileInputStream(basePath)); new BufferedInputStream(new FileInputStream(basePath));
@ -70,7 +115,179 @@ public class CopyConfigDir {
out.close(); out.close();
return true; return true;
} catch (Throwable e) { } catch (Throwable e) {
logger.warning(e.toString());
logger.warning("failed to copy " + basePath.getAbsolutePath() + " to " +
workPath.getAbsolutePath());
return false; return false;
} }
} }
protected static File selectHome() { // throws Exception {
String path_override = System.getenv("I2P_CONFIG");
if (path_override != null) {
File path = new File(path_override);
if (path != null && path.exists()) {
if (path.isDirectory())
return path.getAbsoluteFile();
else
throw new RuntimeException("I2P_CONFIG is not a directory: " + path);
}
}
if (osName() == "windows") {
File i2p = appImageHome();
logger.info("Checking for signs of life in I2P directory: " + i2p);
return i2p;
} else {
File i2p = appImageHome();
File programs = new File(i2p, ".i2p");
logger.info("Linux portable jpackage wrapper starting up, using: " +
programs + " as base config");
return programs.getAbsoluteFile();
}
}
protected static File selectProgramFile() {
String path_override = System.getenv("I2P");
if (path_override != null) {
File path = new File(path_override);
if (path.exists()) {
if (path.isDirectory())
return path.getAbsoluteFile();
else
throw new RuntimeException("I2P is not a directory: " + path);
}
}
if (osName() == "windows") {
File jrehome = new File(System.getProperty("java.home"));
File programs = jrehome.getParentFile();
logger.info("Windows portable jpackage wrapper found, using: " +
programs + " as working config");
return programs.getAbsoluteFile();
} else {
File jrehome = new File(System.getProperty("java.home"));
File programs = new File(jrehome.getParentFile().getParentFile(), "i2p");
logger.info("Linux portable jpackage wrapper found, using: " + programs +
" as working config");
return programs.getAbsoluteFile();
}
}
/**
* get the OS name(windows, mac, linux only)
*
* @return os name in lower-case, "windows" "mac" or "linux"
*/
protected static String osName() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows"))
return "windows";
if (osName.contains("mac"))
return "mac";
return "linux";
}
/**
* get the path to the java home, for jpackage this is related to the
* executable itself, which is handy to know. It's a directory called runtime,
* relative to the root of the app-image on each platform:
*
* Windows - Root of appimage is 1 directory above directory named runtime
* ./runtime
*
* Linux - Root of appimage is 2 directories above directory named runtime
* ./lib/runtime
*
* Mac OSX - Unknown for now
*
* @return
*/
protected static File javaHome() {
File jrehome = new File(System.getProperty("java.home"));
if (jrehome != null) {
if (jrehome.exists()) {
return jrehome;
}
}
return null;
}
/**
* get the path to the root of the app-image root by getting the path to
* java.home and the OS, and traversing up to the app-image root based on that
* information.
*
* @return the app-image root
*/
protected static File appImageHome() {
File jreHome = javaHome();
if (jreHome != null) {
switch (osName()) {
case "windows":
return jreHome.getAbsoluteFile().getParentFile();
case "mac":
case "linux":
return jreHome.getAbsoluteFile().getParentFile().getParentFile();
}
}
return null;
}
/**
* get the path to the default config of the app-image by getting the path to
* java.home and the OS, and traversing up to the app-image root based on that
* information, then appending the config directory to the end onn a
* per-platform basis
*
* @return the app-image root
*/
protected static File appImageConfig() {
File aih = appImageHome();
if (aih == null) {
return null;
}
String osName = osName();
switch (osName) {
case "windows":
File winConfigDir = new File(aih, "config");
if (winConfigDir != null) {
if (winConfigDir.exists()) {
return winConfigDir;
}
}
case "mac":
case "linux":
File linConfigDir = new File(aih, "lib/config");
if (linConfigDir != null) {
if (linConfigDir.exists()) {
return linConfigDir;
}
}
}
return null;
}
protected static boolean copyConfigDir() {
File appImageConfigDir = appImageConfig();
File appImageHomeDir = appImageHome();
return copyConfigDirectory(appImageConfigDir, appImageHomeDir);
}
/**
* set up the path to the log file
*
* @return
*/
protected static File logFile() { return logFile("launcher.log"); }
/**
* set up the path to the log file
*
* @return
*/
protected static File logFile(String p) {
File log = new File(selectProgramFile(), "logs");
if (!log.exists())
log.mkdirs();
return new File(log, p);
}
} }

View File

@ -18,7 +18,6 @@ import net.i2p.router.RouterLaunch;
import net.i2p.update.*; import net.i2p.update.*;
import net.i2p.update.UpdateManager; import net.i2p.update.UpdateManager;
import net.i2p.update.UpdatePostProcessor; import net.i2p.update.UpdatePostProcessor;
import net.i2p.util.SystemVersion;
/** /**
* Launches a router from %PROGRAMFILES%/I2P using configuration data in * Launches a router from %PROGRAMFILES%/I2P using configuration data in
@ -31,9 +30,7 @@ import net.i2p.util.SystemVersion;
* router.pid - the pid of the java process. * router.pid - the pid of the java process.
*/ */
public class WinLauncher extends CopyConfigDir { public class WinLauncher extends CopyConfigDir {
static Logger logger = Logger.getLogger("launcherlog");
static WindowsUpdatePostProcessor wupp = null; static WindowsUpdatePostProcessor wupp = null;
static FileHandler fh;
private static Router i2pRouter; private static Router i2pRouter;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -100,7 +97,7 @@ public class WinLauncher extends CopyConfigDir {
// but probably ceases to be necessary, I can make jpackage generate // but probably ceases to be necessary, I can make jpackage generate
// the installer, **and** I get to build every other kind of jpackage // the installer, **and** I get to build every other kind of jpackage
// powered package. // powered package.
if (!copyConfigDir()){ if (!copyConfigDir()) {
logger.severe("Cannot copy the configuration directory"); logger.severe("Cannot copy the configuration directory");
System.exit(1); System.exit(1);
} }
@ -324,168 +321,6 @@ public class WinLauncher extends CopyConfigDir {
um.register(wupp, UpdateType.ROUTER_DEV_SU3, SU3File.TYPE_EXE); um.register(wupp, UpdateType.ROUTER_DEV_SU3, SU3File.TYPE_EXE);
}; };
private static File selectHome() { // throws Exception {
String path_override = System.getenv("I2P_CONFIG");
if (path_override != null) {
File path = new File(path_override);
if (path != null && path.exists()) {
if (path.isDirectory())
return path.getAbsoluteFile();
else
throw new RuntimeException("I2P_CONFIG is not a directory: " + path);
}
}
if (SystemVersion.isWindows()) {
File i2p = appImageHome();
logger.info("Checking for signs of life in I2P directory: " + i2p);
return i2p;
} else {
File i2p = appImageHome();
File programs = new File(i2p, ".i2p");
logger.info("Linux portable jpackage wrapper starting up, using: " +
programs + " as base config");
return programs.getAbsoluteFile();
}
}
private static File selectProgramFile() {
String path_override = System.getenv("I2P");
if (path_override != null) {
File path = new File(path_override);
if (path.exists()) {
if (path.isDirectory())
return path.getAbsoluteFile();
else
throw new RuntimeException("I2P is not a directory: " + path);
}
}
if (SystemVersion.isWindows()) {
File jrehome = new File(System.getProperty("java.home"));
File programs = jrehome.getParentFile();
logger.info("Windows portable jpackage wrapper found, using: " +
programs + " as working config");
return programs.getAbsoluteFile();
} else {
File jrehome = new File(System.getProperty("java.home"));
File programs = new File(jrehome.getParentFile().getParentFile(), "i2p");
logger.info("Linux portable jpackage wrapper found, using: " + programs +
" as working config");
return programs.getAbsoluteFile();
}
}
/**
* get the OS name(windows, mac, linux only)
*
* @return os name in lower-case, "windows" "mac" or "linux"
*/
private static String osName() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows"))
return "windows";
if (osName.contains("mac"))
return "mac";
return "linux";
}
/**
* get the path to the java home, for jpackage this is related to the
* executable itself, which is handy to know. It's a directory called runtime,
* relative to the root of the app-image on each platform:
*
* Windows - Root of appimage is 1 directory above directory named runtime
* ./runtime
*
* Linux - Root of appimage is 2 directories above directory named runtime
* ./lib/runtime
*
* Mac OSX - Unknown for now
*
* @return
*/
private static File javaHome() {
File jrehome = new File(System.getProperty("java.home"));
if (jrehome != null) {
if (jrehome.exists()) {
return jrehome;
}
}
return null;
}
/**
* get the path to the root of the app-image root by getting the path to
* java.home and the OS, and traversing up to the app-image root based on that
* information.
*
* @return the app-image root
*/
private static File appImageHome() {
File jreHome = javaHome();
if (jreHome != null) {
switch (osName()) {
case "windows":
return jreHome.getAbsoluteFile().getParentFile();
case "mac":
case "linux":
return jreHome.getAbsoluteFile().getParentFile().getParentFile();
}
}
return null;
}
/**
* get the path to the default config of the app-image by getting the path to
* java.home and the OS, and traversing up to the app-image root based on that
* information, then appending the config directory to the end onn a
* per-platform basis
*
* @return the app-image root
*/
private static File appImageConfig() {
File aih = appImageHome();
if (aih == null) {
return null;
}
String osName = osName();
switch (osName) {
case "windows":
File winConfigDir = new File(aih, "config");
if (winConfigDir != null) {
if (winConfigDir.exists()) {
return winConfigDir;
}
}
case "mac":
case "linux":
File linConfigDir = new File(aih, "lib/config");
if (linConfigDir != null) {
if (linConfigDir.exists()) {
return linConfigDir;
}
}
}
return null;
}
private static boolean copyConfigDir() {
File appImageConfigDir = appImageConfig();
File appImageHomeDir = appImageHome();
return copyDirectory(appImageConfigDir, appImageHomeDir);
}
/**
* set up the path to the log file
*
* @return
*/
private static File logFile() {
File log = new File(selectProgramFile(), "logs");
if (!log.exists())
log.mkdirs();
return new File(log, "launcher.log");
}
/** /**
* sleep for 1 second * sleep for 1 second
* *