]> Sergey Matveev's repositories - btrtrc.git/blobdiff - torrent.go
Read from more than a single piece in each read to Torrent storage
[btrtrc.git] / torrent.go
index e034933fba17bd9f1c18ec0f8f8afb85515155d6..ace386eb3fce410cde0a57bf9742fea57e21ee0e 100644 (file)
@@ -1147,9 +1147,19 @@ func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
 
 // Non-blocking read. Client lock is not required.
 func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
-       p := &t.pieces[off/t.info.PieceLength]
-       p.waitNoPendingWrites()
-       return p.Storage().ReadAt(b, off-p.Info().Offset())
+       for len(b) != 0 {
+               p := &t.pieces[off/t.info.PieceLength]
+               p.waitNoPendingWrites()
+               var n1 int
+               n1, err = p.Storage().ReadAt(b, off-p.Info().Offset())
+               if n1 == 0 {
+                       break
+               }
+               off += int64(n1)
+               n += n1
+               b = b[n1:]
+       }
+       return
 }
 
 // Returns an error if the metadata was completed, but couldn't be set for