}
}
+// Adds the reader's torrent offset to the reader object offset (for example the reader might be
+// constrainted to a particular file within the torrent).
func (r *reader) torrentOffset(readerPos int64) int64 {
return r.offset + readerPos
}
if avail == 0 {
return
}
- pi := pieceIndex(r.torrentOffset(pos) / r.t.info.PieceLength)
- ip := r.t.info.Piece(pi)
- po := r.torrentOffset(pos) % r.t.info.PieceLength
- b1 := missinggo.LimitLen(b, ip.Length()-po, avail)
+ firstPieceIndex := pieceIndex(r.torrentOffset(pos) / r.t.info.PieceLength)
+ firstPieceOffset := r.torrentOffset(pos) % r.t.info.PieceLength
+ b1 := missinggo.LimitLen(b, avail)
n, err = r.t.readAt(b1, r.torrentOffset(pos))
if n != 0 {
err = nil
// TODO: Just reset pieces in the readahead window. This might help
// prevent thrashing with small caches and file and piece priorities.
r.log(log.Fstr("error reading torrent %s piece %d offset %d, %d bytes: %v",
- r.t.infoHash.HexString(), pi, po, len(b1), err))
- if !r.t.updatePieceCompletion(pi) {
- r.log(log.Fstr("piece %d completion unchanged", pi))
+ r.t.infoHash.HexString(), firstPieceIndex, firstPieceOffset, len(b1), err))
+ if !r.t.updatePieceCompletion(firstPieceIndex) {
+ r.log(log.Fstr("piece %d completion unchanged", firstPieceIndex))
}
r.t.cl.unlock()
}
// 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