close the file before marking pieces complete

pull/5/head
Zlatin Balevsky 2019-06-16 23:45:23 +01:00
parent 4d001ae74b
commit b507361c58
1 changed files with 35 additions and 31 deletions

View File

@ -66,6 +66,8 @@ class DownloadSession {
int piece int piece
while(true) { while(true) {
piece = downloaded.getRandomPiece() piece = downloaded.getRandomPiece()
if (piece == -1)
return false
if (claimed.isMarked(piece)) { if (claimed.isMarked(piece)) {
if (downloaded.donePieces() + claimed.donePieces() == downloaded.nPieces) { if (downloaded.donePieces() + claimed.donePieces() == downloaded.nPieces) {
log.info("all pieces claimed") log.info("all pieces claimed")
@ -85,7 +87,6 @@ class DownloadSession {
String root = Base64.encode(infoHash.getRoot()) String root = Base64.encode(infoHash.getRoot())
FileChannel channel
try { try {
os.write("GET $root\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("GET $root\r\n".getBytes(StandardCharsets.US_ASCII))
os.write("Range: $start-$end\r\n".getBytes(StandardCharsets.US_ASCII)) os.write("Range: $start-$end\r\n".getBytes(StandardCharsets.US_ASCII))
@ -135,6 +136,8 @@ class DownloadSession {
} }
// start the download // start the download
FileChannel channel
try {
channel = Files.newByteChannel(file.toPath(), EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, channel = Files.newByteChannel(file.toPath(), EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE,
StandardOpenOption.SPARSE, StandardOpenOption.CREATE)) // TODO: double-check, maybe CREATE_NEW StandardOpenOption.SPARSE, StandardOpenOption.CREATE)) // TODO: double-check, maybe CREATE_NEW
mapped = channel.map(FileChannel.MapMode.READ_WRITE, start, end - start + 1) mapped = channel.map(FileChannel.MapMode.READ_WRITE, start, end - start + 1)
@ -165,11 +168,12 @@ class DownloadSession {
System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32) System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32)
if (hash != expected) if (hash != expected)
throw new BadHashException() throw new BadHashException()
} finally {
try { channel?.close() } catch (IOException ignore) {}
}
downloaded.markDownloaded(piece) downloaded.markDownloaded(piece)
} finally { } finally {
claimed.clear(piece) claimed.clear(piece)
try { channel?.close() } catch (IOException ignore) {}
} }
return true return true
} }