diff options
| author | NopoTheGamer <40329022+NopoTheGamer@users.noreply.github.com> | 2024-07-30 04:55:06 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-29 20:55:06 +0200 |
| commit | fed1051b8419e141287c0cebd16cb81b3e4fa6dc (patch) | |
| tree | 399754eb81b078a101ed0cd316476fff3fb61c5c | |
| parent | b2fa8e0277650441da5cdd83e0a53a32854625f3 (diff) | |
| download | notenoughupdates-fed1051b8419e141287c0cebd16cb81b3e4fa6dc.tar.gz notenoughupdates-fed1051b8419e141287c0cebd16cb81b3e4fa6dc.tar.bz2 notenoughupdates-fed1051b8419e141287c0cebd16cb81b3e4fa6dc.zip | |
Add config backup (#1253)
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/core/config/ConfigUtil.java | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/core/config/ConfigUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/core/config/ConfigUtil.java index e6fb8799..7a5a6785 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/core/config/ConfigUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/core/config/ConfigUtil.java @@ -66,15 +66,8 @@ public class ConfigUtil { "Invalid config file '" + file + "'. This will reset the config to default", e ).printStackTrace(); - try { - // Try to save a version of the corrupted config for debugging purposes - Files.copy( - file.toPath(), - new File(file.getParent(), file.getName() + ".corrupted").toPath(), - StandardCopyOption.REPLACE_EXISTING - ); - } catch (Exception ignored) { - } + // Try to save a version of the corrupted config for debugging purposes + makeBackup(file, ".corrupted"); } return null; } @@ -100,7 +93,7 @@ public class ConfigUtil { if (loadConfig(config.getClass(), tempFile, gson, useGzip, false) == null) { System.out.println("Config verification failed for " + tempFile + ", could not save config properly."); - tempFile.delete(); + makeBackup(tempFile, ".backup"); return; } @@ -112,8 +105,26 @@ public class ConfigUtil { Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); } } catch (Exception e) { + makeBackup(tempFile, ".backup"); e.printStackTrace(); - tempFile.delete(); + } + } + + private static void makeBackup(File file, String suffix) { + File backupFile = new File(file.getParent(), file.getName() + "-" + System.currentTimeMillis() + suffix); + System.out.println("trying to make backup: " + backupFile.getName()); + + try { + Files.move(file.toPath(), backupFile.toPath(), StandardCopyOption.ATOMIC_MOVE); + } catch (IOException _) { + try { + Files.move(file.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (Exception __) { + System.out.println("neu config gone"); + } + } + finally { + file.delete(); } } } |
