2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2014-11-29 00:34:19 +08:00
|
|
|
"github.com/anacrolix/libtorgo/bencode"
|
2012-06-28 04:26:56 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
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-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-06-28 17:40:39 +08:00
|
|
|
h.Write(this.Bytes)
|
2012-06-28 04:26:56 +08:00
|
|
|
this.Hash = h.Sum(this.Hash)
|
2014-06-28 17:40:39 +08:00
|
|
|
return bencode.Unmarshal(data, &this.Info)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2014-07-13 15:36:06 +08:00
|
|
|
func (this *InfoEx) MarshalBencode() ([]byte, error) {
|
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
|
|
|
}
|