package torrentfs
import (
- "bytes"
"fmt"
"io/ioutil"
"log"
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
}
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"))
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,
if err != nil {
panic(err)
}
- name := CreateDummyTorrentData(tempDir)
- w := &bytes.Buffer{}
- CreateMetaInfo(name, w)
- metaInfo, _ = metainfo.Load(w)
+ CreateDummyTorrentData(tempDir)
+ metaInfo = GreetingMetaInfo()
return
}
// 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 (
)
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)
}
}
}
m.DisplayName = mi.Info.Name
- m.InfoHash = *mi.Info.Hash
+ m.InfoHash = mi.Info.Hash
return
}