From 417857ee4558c528b123617d2ef148b3b82bca45 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Wed, 7 Oct 2015 15:16:53 +0200 Subject: [PATCH] Fixed a crashed when user attempted to delete a tab of a not yet compiled sketch. Fixes #3913 --- .../src/processing/app/SketchCode.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java index af4db284c..0e3f26560 100644 --- a/arduino-core/src/processing/app/SketchCode.java +++ b/arduino-core/src/processing/app/SketchCode.java @@ -26,8 +26,13 @@ import processing.app.helpers.FileUtils; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static processing.app.I18n.tr; @@ -91,35 +96,39 @@ public class SketchCode { } - protected boolean deleteFile(File tempBuildFolder) { + protected boolean deleteFile(File tempBuildFolder) throws IOException { if (!file.delete()) { return false; } - if (!deleteCompiledFilesFrom(tempBuildFolder)) { - return false; - } + List tempBuildFolders = Stream.of(tempBuildFolder.toPath(), Paths.get(tempBuildFolder.getAbsolutePath(), "sketch")) + .filter(path -> Files.exists(path)) + .collect(Collectors.toList()); - if (!deleteCompiledFilesFrom(new File(tempBuildFolder, "sketch"))) { - return false; + for (Path folder : tempBuildFolders) { + if (!deleteCompiledFilesFrom(folder)) { + return false; + } } return true; } - private boolean deleteCompiledFilesFrom(File tempBuildFolder) { - File[] compiledFiles = tempBuildFolder.listFiles(pathname -> { - return pathname.getName().startsWith(getFileName()); - }); - for (File compiledFile : compiledFiles) { - if (!compiledFile.delete()) { + private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException { + List compiledFiles = Files.list(tempBuildFolder) + .filter(pathname -> pathname.getFileName().toString().startsWith(getFileName())) + .collect(Collectors.toList()); + + for (Path compiledFile : compiledFiles) { + try { + Files.delete(compiledFile); + } catch (IOException e) { return false; } } return true; } - protected boolean renameTo(File what) { boolean success = file.renameTo(what); if (success) {