diff --git a/common/src/main/java/io/bitsquare/common/crypto/KeyStorage.java b/common/src/main/java/io/bitsquare/common/crypto/KeyStorage.java index ee25b622b2..75304d5ba9 100644 --- a/common/src/main/java/io/bitsquare/common/crypto/KeyStorage.java +++ b/common/src/main/java/io/bitsquare/common/crypto/KeyStorage.java @@ -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"); diff --git a/common/src/main/java/io/bitsquare/storage/FileUtil.java b/common/src/main/java/io/bitsquare/storage/FileUtil.java index 84040b3449..b7cae5b327 100644 --- a/common/src/main/java/io/bitsquare/storage/FileUtil.java +++ b/common/src/main/java/io/bitsquare/storage/FileUtil.java @@ -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 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); } } } diff --git a/core/src/main/java/io/bitsquare/btc/WalletService.java b/core/src/main/java/io/bitsquare/btc/WalletService.java index aa3b0d1c10..b72b9f958a 100644 --- a/core/src/main/java/io/bitsquare/btc/WalletService.java +++ b/core/src/main/java/io/bitsquare/btc/WalletService.java @@ -410,7 +410,7 @@ public class WalletService { } public void backupWallet() { - FileUtil.rollingBackup(walletDir, "Bitsquare.wallet"); + FileUtil.rollingBackup(walletDir, "Bitsquare.wallet", 20); } public void clearBackup() { diff --git a/network/src/main/java/io/bitsquare/p2p/P2PService.java b/network/src/main/java/io/bitsquare/p2p/P2PService.java index 13c1561cb6..69f104a01d 100644 --- a/network/src/main/java/io/bitsquare/p2p/P2PService.java +++ b/network/src/main/java/io/bitsquare/p2p/P2PService.java @@ -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()));