14 "github.com/anacrolix/missinggo"
16 "github.com/anacrolix/torrent/bencode"
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"`
25 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
27 func Load(r io.Reader) (*MetaInfo, error) {
29 d := bencode.NewDecoder(r)
37 // Convenience function for loading a MetaInfo from a file.
38 func LoadFromFile(filename string) (*MetaInfo, error) {
39 f, err := os.Open(filename)
47 // The info dictionary.
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"`
57 // This is a helper that sets Files and Pieces from a root path and its
59 func (info *Info) BuildFromFilePath(root string) (err error) {
60 info.Name = filepath.Base(root)
62 err = filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
67 // Directories are implicit in torrent files.
69 } else if path == root {
70 // The root is a file.
71 info.Length = fi.Size()
74 relPath, err := filepath.Rel(root, path)
75 log.Println(relPath, err)
77 return fmt.Errorf("error getting relative path: %s", err)
79 info.Files = append(info.Files, FileInfo{
80 Path: strings.Split(relPath, string(filepath.Separator)),
88 missinggo.SortSlice(info.Files, func(l, r FileInfo) bool {
89 return strings.Join(l.Path, "/") < strings.Join(r.Path, "/")
91 err = info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
92 return os.Open(filepath.Join(root, strings.Join(fi.Path, string(filepath.Separator))))
95 err = fmt.Errorf("error generating pieces: %s", err)
100 func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser, error)) error {
101 for _, fi := range info.UpvertedFiles() {
104 return fmt.Errorf("error opening %v: %s", fi, err)
106 wn, err := io.CopyN(w, r, fi.Length)
108 if wn != fi.Length || err != nil {
109 return fmt.Errorf("error hashing %v: %s", fi, err)
115 // Set info.Pieces by hashing info.Files.
116 func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
117 if info.PieceLength == 0 {
118 return errors.New("piece length must be non-zero")
122 err := info.writeFiles(pw, open)
123 pw.CloseWithError(err)
129 wn, err := io.CopyN(hasher, pr, info.PieceLength)
139 pieces = hasher.Sum(pieces)
140 if wn < info.PieceLength {
148 func (info *Info) TotalLength() (ret int64) {
150 for _, fi := range info.Files {
159 func (info *Info) NumPieces() int {
160 if len(info.Pieces)%20 != 0 {
161 panic(len(info.Pieces))
163 return len(info.Pieces) / 20
166 func (info *Info) IsDir() bool {
167 return len(info.Files) != 0
170 // The files field, converted up from the old single-file in the parent info
171 // dict if necessary. This is a helper to avoid having to conditionally handle
172 // single and multi-file torrent infos.
173 func (info *Info) UpvertedFiles() []FileInfo {
174 if len(info.Files) == 0 {
177 // Callers should determine that Info.Name is the basename, and
178 // thus a regular file.
185 type MetaInfo struct {
186 Info InfoEx `bencode:"info"`
187 Announce string `bencode:"announce,omitempty"`
188 AnnounceList [][]string `bencode:"announce-list,omitempty"`
189 Nodes []Node `bencode:"nodes,omitempty"`
190 CreationDate int64 `bencode:"creation date,omitempty"`
191 Comment string `bencode:"comment,omitempty"`
192 CreatedBy string `bencode:"created by,omitempty"`
193 Encoding string `bencode:"encoding,omitempty"`
194 URLList interface{} `bencode:"url-list,omitempty"`
197 // Encode to bencoded form.
198 func (mi *MetaInfo) Write(w io.Writer) error {
199 return bencode.NewEncoder(w).Encode(mi)
202 // Set good default values in preparation for creating a new MetaInfo file.
203 func (mi *MetaInfo) SetDefaults() {
204 mi.Comment = "yoloham"
205 mi.CreatedBy = "github.com/anacrolix/torrent"
206 mi.CreationDate = time.Now().Unix()
207 mi.Info.PieceLength = 256 * 1024
210 // Creates a Magnet from a MetaInfo.
211 func (mi *MetaInfo) Magnet() (m Magnet) {
212 for _, tier := range mi.AnnounceList {
213 for _, tracker := range tier {
214 m.Trackers = append(m.Trackers, tracker)
217 m.DisplayName = mi.Info.Name
218 m.InfoHash = mi.Info.Hash()