]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo.go
New slices package
[btrtrc.git] / metainfo / metainfo.go
1 package metainfo
2
3 import (
4         "crypto/sha1"
5         "errors"
6         "fmt"
7         "io"
8         "log"
9         "os"
10         "path/filepath"
11         "strings"
12         "time"
13
14         "github.com/anacrolix/missinggo/slices"
15         "github.com/anacrolix/torrent/bencode"
16 )
17
18 // Information specific to a single file inside the MetaInfo structure.
19 type FileInfo struct {
20         Length int64    `bencode:"length"`
21         Path   []string `bencode:"path"`
22 }
23
24 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
25 // failure.
26 func Load(r io.Reader) (*MetaInfo, error) {
27         var mi MetaInfo
28         d := bencode.NewDecoder(r)
29         err := d.Decode(&mi)
30         if err != nil {
31                 return nil, err
32         }
33         return &mi, nil
34 }
35
36 // Convenience function for loading a MetaInfo from a file.
37 func LoadFromFile(filename string) (*MetaInfo, error) {
38         f, err := os.Open(filename)
39         if err != nil {
40                 return nil, err
41         }
42         defer f.Close()
43         return Load(f)
44 }
45
46 // The info dictionary.
47 type Info struct {
48         PieceLength int64      `bencode:"piece length"`
49         Pieces      []byte     `bencode:"pieces"`
50         Name        string     `bencode:"name"`
51         Length      int64      `bencode:"length,omitempty"`
52         Private     *bool      `bencode:"private,omitempty"`
53         Files       []FileInfo `bencode:"files,omitempty"`
54 }
55
56 // This is a helper that sets Files and Pieces from a root path and its
57 // children.
58 func (info *Info) BuildFromFilePath(root string) (err error) {
59         info.Name = filepath.Base(root)
60         info.Files = nil
61         err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
62                 if err != nil {
63                         return err
64                 }
65                 if fi.IsDir() {
66                         // Directories are implicit in torrent files.
67                         return nil
68                 } else if path == root {
69                         // The root is a file.
70                         info.Length = fi.Size()
71                         return nil
72                 }
73                 relPath, err := filepath.Rel(root, path)
74                 log.Println(relPath, err)
75                 if err != nil {
76                         return fmt.Errorf("error getting relative path: %s", err)
77                 }
78                 info.Files = append(info.Files, FileInfo{
79                         Path:   strings.Split(relPath, string(filepath.Separator)),
80                         Length: fi.Size(),
81                 })
82                 return nil
83         })
84         if err != nil {
85                 return
86         }
87         slices.Sort(info.Files, func(l, r FileInfo) bool {
88                 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
89         })
90         err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
91                 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
92         })
93         if err != nil {
94                 err = fmt.Errorf("error generating pieces: %s", err)
95         }
96         return
97 }
98
99 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
100         for _, fi := range info.UpvertedFiles() {
101                 r, err := open(fi)
102                 if err != nil {
103                         return fmt.Errorf("error opening %v: %s", fi, err)
104                 }
105                 wn, err := io.CopyN(w, r, fi.Length)
106                 r.Close()
107                 if wn != fi.Length || err != nil {
108                         return fmt.Errorf("error hashing %v: %s", fi, err)
109                 }
110         }
111         return nil
112 }
113
114 // Set info.Pieces by hashing info.Files.
115 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
116         if info.PieceLength == 0 {
117                 return errors.New("piece length must be non-zero")
118         }
119         pr, pw := io.Pipe()
120         go func() {
121                 err := info.writeFiles(pw, open)
122                 pw.CloseWithError(err)
123         }()
124         defer pr.Close()
125         var pieces []byte
126         for {
127                 hasher := sha1.New()
128                 wn, err := io.CopyN(hasher, pr, info.PieceLength)
129                 if err == io.EOF {
130                         err = nil
131                 }
132                 if err != nil {
133                         return err
134                 }
135                 if wn == 0 {
136                         break
137                 }
138                 pieces = hasher.Sum(pieces)
139                 if wn < info.PieceLength {
140                         break
141                 }
142         }
143         info.Pieces = pieces
144         return nil
145 }
146
147 func (info *Info) TotalLength() (ret int64) {
148         if info.IsDir() {
149                 for _, fi := range info.Files {
150                         ret += fi.Length
151                 }
152         } else {
153                 ret = info.Length
154         }
155         return
156 }
157
158 func (info *Info) NumPieces() int {
159         if len(info.Pieces)%20 != 0 {
160                 panic(len(info.Pieces))
161         }
162         return len(info.Pieces) / 20
163 }
164
165 func (info *Info) IsDir() bool {
166         return len(info.Files) != 0
167 }
168
169 // The files field, converted up from the old single-file in the parent info
170 // dict if necessary. This is a helper to avoid having to conditionally handle
171 // single and multi-file torrent infos.
172 func (info *Info) UpvertedFiles() []FileInfo {
173         if len(info.Files) == 0 {
174                 return []FileInfo{{
175                         Length: info.Length,
176                         // Callers should determine that Info.Name is the basename, and
177                         // thus a regular file.
178                         Path: nil,
179                 }}
180         }
181         return info.Files
182 }
183
184 type MetaInfo struct {
185         Info         InfoEx      `bencode:"info"`
186         Announce     string      `bencode:"announce,omitempty"`
187         AnnounceList [][]string  `bencode:"announce-list,omitempty"`
188         Nodes        []Node      `bencode:"nodes,omitempty"`
189         CreationDate int64       `bencode:"creation date,omitempty"`
190         Comment      string      `bencode:"comment,omitempty"`
191         CreatedBy    string      `bencode:"created by,omitempty"`
192         Encoding     string      `bencode:"encoding,omitempty"`
193         URLList      interface{} `bencode:"url-list,omitempty"`
194 }
195
196 // Encode to bencoded form.
197 func (mi *MetaInfo) Write(w io.Writer) error {
198         return bencode.NewEncoder(w).Encode(mi)
199 }
200
201 // Set good default values in preparation for creating a new MetaInfo file.
202 func (mi *MetaInfo) SetDefaults() {
203         mi.Comment = "yoloham"
204         mi.CreatedBy = "github.com/anacrolix/torrent"
205         mi.CreationDate = time.Now().Unix()
206         mi.Info.PieceLength = 256 * 1024
207 }
208
209 // Creates a Magnet from a MetaInfo.
210 func (mi *MetaInfo) Magnet() (m Magnet) {
211         for _, tier := range mi.AnnounceList {
212                 for _, tracker := range tier {
213                         m.Trackers = append(m.Trackers, tracker)
214                 }
215         }
216         m.DisplayName = mi.Info.Name
217         m.InfoHash = mi.Info.Hash()
218         return
219 }