From c3d9e852ba99700c741978cd4a778e8160ccb91e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 07:49:06 +0100 Subject: [PATCH 1/2] separate incomplete files --- .../com/muwire/core/download/Downloader.groovy | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index a575999c..36da8fcf 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -4,6 +4,9 @@ import com.muwire.core.InfoHash import com.muwire.core.Persona import com.muwire.core.connection.Endpoint +import java.nio.file.AtomicMoveNotSupportedException +import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ExecutorService import java.util.concurrent.Executors @@ -43,6 +46,7 @@ public class Downloader { private final Set destinations private final int nPieces private final File piecesFile + private final File incompleteFile final int pieceSizePow2 private final Map activeWorkers = new ConcurrentHashMap<>() @@ -64,6 +68,7 @@ public class Downloader { this.connector = connector this.destinations = destinations this.piecesFile = new File(incompletes, file.getName()+".pieces") + this.incompleteFile = new File(incompletes, file.getName()+".part") this.pieceSizePow2 = pieceSizePow2 this.pieceSize = 1 << pieceSizePow2 @@ -240,7 +245,7 @@ public class Downloader { currentState = WorkerState.DOWNLOADING boolean requestPerformed while(!pieces.isComplete()) { - currentSession = new DownloadSession(me.toBase64(), pieces, getInfoHash(), endpoint, file, pieceSize, length) + currentSession = new DownloadSession(me.toBase64(), pieces, getInfoHash(), endpoint, incompleteFile, pieceSize, length) requestPerformed = currentSession.request() if (!requestPerformed) break @@ -255,6 +260,12 @@ public class Downloader { piecesFileClosed = true piecesFile.delete() } + try { + Files.move(incompleteFile.toPath(), file.toPath(), StandardCopyOption.ATOMIC_MOVE) + } catch (AtomicMoveNotSupportedException e) { + Files.copy(incompleteFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING) + incompleteFile.delete() + } eventBus.publish( new FileDownloadedEvent( downloadedFile : new DownloadedFile(file, getInfoHash(), pieceSizePow2, Collections.emptySet()), From c4f48c02b6c5f5846e9b8f24a5bc3bc4b1afbe3a Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 12:33:44 +0100 Subject: [PATCH 2/2] delete incomplete file on cancel --- core/src/main/groovy/com/muwire/core/download/Downloader.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index 36da8fcf..6d772c5d 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -180,11 +180,11 @@ public class Downloader { public void cancel() { cancelled = true stop() - file.delete() synchronized(piecesFile) { piecesFileClosed = true piecesFile.delete() } + incompleteFile.delete() } void stop() {