From: Matt Joiner Date: Tue, 27 Oct 2020 01:24:43 +0000 (+1100) Subject: Read from more than a single piece in each read to Torrent storage X-Git-Tag: v1.19.0~62 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=1bfca56e9452e7883aa2a6669b920573ec499b1c;p=btrtrc.git Read from more than a single piece in each read to Torrent storage --- diff --git a/reader.go b/reader.go index 7e00d4ea..8a74873b 100644 --- a/reader.go +++ b/reader.go @@ -204,6 +204,8 @@ func (r *reader) waitAvailable(pos, wanted int64, ctxErr *error) (avail int64, e } } +// 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 } @@ -220,10 +222,9 @@ func (r *reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err erro 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 @@ -233,9 +234,9 @@ func (r *reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err erro // 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() } diff --git a/torrent.go b/torrent.go index e034933f..ace386eb 100644 --- a/torrent.go +++ b/torrent.go @@ -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