1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-26 20:54:22 +01:00

Avoid using incomplete tmp file for board manager jsons

Fixes https://github.com/arduino/Arduino/issues/6628
This commit is contained in:
Martino Facchin 2017-09-11 15:22:05 +02:00
parent 60f267745b
commit e35c67b6a9
3 changed files with 28 additions and 4 deletions

View File

@ -52,6 +52,10 @@ public class DownloadableContributionsDownloader {
} }
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception { public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception {
return download(contribution, progress, statusText, progressListener, false);
}
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
URL url = new URL(contribution.getUrl()); URL url = new URL(contribution.getUrl());
Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName()); Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName());
@ -66,7 +70,7 @@ public class DownloadableContributionsDownloader {
while (true) { while (true) {
// Need to download or resume downloading? // Need to download or resume downloading?
if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) { if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) {
download(url, outputFile.toFile(), progress, statusText, progressListener); download(url, outputFile.toFile(), progress, statusText, progressListener, noResume);
downloaded = true; downloaded = true;
} }
@ -113,6 +117,10 @@ public class DownloadableContributionsDownloader {
} }
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception { public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
download(url, tmpFile, progress, statusText, progressListener, false);
}
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener, boolean noResume) throws Exception {
FileDownloader downloader = new FileDownloader(url, tmpFile); FileDownloader downloader = new FileDownloader(url, tmpFile);
downloader.addObserver((o, arg) -> { downloader.addObserver((o, arg) -> {
FileDownloader me = (FileDownloader) o; FileDownloader me = (FileDownloader) o;
@ -126,7 +134,7 @@ public class DownloadableContributionsDownloader {
progress.setProgress(me.getProgress()); progress.setProgress(me.getProgress());
progressListener.onProgress(progress); progressListener.onProgress(progress);
}); });
downloader.download(); downloader.download(noResume);
if (!downloader.isCompleted()) { if (!downloader.isCompleted()) {
throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError()); throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError());
} }

View File

@ -329,7 +329,8 @@ public class ContributionInstaller {
File outputFile = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]); File outputFile = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]);
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp"); File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.indexer.getStagingFolder()); DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.indexer.getStagingFolder());
downloader.download(url, tmpFile, progress, statusText, progressListener); boolean noResume = true;
downloader.download(url, tmpFile, progress, statusText, progressListener, noResume);
Files.deleteIfExists(outputFile.toPath()); Files.deleteIfExists(outputFile.toPath());
Files.move(tmpFile.toPath(), outputFile.toPath()); Files.move(tmpFile.toPath(), outputFile.toPath());

View File

@ -122,10 +122,14 @@ public class FileDownloader extends Observable {
} }
public void download() throws InterruptedException { public void download() throws InterruptedException {
download(false);
}
public void download(boolean noResume) throws InterruptedException {
if ("file".equals(downloadUrl.getProtocol())) { if ("file".equals(downloadUrl.getProtocol())) {
saveLocalFile(); saveLocalFile();
} else { } else {
downloadFile(); downloadFile(noResume);
} }
} }
@ -140,12 +144,23 @@ public class FileDownloader extends Observable {
} }
private void downloadFile() throws InterruptedException { private void downloadFile() throws InterruptedException {
downloadFile(false);
}
private void downloadFile(boolean noResume) throws InterruptedException {
RandomAccessFile file = null; RandomAccessFile file = null;
try { try {
// Open file and seek to the end of it // Open file and seek to the end of it
file = new RandomAccessFile(outputFile, "rw"); file = new RandomAccessFile(outputFile, "rw");
initialSize = file.length(); initialSize = file.length();
if (noResume && initialSize > 0) {
// delete file and restart downloading
Files.delete(outputFile.toPath());
initialSize = 0;
}
file.seek(initialSize); file.seek(initialSize);
setStatus(Status.CONNECTING); setStatus(Status.CONNECTING);