From 2d1f49a077368f5dce7a419095e6ec7f716b1e49 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 4 Nov 2016 18:30:52 +0100 Subject: [PATCH] Fix symlinks being replaced with files on save Fixes #5478 Backported from Processing https://github.com/processing/processing/blob/0abee5af6ad3b11cf2b73bb794b8a97c157c4762/app/src/processing/app/Util.java#L174 --- arduino-core/src/processing/app/BaseNoGui.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index e1dc220b0..add63378a 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -1061,11 +1061,22 @@ public class BaseNoGui { } /** - * Spew the contents of a String object out to a file. + * Save the content of a String into a file + * - Save the content into a temp file + * - Find the canonical path of the file (if it's a symlink, follow it) + * - Remove the original file + * - Move temp file to original path + * This ensures that the file is not getting truncated if the disk is full */ static public void saveFile(String str, File file) throws IOException { File temp = File.createTempFile(file.getName(), null, file.getParentFile()); PApplet.saveStrings(temp, new String[] { str }); + + try { + file = file.getCanonicalFile(); + } catch (IOException e) { + } + if (file.exists()) { boolean result = file.delete(); if (!result) {