]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo.go
Doc metainfo.Info.BuildFromFilePath
[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"
15
16         "github.com/anacrolix/torrent/bencode"
17 )
18
19 // Information specific to a single file inside the MetaInfo structure.
20 type FileInfo struct {
21         Length int64    `bencode:"length"`
22         Path   []string `bencode:"path"`
23 }
24
25 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
26 // failure.
27 func Load(r io.Reader) (*MetaInfo, error) {
28         var mi MetaInfo
29         d := bencode.NewDecoder(r)
30         err := d.Decode(&mi)
31         if err != nil {
32                 return nil, err
33         }
34         return &mi, nil
35 }
36
37 // Convenience function for loading a MetaInfo from a file.
38 func LoadFromFile(filename string) (*MetaInfo, error) {
39         f, err := os.Open(filename)
40         if err != nil {
41                 return nil, err
42         }
43         defer f.Close()
44         return Load(f)
45 }
46
47 // The info dictionary.
48 type Info struct {
49         PieceLength int64      `bencode:"piece length"`
50         Pieces      []byte     `bencode:"pieces"`
51         Name        string     `bencode:"name"`
52         Length      int64      `bencode:"length,omitempty"`
53         Private     *bool      `bencode:"private,omitempty"`
54         Files       []FileInfo `bencode:"files,omitempty"`
55 }
56
57 // This is a helper that sets Files and Pieces from a root path and its
58 // children.
59 func (info *Info) BuildFromFilePath(root string) (err error) {
60         info.Name = filepath.Base(root)
61         info.Files = nil
62         err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
63                 log.Println(path, root, err)
64                 if fi.IsDir() {
65                         // Directories are implicit in torrent files.
66                         return nil
67                 } else if path == root {
68                         // The root is a file.
69                         info.Length = fi.Size()
70                         return nil
71                 }
72                 relPath, err := filepath.Rel(root, path)
73                 log.Println(relPath, err)
74                 if err != nil {
75                         return fmt.Errorf("error getting relative path: %s", err)
76                 }
77                 info.Files = append(info.Files, FileInfo{
78                         Path:   strings.Split(relPath, string(filepath.Separator)),
79                         Length: fi.Size(),
80                 })
81                 return nil
82         })
83         if err != nil {
84                 return
85         }
86         err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
87                 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
88         })
89         if err != nil {
90                 err = fmt.Errorf("error generating pieces: %s", err)
91         }
92         return
93 }
94
95 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
96         for _, fi := range info.UpvertedFiles() {
97                 r, err := open(fi)
98                 if err != nil {
99                         return fmt.Errorf("error opening %v: %s", fi, err)
100                 }
101                 wn, err := io.CopyN(w, r, fi.Length)
102                 r.Close()
103                 if wn != fi.Length || err != nil {
104                         return fmt.Errorf("error hashing %v: %s", fi, err)
105                 }
106         }
107         return nil
108 }
109
110 // Set info.Pieces by hashing info.Files.
111 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
112         if info.PieceLength == 0 {
113                 return errors.New("piece length must be non-zero")
114         }
115         pr, pw := io.Pipe()
116         go func() {
117                 err := info.writeFiles(pw, open)
118                 pw.CloseWithError(err)
119         }()
120         defer pr.Close()
121         var pieces []byte
122         for {
123                 hasher := sha1.New()
124                 wn, err := io.CopyN(hasher, pr, info.PieceLength)
125                 if err == io.EOF {
126                         err = nil
127                 }
128                 if err != nil {
129                         return err
130                 }
131                 if wn == 0 {
132                         break
133                 }
134                 pieces = hasher.Sum(pieces)
135                 if wn < info.PieceLength {
136                         break
137                 }
138         }
139         info.Pieces = pieces
140         return nil
141 }
142
143 func (info *Info) TotalLength() (ret int64) {
144         if info.IsDir() {
145                 for _, fi := range info.Files {
146                         ret += fi.Length
147                 }
148         } else {
149                 ret = info.Length
150         }
151         return
152 }
153
154 func (info *Info) NumPieces() int {
155         if len(info.Pieces)%20 != 0 {
156                 panic(len(info.Pieces))
157         }
158         return len(info.Pieces) / 20
159 }
160
161 func (info *InfoEx) Piece(i int) Piece {
162         return Piece{info, i}
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 // The info dictionary with its hash and raw bytes exposed, as these are
185 // important to Bittorrent.
186 type InfoEx struct {
187         Info
188         Hash  Hash   // Only set when unmarshalling or UpdateHash.
189         Bytes []byte // Only set when unmarshalling or UpdateBytes.
190 }
191
192 var (
193         _ bencode.Marshaler   = InfoEx{}
194         _ bencode.Unmarshaler = &InfoEx{}
195 )
196
197 func (ie *InfoEx) UnmarshalBencode(data []byte) error {
198         ie.Bytes = append([]byte(nil), data...)
199         h := sha1.New()
200         _, err := h.Write(ie.Bytes)
201         if err != nil {
202                 panic(err)
203         }
204         missinggo.CopyExact(&ie.Hash, h.Sum(nil))
205         return bencode.Unmarshal(data, &ie.Info)
206 }
207
208 func (ie InfoEx) MarshalBencode() ([]byte, error) {
209         return bencode.Marshal(&ie.Info)
210 }
211
212 type MetaInfo struct {
213         Info         InfoEx      `bencode:"info"`
214         Announce     string      `bencode:"announce,omitempty"`
215         AnnounceList [][]string  `bencode:"announce-list,omitempty"`
216         Nodes        []Node      `bencode:"nodes,omitempty"`
217         CreationDate int64       `bencode:"creation date,omitempty"`
218         Comment      string      `bencode:"comment,omitempty"`
219         CreatedBy    string      `bencode:"created by,omitempty"`
220         Encoding     string      `bencode:"encoding,omitempty"`
221         URLList      interface{} `bencode:"url-list,omitempty"`
222 }
223
224 // Encode to bencoded form.
225 func (mi *MetaInfo) Write(w io.Writer) error {
226         return bencode.NewEncoder(w).Encode(mi)
227 }
228
229 // Set good default values in preparation for creating a new MetaInfo file.
230 func (mi *MetaInfo) SetDefaults() {
231         mi.Comment = "yoloham"
232         mi.CreatedBy = "github.com/anacrolix/torrent"
233         mi.CreationDate = time.Now().Unix()
234         mi.Info.PieceLength = 256 * 1024
235 }
236
237 // Magnetize creates a Magnet from a MetaInfo
238 func (mi *MetaInfo) Magnet() (m Magnet) {
239         for _, tier := range mi.AnnounceList {
240                 for _, tracker := range tier {
241                         m.Trackers = append(m.Trackers, tracker)
242                 }
243         }
244         m.DisplayName = mi.Info.Name
245         m.InfoHash = mi.Info.Hash
246         return
247 }