Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/main/java/meteordevelopment/meteorclient/systems/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,26 @@ public void save(File folder) {
CompoundTag tag = toTag();
if (tag == null) return;

try {
File tempFile = File.createTempFile(MeteorClient.MOD_ID, file.getName());
NbtIo.write(tag, tempFile.toPath());

if (folder != null) file = new File(folder, file.getName());
if (folder != null) file = new File(folder, file.getName());

try {
file.getParentFile().mkdirs();

// The temp file has to live next to the target, otherwise the move crosses a filesystem
// boundary and falls back to a copy that truncates the old file before writing
File tempFile = File.createTempFile(MeteorClient.MOD_ID, file.getName(), file.getParentFile());

try {
Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException _) {
StreamUtils.copy(tempFile, file);
}
NbtIo.write(tag, tempFile.toPath());

tempFile.delete();
try {
Files.move(tempFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException _) {
StreamUtils.copy(tempFile, file);
}
} finally {
tempFile.delete();
}
} catch (IOException e) {
MeteorClient.LOG.error("Error saving {}. Possibly corrupted?", this.name, e);
}
Expand Down