torrent/README => metainfo/README | 0 torrent/builder.go => metainfo/builder.go | 6 +++--- torrent/metainfo.go => metainfo/metainfo.go | 69 +++++++++++++++++++---------------------------------- torrent/metainfo_test.go => metainfo/metainfo_test.go | 2 +- diff --git a/torrent/README b/metainfo/README 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 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 rename from torrent/_testdata/continuum.torrent rename to metainfo/_testdata/continuum.torrent diff --git a/torrent/builder.go b/metainfo/builder.go rename from torrent/builder.go rename to metainfo/builder.go index a32abd29c83cd8e0b120884e3cd5a13e673130be..d4163f82411fd52fb8c89f6b0503f9226964ce16 100644 --- a/torrent/builder.go +++ b/metainfo/builder.go @@ -1,4 +1,4 @@ -package torrent +package metainfo import ( "crypto/sha1" @@ -371,9 +371,9 @@ } 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 rename from torrent/metainfo.go rename to metainfo/metainfo.go index 51f481fcb789adcafb97800c338449a96ef42dae..68cbc75b1dae179b7dcebfc7d7b3606914d91ad1 100644 --- a/torrent/metainfo.go +++ b/metainfo/metainfo.go @@ -1,4 +1,4 @@ -package torrent +package metainfo import ( "crypto/sha1" @@ -11,26 +11,23 @@ ) // 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 @@ if err != nil { 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 @@ defer f.Close() 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 rename from torrent/metainfo_test.go rename to metainfo/metainfo_test.go index 440a029f53874aa41f7874687918f2525ef1ce0f..b7cb314607381b16dfe04b40810c8b4f1088a5dd 100644 --- a/torrent/metainfo_test.go +++ b/metainfo/metainfo_test.go @@ -1,4 +1,4 @@ -package torrent +package metainfo import "testing" import "path"