2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2015-10-29 22:21:09 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2012-06-28 04:26:56 +08:00
|
|
|
"io"
|
2015-10-29 22:21:09 +08:00
|
|
|
"log"
|
2012-06-28 04:26:56 +08:00
|
|
|
"os"
|
2015-10-29 22:21:09 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-12-02 13:28:22 +08:00
|
|
|
|
2016-03-28 17:38:30 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2012-06-28 04:26:56 +08:00
|
|
|
)
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
// Information specific to a single file inside the MetaInfo structure.
|
2012-06-28 04:26:56 +08:00
|
|
|
type FileInfo struct {
|
2014-06-28 17:40:39 +08:00
|
|
|
Length int64 `bencode:"length"`
|
|
|
|
Path []string `bencode:"path"`
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
|
|
|
|
// failure.
|
|
|
|
func Load(r io.Reader) (*MetaInfo, error) {
|
|
|
|
var mi MetaInfo
|
2012-06-28 04:26:56 +08:00
|
|
|
d := bencode.NewDecoder(r)
|
2014-07-13 15:36:06 +08:00
|
|
|
err := d.Decode(&mi)
|
2012-06-28 04:26:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-07-02 03:05:10 +08:00
|
|
|
return &mi, nil
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// Convenience function for loading a MetaInfo from a file.
|
|
|
|
func LoadFromFile(filename string) (*MetaInfo, error) {
|
2012-06-28 04:26:56 +08:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2012-06-30 06:23:02 +08:00
|
|
|
return Load(f)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
// The info dictionary.
|
2014-06-28 17:40:39 +08:00
|
|
|
type Info struct {
|
|
|
|
PieceLength int64 `bencode:"piece length"`
|
|
|
|
Pieces []byte `bencode:"pieces"`
|
|
|
|
Name string `bencode:"name"`
|
|
|
|
Length int64 `bencode:"length,omitempty"`
|
2016-04-30 09:59:10 +08:00
|
|
|
Private *bool `bencode:"private,omitempty"`
|
2014-06-28 17:40:39 +08:00
|
|
|
Files []FileInfo `bencode:"files,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:21:09 +08:00
|
|
|
func (info *Info) BuildFromFilePath(root string) (err error) {
|
|
|
|
info.Name = filepath.Base(root)
|
|
|
|
info.Files = nil
|
|
|
|
err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
log.Println(path, root, err)
|
|
|
|
if fi.IsDir() {
|
|
|
|
// Directories are implicit in torrent files.
|
|
|
|
return nil
|
|
|
|
} else if path == root {
|
|
|
|
// The root is a file.
|
|
|
|
info.Length = fi.Size()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
relPath, err := filepath.Rel(root, path)
|
|
|
|
log.Println(relPath, err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting relative path: %s", err)
|
|
|
|
}
|
|
|
|
info.Files = append(info.Files, FileInfo{
|
|
|
|
Path: strings.Split(relPath, string(filepath.Separator)),
|
|
|
|
Length: fi.Size(),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
|
|
|
|
return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error generating pieces: %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
|
|
|
|
for _, fi := range info.UpvertedFiles() {
|
|
|
|
r, err := open(fi)
|
|
|
|
if err != nil {
|
2016-02-01 21:44:29 +08:00
|
|
|
return fmt.Errorf("error opening %v: %s", fi, err)
|
2015-10-29 22:21:09 +08:00
|
|
|
}
|
|
|
|
wn, err := io.CopyN(w, r, fi.Length)
|
|
|
|
r.Close()
|
|
|
|
if wn != fi.Length || err != nil {
|
2016-02-01 21:44:29 +08:00
|
|
|
return fmt.Errorf("error hashing %v: %s", fi, err)
|
2015-10-29 22:21:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set info.Pieces by hashing info.Files.
|
|
|
|
func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
|
|
|
|
if info.PieceLength == 0 {
|
|
|
|
return errors.New("piece length must be non-zero")
|
|
|
|
}
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
err := info.writeFiles(pw, open)
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
}()
|
|
|
|
defer pr.Close()
|
|
|
|
var pieces []byte
|
|
|
|
for {
|
|
|
|
hasher := sha1.New()
|
|
|
|
wn, err := io.CopyN(hasher, pr, info.PieceLength)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if wn == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pieces = hasher.Sum(pieces)
|
|
|
|
if wn < info.PieceLength {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info.Pieces = pieces
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (info *Info) TotalLength() (ret int64) {
|
|
|
|
if info.IsDir() {
|
|
|
|
for _, fi := range info.Files {
|
2015-02-26 22:41:35 +08:00
|
|
|
ret += fi.Length
|
|
|
|
}
|
|
|
|
} else {
|
2016-04-19 12:11:11 +08:00
|
|
|
ret = info.Length
|
2015-02-26 22:41:35 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (info *Info) NumPieces() int {
|
|
|
|
if len(info.Pieces)%20 != 0 {
|
|
|
|
panic(len(info.Pieces))
|
2015-10-29 22:21:09 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return len(info.Pieces) / 20
|
2015-02-26 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (info *InfoEx) Piece(i int) Piece {
|
|
|
|
return Piece{info, i}
|
2015-02-26 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (info *Info) IsDir() bool {
|
|
|
|
return len(info.Files) != 0
|
2015-02-26 19:12:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-27 22:26:18 +08:00
|
|
|
// The files field, converted up from the old single-file in the parent info
|
|
|
|
// dict if necessary. This is a helper to avoid having to conditionally handle
|
|
|
|
// single and multi-file torrent infos.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (info *Info) UpvertedFiles() []FileInfo {
|
|
|
|
if len(info.Files) == 0 {
|
2015-01-27 22:26:18 +08:00
|
|
|
return []FileInfo{{
|
2016-04-19 12:11:11 +08:00
|
|
|
Length: info.Length,
|
2015-02-26 19:12:02 +08:00
|
|
|
// Callers should determine that Info.Name is the basename, and
|
|
|
|
// thus a regular file.
|
|
|
|
Path: nil,
|
2015-01-27 22:26:18 +08:00
|
|
|
}}
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return info.Files
|
2015-01-27 22:26:18 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
// The info dictionary with its hash and raw bytes exposed, as these are
|
|
|
|
// important to Bittorrent.
|
|
|
|
type InfoEx struct {
|
2014-06-28 17:40:39 +08:00
|
|
|
Info
|
2016-04-30 09:07:29 +08:00
|
|
|
Hash Hash // Only set when unmarshalling or UpdateHash.
|
|
|
|
Bytes []byte // Only set when unmarshalling or UpdateBytes.
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 13:28:22 +08:00
|
|
|
var (
|
|
|
|
_ bencode.Marshaler = InfoEx{}
|
|
|
|
_ bencode.Unmarshaler = &InfoEx{}
|
|
|
|
)
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ie *InfoEx) UnmarshalBencode(data []byte) error {
|
2016-04-30 09:07:29 +08:00
|
|
|
ie.Bytes = append([]byte(nil), data...)
|
2012-06-28 04:26:56 +08:00
|
|
|
h := sha1.New()
|
2016-04-19 12:11:11 +08:00
|
|
|
_, err := h.Write(ie.Bytes)
|
2014-12-02 13:28:22 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-30 09:07:29 +08:00
|
|
|
missinggo.CopyExact(&ie.Hash, h.Sum(nil))
|
2016-04-19 12:11:11 +08:00
|
|
|
return bencode.Unmarshal(data, &ie.Info)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ie InfoEx) MarshalBencode() ([]byte, error) {
|
|
|
|
return bencode.Marshal(&ie.Info)
|
2012-07-06 00:44:27 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
type MetaInfo struct {
|
|
|
|
Info InfoEx `bencode:"info"`
|
2015-05-03 18:30:27 +08:00
|
|
|
Announce string `bencode:"announce,omitempty"`
|
2014-07-13 15:36:06 +08:00
|
|
|
AnnounceList [][]string `bencode:"announce-list,omitempty"`
|
2016-02-23 19:28:23 +08:00
|
|
|
Nodes []Node `bencode:"nodes,omitempty"`
|
2014-07-13 15:36:06 +08:00
|
|
|
CreationDate int64 `bencode:"creation date,omitempty"`
|
|
|
|
Comment string `bencode:"comment,omitempty"`
|
|
|
|
CreatedBy string `bencode:"created by,omitempty"`
|
|
|
|
Encoding string `bencode:"encoding,omitempty"`
|
|
|
|
URLList interface{} `bencode:"url-list,omitempty"`
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
2015-10-29 22:21:09 +08:00
|
|
|
|
|
|
|
// Encode to bencoded form.
|
|
|
|
func (mi *MetaInfo) Write(w io.Writer) error {
|
|
|
|
return bencode.NewEncoder(w).Encode(mi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set good default values in preparation for creating a new MetaInfo file.
|
|
|
|
func (mi *MetaInfo) SetDefaults() {
|
|
|
|
mi.Comment = "yoloham"
|
|
|
|
mi.CreatedBy = "github.com/anacrolix/torrent"
|
|
|
|
mi.CreationDate = time.Now().Unix()
|
|
|
|
mi.Info.PieceLength = 256 * 1024
|
|
|
|
}
|
2016-03-28 17:38:30 +08:00
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
// Magnetize creates a Magnet from a MetaInfo
|
|
|
|
func (mi *MetaInfo) Magnet() (m Magnet) {
|
|
|
|
for _, tier := range mi.AnnounceList {
|
|
|
|
for _, tracker := range tier {
|
|
|
|
m.Trackers = append(m.Trackers, tracker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.DisplayName = mi.Info.Name
|
2016-04-30 09:07:29 +08:00
|
|
|
m.InfoHash = mi.Info.Hash
|
2016-04-04 11:48:39 +08:00
|
|
|
return
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|