12 "github.com/anacrolix/missinggo/slices"
15 // The info dictionary.
17 PieceLength int64 `bencode:"piece length"`
18 Pieces []byte `bencode:"pieces"`
19 Name string `bencode:"name"`
20 Length int64 `bencode:"length,omitempty"`
21 Private *bool `bencode:"private,omitempty"`
22 // TODO: Document this field.
23 Source string `bencode:"source,omitempty"`
24 Files []FileInfo `bencode:"files,omitempty"`
27 // This is a helper that sets Files and Pieces from a root path and its
29 func (info *Info) BuildFromFilePath(root string) (err error) {
30 info.Name = filepath.Base(root)
32 err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
37 // Directories are implicit in torrent files.
39 } else if path == root {
40 // The root is a file.
41 info.Length = fi.Size()
44 relPath, err := filepath.Rel(root, path)
46 return fmt.Errorf("error getting relative path: %s", err)
48 info.Files = append(info.Files, FileInfo{
49 Path: strings.Split(relPath, string(filepath.Separator)),
57 slices.Sort(info.Files, func(l, r FileInfo) bool {
58 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
60 err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
61 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
64 err = fmt.Errorf("error generating pieces: %s", err)
69 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
70 for _, fi := range info.UpvertedFiles() {
73 return fmt.Errorf("error opening %v: %s", fi, err)
75 wn, err := io.CopyN(w, r, fi.Length)
77 if wn != fi.Length || err != nil {
78 return fmt.Errorf("error hashing %v: %s", fi, err)
84 // Set info.Pieces by hashing info.Files.
85 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
86 if info.PieceLength == 0 {
87 return errors.New("piece length must be non-zero")
91 err := info.writeFiles(pw, open)
92 pw.CloseWithError(err)
98 wn, err := io.CopyN(hasher, pr, info.PieceLength)
108 pieces = hasher.Sum(pieces)
109 if wn < info.PieceLength {
117 func (info *Info) TotalLength() (ret int64) {
119 for _, fi := range info.Files {
128 func (info *Info) NumPieces() int {
129 if len(info.Pieces)%20 != 0 {
130 panic(len(info.Pieces))
132 return len(info.Pieces) / 20
135 func (info *Info) IsDir() bool {
136 return len(info.Files) != 0
139 // The files field, converted up from the old single-file in the parent info
140 // dict if necessary. This is a helper to avoid having to conditionally handle
141 // single and multi-file torrent infos.
142 func (info *Info) UpvertedFiles() []FileInfo {
143 if len(info.Files) == 0 {
146 // Callers should determine that Info.Name is the basename, and
147 // thus a regular file.
154 func (info *Info) Piece(index int) Piece {
155 return Piece{info, index}