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,17 +1486,24 @@ 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) {
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 // delete cache file if failed to open
try { File cacheFile = new File(cachePath);
walletFull = MoneroWalletFull.openWallet(config); if (cacheFile.exists()) cacheFile.delete();
log.warn("Successfully opened full wallet using backup cache"); File unportableCacheFile = new File(cachePath + ".unportable");
retrySuccessful = true; if (unportableCacheFile.exists()) unportableCacheFile.delete();
} catch (Exception e2) { }
// ignore
} }
// handle success or failure // handle success or failure
@ -1505,14 +1512,30 @@ public class XmrWalletService extends XmrWalletBase {
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 // retry opening wallet after cache deleted
log.warn("Failed to open full wallet using backup cache, restoring original cache"); try {
File cacheFile = new File(cachePath); log.warn("Failed to open full wallet using backup cache files, retrying with cache deleted");
if (cacheFile.exists()) cacheFile.delete(); walletFull = MoneroWalletFull.openWallet(config);
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath)); log.warn("Successfully opened full wallet after cache deleted");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
}
// throw exception // handle success or failure
throw e; 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) { } 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,17 +1614,24 @@ 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) {
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 // delete cache file if failed to open
try { File cacheFile = new File(cachePath);
walletRpc.openWallet(config); if (cacheFile.exists()) cacheFile.delete();
log.warn("Successfully opened RPC wallet using backup cache"); File unportableCacheFile = new File(cachePath + ".unportable");
retrySuccessful = true; if (unportableCacheFile.exists()) unportableCacheFile.delete();
} catch (Exception e2) { }
// ignore
} }
// handle success or failure // handle success or failure
@ -1610,14 +1640,30 @@ public class XmrWalletService extends XmrWalletBase {
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 // retry opening wallet after cache deleted
log.warn("Failed to open RPC wallet using backup cache, restoring original cache"); try {
File cacheFile = new File(cachePath); log.warn("Failed to open RPC wallet using backup cache files, retrying with cache deleted");
if (cacheFile.exists()) cacheFile.delete(); walletRpc.openWallet(config);
if (originalCacheBackup.exists()) originalCacheBackup.renameTo(new File(cachePath)); log.warn("Successfully opened RPC wallet after cache deleted");
retrySuccessful = true;
} catch (Exception e2) {
// ignore
}
// throw exception // handle success or failure
throw e; 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) { } catch (Exception e2) {
throw e; // throw original exception throw e; // throw original exception