Files
i2p.firefox/java/net/i2p/router/CopyConfigDir.java

151 lines
4.4 KiB
Java
Raw Normal View History

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;
import java.io.IOException;
2022-09-09 00:52:54 -04:00
import java.io.InputStream;
import java.io.OutputStream;
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
public class CopyConfigDir extends I2PAppUtil {
2024-03-08 18:44:24 -05:00
final Log logger;
2024-03-08 18:44:24 -05:00
public CopyConfigDir(RouterContext ctx) {
logger = ctx.logManager().getLog(CopyConfigDir.class);
}
2022-09-09 00:52:54 -04: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);
}
public boolean copyDirectory(File baseDir, File workDir) {
2022-09-09 00:52:54 -04:00
for (File file : baseDir.listFiles()) {
String fPath = file.getAbsolutePath().replace(
file.getParentFile().getAbsolutePath(), "");
String newPath = workDir.toString() + fPath;
if (file.isDirectory())
if (copyDirectory(file, new File(newPath)))
return false;
if (file.isFile())
if (0 == copyFile(file, new File(newPath), true))
return false;
}
return true;
}
public boolean copyConfigDirectory(File baseDir, File workDir) {
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 (!copyConfigDirectory(file, new File(newPath)))
return false;
if (file.isFile()) {
int cnr = copyFileNeverOverwrite(file, new File(newPath));
if (0 == cnr)
return false;
if (1 == cnr) {
logger.info(
"using jpackaged configs in a jpackaged install, creating jpackaged file");
File jpackagedConfigsInUse = new File(appImageHome(), "jpackaged");
if (!jpackagedConfigsInUse.exists()) {
try {
jpackagedConfigsInUse.createNewFile();
} catch (IOException e) {
2024-03-08 18:44:24 -05:00
logger.warn(
"Error creating jpackaged file, delete config files manually when uninstalling");
}
}
}
if (-1 == cnr) {
logger.info(
"not overwriting existing config file, not creating jpackaged file");
}
}
2022-09-09 00:52:54 -04:00
}
return true;
}
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);
}
public int copyFileNeverOverwrite(File basePath, File workPath) {
2022-09-09 00:52:54 -04:00
return copyFile(basePath, workPath, false);
}
public int copyFile(File basePath, File workPath, boolean overWrite) {
2022-09-09 00:52:54 -04:00
if (!basePath.exists()) {
logger.info(basePath.getAbsolutePath() + " doesn't exist, not copying");
return 0;
2022-09-09 00:52:54 -04:00
}
2022-09-09 00:52:54 -04:00
if (!overWrite && workPath.exists()) {
logger.info(workPath.getAbsolutePath() +
" already exists, not overwriting");
return -1;
}
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();
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());
return 0;
2022-09-09 00:52:54 -04:00
}
}
protected boolean copyConfigDir() {
File appImageConfigDir = appImageConfig();
File appImageHomeDir = selectHome();
return copyConfigDirectory(appImageConfigDir, appImageHomeDir);
}
/**
* set up the path to the log file
*
* @return
*/
protected File logFile() { return logFile("launcher.log"); }
/**
* set up the path to the log file
*
* @return
*/
protected File logFile(String p) {
File log = new File(selectHome(), "logs");
if (!log.exists())
log.mkdirs();
return new File(log, p);
}
2022-09-09 00:52:54 -04:00
}