client.go | 4 ++-- t.go | 4 ++-- torrent.go | 18 +++++++++++------- diff --git a/client.go b/client.go index d8b1706478db3e8f7b165008e5af16adb188cc5f..28dbc8ed4466853a9be6abcc76276beab6d1ba3e 100644 --- a/client.go +++ b/client.go @@ -166,8 +166,8 @@ fmt.Fprintf( w, "%f%% of %d bytes (%s)", 100*(1-float64(t.bytesMissingLocked())/float64(t.info.TotalLength())), - *t.length, - humanize.Bytes(uint64(*t.length))) + t.length(), + humanize.Bytes(uint64(t.length()))) } else { w.WriteString("") } diff --git a/t.go b/t.go index 324f761f1965bfc97298d5990c69b925740141cb..a20e02c869f50453da296df548795d29ae909150 100644 --- a/t.go +++ b/t.go @@ -32,7 +32,7 @@ // Returns a Reader bound to the torrent's data. All read calls block until the data requested is // actually available. Note that you probably want to ensure the Torrent Info is available first. func (t *Torrent) NewReader() Reader { - return t.newReader(0, *t.length) + return t.newReader(0, t.length()) } func (t *Torrent) newReader(offset, length int64) Reader { @@ -147,7 +147,7 @@ // The completed length of all the torrent data, in all its files. This is // derived from the torrent info, when it is available. func (t *Torrent) Length() int64 { - return *t.length + return t._length.Value() } // Returns a run-time generated metainfo for the torrent that includes the diff --git a/torrent.go b/torrent.go index bd7b75738471913dc9ddb2da784f82b71f814800..cc9458d1388dc654beda49274ea61500087aef8c 100644 --- a/torrent.go +++ b/torrent.go @@ -74,7 +74,7 @@ chunkSize pp.Integer chunkPool sync.Pool // Total length of the torrent in bytes. Stored because it's not O(1) to // get this from the info dict. - length *int64 + _length Option[int64] // The storage to open when the info dict becomes available. storageOpener *storage.Client @@ -165,6 +165,10 @@ // Large allocations reused between request state updates. requestPieceStates []request_strategy.PieceRequestOrderState requestIndexes []RequestIndex +} + +func (t *Torrent) length() int64 { + return t._length.Value() } func (t *Torrent) selectivePieceAvailabilityFromPeers(i pieceIndex) (count int) { @@ -420,7 +424,7 @@ var l int64 for _, f := range t.info.UpvertedFiles() { l += f.Length } - t.length = &l + t._length = Some(l) } // TODO: This shouldn't fail for storage reasons. Instead we should handle storage failure @@ -895,13 +899,13 @@ return } func (t *Torrent) requestOffset(r Request) int64 { - return torrentRequestOffset(*t.length, int64(t.usualPieceSize()), r) + return torrentRequestOffset(t.length(), int64(t.usualPieceSize()), r) } // Return the request that would include the given offset into the torrent data. Returns !ok if // there is no such request. func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) { - return torrentOffsetRequest(*t.length, t.info.PieceLength, int64(t.chunkSize), off) + return torrentOffsetRequest(t.length(), t.info.PieceLength, int64(t.chunkSize), off) } func (t *Torrent) writeChunk(piece int, begin int64, data []byte) (err error) { @@ -949,7 +953,7 @@ // There will be no variance amongst pieces. Only pain. return 0 } if piece == t.numPieces()-1 { - ret := pp.Integer(*t.length % t.info.PieceLength) + ret := pp.Integer(t.length() % t.info.PieceLength) if ret != 0 { return ret } @@ -1225,7 +1229,7 @@ } // Returns the range of pieces [begin, end) that contains the extent of bytes. func (t *Torrent) byteRegionPieces(off, size int64) (begin, end pieceIndex) { - if off >= *t.length { + if off >= t.length() { return } if off < 0 { @@ -1442,7 +1446,7 @@ func (t *Torrent) bytesCompleted() int64 { if !t.haveInfo() { return 0 } - return *t.length - t.bytesLeft() + return t.length() - t.bytesLeft() } func (t *Torrent) SetInfoBytes(b []byte) (err error) {