From 0388ec1eeee8ab3a90e8730a4266f4be620d30de Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 29 Aug 2017 15:16:53 +1000 Subject: [PATCH] Synchronize Torrent.Info --- client.go | 4 ++-- reader.go | 6 +++--- t.go | 4 ++-- torrent.go | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 035af50d..d81697b8 100644 --- a/client.go +++ b/client.go @@ -141,8 +141,8 @@ func (cl *Client) WriteStatus(_w io.Writer) { fmt.Fprint(w, t.name()) } fmt.Fprint(w, "\n") - if t.Info() != nil { - fmt.Fprintf(w, "%f%% of %d bytes (%s)", 100*(1-float64(t.bytesMissingLocked())/float64(t.Info().TotalLength())), t.length, humanize.Bytes(uint64(t.Info().TotalLength()))) + if t.info != nil { + fmt.Fprintf(w, "%f%% of %d bytes (%s)", 100*(1-float64(t.bytesMissingLocked())/float64(t.info.TotalLength())), t.length, humanize.Bytes(uint64(t.info.TotalLength()))) } else { w.WriteString("") } diff --git a/reader.go b/reader.go index 7da3b48a..5afab9aa 100644 --- a/reader.go +++ b/reader.go @@ -195,9 +195,9 @@ func (r *Reader) readOnceAt(b []byte, pos int64, ctxErr *error) (n int, err erro } } b1 := b[:avail] - pi := int(pos / r.t.Info().PieceLength) - ip := r.t.Info().Piece(pi) - po := pos % r.t.Info().PieceLength + pi := int(pos / r.t.info.PieceLength) + ip := r.t.info.Piece(pi) + po := pos % r.t.info.PieceLength missinggo.LimitLen(&b1, ip.Length()-po) n, err = r.t.readAt(b1, pos) if n != 0 { diff --git a/t.go b/t.go index abd9d354..13722d8a 100644 --- a/t.go +++ b/t.go @@ -24,6 +24,8 @@ func (t *Torrent) GotInfo() <-chan struct{} { // Returns the metainfo info dictionary, or nil if it's not yet available. func (t *Torrent) Info() *metainfo.Info { + t.cl.mu.Lock() + defer t.cl.mu.Unlock() return t.info } @@ -161,9 +163,7 @@ func (t *Torrent) CancelPieces(begin, end int) { // Returns handles to the files in the torrent. This requires the metainfo is // available first. func (t *Torrent) Files() (ret []File) { - t.cl.mu.Lock() info := t.Info() - t.cl.mu.Unlock() if info == nil { return } diff --git a/torrent.go b/torrent.go index c2be1f06..eb4f7a7d 100644 --- a/torrent.go +++ b/torrent.go @@ -442,7 +442,7 @@ func (t *Torrent) writeStatus(w io.Writer) { return "?" } }()) - if t.Info() != nil { + if t.info != nil { fmt.Fprintf(w, "Num Pieces: %d\n", t.numPieces()) fmt.Fprint(w, "Piece States:") for _, psr := range t.pieceStateRuns() { -- 2.48.1