From: Matt Joiner Date: Sat, 28 Jun 2014 09:40:39 +0000 (+1000) Subject: Rename torrent package to metainfo; expose MetaInfo["info"] so it can be used on... X-Git-Tag: v1.0.0~1199^2~11 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a44d2d88c3ebb8c8d06a8e261d7c3b960d05b0d7;p=btrtrc.git Rename torrent package to metainfo; expose MetaInfo["info"] so it can be used on its own --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 --- diff --git a/torrent/README b/metainfo/README similarity index 100% rename from torrent/README rename to metainfo/README diff --git a/torrent/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent b/metainfo/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent similarity index 100% rename from torrent/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent rename to metainfo/_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent diff --git a/torrent/_testdata/continuum.torrent b/metainfo/_testdata/continuum.torrent similarity index 100% rename from torrent/_testdata/continuum.torrent rename to metainfo/_testdata/continuum.torrent diff --git a/torrent/builder.go b/metainfo/builder.go similarity index 99% rename from torrent/builder.go rename to metainfo/builder.go index a32abd29..d4163f82 100644 --- a/torrent/builder.go +++ b/metainfo/builder.go @@ -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, } diff --git a/torrent/metainfo.go b/metainfo/metainfo.go similarity index 70% rename from torrent/metainfo.go rename to metainfo/metainfo.go index 51f481fc..68cbc75b 100644 --- a/torrent/metainfo.go +++ b/metainfo/metainfo.go @@ -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 { diff --git a/torrent/metainfo_test.go b/metainfo/metainfo_test.go similarity index 97% rename from torrent/metainfo_test.go rename to metainfo/metainfo_test.go index 440a029f..b7cb3146 100644 --- a/torrent/metainfo_test.go +++ b/metainfo/metainfo_test.go @@ -1,4 +1,4 @@ -package torrent +package metainfo import "testing" import "path"