Skip to content

Commit e35c67b

Browse files
committed
Avoid using incomplete tmp file for board manager jsons
Fixes arduino#6628
1 parent 60f2677 commit e35c67b

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public DownloadableContributionsDownloader(File _stagingFolder) {
5252
}
5353

5454
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception {
55+
return download(contribution, progress, statusText, progressListener, false);
56+
}
57+
58+
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
5559
URL url = new URL(contribution.getUrl());
5660
Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName());
5761

@@ -66,7 +70,7 @@ public File download(DownloadableContribution contribution, Progress progress, f
6670
while (true) {
6771
// Need to download or resume downloading?
6872
if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) {
69-
download(url, outputFile.toFile(), progress, statusText, progressListener);
73+
download(url, outputFile.toFile(), progress, statusText, progressListener, noResume);
7074
downloaded = true;
7175
}
7276

@@ -113,6 +117,10 @@ private boolean hasChecksum(DownloadableContribution contribution) {
113117
}
114118

115119
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
120+
download(url, tmpFile, progress, statusText, progressListener, false);
121+
}
122+
123+
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
116124
FileDownloader downloader = new FileDownloader(url, tmpFile);
117125
downloader.addObserver((o, arg) -> {
118126
FileDownloader me = (FileDownloader) o;
@@ -126,7 +134,7 @@ public void download(URL url, File tmpFile, Progress progress, String statusText
126134
progress.setProgress(me.getProgress());
127135
progressListener.onProgress(progress);
128136
});
129-
downloader.download();
137+
downloader.download(noResume);
130138
if (!downloader.isCompleted()) {
131139
throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError());
132140
}

arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ private File download(MultiStepProgress progress, String packageIndexUrl, Progre
329329
File outputFile = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]);
330330
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
331331
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.indexer.getStagingFolder());
332-
downloader.download(url, tmpFile, progress, statusText, progressListener);
332+
boolean noResume = true;
333+
downloader.download(url, tmpFile, progress, statusText, progressListener, noResume);
333334

334335
Files.deleteIfExists(outputFile.toPath());
335336
Files.move(tmpFile.toPath(), outputFile.toPath());

arduino-core/src/cc/arduino/utils/network/FileDownloader.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ public void setStatus(Status status) {
122122
}
123123

124124
public void download() throws InterruptedException {
125+
download(false);
126+
}
127+
128+
public void download(boolean noResume) throws InterruptedException {
125129
if ("file".equals(downloadUrl.getProtocol())) {
126130
saveLocalFile();
127131
} else {
128-
downloadFile();
132+
downloadFile(noResume);
129133
}
130134
}
131135

@@ -140,12 +144,23 @@ private void saveLocalFile() {
140144
}
141145

142146
private void downloadFile() throws InterruptedException {
147+
downloadFile(false);
148+
}
149+
150+
private void downloadFile(boolean noResume) throws InterruptedException {
143151
RandomAccessFile file = null;
144152

145153
try {
146154
// Open file and seek to the end of it
147155
file = new RandomAccessFile(outputFile, "rw");
148156
initialSize = file.length();
157+
158+
if (noResume && initialSize > 0) {
159+
// delete file and restart downloading
160+
Files.delete(outputFile.toPath());
161+
initialSize = 0;
162+
}
163+
149164
file.seek(initialSize);
150165

151166
setStatus(Status.CONNECTING);

0 commit comments

Comments
 (0)