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
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-10-29 22:21:09 +08:00
|
|
|
if len(me.Pieces)%20 != 0 {
|
|
|
|
panic(len(me.Pieces))
|
|
|
|
}
|
2015-02-26 22:41:35 +08:00
|
|
|
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"`
|
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
|
|
|
|
}
|