From 18696f9b88cd8bd2526d0533227fb84b1af00910 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 21 Feb 2022 10:43:56 +0000 Subject: [PATCH] remove incomplete file and pieces file if download is hopeless --- .../core/download/NetworkDownloader.groovy | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/download/NetworkDownloader.groovy b/core/src/main/groovy/com/muwire/core/download/NetworkDownloader.groovy index 442b3d9b..74d62c3b 100644 --- a/core/src/main/groovy/com/muwire/core/download/NetworkDownloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/NetworkDownloader.groovy @@ -114,6 +114,15 @@ class NetworkDownloader extends Downloader { } } } + + private void closePiecesFile() { + synchronized (piecesFile) { + if (piecesFileClosed) + return + piecesFileClosed = true + piecesFile.delete() + } + } long donePieces() { pieces.donePieces() @@ -409,8 +418,12 @@ class NetworkDownloader extends Downloader { if (!cancelled) { log.log(Level.WARNING, "Exception while downloading", DataUtil.findRoot(bad)) markFailed(destination) - if (!hasLiveSources() && hopelessEventFired.compareAndSet(false, true)) + if (!hasLiveSources() && hopelessEventFired.compareAndSet(false, true)) { + log.info("Download hopeless, removing incomplete file") + incompleteFile.delete() + closePiecesFile() eventBus.publish(new DownloadHopelessEvent(downloader: NetworkDownloader.this)) + } } } finally { currentState = WorkerState.FINISHED @@ -418,10 +431,7 @@ class NetworkDownloader extends Downloader { if (!cancelled) { writePieces() if (pieces.isComplete() && eventFired.compareAndSet(false, true)) { - synchronized (piecesFile) { - piecesFileClosed = true - piecesFile.delete() - } + closePiecesFile() activeWorkers.values().each { if (it.destination != destination) it.cancel()