]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Get infohash selection working when adding v2 torrents
authorMatt Joiner <anacrolix@gmail.com>
Tue, 27 Feb 2024 12:30:53 +0000 (23:30 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 2 Mar 2024 02:02:37 +0000 (13:02 +1100)
spec.go
torrent.go

diff --git a/spec.go b/spec.go
index f1ef584ea78e1bdc66664edcdfbf6d7b5fc9409a..29d30adc9fe8ffb1db4beb1884996c2127db31cc 100644 (file)
--- a/spec.go
+++ b/spec.go
@@ -69,13 +69,18 @@ func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
        if err != nil {
                err = fmt.Errorf("unmarshalling info: %w", err)
        }
+       v1Ih := mi.HashInfoBytes()
        var v2Infohash g.Option[infohash_v2.T]
        if info.HasV2() {
                v2Infohash.Set(infohash_v2.HashBytes(mi.InfoBytes))
+               if !info.HasV1() {
+                       v1Ih = v2Infohash.Value.ToShort()
+               }
        }
+
        return &TorrentSpec{
                Trackers:    mi.UpvertedAnnounceList(),
-               InfoHash:    mi.HashInfoBytes(),
+               InfoHash:    v1Ih,
                InfoHashV2:  v2Infohash,
                PieceLayers: mi.PieceLayers,
                InfoBytes:   mi.InfoBytes,
index e2afb3e3f2513c495018234b6edb3f810c9118c1..8416e4ba0935a7f2a8093c84844242b02271fc36 100644 (file)
@@ -8,6 +8,7 @@ import (
        "errors"
        "fmt"
        "github.com/anacrolix/torrent/merkle"
+       "github.com/anacrolix/torrent/types/infohash"
        infohash_v2 "github.com/anacrolix/torrent/types/infohash-v2"
        "io"
        "math/rand"
@@ -538,13 +539,19 @@ func (t *Torrent) onSetInfo() {
 
 // Called when metadata for a torrent becomes available.
 func (t *Torrent) setInfoBytesLocked(b []byte) error {
-       if metainfo.HashBytes(b) != t.infoHash {
+       v2Hash := infohash_v2.HashBytes(b)
+       v1Hash := infohash.HashBytes(b)
+       v2Short := v2Hash.ToShort()
+       if v2Short != t.infoHash && v1Hash != t.infoHash {
                return errors.New("info bytes have wrong hash")
        }
        var info metainfo.Info
        if err := bencode.Unmarshal(b, &info); err != nil {
                return fmt.Errorf("error unmarshalling info bytes: %s", err)
        }
+       if info.HasV2() {
+               t.infoHashV2.Set(v2Hash)
+       }
        t.metadataBytes = b
        t.metadataCompletedChunks = nil
        if t.info != nil {