From 1029e0b78db419a3e52cdae7b7c565a81009e263 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 28 Dec 2015 14:32:20 +0100 Subject: [PATCH 1/2] Delete temporary sketch copy after build When a sketch has unsaved changes, a temporary copy of the sketch is made with those changes applied. This copy is then passed to arduino-builder. Previously, this temporary copy was kept around and only deleted when the IDE was closed. However, all files were written to it again on every build, so keeping the old files around did not serve any real purpose. When a file was renamed in the IDE, the original name would still be present in the temporary copy, and could cause linker errors because both were compiled. This commit makes sure the temporary copy is deleted after every build, instead of at IDE exit, which fixes this problem with renames. When a file is deleted from the sketch, the file would also be deleted from the temporary copy, presumably to fix this same problem for deletes (but renames were forgotten). With this commit, this special handling for deleting files is no longer needed, so it is removed. This fixes #4335 --- app/src/processing/app/Sketch.java | 17 +++++++++++++---- arduino-core/src/processing/app/SketchCode.java | 7 +++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index aa2c8c719..8404021bf 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -26,7 +26,6 @@ package processing.app; import cc.arduino.Compiler; import cc.arduino.CompilerProgressListener; import cc.arduino.UploaderUtils; -import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.Uploader; import org.apache.commons.codec.digest.DigestUtils; import processing.app.debug.RunnerException; @@ -463,7 +462,7 @@ public class Sketch { } else { // delete the file - if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath(), Paths.get(System.getProperty("java.io.tmpdir"), "arduino_" + DigestUtils.md5Hex(getMainFilePath())))) { + if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath())) { Base.showMessage(tr("Couldn't do it"), I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName())); return; @@ -1100,17 +1099,27 @@ public class Sketch { CompilerProgressListener progressListener = editor.status::progressUpdate; + boolean deleteTemp = false; String pathToSketch = data.getMainFilePath(); if (isModified()) { + // If any files are modified, make a copy of the sketch with the changes + // saved, so arduino-builder will see the modifications. pathToSketch = saveSketchInTempFolder(); + deleteTemp = true; } - return new Compiler(pathToSketch, data, buildPath).build(progressListener, save); + try { + return new Compiler(pathToSketch, data, buildPath).build(progressListener, + save); + } finally { + // Make sure we clean up any temporary sketch copy + if (deleteTemp) + FileUtils.recursiveDelete(new File(pathToSketch).getParentFile()); + } } private String saveSketchInTempFolder() throws IOException { File tempFolder = FileUtils.createTempFolder("arduino_", DigestUtils.md5Hex(data.getMainFilePath())); - DeleteFilesOnShutdown.add(tempFolder); FileUtils.copy(getFolder(), tempFolder); for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) { diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java index 88d2a5196..19c91d70f 100644 --- a/arduino-core/src/processing/app/SketchCode.java +++ b/arduino-core/src/processing/app/SketchCode.java @@ -91,14 +91,13 @@ public class SketchCode { } - protected boolean deleteFile(Path tempBuildFolder, Path tempUnsavedSketchPath) throws IOException { + protected boolean deleteFile(Path tempBuildFolder) throws IOException { if (!file.delete()) { return false; } - List tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"), tempUnsavedSketchPath) - .filter(path -> Files.exists(path)) - .collect(Collectors.toList()); + List tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch")) + .filter(path -> Files.exists(path)).collect(Collectors.toList()); for (Path folder : tempBuildFolders) { if (!deleteCompiledFilesFrom(folder)) { From 7949e7e81f24c089e73fa8b36310513799e31a18 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 28 Dec 2015 15:24:17 +0100 Subject: [PATCH 2/2] Randomize temporary sketch copy directory name When a sketch has unsaved changes, a temporary copy of the sketch is made with those changes applied. This copy is then passed to arduino-builder. Previously, the name of this directory contained a hash of the main sketch filename, so the same directory would be used between builds. Now that this directory is deleted after every build, it can just use a randomized directory name, which is what this commit does. Addtionally, the prefix used for generating the name is changed from "arduino_" to "arduino_modified_sketch_" to make it slightly clearer what the directory is for (just in case it somehow survives the build, or a user sees it during the build). --- app/src/processing/app/Sketch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 8404021bf..444888c90 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -1119,7 +1119,7 @@ public class Sketch { } private String saveSketchInTempFolder() throws IOException { - File tempFolder = FileUtils.createTempFolder("arduino_", DigestUtils.md5Hex(data.getMainFilePath())); + File tempFolder = FileUtils.createTempFolder("arduino_modified_sketch_"); FileUtils.copy(getFolder(), tempFolder); for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) {