From e35c67b6a9696ad6c76c9c919b5dd7a524aeb59f Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 11 Sep 2017 15:22:05 +0200 Subject: [PATCH] Avoid using incomplete tmp file for board manager jsons Fixes https://github.com/arduino/Arduino/issues/6628 --- .../DownloadableContributionsDownloader.java | 12 ++++++++++-- .../packages/ContributionInstaller.java | 3 ++- .../arduino/utils/network/FileDownloader.java | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java b/arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java index 025991166..3157514f8 100644 --- a/arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java +++ b/arduino-core/src/cc/arduino/contributions/DownloadableContributionsDownloader.java @@ -52,6 +52,10 @@ public class DownloadableContributionsDownloader { } 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()); Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName()); @@ -66,7 +70,7 @@ public class DownloadableContributionsDownloader { while (true) { // Need to download or resume downloading? 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; } @@ -113,6 +117,10 @@ public class DownloadableContributionsDownloader { } 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); downloader.addObserver((o, arg) -> { FileDownloader me = (FileDownloader) o; @@ -126,7 +134,7 @@ public class DownloadableContributionsDownloader { progress.setProgress(me.getProgress()); progressListener.onProgress(progress); }); - downloader.download(); + downloader.download(noResume); if (!downloader.isCompleted()) { throw new Exception(format(tr("Error downloading {0}"), url), downloader.getError()); } diff --git a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java index e1ab6867f..a5ebb6041 100644 --- a/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java +++ b/arduino-core/src/cc/arduino/contributions/packages/ContributionInstaller.java @@ -329,7 +329,8 @@ public class ContributionInstaller { File outputFile = BaseNoGui.indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]); File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp"); 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.move(tmpFile.toPath(), outputFile.toPath()); diff --git a/arduino-core/src/cc/arduino/utils/network/FileDownloader.java b/arduino-core/src/cc/arduino/utils/network/FileDownloader.java index 314ec0723..edf054d23 100644 --- a/arduino-core/src/cc/arduino/utils/network/FileDownloader.java +++ b/arduino-core/src/cc/arduino/utils/network/FileDownloader.java @@ -122,10 +122,14 @@ public class FileDownloader extends Observable { } public void download() throws InterruptedException { + download(false); + } + + public void download(boolean noResume) throws InterruptedException { if ("file".equals(downloadUrl.getProtocol())) { saveLocalFile(); } else { - downloadFile(); + downloadFile(noResume); } } @@ -140,12 +144,23 @@ public class FileDownloader extends Observable { } private void downloadFile() throws InterruptedException { + downloadFile(false); + } + + private void downloadFile(boolean noResume) throws InterruptedException { RandomAccessFile file = null; try { // Open file and seek to the end of it file = new RandomAccessFile(outputFile, "rw"); initialSize = file.length(); + + if (noResume && initialSize > 0) { + // delete file and restart downloading + Files.delete(outputFile.toPath()); + initialSize = 0; + } + file.seek(initialSize); setStatus(Status.CONNECTING);