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