From 28c9ec2bd11925f0d432ee601c891ef234f8aaec Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 30 Apr 2016 11:07:29 +1000 Subject: [PATCH] Some changes to metainfo.InfoEx and testutil --- fs/torrentfs_test.go | 7 ++----- internal/testutil/testutil.go | 37 ++++++++++++++++++----------------- metainfo/metainfo.go | 14 +++++-------- torrent.go | 2 +- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 187bb12e..688c7ac6 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -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 } diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index b43ddffa..e9d21039 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -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 } diff --git a/metainfo/metainfo.go b/metainfo/metainfo.go index 3b2a04a2..d02bf3ca 100644 --- a/metainfo/metainfo.go +++ b/metainfo/metainfo.go @@ -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 } diff --git a/torrent.go b/torrent.go index 849be74b..834b315d 100644 --- a/torrent.go +++ b/torrent.go @@ -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 { -- 2.48.1