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
|
|
|
|
|
|
|
"github.com/anacrolix/libtorgo/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 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"`
|
|
|
|
Announce string `bencode:"announce"`
|
|
|
|
AnnounceList [][]string `bencode:"announce-list,omitempty"`
|
|
|
|
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
|
|
|
}
|