]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use Option for cached Torrent length v1.43.1
authorMatt Joiner <anacrolix@gmail.com>
Thu, 12 May 2022 03:47:12 +0000 (13:47 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 12 May 2022 03:47:12 +0000 (13:47 +1000)
Fixes https://github.com/anacrolix/torrent/issues/630.

client.go
t.go
torrent.go

index d8b1706478db3e8f7b165008e5af16adb188cc5f..28dbc8ed4466853a9be6abcc76276beab6d1ba3e 100644 (file)
--- a/client.go
+++ b/client.go
@@ -166,8 +166,8 @@ func (cl *Client) WriteStatus(_w io.Writer) {
                                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("<missing metainfo>")
                }
diff --git a/t.go b/t.go
index 324f761f1965bfc97298d5990c69b925740141cb..a20e02c869f50453da296df548795d29ae909150 100644 (file)
--- a/t.go
+++ b/t.go
@@ -32,7 +32,7 @@ func (t *Torrent) Info() (info *metainfo.Info) {
 // 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 @@ func (t *Torrent) Name() string {
 // 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
index bd7b75738471913dc9ddb2da784f82b71f814800..cc9458d1388dc654beda49274ea61500087aef8c 100644 (file)
@@ -74,7 +74,7 @@ type Torrent struct {
        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
@@ -167,6 +167,10 @@ type Torrent struct {
        requestIndexes     []RequestIndex
 }
 
+func (t *Torrent) length() int64 {
+       return t._length.Value()
+}
+
 func (t *Torrent) selectivePieceAvailabilityFromPeers(i pieceIndex) (count int) {
        // This could be done with roaring.BitSliceIndexing.
        t.iterPeers(func(peer *Peer) {
@@ -420,7 +424,7 @@ func (t *Torrent) cacheLength() {
        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 @@ func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
 }
 
 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 @@ func (t *Torrent) pieceLength(piece pieceIndex) pp.Integer {
                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 @@ func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) {
 
 // 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) {