try opening wallets with all backup cache files then no cache file

This commit is contained in:
woodser 2024-11-08 08:22:16 -05:00
parent b348a81f13
commit 577cfa249e
2 changed files with 91 additions and 40 deletions

View file

@ -74,7 +74,7 @@ public class FileUtil {
}
}
public static File getLatestBackupFile(File dir, String fileName) {
public static List<File> getBackupFiles(File dir, String fileName) {
File backupDir = new File(Paths.get(dir.getAbsolutePath(), BACKUP_DIR).toString());
if (!backupDir.exists()) return null;
String dirName = "backups_" + fileName;
@ -82,9 +82,14 @@ public class FileUtil {
File backupFileDir = new File(Paths.get(backupDir.getAbsolutePath(), dirName).toString());
if (!backupFileDir.exists()) return null;
File[] files = backupFileDir.listFiles();
if (files == null || files.length == 0) return null;
Arrays.sort(files, Comparator.comparing(File::getName));
return files[files.length - 1];
return Arrays.asList(files);
}
public static File getLatestBackupFile(File dir, String fileName) {
List<File> files = getBackupFiles(dir, fileName);
if (files.isEmpty()) return null;
files.sort(Comparator.comparing(File::getName));
return files.get(files.size() - 1);
}
public static void deleteRollingBackup(File dir, String fileName) {

View file

@ -1477,7 +1477,7 @@ public class XmrWalletService extends XmrWalletBase {
try {
walletFull = MoneroWalletFull.openWallet(config);
} catch (Exception e) {
log.warn("Failed to open full wallet '{}', attempting to use backup cache, error={}", config.getPath(), e.getMessage());
log.warn("Failed to open full wallet '{}', attempting to use backup cache files, error={}", config.getPath(), e.getMessage());
boolean retrySuccessful = false;
try {
@ -1486,17 +1486,24 @@ public class XmrWalletService extends XmrWalletBase {
File originalCacheFile = new File(cachePath);
if (originalCacheFile.exists()) originalCacheFile.renameTo(new File(cachePath + ".backup"));
// copy latest wallet cache backup to main folder
File backupCacheFile = FileUtil.getLatestBackupFile(walletDir, getWalletName(config.getPath()));
if (backupCacheFile != null) FileUtil.copyFile(backupCacheFile, new File(cachePath));
// try opening wallet with backup cache files in descending order
List<File> backupCacheFiles = FileUtil.getBackupFiles(walletDir, getWalletName(config.getPath()));
Collections.reverse(backupCacheFiles);
for (File backupCacheFile : backupCacheFiles) {
try {
FileUtil.copyFile(backupCacheFile, new File(cachePath));
walletFull = MoneroWalletFull.openWallet(config);
log.warn("Successfully opened full wallet using backup cache");
retrySuccessful = true;
break;
} catch (Exception e2) {
// retry opening wallet without original cache
try {
walletFull = MoneroWalletFull.openWallet(config);
log.warn("Successfully opened full wallet using backup cache");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
// delete cache file if failed to open
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
File unportableCacheFile = new File(cachePath + ".unportable");
if (unportableCacheFile.exists()) unportableCacheFile.delete();
}
}
// handle success or failure
@ -1505,14 +1512,30 @@ public class XmrWalletService extends XmrWalletBase {
if (originalCacheBackup.exists()) originalCacheBackup.delete(); // delete original wallet cache backup
} else {
// restore original wallet cache
log.warn("Failed to open full wallet using backup cache, restoring original cache");
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath));
// retry opening wallet after cache deleted
try {
log.warn("Failed to open full wallet using backup cache files, retrying with cache deleted");
walletFull = MoneroWalletFull.openWallet(config);
log.warn("Successfully opened full wallet after cache deleted");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
}
// throw exception
throw e;
// handle success or failure
if (retrySuccessful) {
if (originalCacheBackup.exists()) originalCacheBackup.delete(); // delete original wallet cache backup
} else {
// restore original wallet cache
log.warn("Failed to open full wallet after deleting cache, restoring original cache");
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath));
// throw original exception
throw e;
}
}
} catch (Exception e2) {
throw e; // throw original exception
@ -1582,7 +1605,7 @@ public class XmrWalletService extends XmrWalletBase {
try {
walletRpc.openWallet(config);
} catch (Exception e) {
log.warn("Failed to open RPC wallet '{}', attempting to use backup cache, error={}", config.getPath(), e.getMessage());
log.warn("Failed to open RPC wallet '{}', attempting to use backup cache files, error={}", config.getPath(), e.getMessage());
boolean retrySuccessful = false;
try {
@ -1591,17 +1614,24 @@ public class XmrWalletService extends XmrWalletBase {
File originalCacheFile = new File(cachePath);
if (originalCacheFile.exists()) originalCacheFile.renameTo(new File(cachePath + ".backup"));
// copy latest wallet cache backup to main folder
File backupCacheFile = FileUtil.getLatestBackupFile(walletDir, config.getPath());
if (backupCacheFile != null) FileUtil.copyFile(backupCacheFile, new File(cachePath));
// try opening wallet with backup cache files in descending order
List<File> backupCacheFiles = FileUtil.getBackupFiles(walletDir, config.getPath());
Collections.reverse(backupCacheFiles);
for (File backupCacheFile : backupCacheFiles) {
try {
FileUtil.copyFile(backupCacheFile, new File(cachePath));
walletRpc.openWallet(config);
log.warn("Successfully opened RPC wallet using backup cache");
retrySuccessful = true;
break;
} catch (Exception e2) {
// retry opening wallet without original cache
try {
walletRpc.openWallet(config);
log.warn("Successfully opened RPC wallet using backup cache");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
// delete cache file if failed to open
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
File unportableCacheFile = new File(cachePath + ".unportable");
if (unportableCacheFile.exists()) unportableCacheFile.delete();
}
}
// handle success or failure
@ -1610,14 +1640,30 @@ public class XmrWalletService extends XmrWalletBase {
if (originalCacheBackup.exists()) originalCacheBackup.delete(); // delete original wallet cache backup
} else {
// restore original wallet cache
log.warn("Failed to open RPC wallet using backup cache, restoring original cache");
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath));
// retry opening wallet after cache deleted
try {
log.warn("Failed to open RPC wallet using backup cache files, retrying with cache deleted");
walletRpc.openWallet(config);
log.warn("Successfully opened RPC wallet after cache deleted");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
}
// throw exception
throw e;
// handle success or failure
if (retrySuccessful) {
if (originalCacheBackup.exists()) originalCacheBackup.delete(); // delete original wallet cache backup
} else {
// restore original wallet cache
log.warn("Failed to open RPC wallet after deleting cache, restoring original cache");
File cacheFile = new File(cachePath);
if (cacheFile.exists()) cacheFile.delete();
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath));
// throw original exception
throw e;
}
}
} catch (Exception e2) {
throw e; // throw original exception