Skip to content

Commit 1c206b6

Browse files
committed
Prefer atomic file moves when dealing with System configs; should prevent potential data loss scenarios.
closes #5656
1 parent 6c8f7de commit 1c206b6

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

  • src/main/java/meteordevelopment/meteorclient/systems

src/main/java/meteordevelopment/meteorclient/systems/System.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18+
import java.nio.file.AtomicMoveNotSupportedException;
19+
import java.nio.file.Files;
20+
import java.nio.file.StandardCopyOption;
1821
import java.time.ZonedDateTime;
1922
import java.time.format.DateTimeFormatter;
2023
import java.util.Locale;
@@ -51,10 +54,16 @@ public void save(File folder) {
5154
if (folder != null) file = new File(folder, file.getName());
5255

5356
file.getParentFile().mkdirs();
54-
StreamUtils.copy(tempFile, file);
57+
58+
try {
59+
Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
60+
} catch (AtomicMoveNotSupportedException e) {
61+
StreamUtils.copy(tempFile, file);
62+
}
63+
5564
tempFile.delete();
5665
} catch (IOException e) {
57-
e.printStackTrace();
66+
MeteorClient.LOG.error("Error saving {}. Possibly corrupted?", this.name, e);
5867
}
5968
}
6069

@@ -75,14 +84,19 @@ public void load(File folder) {
7584
} catch (CrashException e) {
7685
String backupName = FilenameUtils.removeExtension(file.getName()) + "-" + ZonedDateTime.now().format(DATE_TIME_FORMATTER) + ".backup.nbt";
7786
File backup = new File(file.getParentFile(), backupName);
78-
StreamUtils.copy(file, backup);
79-
MeteorClient.LOG.error("Error loading {}. Possibly corrupted?", this.name);
87+
88+
try {
89+
Files.move(file.toPath(), backup.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
90+
} catch (AtomicMoveNotSupportedException ex) {
91+
StreamUtils.copy(file, backup);
92+
}
93+
94+
MeteorClient.LOG.error("Error loading {}. Possibly corrupted?", this.name, e);
8095
MeteorClient.LOG.info("Saved settings backup to '{}'.", backup);
81-
e.printStackTrace();
8296
}
8397
}
8498
} catch (IOException e) {
85-
e.printStackTrace();
99+
MeteorClient.LOG.error("Error loading {}. Possibly corrupted?", this.name, e);
86100
}
87101
}
88102

0 commit comments

Comments
 (0)