]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix bytesLeft for files (#348)
authori96751414 <i96751414@gmail.com>
Sat, 30 Nov 2019 08:53:35 +0000 (08:53 +0000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 30 Nov 2019 08:53:35 +0000 (19:53 +1100)
* Refactor (*File).bytesLeft

* Always use the same piece size

file.go

diff --git a/file.go b/file.go
index 5c6d8ebea27e6cb5ff8b0f4d4fcbf0731a204ea4..2f0e6b6056c5a2e464b656dc4c5fe4ce51e0a1af 100644 (file)
--- a/file.go
+++ b/file.go
@@ -6,7 +6,6 @@ import (
        "github.com/anacrolix/missinggo/bitmap"
 
        "github.com/anacrolix/torrent/metainfo"
-       pp "github.com/anacrolix/torrent/peer_protocol"
 )
 
 // Provides access to regions of torrent data that correspond to its files.
@@ -56,30 +55,24 @@ func (f *File) bytesCompleted() int64 {
 }
 
 func (f *File) bytesLeft() (left int64) {
+       pieceSize := int64(f.t.usualPieceSize())
        firstPieceIndex := f.firstPieceIndex()
-       endPieceIndex := f.endPieceIndex()
-       bitmap.Flip(f.t.completedPieces, firstPieceIndex, endPieceIndex+1).IterTyped(func(piece int) bool {
-               p := &f.t.pieces[piece]
-               left += int64(p.length() - p.numDirtyBytes())
-               return true
-       })
-       startPiece := f.t.piece(firstPieceIndex)
-       endChunk := int(f.offset%f.t.info.PieceLength) * int(startPiece.numChunks()) / int(startPiece.length())
-       bitmap.Flip(startPiece.dirtyChunks, 0, endChunk).IterTyped(func(chunk int) bool {
-               left -= int64(startPiece.chunkSize())
-               return true
-       })
-       endPiece := f.t.piece(endPieceIndex)
-       startChunk := int((f.offset+f.length)%f.t.info.PieceLength) * int(endPiece.numChunks()) / int(endPiece.length())
-       lastChunkIndex := int(endPiece.lastChunkIndex())
-       bitmap.Flip(endPiece.dirtyChunks, startChunk, int(endPiece.numChunks())).IterTyped(func(chunk int) bool {
-               if chunk == lastChunkIndex {
-                       left -= int64(endPiece.chunkIndexSpec(pp.Integer(chunk)).Length)
-               } else {
-                       left -= int64(endPiece.chunkSize())
+       endPieceIndex := f.endPieceIndex() - 1
+       bitmap.Flip(f.t.completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
+               if piece >= endPieceIndex {
+                       return false
+               }
+               if piece > firstPieceIndex {
+                       left += pieceSize
                }
                return true
        })
+       if !f.t.pieceComplete(firstPieceIndex) {
+               left += pieceSize - (f.offset % pieceSize)
+       }
+       if !f.t.pieceComplete(endPieceIndex) {
+               left += (f.offset + f.length) % pieceSize
+       }
        return
 }