]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/info.go
462288d33437c236fade46744aef871b8a3bca65
[btrtrc.git] / metainfo / info.go
1 package metainfo
2
3 import (
4         "crypto/sha1"
5         "errors"
6         "fmt"
7         "io"
8         "os"
9         "path/filepath"
10         "strings"
11
12         "github.com/anacrolix/missinggo/slices"
13 )
14
15 // The info dictionary.
16 type Info struct {
17         PieceLength int64      `bencode:"piece length"`
18         Pieces      []byte     `bencode:"pieces"`
19         Name        string     `bencode:"name"`
20         Length      int64      `bencode:"length,omitempty"`
21         Private     *bool      `bencode:"private,omitempty"`
22         Source      string     `bencode:"source,omitempty"`
23         Files       []FileInfo `bencode:"files,omitempty"`
24 }
25
26 // This is a helper that sets Files and Pieces from a root path and its
27 // children.
28 func (info *Info) BuildFromFilePath(root string) (err error) {
29         info.Name = filepath.Base(root)
30         info.Files = nil
31         err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
32                 if err != nil {
33                         return err
34                 }
35                 if fi.IsDir() {
36                         // Directories are implicit in torrent files.
37                         return nil
38                 } else if path == root {
39                         // The root is a file.
40                         info.Length = fi.Size()
41                         return nil
42                 }
43                 relPath, err := filepath.Rel(root, path)
44                 if err != nil {
45                         return fmt.Errorf("error getting relative path: %s", err)
46                 }
47                 info.Files = append(info.Files, FileInfo{
48                         Path:   strings.Split(relPath, string(filepath.Separator)),
49                         Length: fi.Size(),
50                 })
51                 return nil
52         })
53         if err != nil {
54                 return
55         }
56         slices.Sort(info.Files, func(l, r FileInfo) bool {
57                 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
58         })
59         err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
60                 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
61         })
62         if err != nil {
63                 err = fmt.Errorf("error generating pieces: %s", err)
64         }
65         return
66 }
67
68 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
69         for _, fi := range info.UpvertedFiles() {
70                 r, err := open(fi)
71                 if err != nil {
72                         return fmt.Errorf("error opening %v: %s", fi, err)
73                 }
74                 wn, err := io.CopyN(w, r, fi.Length)
75                 r.Close()
76                 if wn != fi.Length || err != nil {
77                         return fmt.Errorf("error hashing %v: %s", fi, err)
78                 }
79         }
80         return nil
81 }
82
83 // Set info.Pieces by hashing info.Files.
84 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
85         if info.PieceLength == 0 {
86                 return errors.New("piece length must be non-zero")
87         }
88         pr, pw := io.Pipe()
89         go func() {
90                 err := info.writeFiles(pw, open)
91                 pw.CloseWithError(err)
92         }()
93         defer pr.Close()
94         var pieces []byte
95         for {
96                 hasher := sha1.New()
97                 wn, err := io.CopyN(hasher, pr, info.PieceLength)
98                 if err == io.EOF {
99                         err = nil
100                 }
101                 if err != nil {
102                         return err
103                 }
104                 if wn == 0 {
105                         break
106                 }
107                 pieces = hasher.Sum(pieces)
108                 if wn < info.PieceLength {
109                         break
110                 }
111         }
112         info.Pieces = pieces
113         return nil
114 }
115
116 func (info *Info) TotalLength() (ret int64) {
117         if info.IsDir() {
118                 for _, fi := range info.Files {
119                         ret += fi.Length
120                 }
121         } else {
122                 ret = info.Length
123         }
124         return
125 }
126
127 func (info *Info) NumPieces() int {
128         if len(info.Pieces)%20 != 0 {
129                 panic(len(info.Pieces))
130         }
131         return len(info.Pieces) / 20
132 }
133
134 func (info *Info) IsDir() bool {
135         return len(info.Files) != 0
136 }
137
138 // The files field, converted up from the old single-file in the parent info
139 // dict if necessary. This is a helper to avoid having to conditionally handle
140 // single and multi-file torrent infos.
141 func (info *Info) UpvertedFiles() []FileInfo {
142         if len(info.Files) == 0 {
143                 return []FileInfo{{
144                         Length: info.Length,
145                         // Callers should determine that Info.Name is the basename, and
146                         // thus a regular file.
147                         Path: nil,
148                 }}
149         }
150         return info.Files
151 }
152
153 func (info *Info) Piece(index int) Piece {
154         return Piece{info, index}
155 }