Add param for num of max backup files and increase it to 20 for wallet and keys.

This commit is contained in:
Manfred Karrer 2016-08-07 13:03:41 +02:00
parent 4cefc45231
commit ccb6ce88c8
4 changed files with 12 additions and 10 deletions

View File

@ -88,7 +88,7 @@ public class KeyStorage {
}
public KeyPair loadKeyPair(KeyEntry keyEntry) {
FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key");
FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key", 20);
// long now = System.currentTimeMillis();
try {
KeyFactory keyFactory = KeyFactory.getInstance(keyEntry.getAlgorithm(), "BC");

View File

@ -14,10 +14,12 @@ import java.util.List;
public class FileUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
/** Number of copies to keep in backup directory. */
private static final int KEPT_BACKUPS = 10;
public static void rollingBackup(File dir, String fileName) {
rollingBackup(dir, fileName, 10);
}
public static void rollingBackup(File dir, String fileName, int numMaxBackupFiles) {
if (dir.exists()) {
File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
if (!backupDir.exists())
@ -39,7 +41,7 @@ public class FileUtil {
try {
Files.copy(origFile, backupFile);
pruneBackup(backupFileDir);
pruneBackup(backupFileDir, numMaxBackupFiles);
} catch (IOException e) {
log.error("Backup key failed: " + e.getMessage());
e.printStackTrace();
@ -48,22 +50,22 @@ public class FileUtil {
}
}
private static void pruneBackup(File backupDir) {
private static void pruneBackup(File backupDir, int numMaxBackupFiles) {
if (backupDir.isDirectory()) {
File[] files = backupDir.listFiles();
if (files != null) {
List<File> filesList = Arrays.asList(files);
if (filesList.size() > KEPT_BACKUPS) {
if (filesList.size() > numMaxBackupFiles) {
filesList.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
File file = filesList.get(0);
if (file.isFile()) {
if (!file.delete())
log.error("Failed to delete file: " + file);
else
pruneBackup(backupDir);
pruneBackup(backupDir, numMaxBackupFiles);
} else {
pruneBackup(new File(Paths.get(backupDir.getAbsolutePath(), file.getName()).toString()));
pruneBackup(new File(Paths.get(backupDir.getAbsolutePath(), file.getName()).toString()), numMaxBackupFiles);
}
}
}

View File

@ -410,7 +410,7 @@ public class WalletService {
}
public void backupWallet() {
FileUtil.rollingBackup(walletDir, "Bitsquare.wallet");
FileUtil.rollingBackup(walletDir, "Bitsquare.wallet", 20);
}
public void clearBackup() {

View File

@ -174,7 +174,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
String myAddress,
String banList) {
if (!useLocalhost)
FileUtil.rollingBackup(new File(Paths.get(torDir.getAbsolutePath(), "hiddenservice").toString()), "private_key");
FileUtil.rollingBackup(new File(Paths.get(torDir.getAbsolutePath(), "hiddenservice").toString()), "private_key", 20);
if (banList != null && !banList.isEmpty())
BanList.setList(Arrays.asList(banList.replace(" ", "").split(",")).stream().map(NodeAddress::new).collect(Collectors.toList()));