]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/info.go
Drop support for go 1.20
[btrtrc.git] / metainfo / info.go
1 package metainfo
2
3 import (
4         "errors"
5         "fmt"
6         "io"
7         "os"
8         "path/filepath"
9         "strings"
10
11         "github.com/anacrolix/missinggo/v2/slices"
12 )
13
14 // The info dictionary.
15 type Info struct {
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
25 }
26
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.
35 const NoName = "-"
36
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)
41                 switch b {
42                 case ".", "..", string(filepath.Separator):
43                         return NoName
44                 default:
45                         return b
46                 }
47         }()
48         info.Files = nil
49         err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
50                 if err != nil {
51                         return err
52                 }
53                 if fi.IsDir() {
54                         // Directories are implicit in torrent files.
55                         return nil
56                 } else if path == root {
57                         // The root is a file.
58                         info.Length = fi.Size()
59                         return nil
60                 }
61                 relPath, err := filepath.Rel(root, path)
62                 if err != nil {
63                         return fmt.Errorf("error getting relative path: %s", err)
64                 }
65                 info.Files = append(info.Files, FileInfo{
66                         Path:   strings.Split(relPath, string(filepath.Separator)),
67                         Length: fi.Size(),
68                 })
69                 return nil
70         })
71         if err != nil {
72                 return
73         }
74         slices.Sort(info.Files, func(l, r FileInfo) bool {
75                 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
76         })
77         if info.PieceLength == 0 {
78                 info.PieceLength = ChoosePieceLength(info.TotalLength())
79         }
80         err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
81                 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
82         })
83         if err != nil {
84                 err = fmt.Errorf("error generating pieces: %s", err)
85         }
86         return
87 }
88
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() {
93                 r, err := open(fi)
94                 if err != nil {
95                         return fmt.Errorf("error opening %v: %s", fi, err)
96                 }
97                 wn, err := io.CopyN(w, r, fi.Length)
98                 r.Close()
99                 if wn != fi.Length {
100                         return fmt.Errorf("error copying %v: %s", fi, err)
101                 }
102         }
103         return nil
104 }
105
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")
111         }
112         pr, pw := io.Pipe()
113         go func() {
114                 err := info.writeFiles(pw, open)
115                 pw.CloseWithError(err)
116         }()
117         defer pr.Close()
118         info.Pieces, err = GeneratePieces(pr, info.PieceLength, nil)
119         return
120 }
121
122 func (info *Info) TotalLength() (ret int64) {
123         if info.IsDir() {
124                 for _, fi := range info.Files {
125                         ret += fi.Length
126                 }
127         } else {
128                 ret = info.Length
129         }
130         return
131 }
132
133 func (info *Info) NumPieces() int {
134         return len(info.Pieces) / 20
135 }
136
137 func (info *Info) IsDir() bool {
138         return len(info.Files) != 0
139 }
140
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 {
146                 return []FileInfo{{
147                         Length: info.Length,
148                         // Callers should determine that Info.Name is the basename, and
149                         // thus a regular file.
150                         Path: nil,
151                 }}
152         }
153         return info.Files
154 }
155
156 func (info *Info) Piece(index int) Piece {
157         return Piece{info, pieceIndex(index)}
158 }
159
160 func (info Info) BestName() string {
161         if info.NameUtf8 != "" {
162                 return info.NameUtf8
163         }
164         return info.Name
165 }