2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"io"
|
|
|
|
"os"
|
2014-12-02 13:28:22 +08:00
|
|
|
|
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"`
|
|
|
|
Private bool `bencode:"private,omitempty"`
|
|
|
|
Files []FileInfo `bencode:"files,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-02-26 22:41:35 +08:00
|
|
|
func (me *Info) TotalLength() (ret int64) {
|
|
|
|
if me.IsDir() {
|
|
|
|
for _, fi := range me.Files {
|
|
|
|
ret += fi.Length
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = me.Length
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Info) NumPieces() int {
|
|
|
|
return len(me.Pieces) / 20
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:31:34 +08:00
|
|
|
type Piece struct {
|
2015-02-26 22:41:35 +08:00
|
|
|
Info *Info
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:31:34 +08:00
|
|
|
func (me Piece) Length() int64 {
|
2015-02-26 22:41:35 +08:00
|
|
|
if me.i == me.Info.NumPieces()-1 {
|
|
|
|
return me.Info.TotalLength() - int64(me.i)*me.Info.PieceLength
|
|
|
|
}
|
|
|
|
return me.Info.PieceLength
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:31:34 +08:00
|
|
|
func (me Piece) Offset() int64 {
|
2015-03-04 10:03:47 +08:00
|
|
|
return int64(me.i) * me.Info.PieceLength
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:31:34 +08:00
|
|
|
func (me Piece) Hash() []byte {
|
2015-02-27 10:00:05 +08:00
|
|
|
return me.Info.Pieces[me.i*20 : (me.i+1)*20]
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:31:34 +08:00
|
|
|
func (me *Info) Piece(i int) Piece {
|
|
|
|
return Piece{me, i}
|
2015-02-26 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
2015-02-26 19:12:02 +08:00
|
|
|
func (i *Info) IsDir() bool {
|
|
|
|
return len(i.Files) != 0
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (i *Info) UpvertedFiles() []FileInfo {
|
|
|
|
if len(i.Files) == 0 {
|
|
|
|
return []FileInfo{{
|
|
|
|
Length: i.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
|
|
|
}}
|
|
|
|
}
|
|
|
|
return i.Files
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Hash []byte
|
|
|
|
Bytes []byte
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 13:28:22 +08:00
|
|
|
var (
|
|
|
|
_ bencode.Marshaler = InfoEx{}
|
|
|
|
_ bencode.Unmarshaler = &InfoEx{}
|
|
|
|
)
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
func (this *InfoEx) UnmarshalBencode(data []byte) error {
|
2014-06-28 17:40:39 +08:00
|
|
|
this.Bytes = make([]byte, 0, len(data))
|
|
|
|
this.Bytes = append(this.Bytes, data...)
|
2012-06-28 04:26:56 +08:00
|
|
|
h := sha1.New()
|
2014-12-02 13:28:22 +08:00
|
|
|
_, err := h.Write(this.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
this.Hash = h.Sum(nil)
|
2014-06-28 17:40:39 +08:00
|
|
|
return bencode.Unmarshal(data, &this.Info)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 13:28:22 +08:00
|
|
|
func (this InfoEx) MarshalBencode() ([]byte, error) {
|
|
|
|
if this.Bytes != nil {
|
|
|
|
return this.Bytes, nil
|
|
|
|
}
|
2014-06-28 17:40:39 +08:00
|
|
|
return bencode.Marshal(&this.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"`
|
2015-05-03 18:30:27 +08:00
|
|
|
Nodes [][]string `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
|
|
|
}
|