11 "github.com/anacrolix/missinggo/slices"
14 // The info dictionary.
16 PieceLength int64 `bencode:"piece length"` // BEP3
17 Pieces []byte `bencode:"pieces"` // BEP3
18 Name string `bencode:"name"` // BEP3
19 Length int64 `bencode:"length,omitempty"` // BEP3, mutually exclusive with Files
20 Private *bool `bencode:"private,omitempty"` // BEP27
21 // TODO: Document this field.
22 Source string `bencode:"source,omitempty"`
23 Files []FileInfo `bencode:"files,omitempty"` // BEP3, mutually exclusive with Length
26 // This is a helper that sets Files and Pieces from a root path and its
28 func (info *Info) BuildFromFilePath(root string) (err error) {
29 info.Name = filepath.Base(root)
31 err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
36 // Directories are implicit in torrent files.
38 } else if path == root {
39 // The root is a file.
40 info.Length = fi.Size()
43 relPath, err := filepath.Rel(root, path)
45 return fmt.Errorf("error getting relative path: %s", err)
47 info.Files = append(info.Files, FileInfo{
48 Path: strings.Split(relPath, string(filepath.Separator)),
56 slices.Sort(info.Files, func(l, r FileInfo) bool {
57 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
59 err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
60 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
63 err = fmt.Errorf("error generating pieces: %s", err)
68 // Concatenates all the files in the torrent into w. open is a function that
69 // gets at the contents of the given file.
70 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
71 for _, fi := range info.UpvertedFiles() {
74 return fmt.Errorf("error opening %v: %s", fi, err)
76 wn, err := io.CopyN(w, r, fi.Length)
79 return fmt.Errorf("error copying %v: %s", fi, err)
85 // Sets Pieces (the block of piece hashes in the Info) by using the passed
86 // function to get at the torrent data.
87 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) (err error) {
88 if info.PieceLength == 0 {
89 return errors.New("piece length must be non-zero")
93 err := info.writeFiles(pw, open)
94 pw.CloseWithError(err)
97 info.Pieces, err = GeneratePieces(pr, info.PieceLength, nil)
101 func (info *Info) TotalLength() (ret int64) {
103 for _, fi := range info.Files {
112 func (info *Info) NumPieces() int {
113 return len(info.Pieces) / 20
116 func (info *Info) IsDir() bool {
117 return len(info.Files) != 0
120 // The files field, converted up from the old single-file in the parent info
121 // dict if necessary. This is a helper to avoid having to conditionally handle
122 // single and multi-file torrent infos.
123 func (info *Info) UpvertedFiles() []FileInfo {
124 if len(info.Files) == 0 {
127 // Callers should determine that Info.Name is the basename, and
128 // thus a regular file.
135 func (info *Info) Piece(index int) Piece {
136 return Piece{info, pieceIndex(index)}