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

View file

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