From 7863fb763297975defd3e860c6da3660fae365b1 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 7 Nov 2022 08:29:29 +0200 Subject: [PATCH] delay wallet file deletion to allow for database compaction and show error on failure --- .../sparrowwallet/sparrow/AppController.java | 17 +++++++++++++- .../com/sparrowwallet/sparrow/io/IOUtils.java | 2 +- .../com/sparrowwallet/sparrow/io/Storage.java | 22 +++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index ae56d7d7..39f27736 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1905,7 +1905,22 @@ public class AppController implements Initializable { private void deleteStorage(Storage storage) { if(storage.isClosed()) { - Platform.runLater(storage::delete); + Platform.runLater(() -> { + Storage.DeleteWalletService deleteWalletService = new Storage.DeleteWalletService(storage); + deleteWalletService.setDelay(Duration.seconds(3)); + deleteWalletService.setPeriod(Duration.hours(1)); + deleteWalletService.setOnSucceeded(event -> { + deleteWalletService.cancel(); + if(!deleteWalletService.getValue()) { + showErrorDialog("Error deleting wallet", "Could not delete " + storage.getWalletFile().getName() + ". Please delete this file manually."); + } + }); + deleteWalletService.setOnFailed(event -> { + deleteWalletService.cancel(); + showErrorDialog("Error deleting wallet", "Could not delete " + storage.getWalletFile().getName() + ". Please delete this file manually."); + }); + deleteWalletService.start(); + }); } else { Platform.runLater(() -> deleteStorage(storage)); } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java index ab918ab9..9122fd0f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/IOUtils.java @@ -142,7 +142,7 @@ public class IOUtils { pos += data.length; } } catch(IOException e) { - log.warn("Error overwriting file for deletion " + file.getName(), e); + log.warn("Error overwriting file for deletion: " + file.getName(), e); } return file.delete(); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index fa89e5f1..660a0c9b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -10,6 +10,7 @@ import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.SparrowWallet; import com.sparrowwallet.sparrow.soroban.Soroban; import com.sparrowwallet.sparrow.whirlpool.Whirlpool; +import javafx.concurrent.ScheduledService; import javafx.concurrent.Service; import javafx.concurrent.Task; import org.apache.commons.lang3.concurrent.BasicThreadFactory; @@ -263,9 +264,9 @@ public class Storage { persistence.copyWallet(walletFile, outputStream); } - public void delete() { + public boolean delete() { deleteBackups(); - IOUtils.secureDelete(walletFile); + return IOUtils.secureDelete(walletFile); } public void deleteBackups() { @@ -732,4 +733,21 @@ public class Storage { }; } } + + public static class DeleteWalletService extends ScheduledService { + private final Storage storage; + + public DeleteWalletService(Storage storage) { + this.storage = storage; + } + + @Override + protected Task createTask() { + return new Task<>() { + protected Boolean call() { + return storage.delete(); + } + }; + } + } }