2022-09-09 00:52:54 -04:00
|
|
|
package net.i2p.router;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2022-09-12 18:53:20 -04:00
|
|
|
import java.io.IOException;
|
2022-09-09 00:52:54 -04:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2022-09-12 18:53:20 -04:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.SimpleFormatter;
|
2024-03-08 18:44:24 -05:00
|
|
|
import net.i2p.util.Log;
|
2022-09-09 00:52:54 -04:00
|
|
|
|
2024-03-08 19:04:56 -05:00
|
|
|
public class CopyConfigDir extends WindowsAppUtil {
|
2024-03-08 18:44:24 -05:00
|
|
|
final Log logger;
|
2022-09-12 18:53:20 -04:00
|
|
|
|
2024-03-08 18:44:24 -05:00
|
|
|
public CopyConfigDir(RouterContext ctx) {
|
|
|
|
logger = ctx.logManager().getLog(CopyConfigDir.class);
|
2022-09-12 18:53:20 -04:00
|
|
|
}
|
2022-09-09 00:52:54 -04:00
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public boolean copyDirectory(String baseDir, String workDir) {
|
2022-09-09 00:52:54 -04:00
|
|
|
File baseFile = new File(baseDir);
|
|
|
|
File workFile = new File(workDir);
|
|
|
|
return copyDirectory(baseFile, workFile);
|
|
|
|
}
|
2022-09-12 18:53:20 -04:00
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public boolean copyDirectory(File baseDir, File workDir) {
|
2022-09-09 00:52:54 -04:00
|
|
|
for (File file : baseDir.listFiles()) {
|
2022-09-12 18:53:20 -04:00
|
|
|
String fPath = file.getAbsolutePath().replace(
|
|
|
|
file.getParentFile().getAbsolutePath(), "");
|
|
|
|
String newPath = workDir.toString() + fPath;
|
2022-09-10 22:14:34 -04:00
|
|
|
if (file.isDirectory())
|
2022-09-12 18:53:20 -04:00
|
|
|
if (copyDirectory(file, new File(newPath)))
|
|
|
|
return false;
|
2022-09-10 22:14:34 -04:00
|
|
|
if (file.isFile())
|
2022-09-20 12:34:52 -04:00
|
|
|
if (0 == copyFile(file, new File(newPath), true))
|
2022-09-12 18:53:20 -04:00
|
|
|
return false;
|
2022-09-10 22:14:34 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public boolean copyConfigDirectory(File baseDir, File workDir) {
|
2022-09-10 22:14:34 -04:00
|
|
|
for (File file : baseDir.listFiles()) {
|
2022-09-12 18:53:20 -04:00
|
|
|
// System.out.println(file.getAbsolutePath());
|
|
|
|
String fPath = file.getAbsolutePath().replace(
|
|
|
|
file.getParentFile().getAbsolutePath(), "");
|
|
|
|
String newPath = workDir.toString() + fPath;
|
2022-09-10 22:14:34 -04:00
|
|
|
if (file.isDirectory())
|
2022-09-12 18:53:20 -04:00
|
|
|
if (!copyConfigDirectory(file, new File(newPath)))
|
|
|
|
return false;
|
2022-09-20 12:34:52 -04:00
|
|
|
if (file.isFile()) {
|
|
|
|
int cnr = copyFileNeverOverwrite(file, new File(newPath));
|
|
|
|
if (0 == cnr)
|
2022-09-12 18:53:20 -04:00
|
|
|
return false;
|
2022-09-20 12:34:52 -04:00
|
|
|
if (1 == cnr) {
|
2022-10-23 15:35:26 -04:00
|
|
|
logger.info(
|
|
|
|
"using jpackaged configs in a jpackaged install, creating jpackaged file");
|
2022-09-20 12:35:59 -04:00
|
|
|
File jpackagedConfigsInUse = new File(appImageHome(), "jpackaged");
|
2022-10-23 15:35:26 -04:00
|
|
|
if (!jpackagedConfigsInUse.exists()) {
|
|
|
|
try {
|
2022-09-20 12:35:59 -04:00
|
|
|
jpackagedConfigsInUse.createNewFile();
|
2022-10-23 15:35:26 -04:00
|
|
|
} catch (IOException e) {
|
2024-03-08 18:44:24 -05:00
|
|
|
logger.warn(
|
2022-10-23 15:35:26 -04:00
|
|
|
"Error creating jpackaged file, delete config files manually when uninstalling");
|
2022-09-20 12:35:59 -04:00
|
|
|
}
|
2022-09-20 12:34:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (-1 == cnr) {
|
2022-10-23 15:35:26 -04:00
|
|
|
logger.info(
|
|
|
|
"not overwriting existing config file, not creating jpackaged file");
|
2022-09-20 12:34:52 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 00:52:54 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public int copyFileNeverOverwrite(String basePath, String workPath) {
|
2022-09-09 00:52:54 -04:00
|
|
|
File baseFile = new File(basePath);
|
|
|
|
File workFile = new File(workPath);
|
|
|
|
return copyFileNeverOverwrite(baseFile, workFile);
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public int copyFileNeverOverwrite(File basePath, File workPath) {
|
2022-09-09 00:52:54 -04:00
|
|
|
return copyFile(basePath, workPath, false);
|
|
|
|
}
|
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
public int copyFile(File basePath, File workPath, boolean overWrite) {
|
2022-09-09 00:52:54 -04:00
|
|
|
if (!basePath.exists()) {
|
2022-09-12 18:53:20 -04:00
|
|
|
logger.info(basePath.getAbsolutePath() + " doesn't exist, not copying");
|
2022-09-20 12:34:52 -04:00
|
|
|
return 0;
|
2022-09-09 00:52:54 -04:00
|
|
|
}
|
2022-09-12 18:53:20 -04:00
|
|
|
|
2022-09-09 00:52:54 -04:00
|
|
|
if (!overWrite && workPath.exists()) {
|
2022-09-12 18:53:20 -04:00
|
|
|
logger.info(workPath.getAbsolutePath() +
|
|
|
|
" already exists, not overwriting");
|
2022-09-20 12:34:52 -04:00
|
|
|
return -1;
|
2022-09-12 18:53:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
File workDir = workPath.getParentFile();
|
|
|
|
if (!workDir.exists()) {
|
|
|
|
workDir.mkdirs();
|
2022-09-09 00:52:54 -04:00
|
|
|
}
|
|
|
|
try (InputStream in =
|
|
|
|
new BufferedInputStream(new FileInputStream(basePath));
|
|
|
|
OutputStream out =
|
|
|
|
new BufferedOutputStream(new FileOutputStream(workPath))) {
|
|
|
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int lengthRead;
|
|
|
|
while ((lengthRead = in.read(buffer)) > 0) {
|
|
|
|
out.write(buffer, 0, lengthRead);
|
|
|
|
out.flush();
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
out.close();
|
2022-09-20 12:34:52 -04:00
|
|
|
return 1;
|
2022-09-09 00:52:54 -04:00
|
|
|
} catch (Throwable e) {
|
2024-03-08 18:44:24 -05:00
|
|
|
logger.warn(e.toString());
|
|
|
|
logger.warn("failed to copy " + basePath.getAbsolutePath() + " to " +
|
|
|
|
workPath.getAbsolutePath());
|
2022-09-20 12:34:52 -04:00
|
|
|
return 0;
|
2022-09-09 00:52:54 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-12 18:53:20 -04:00
|
|
|
|
2023-02-08 21:47:54 +00:00
|
|
|
protected boolean copyConfigDir() {
|
2022-09-12 18:53:20 -04:00
|
|
|
File appImageConfigDir = appImageConfig();
|
2022-09-13 13:49:16 -04:00
|
|
|
File appImageHomeDir = selectHome();
|
2022-09-12 18:53:20 -04:00
|
|
|
return copyConfigDirectory(appImageConfigDir, appImageHomeDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set up the path to the log file
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2023-02-08 21:47:54 +00:00
|
|
|
protected File logFile() { return logFile("launcher.log"); }
|
2022-09-12 18:53:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set up the path to the log file
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2023-02-08 21:47:54 +00:00
|
|
|
protected File logFile(String p) {
|
2022-09-12 18:53:20 -04:00
|
|
|
File log = new File(selectProgramFile(), "logs");
|
|
|
|
if (!log.exists())
|
|
|
|
log.mkdirs();
|
|
|
|
return new File(log, p);
|
|
|
|
}
|
2022-09-09 00:52:54 -04:00
|
|
|
}
|