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