diff --git a/src/main/java/meteordevelopment/meteorclient/systems/System.java b/src/main/java/meteordevelopment/meteorclient/systems/System.java index 24c350c335..ffdbdeebb6 100644 --- a/src/main/java/meteordevelopment/meteorclient/systems/System.java +++ b/src/main/java/meteordevelopment/meteorclient/systems/System.java @@ -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); }