]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo.go
0ded430b01ee280d1eb15d34a4dd509b8a8db65f
[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 func (info *Info) BuildFromFilePath(root string) (err error) {
58         info.Name = filepath.Base(root)
59         info.Files = nil
60         err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
61                 log.Println(path, root, err)
62                 if fi.IsDir() {
63                         // Directories are implicit in torrent files.
64                         return nil
65                 } else if path == root {
66                         // The root is a file.
67                         info.Length = fi.Size()
68                         return nil
69                 }
70                 relPath, err := filepath.Rel(root, path)
71                 log.Println(relPath, err)
72                 if err != nil {
73                         return fmt.Errorf("error getting relative path: %s", err)
74                 }
75                 info.Files = append(info.Files, FileInfo{
76                         Path:   strings.Split(relPath, string(filepath.Separator)),
77                         Length: fi.Size(),
78                 })
79                 return nil
80         })
81         if err != nil {
82                 return
83         }
84         err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
85                 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
86         })
87         if err != nil {
88                 err = fmt.Errorf("error generating pieces: %s", err)
89         }
90         return
91 }
92
93 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
94         for _, fi := range info.UpvertedFiles() {
95                 r, err := open(fi)
96                 if err != nil {
97                         return fmt.Errorf("error opening %v: %s", fi, err)
98                 }
99                 wn, err := io.CopyN(w, r, fi.Length)
100                 r.Close()
101                 if wn != fi.Length || err != nil {
102                         return fmt.Errorf("error hashing %v: %s", fi, err)
103                 }
104         }
105         return nil
106 }
107
108 // Set info.Pieces by hashing info.Files.
109 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
110         if info.PieceLength == 0 {
111                 return errors.New("piece length must be non-zero")
112         }
113         pr, pw := io.Pipe()
114         go func() {
115                 err := info.writeFiles(pw, open)
116                 pw.CloseWithError(err)
117         }()
118         defer pr.Close()
119         var pieces []byte
120         for {
121                 hasher := sha1.New()
122                 wn, err := io.CopyN(hasher, pr, info.PieceLength)
123                 if err == io.EOF {
124                         err = nil
125                 }
126                 if err != nil {
127                         return err
128                 }
129                 if wn == 0 {
130                         break
131                 }
132                 pieces = hasher.Sum(pieces)
133                 if wn < info.PieceLength {
134                         break
135                 }
136         }
137         info.Pieces = pieces
138         return nil
139 }
140
141 func (me *Info) TotalLength() (ret int64) {
142         if me.IsDir() {
143                 for _, fi := range me.Files {
144                         ret += fi.Length
145                 }
146         } else {
147                 ret = me.Length
148         }
149         return
150 }
151
152 func (me *Info) NumPieces() int {
153         if len(me.Pieces)%20 != 0 {
154                 panic(len(me.Pieces))
155         }
156         return len(me.Pieces) / 20
157 }
158
159 type Piece struct {
160         Info *Info
161         i    int
162 }
163
164 func (me Piece) Length() int64 {
165         if me.i == me.Info.NumPieces()-1 {
166                 return me.Info.TotalLength() - int64(me.i)*me.Info.PieceLength
167         }
168         return me.Info.PieceLength
169 }
170
171 func (me Piece) Offset() int64 {
172         return int64(me.i) * me.Info.PieceLength
173 }
174
175 func (me Piece) Hash() (ret InfoHash) {
176         missinggo.CopyExact(&ret, me.Info.Pieces[me.i*20:(me.i+1)*20])
177         return
178 }
179
180 func (me *Info) Piece(i int) Piece {
181         return Piece{me, i}
182 }
183
184 func (i *Info) IsDir() bool {
185         return len(i.Files) != 0
186 }
187
188 // The files field, converted up from the old single-file in the parent info
189 // dict if necessary. This is a helper to avoid having to conditionally handle
190 // single and multi-file torrent infos.
191 func (i *Info) UpvertedFiles() []FileInfo {
192         if len(i.Files) == 0 {
193                 return []FileInfo{{
194                         Length: i.Length,
195                         // Callers should determine that Info.Name is the basename, and
196                         // thus a regular file.
197                         Path: nil,
198                 }}
199         }
200         return i.Files
201 }
202
203 // The info dictionary with its hash and raw bytes exposed, as these are
204 // important to Bittorrent.
205 type InfoEx struct {
206         Info
207         Hash  []byte
208         Bytes []byte
209 }
210
211 var (
212         _ bencode.Marshaler   = InfoEx{}
213         _ bencode.Unmarshaler = &InfoEx{}
214 )
215
216 func (this *InfoEx) UnmarshalBencode(data []byte) error {
217         this.Bytes = make([]byte, 0, len(data))
218         this.Bytes = append(this.Bytes, data...)
219         h := sha1.New()
220         _, err := h.Write(this.Bytes)
221         if err != nil {
222                 panic(err)
223         }
224         this.Hash = h.Sum(nil)
225         return bencode.Unmarshal(data, &this.Info)
226 }
227
228 func (this InfoEx) MarshalBencode() ([]byte, error) {
229         if this.Bytes != nil {
230                 return this.Bytes, nil
231         }
232         return bencode.Marshal(&this.Info)
233 }
234
235 type MetaInfo struct {
236         Info         InfoEx      `bencode:"info"`
237         Announce     string      `bencode:"announce,omitempty"`
238         AnnounceList [][]string  `bencode:"announce-list,omitempty"`
239         Nodes        []Node      `bencode:"nodes,omitempty"`
240         CreationDate int64       `bencode:"creation date,omitempty"`
241         Comment      string      `bencode:"comment,omitempty"`
242         CreatedBy    string      `bencode:"created by,omitempty"`
243         Encoding     string      `bencode:"encoding,omitempty"`
244         URLList      interface{} `bencode:"url-list,omitempty"`
245 }
246
247 // Encode to bencoded form.
248 func (mi *MetaInfo) Write(w io.Writer) error {
249         return bencode.NewEncoder(w).Encode(mi)
250 }
251
252 // Set good default values in preparation for creating a new MetaInfo file.
253 func (mi *MetaInfo) SetDefaults() {
254         mi.Comment = "yoloham"
255         mi.CreatedBy = "github.com/anacrolix/torrent"
256         mi.CreationDate = time.Now().Unix()
257         mi.Info.PieceLength = 256 * 1024
258 }
259
260 type InfoHash [20]byte