14 "github.com/anacrolix/missinggo/slices"
15 "github.com/anacrolix/torrent/bencode"
18 // Information specific to a single file inside the MetaInfo structure.
19 type FileInfo struct {
20 Length int64 `bencode:"length"`
21 Path []string `bencode:"path"`
24 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
26 func Load(r io.Reader) (*MetaInfo, error) {
28 d := bencode.NewDecoder(r)
36 // Convenience function for loading a MetaInfo from a file.
37 func LoadFromFile(filename string) (*MetaInfo, error) {
38 f, err := os.Open(filename)
46 // The info dictionary.
48 PieceLength int64 `bencode:"piece length"`
49 Pieces []byte `bencode:"pieces"`
50 Name string `bencode:"name"`
51 Length int64 `bencode:"length,omitempty"`
52 Private *bool `bencode:"private,omitempty"`
53 Files []FileInfo `bencode:"files,omitempty"`
56 // This is a helper that sets Files and Pieces from a root path and its
58 func (info *Info) BuildFromFilePath(root string) (err error) {
59 info.Name = filepath.Base(root)
61 err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
66 // Directories are implicit in torrent files.
68 } else if path == root {
69 // The root is a file.
70 info.Length = fi.Size()
73 relPath, err := filepath.Rel(root, path)
74 log.Println(relPath, err)
76 return fmt.Errorf("error getting relative path: %s", err)
78 info.Files = append(info.Files, FileInfo{
79 Path: strings.Split(relPath, string(filepath.Separator)),
87 slices.Sort(info.Files, func(l, r FileInfo) bool {
88 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
90 err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
91 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
94 err = fmt.Errorf("error generating pieces: %s", err)
99 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
100 for _, fi := range info.UpvertedFiles() {
103 return fmt.Errorf("error opening %v: %s", fi, err)
105 wn, err := io.CopyN(w, r, fi.Length)
107 if wn != fi.Length || err != nil {
108 return fmt.Errorf("error hashing %v: %s", fi, err)
114 // Set info.Pieces by hashing info.Files.
115 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
116 if info.PieceLength == 0 {
117 return errors.New("piece length must be non-zero")
121 err := info.writeFiles(pw, open)
122 pw.CloseWithError(err)
128 wn, err := io.CopyN(hasher, pr, info.PieceLength)
138 pieces = hasher.Sum(pieces)
139 if wn < info.PieceLength {
147 func (info *Info) TotalLength() (ret int64) {
149 for _, fi := range info.Files {
158 func (info *Info) NumPieces() int {
159 if len(info.Pieces)%20 != 0 {
160 panic(len(info.Pieces))
162 return len(info.Pieces) / 20
165 func (info *Info) IsDir() bool {
166 return len(info.Files) != 0
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 {
176 // Callers should determine that Info.Name is the basename, and
177 // thus a regular file.
184 type MetaInfo struct {
185 Info InfoEx `bencode:"info"`
186 Announce string `bencode:"announce,omitempty"`
187 AnnounceList [][]string `bencode:"announce-list,omitempty"`
188 Nodes []Node `bencode:"nodes,omitempty"`
189 CreationDate int64 `bencode:"creation date,omitempty"`
190 Comment string `bencode:"comment,omitempty"`
191 CreatedBy string `bencode:"created by,omitempty"`
192 Encoding string `bencode:"encoding,omitempty"`
193 URLList interface{} `bencode:"url-list,omitempty"`
196 // Encode to bencoded form.
197 func (mi *MetaInfo) Write(w io.Writer) error {
198 return bencode.NewEncoder(w).Encode(mi)
201 // Set good default values in preparation for creating a new MetaInfo file.
202 func (mi *MetaInfo) SetDefaults() {
203 mi.Comment = "yoloham"
204 mi.CreatedBy = "github.com/anacrolix/torrent"
205 mi.CreationDate = time.Now().Unix()
206 mi.Info.PieceLength = 256 * 1024
209 // Creates a Magnet from a MetaInfo.
210 func (mi *MetaInfo) Magnet() (m Magnet) {
211 for _, tier := range mi.AnnounceList {
212 for _, tracker := range tier {
213 m.Trackers = append(m.Trackers, tracker)
216 if m.Trackers == nil && mi.Announce != "" {
217 m.Trackers = []string{mi.Announce}
219 m.DisplayName = mi.Info.Name
220 m.InfoHash = mi.Info.Hash()