rename wallet backups along with the wallet when renaming
What changed, and why it matters
This commit fixes a bug in Sparrow Wallet where renaming a wallet left its automatic backups behind under the old name. Previously, the old backups could be orphaned or deleted while a new set of backups was created under the new wallet name, potentially causing confusion or loss of backup continuity. The change makes the app rename existing backup files to match the new wallet name when the wallet is renamed.
Treat as a routine bug-fix / data-integrity improvement. No immediate security response is required, but users who renamed wallets before this fix should verify that their backup directories contain the expected backups under the current wallet name and remove any orphaned old-name backups manually if desired.
Security signals we found
Data integrity / backup consistency fix
Potential backup orphaning or deletion before patch
Atomic file move used for backup renaming
No overwrite behavior when target backup already exists
Evidence from the diff
The patch adds a renameBackups(Storage renamedStorage) method to Storage.java and calls it from AppController.java before deleteStorage() when a wallet is renamed. It iterates over files in the wallet backup directory whose names start with the old wallet name, computes a corresponding new filename using the renamed wallet name, and moves each backup file with Files.move(). A new unit test verifies that backups are renamed and that an existing target backup file is not overwritten (the source backup is kept instead).
Changed components
Sparrow Wallet desktop applicationWallet backup storage subsystemAppController wallet rename flowStorage I/O classInspect captured patch +49 / −1
### src/main/java/com/sparrowwallet/sparrow/AppController.java
@@ -1817,7 +1817,9 @@ public void addWalletTab(Storage storage, Wallet wallet) {
File oldWalletFile = renamedWallets.remove(storage.getWalletFile());
if(oldWalletFile != null) {
- deleteStorage(new Storage(oldWalletFile), false);
+ Storage oldStorage = new Storage(oldWalletFile);
+ oldStorage.renameBackups(storage);
+ deleteStorage(oldStorage, false);
}
} else {
for(Tab walletTab : tabs.getTabs()) {
### src/main/java/com/sparrowwallet/sparrow/io/Storage.java
@@ -296,6 +296,25 @@ File[] getBackups(String prefix) {
return getBackups(getWalletsBackupDir(), prefix);
}
+ public void renameBackups(Storage renamedStorage) {
+ if(isInWalletsDir()) {
+ renameBackups(getWalletsBackupDir(), renamedStorage);
+ }
+ }
+
+ void renameBackups(File backupDir, Storage renamedStorage) {
+ String walletName = persistence.getWalletName(walletFile, null);
+ String renamedWalletName = renamedStorage.persistence.getWalletName(renamedStorage.walletFile, null);
+ for(File backup : getBackups(backupDir, null)) {
+ File renamedBackup = new File(backupDir, renamedWalletName + backup.getName().substring(walletName.length()));
+ try {
+ Files.move(backup.toPath(), renamedBackup.toPath());
+ } catch(IOException e) {
+ log.error("Error renaming wallet backup " + backup.getName() + " to " + renamedBackup.getName(), e);
+ }
+ }
+ }
+
File[] getBackups(File backupDir, String prefix) {
String walletName = persistence.getWalletName(walletFile, null);
String extension = walletFile.getName().substring(walletName.length());
### src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java
@@ -168,6 +168,33 @@ public void getBackupsExcludesBackupsOfAnExistingJsonWallet() throws IOException
assertBackups(backupDir, PersistenceType.JSON, "Separate", "Separate-20200101120000");
}
+ @Test
+ public void renameBackupsMovesBackupsToTheRenamedWallet() throws IOException {
+ File backupDir = createBackupDir("Old-20250101120000.mv.db", "Old-20210101120000.json", "Old-20200101120000",
+ "OldX-20250101120000.mv.db", "Old-2021-20250101120000.mv.db", "New-20240101120000.mv.db");
+
+ Storage oldStorage = new Storage(PersistenceType.DB, new File(tempDir.toFile(), "Old.mv.db"));
+ oldStorage.renameBackups(backupDir, new Storage(PersistenceType.DB, new File(tempDir.toFile(), "New.mv.db")));
+
+ assertBackups(backupDir, PersistenceType.DB, "Old.mv.db");
+ assertBackups(backupDir, PersistenceType.DB, "New.mv.db", "New-20250101120000.mv.db", "New-20240101120000.mv.db", "New-20210101120000.json", "New-20200101120000");
+ assertBackups(backupDir, PersistenceType.DB, "OldX.mv.db", "OldX-20250101120000.mv.db");
+ assertBackups(backupDir, PersistenceType.DB, "Old-2021.mv.db", "Old-2021-20250101120000.mv.db");
+ }
+
+ @Test
+ public void renameBackupsDoesNotReplaceAnExistingBackup() throws IOException {
+ File backupDir = createBackupDir("Old-20250101120000.mv.db", "New-20250101120000.mv.db");
+ Files.writeString(backupDir.toPath().resolve("Old-20250101120000.mv.db"), "old");
+ Files.writeString(backupDir.toPath().resolve("New-20250101120000.mv.db"), "new");
+
+ Storage oldStorage = new Storage(PersistenceType.DB, new File(tempDir.toFile(), "Old.mv.db"));
+ oldStorage.renameBackups(backupDir, new Storage(PersistenceType.DB, new File(tempDir.toFile(), "New.mv.db")));
+
+ Assertions.assertEquals("new", Files.readString(backupDir.toPath().resolve("New-20250101120000.mv.db")));
+ assertBackups(backupDir, PersistenceType.DB, "Old.mv.db", "Old-20250101120000.mv.db");
+ }
+
@Test
public void getBackupsTreatsAWalletNameLiterally() throws IOException {
File backupDir = createBackupDir("SavingsXold-20250101120000.mv.db");Why this scored 21/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.