]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Rename torrent package to metainfo; expose MetaInfo["info"] so it can be used on...
authorMatt Joiner <anacrolix@gmail.com>
Sat, 28 Jun 2014 09:40:39 +0000 (19:40 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 28 Jun 2014 09:40:39 +0000 (19:40 +1000)
--HG--
rename : torrent/README => metainfo/README
rename : torrent/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent => metainfo/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent
rename : torrent/_testdata/continuum.torrent => metainfo/_testdata/continuum.torrent
rename : torrent/builder.go => metainfo/builder.go
rename : torrent/metainfo.go => metainfo/metainfo.go
rename : torrent/metainfo_test.go => metainfo/metainfo_test.go

metainfo/README [moved from torrent/README with 100% similarity]
metainfo/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent [moved from torrent/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent with 100% similarity]
metainfo/_testdata/continuum.torrent [moved from torrent/_testdata/continuum.torrent with 100% similarity]
metainfo/builder.go [moved from torrent/builder.go with 99% similarity]
metainfo/metainfo.go [moved from torrent/metainfo.go with 70% similarity]
metainfo/metainfo_test.go [moved from torrent/metainfo_test.go with 97% similarity]

similarity index 100%
rename from torrent/README
rename to metainfo/README
similarity index 99%
rename from torrent/builder.go
rename to metainfo/builder.go
index a32abd29c83cd8e0b120884e3cd5a13e673130be..d4163f82411fd52fb8c89f6b0503f9226964ce16 100644 (file)
@@ -1,4 +1,4 @@
-package torrent
+package metainfo
 
 import (
        "crypto/sha1"
@@ -371,9 +371,9 @@ func (b *Batch) write_torrent(w io.Writer) error {
        if len(b.files) == 1 {
                td.Info.Length = b.files[0].size
        } else {
-               td.Info.Files = make([]torrent_info_file, len(b.files))
+               td.Info.Files = make([]FileInfo, len(b.files))
                for i, f := range b.files {
-                       td.Info.Files[i] = torrent_info_file{
+                       td.Info.Files[i] = FileInfo{
                                Path:   f.splitpath,
                                Length: f.size,
                        }
similarity index 70%
rename from torrent/metainfo.go
rename to metainfo/metainfo.go
index 51f481fcb789adcafb97800c338449a96ef42dae..68cbc75b1dae179b7dcebfc7d7b3606914d91ad1 100644 (file)
@@ -1,4 +1,4 @@
-package torrent
+package metainfo
 
 import (
        "crypto/sha1"
@@ -11,26 +11,23 @@ import (
 
 // Information specific to a single file inside the MetaInfo structure..
 type FileInfo struct {
-       Length int64
-       Path   []string
+       Length int64    `bencode:"length"`
+       Path   []string `bencode:"path"`
 }
 
 // MetaInfo is the type you should use when reading torrent files. See Load and
 // LoadFromFile functions. All the fields are intended to be read-only. If
 // 'len(Files) == 1', then the FileInfo.Path is nil in that entry.
 type MetaInfo struct {
-       Name         string
-       Files        []FileInfo
+       Info
        InfoHash     []byte
-       PieceLength  int64
-       Pieces       []byte
-       Private      bool
        AnnounceList [][]string
        CreationDate time.Time
        Comment      string
        CreatedBy    string
        Encoding     string
        WebSeedURLs  []string
+       InfoBytes    []byte
 }
 
 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
@@ -44,25 +41,9 @@ func Load(r io.Reader) (*MetaInfo, error) {
                return nil, err
        }
 
-       // post-parse processing
-       mi.Name = data.Info.Name
-       if len(data.Info.Files) > 0 {
-               files := make([]FileInfo, len(data.Info.Files))
-               for i, fi := range data.Info.Files {
-                       files[i] = FileInfo{
-                               Length: fi.Length,
-                               Path:   fi.Path,
-                       }
-               }
-               mi.Files = files
-       } else {
-               mi.Files = []FileInfo{{Length: data.Info.Length, Path: nil}}
-       }
+       mi.Info = data.Info.Info
+       mi.InfoBytes = data.Info.Bytes
        mi.InfoHash = data.Info.Hash
-       mi.PieceLength = data.Info.PieceLength
-       mi.Pieces = data.Info.Pieces
-       mi.Private = data.Info.Private
-
        if len(data.AnnounceList) > 0 {
                mi.AnnounceList = data.AnnounceList
        } else {
@@ -103,38 +84,36 @@ func LoadFromFile(filename string) (*MetaInfo, error) {
        return Load(f)
 }
 
+type Info struct {
+       PieceLength int64      `bencode:"piece length"`
+       Pieces      []byte     `bencode:"pieces"`
+       Name        string     `bencode:"name"`
+       Length      int64      `bencode:"length,omitempty"`
+       Private     bool       `bencode:"private,omitempty"`
+       Files       []FileInfo `bencode:"files,omitempty"`
+}
+
 //----------------------------------------------------------------------------
 // unmarshal structures
 //----------------------------------------------------------------------------
 
-type torrent_info_file struct {
-       Path   []string `bencode:"path"`
-       Length int64    `bencode:"length"`
-}
-
-type torrent_info struct {
-       PieceLength int64               `bencode:"piece length"`
-       Pieces      []byte              `bencode:"pieces"`
-       Name        string              `bencode:"name"`
-       Length      int64               `bencode:"length,omitempty"`
-       Private     bool                `bencode:"private,omitempty"`
-       Files       []torrent_info_file `bencode:"files,omitempty"`
-}
-
 type torrent_info_ex struct {
-       torrent_info
-       Hash []byte
+       Info
+       Hash  []byte
+       Bytes []byte
 }
 
 func (this *torrent_info_ex) UnmarshalBencode(data []byte) error {
+       this.Bytes = make([]byte, 0, len(data))
+       this.Bytes = append(this.Bytes, data...)
        h := sha1.New()
-       h.Write(data)
+       h.Write(this.Bytes)
        this.Hash = h.Sum(this.Hash)
-       return bencode.Unmarshal(data, &this.torrent_info)
+       return bencode.Unmarshal(data, &this.Info)
 }
 
 func (this *torrent_info_ex) MarshalBencode() ([]byte, error) {
-       return bencode.Marshal(&this.torrent_info)
+       return bencode.Marshal(&this.Info)
 }
 
 type torrent_data struct {
similarity index 97%
rename from torrent/metainfo_test.go
rename to metainfo/metainfo_test.go
index 440a029f53874aa41f7874687918f2525ef1ce0f..b7cb314607381b16dfe04b40810c8b4f1088a5dd 100644 (file)
@@ -1,4 +1,4 @@
-package torrent
+package metainfo
 
 import "testing"
 import "path"