From 9dd6068e6976807163fb684207620326157bb3e1 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 4 Oct 2021 13:49:41 +0200 Subject: [PATCH] make temp backup permanent when created in a previous process --- .../java/com/sparrowwallet/sparrow/io/Storage.java | 14 ++++++++++++++ .../sparrowwallet/sparrow/io/db/DbPersistence.java | 1 + 2 files changed, 15 insertions(+) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 4d9d043f..a98ca266 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -173,6 +173,20 @@ public class Storage { } public void deleteTempBackups() { + File[] backups = getBackups(Storage.TEMP_BACKUP_PREFIX); + if(backups.length > 0) { + try { + Date date = BACKUP_DATE_FORMAT.parse(getBackupDate(backups[0].getName())); + ProcessHandle.Info processInfo = ProcessHandle.current().info(); + if(processInfo.startInstant().isPresent() && processInfo.startInstant().get().isAfter(date.toInstant())) { + File permanent = new File(backups[0].getParent(), backups[0].getName().substring(Storage.TEMP_BACKUP_PREFIX.length() + 1)); + backups[0].renameTo(permanent); + } + } catch(Exception e) { + log.error("Error copying temporary to permanent backup", e); + } + } + deleteBackups(Storage.TEMP_BACKUP_PREFIX); } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java index 7811088f..1dfea6af 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java @@ -88,6 +88,7 @@ public class DbPersistence implements Persistence { Persistence backupPersistence = PersistenceType.DB.getInstance(); backupPersistence.setKeyDeriver(keyDeriver); backupWallet = backupPersistence.loadWallet(new Storage(backupPersistence, backupFile), password, encryptionKey).getWallet(); + backupPersistence.close(); } Map childWallets = loadChildWallets(storage, masterWallet, backupWallet, encryptionKey);