]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Some changes to metainfo.InfoEx and testutil
authorMatt Joiner <anacrolix@gmail.com>
Sat, 30 Apr 2016 01:07:29 +0000 (11:07 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 30 Apr 2016 01:07:29 +0000 (11:07 +1000)
fs/torrentfs_test.go
internal/testutil/testutil.go
metainfo/metainfo.go
torrent.go

index 187bb12ed41518eb85729e30c585186b53cb7592..688c7ac6996de10ed4d0a00dd34d18266f5bd5bc 100644 (file)
@@ -1,7 +1,6 @@
 package torrentfs
 
 import (
-       "bytes"
        "fmt"
        "io/ioutil"
        "log"
@@ -73,10 +72,8 @@ func newGreetingLayout() (tl testLayout, err error) {
        os.Mkdir(tl.Completed, 0777)
        tl.MountDir = filepath.Join(tl.BaseDir, "mnt")
        os.Mkdir(tl.MountDir, 0777)
-       name := testutil.CreateDummyTorrentData(tl.Completed)
-       metaInfoBuf := &bytes.Buffer{}
-       testutil.CreateMetaInfo(name, metaInfoBuf)
-       tl.Metainfo, err = metainfo.Load(metaInfoBuf)
+       testutil.CreateDummyTorrentData(tl.Completed)
+       tl.Metainfo = testutil.GreetingMetaInfo()
        return
 }
 
index b43ddffa64f6064b385bff03ba06b5260d41dcb0..e9d21039dc1af653a9483d22cb94b0580266d0d9 100644 (file)
@@ -6,20 +6,25 @@
 package testutil
 
 import (
-       "bytes"
+       "crypto/sha1"
        "fmt"
        "io"
        "io/ioutil"
        "net/http"
        "os"
        "path/filepath"
+       "strings"
 
        "github.com/anacrolix/missinggo"
 
+       "github.com/anacrolix/torrent/bencode"
        "github.com/anacrolix/torrent/metainfo"
 )
 
-const GreetingFileContents = "hello, world\n"
+const (
+       GreetingFileContents = "hello, world\n"
+       GreetingFileName     = "greeting"
+)
 
 func CreateDummyTorrentData(dirName string) string {
        f, _ := os.Create(filepath.Join(dirName, "greeting"))
@@ -27,25 +32,23 @@ func CreateDummyTorrentData(dirName string) string {
        f.WriteString(GreetingFileContents)
        return f.Name()
 }
-
-// Writes to w, a metainfo containing the file at name.
-func CreateMetaInfo(name string, w io.Writer) {
-       var mi metainfo.MetaInfo
-       mi.Info.Name = filepath.Base(name)
-       fi, _ := os.Stat(name)
-       mi.Info.Length = fi.Size()
+func GreetingMetaInfo() (mi *metainfo.MetaInfo) {
+       mi = new(metainfo.MetaInfo)
+       mi.Info.Name = GreetingFileName
+       mi.Info.Length = int64(len(GreetingFileContents))
        mi.Announce = "lol://cheezburger"
        mi.Info.PieceLength = 5
        err := mi.Info.GeneratePieces(func(metainfo.FileInfo) (io.ReadCloser, error) {
-               return os.Open(name)
+               return ioutil.NopCloser(strings.NewReader(GreetingFileContents)), nil
        })
        if err != nil {
                panic(err)
        }
-       err = mi.Write(w)
-       if err != nil {
-               panic(err)
-       }
+       mi.Info.Bytes, _ = bencode.Marshal(&mi.Info.Info)
+       h := sha1.New()
+       h.Write(mi.Info.Bytes)
+       missinggo.CopyExact(&mi.Info.Hash, h.Sum(nil))
+       return
 }
 
 // Gives a temporary directory containing the completed "greeting" torrent,
@@ -56,10 +59,8 @@ func GreetingTestTorrent() (tempDir string, metaInfo *metainfo.MetaInfo) {
        if err != nil {
                panic(err)
        }
-       name := CreateDummyTorrentData(tempDir)
-       w := &bytes.Buffer{}
-       CreateMetaInfo(name, w)
-       metaInfo, _ = metainfo.Load(w)
+       CreateDummyTorrentData(tempDir)
+       metaInfo = GreetingMetaInfo()
        return
 }
 
index 3b2a04a2ff43b20a68d360f9c79ebca692be2a61..d02bf3ca5b5558f3b89af4670f1f94845663a606 100644 (file)
@@ -183,8 +183,8 @@ func (info *Info) UpvertedFiles() []FileInfo {
 // important to Bittorrent.
 type InfoEx struct {
        Info
-       Hash  *Hash
-       Bytes []byte
+       Hash  Hash   // Only set when unmarshalling or UpdateHash.
+       Bytes []byte // Only set when unmarshalling or UpdateBytes.
 }
 
 var (
@@ -193,21 +193,17 @@ var (
 )
 
 func (ie *InfoEx) UnmarshalBencode(data []byte) error {
-       ie.Bytes = append(make([]byte, 0, len(data)), data...)
+       ie.Bytes = append([]byte(nil), data...)
        h := sha1.New()
        _, err := h.Write(ie.Bytes)
        if err != nil {
                panic(err)
        }
-       ie.Hash = new(Hash)
-       missinggo.CopyExact(ie.Hash, h.Sum(nil))
+       missinggo.CopyExact(&ie.Hash, h.Sum(nil))
        return bencode.Unmarshal(data, &ie.Info)
 }
 
 func (ie InfoEx) MarshalBencode() ([]byte, error) {
-       if ie.Bytes != nil {
-               return ie.Bytes, nil
-       }
        return bencode.Marshal(&ie.Info)
 }
 
@@ -244,6 +240,6 @@ func (mi *MetaInfo) Magnet() (m Magnet) {
                }
        }
        m.DisplayName = mi.Info.Name
-       m.InfoHash = *mi.Info.Hash
+       m.InfoHash = mi.Info.Hash
        return
 }
index 849be74ba261c06bae73c5608bb472b9d49317bb..834b315d660d63bd3dd94b3badd49cf8252ad4ea 100644 (file)
@@ -224,7 +224,7 @@ func (t *Torrent) setMetadata(md *metainfo.Info, infoBytes []byte) (err error) {
        t.info = &metainfo.InfoEx{
                Info:  *md,
                Bytes: infoBytes,
-               Hash:  &t.infoHash,
+               Hash:  t.infoHash,
        }
        t.storage, err = t.storageOpener.OpenTorrent(t.info)
        if err != nil {