Skip to content

Commit 417857e

Browse files
author
Federico Fissore
committed
Fixed a crashed when user attempted to delete a tab of a not yet compiled sketch. Fixes #3913
1 parent 9af0eee commit 417857e

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

arduino-core/src/processing/app/SketchCode.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@
2626

2727
import java.io.File;
2828
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.nio.file.Paths;
2932
import java.util.Arrays;
3033
import java.util.List;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
3136

3237
import static processing.app.I18n.tr;
3338

@@ -91,35 +96,39 @@ protected boolean fileReadOnly() {
9196
}
9297

9398

94-
protected boolean deleteFile(File tempBuildFolder) {
99+
protected boolean deleteFile(File tempBuildFolder) throws IOException {
95100
if (!file.delete()) {
96101
return false;
97102
}
98103

99-
if (!deleteCompiledFilesFrom(tempBuildFolder)) {
100-
return false;
101-
}
104+
List<Path> tempBuildFolders = Stream.of(tempBuildFolder.toPath(), Paths.get(tempBuildFolder.getAbsolutePath(), "sketch"))
105+
.filter(path -> Files.exists(path))
106+
.collect(Collectors.toList());
102107

103-
if (!deleteCompiledFilesFrom(new File(tempBuildFolder, "sketch"))) {
104-
return false;
108+
for (Path folder : tempBuildFolders) {
109+
if (!deleteCompiledFilesFrom(folder)) {
110+
return false;
111+
}
105112
}
106113

107114
return true;
108115
}
109116

110-
private boolean deleteCompiledFilesFrom(File tempBuildFolder) {
111-
File[] compiledFiles = tempBuildFolder.listFiles(pathname -> {
112-
return pathname.getName().startsWith(getFileName());
113-
});
114-
for (File compiledFile : compiledFiles) {
115-
if (!compiledFile.delete()) {
117+
private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException {
118+
List<Path> compiledFiles = Files.list(tempBuildFolder)
119+
.filter(pathname -> pathname.getFileName().toString().startsWith(getFileName()))
120+
.collect(Collectors.toList());
121+
122+
for (Path compiledFile : compiledFiles) {
123+
try {
124+
Files.delete(compiledFile);
125+
} catch (IOException e) {
116126
return false;
117127
}
118128
}
119129
return true;
120130
}
121131

122-
123132
protected boolean renameTo(File what) {
124133
boolean success = file.renameTo(what);
125134
if (success) {

0 commit comments

Comments
 (0)