delay wallet file deletion to allow for database compaction and show error on failure

This commit is contained in:
Craig Raw 2022-11-07 08:29:29 +02:00
parent 6481d83b0c
commit 7863fb7632
3 changed files with 37 additions and 4 deletions

View file

@ -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));
}

View file

@ -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();

View file

@ -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<Boolean> {
private final Storage storage;
public DeleteWalletService(Storage storage) {
this.storage = storage;
}
@Override
protected Task<Boolean> createTask() {
return new Task<>() {
protected Boolean call() {
return storage.delete();
}
};
}
}
}