fix: do not overwrite monero backup files if they exist. (#2202)

cyan committed Apr 14, 2025 at 18:46 UTC 5f4dc95ca521261b830ecb9c263c13535df51609
1 file changed +9 -5
cw_core/lib/monero_wallet_utils.dart
+9 -5
@@ -19,15 +19,15 @@ Future<void> backupWalletFiles(String name) async {
19 final newKeysFilePath = backupFileName(keysFile.path);
20 final newAddressListFilePath = backupFileName(addressListFile.path);
21
22 - if (cacheFile.existsSync()) {
22 + if (cacheFile.existsSync() && !File(newCacheFilePath).existsSync()) {
23 await cacheFile.copy(newCacheFilePath);
24 }
25
26 - if (keysFile.existsSync()) {
26 + if (keysFile.existsSync() && !File(newKeysFilePath).existsSync()) {
27 await keysFile.copy(newKeysFilePath);
28 }
29
30 - if (addressListFile.existsSync()) {
30 + if (addressListFile.existsSync() && !File(newAddressListFilePath).existsSync()) {
31 await addressListFile.copy(newAddressListFilePath);
32 }
33 }
@@ -83,10 +83,13 @@ Future<bool> backupWalletFilesExists(String name) async {
83 Future<void> removeCache(String name) async {
84 final path = await pathForWallet(name: name, type: WalletType.monero);
85 final cacheFile = File(path);
86 -
86 + final backgroundCacheFile = File(path + ".background");
87 if (cacheFile.existsSync()) {
88 cacheFile.deleteSync();
89 }
90 + if (backgroundCacheFile.existsSync()) {
91 + backgroundCacheFile.deleteSync();
92 + }
93 }
94
95 Future<void> restoreOrResetWalletFiles(String name) async {
@@ -94,7 +97,8 @@ Future<void> restoreOrResetWalletFiles(String name) async {
97
98 if (backupsExists) {
99 await removeCache(name);
97 -
100 + // TODO(mrcyjanek): is this needed?
101 + // If we remove cache then wallet should be restored from .keys file.
102 await restoreWalletFiles(name);
103 }
104 }