11 "github.com/anacrolix/missinggo/v2/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 NameUtf8 string `bencode:"name.utf-8,omitempty"`
20 Length int64 `bencode:"length,omitempty"` // BEP3, mutually exclusive with Files
21 Private *bool `bencode:"private,omitempty"` // BEP27
22 // TODO: Document this field.
23 Source string `bencode:"source,omitempty"`
24 Files []FileInfo `bencode:"files,omitempty"` // BEP3, mutually exclusive with Length
27 // The Info.Name field is "advisory". For multi-file torrents it's usually a suggested directory
28 // name. There are situations where we don't want a directory (like using the contents of a torrent
29 // as the immediate contents of a directory), or the name is invalid. Transmission will inject the
30 // name of the torrent file if it doesn't like the name, resulting in a different infohash
31 // (https://github.com/transmission/transmission/issues/1775). To work around these situations, we
32 // will use a sentinel name for compatibility with Transmission and to signal to our own client that
33 // we intended to have no directory name. By exposing it in the API we can check for references to
34 // this behaviour within this implementation.
37 // This is a helper that sets Files and Pieces from a root path and its children.
38 func (info *Info) BuildFromFilePath(root string) (err error) {
39 info.Name = func() string {
40 b := filepath.Base(root)
42 case ".", "..", string(filepath.Separator):
49 err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
54 // Directories are implicit in torrent files.
56 } else if path == root {
57 // The root is a file.
58 info.Length = fi.Size()
61 relPath, err := filepath.Rel(root, path)
63 return fmt.Errorf("error getting relative path: %s", err)
65 info.Files = append(info.Files, FileInfo{
66 Path: strings.Split(relPath, string(filepath.Separator)),
74 slices.Sort(info.Files, func(l, r FileInfo) bool {
75 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
77 if info.PieceLength == 0 {
78 info.PieceLength = ChoosePieceLength(info.TotalLength())
80 err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
81 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
84 err = fmt.Errorf("error generating pieces: %s", err)
89 // Concatenates all the files in the torrent into w. open is a function that
90 // gets at the contents of the given file.
91 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
92 for _, fi := range info.UpvertedFiles() {
95 return fmt.Errorf("error opening %v: %s", fi, err)
97 wn, err := io.CopyN(w, r, fi.Length)
100 return fmt.Errorf("error copying %v: %s", fi, err)
106 // Sets Pieces (the block of piece hashes in the Info) by using the passed
107 // function to get at the torrent data.
108 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) (err error) {
109 if info.PieceLength == 0 {
110 return errors.New("piece length must be non-zero")
114 err := info.writeFiles(pw, open)
115 pw.CloseWithError(err)
118 info.Pieces, err = GeneratePieces(pr, info.PieceLength, nil)
122 func (info *Info) TotalLength() (ret int64) {
124 for _, fi := range info.Files {
133 func (info *Info) NumPieces() int {
134 return len(info.Pieces) / 20
137 func (info *Info) IsDir() bool {
138 return len(info.Files) != 0
141 // The files field, converted up from the old single-file in the parent info
142 // dict if necessary. This is a helper to avoid having to conditionally handle
143 // single and multi-file torrent infos.
144 func (info *Info) UpvertedFiles() []FileInfo {
145 if len(info.Files) == 0 {
148 // Callers should determine that Info.Name is the basename, and
149 // thus a regular file.
156 func (info *Info) Piece(index int) Piece {
157 return Piece{info, pieceIndex(index)}
160 func (info Info) BestName() string {
161 if info.NameUtf8 != "" {