10 "github.com/anacrolix/log"
11 "github.com/anacrolix/missinggo/v2"
14 // Accesses Torrent data via a Client. Reads block until the data is available. Seeks and readahead
15 // also drive Client behaviour.
16 type Reader interface {
18 missinggo.ReadContexter
19 // Configure the number of bytes ahead of a read that should also be prioritized in preparation
20 // for further reads. Overridden by non-nil readahead func, see SetReadaheadFunc.
22 // If non-nil, the provided function is called when the implementation needs to know the
23 // readahead for the current reader. Calls occur during Reads and Seeks, and while the Client is
25 SetReadaheadFunc(ReadaheadFunc)
26 // Don't wait for pieces to complete and be verified. Read calls return as soon as they can when
27 // the underlying chunks become available.
31 // Piece range by piece index, [begin, end).
32 type pieceRange struct {
36 type ReadaheadContext struct {
37 ContiguousReadStartPos int64
41 // Returns the desired readahead for a Reader.
42 type ReadaheadFunc func(ReadaheadContext) int64
46 // Adjust the read/seek window to handle Readers locked to File extents and the like.
49 // Function to dynamically calculate readahead. If nil, readahead is static.
50 readaheadFunc ReadaheadFunc
52 // Required when modifying pos and readahead.
56 // Position that reads have continued contiguously from.
57 contiguousReadStartPos int64
58 // The cached piece range this reader wants downloaded. The zero value corresponds to nothing.
59 // We cache this so that changes can be detected, and bubbled up to the Torrent only as
63 // Reads have been initiated since the last seek. This is used to prevent readaheads occurring
64 // after a seek or with a new reader at the starting position.
69 var _ io.ReadSeekCloser = (*reader)(nil)
71 func (r *reader) SetResponsive() {
73 r.t.cl.event.Broadcast()
76 // Disable responsive mode. TODO: Remove?
77 func (r *reader) SetNonResponsive() {
79 r.t.cl.event.Broadcast()
82 func (r *reader) SetReadahead(readahead int64) {
84 r.readahead = readahead
90 func (r *reader) SetReadaheadFunc(f ReadaheadFunc) {
97 // How many bytes are available to read. Max is the most we could require.
98 func (r *reader) available(off, max int64) (ret int64) {
101 req, ok := r.t.offsetRequest(off)
105 if !r.responsive && !r.t.pieceComplete(pieceIndex(req.Index)) {
108 if !r.t.haveChunk(req) {
111 len1 := int64(req.Length) - (off - r.t.requestOffset(req))
116 // Ensure that ret hasn't exceeded our original max.
123 // Calculates the pieces this reader wants downloaded, ignoring the cached value at r.pieces.
124 func (r *reader) piecesUncached() (ret pieceRange) {
126 if r.readaheadFunc != nil {
127 ra = r.readaheadFunc(ReadaheadContext{
128 ContiguousReadStartPos: r.contiguousReadStartPos,
133 // Needs to be at least 1, because [x, x) means we don't want
140 if ra > r.length-r.pos {
141 ra = r.length - r.pos
143 ret.begin, ret.end = r.t.byteRegionPieces(r.torrentOffset(r.pos), ra)
147 func (r *reader) Read(b []byte) (n int, err error) {
148 return r.ReadContext(context.Background(), b)
151 func (r *reader) ReadContext(ctx context.Context, b []byte) (n int, err error) {
154 // TODO: Rework reader piece priorities so we don't have to push updates in to the Client
155 // and take the lock here.
160 n, err = r.readOnceAt(ctx, b, r.pos)
162 if err == nil && len(b) > 0 {
163 panic("expected error")
173 if r.pos >= r.length {
175 } else if err == io.EOF {
176 err = io.ErrUnexpectedEOF
181 var closedChan = make(chan struct{})
187 // Wait until some data should be available to read. Tickles the client if it isn't. Returns how
188 // much should be readable without blocking.
189 func (r *reader) waitAvailable(ctx context.Context, pos, wanted int64, wait bool) (avail int64, err error) {
193 avail = r.available(pos, wanted)
194 readerCond := t.piece(int((r.offset + pos) / t.info.PieceLength)).readerCond.Signaled()
199 var dontWait <-chan struct{}
200 if !wait || wanted == 0 {
201 dontWait = closedChan
204 case <-r.t.closed.Done():
205 err = errors.New("torrent closed")
210 case <-r.t.dataDownloadDisallowed.On():
211 err = errors.New("torrent data downloading disabled")
212 case <-r.t.networkingEnabled.Off():
213 err = errors.New("torrent networking disabled")
222 // Adds the reader's torrent offset to the reader object offset (for example the reader might be
223 // constrainted to a particular file within the torrent).
224 func (r *reader) torrentOffset(readerPos int64) int64 {
225 return r.offset + readerPos
228 // Performs at most one successful read to torrent storage.
229 func (r *reader) readOnceAt(ctx context.Context, b []byte, pos int64) (n int, err error) {
236 avail, err = r.waitAvailable(ctx, pos, int64(len(b)), n == 0)
240 firstPieceIndex := pieceIndex(r.torrentOffset(pos) / r.t.info.PieceLength)
241 firstPieceOffset := r.torrentOffset(pos) % r.t.info.PieceLength
242 b1 := missinggo.LimitLen(b, avail)
243 n, err = r.t.readAt(b1, r.torrentOffset(pos))
248 if r.t.closed.IsSet() {
249 err = fmt.Errorf("reading from closed torrent: %w", err)
253 // I think there's a panic here caused by the Client being closed before obtaining this
254 // lock. TestDropTorrentWithMmapStorageWhileHashing seems to tickle occasionally in CI.
256 // Just add exceptions already.
257 defer r.t.cl.unlock()
258 if r.t.closed.IsSet() {
259 // Can't update because Torrent's piece order is removed from Client.
262 // TODO: Just reset pieces in the readahead window. This might help
263 // prevent thrashing with small caches and file and piece priorities.
264 r.log(log.Fstr("error reading torrent %s piece %d offset %d, %d bytes: %v",
265 r.t.infoHash.HexString(), firstPieceIndex, firstPieceOffset, len(b1), err))
266 if !r.t.updatePieceCompletion(firstPieceIndex) {
267 r.log(log.Fstr("piece %d completion unchanged", firstPieceIndex))
269 // Update the rest of the piece completions in the readahead window, without alerting to
270 // changes (since only the first piece, the one above, could have generated the read error
271 // we're currently handling).
272 if r.pieces.begin != firstPieceIndex {
273 panic(fmt.Sprint(r.pieces.begin, firstPieceIndex))
275 for index := r.pieces.begin + 1; index < r.pieces.end; index++ {
276 r.t.updatePieceCompletion(index)
283 func (r *reader) Close() error {
290 func (r *reader) posChanged() {
291 to := r.piecesUncached()
297 // log.Printf("reader pos changed %v->%v", from, to)
298 r.t.readerPosChanged(from, to)
301 func (r *reader) Seek(off int64, whence int) (newPos int64, err error) {
310 newPos = r.length + off
313 return 0, errors.New("bad whence")
318 r.contiguousReadStartPos = newPos
325 func (r *reader) log(m log.Msg) {
326 r.t.logger.LogLevel(log.Debug, m.Skip(1))
329 // Implementation inspired by https://news.ycombinator.com/item?id=27019613.
330 func defaultReadaheadFunc(r ReadaheadContext) int64 {
331 return r.CurrentPos - r.ContiguousReadStartPos