From 8c8dc09615ebb4467be7d357b1e3ae70625b286c Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 5 Jul 2016 02:45:58 +0200 Subject: cleanup --- meta/torrent.go | 35 +++++++++++++++++++---------------- meta/torrent_test.go | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'meta') diff --git a/meta/torrent.go b/meta/torrent.go index 64fe34c..fe5b927 100644 --- a/meta/torrent.go +++ b/meta/torrent.go @@ -34,22 +34,29 @@ type Info struct { Private bool `bencode:"private"` } -func (i Info) pieces() []Piece { - if i.Length == 0 { - for _, f := range i.Files { - i.Length += f.Length - } +func (i Info) GetLength() int { + if i.Length > 0 { + return i.Length + } + var l int + for _, f := range i.Files { + l += f.Length } - n := i.Length / i.PieceLength - l := i.Length % i.PieceLength - if l > 0 { + return l +} + +func (i Info) GetPieces() []Piece { + length := i.GetLength() + n := length / i.PieceLength + last := length % i.PieceLength + if last > 0 { n++ } p := make([]Piece, n) for k := 0; k < n; k++ { p[k].Length = i.PieceLength - if k+1 == n && l > 0 { - p[k].Length = l + if k+1 == n { + p[k].Length = length } off := k * sha1.Size copy(p[k].CheckSum[:], i.Pieces[off:off+sha1.Size]) @@ -180,12 +187,8 @@ func (i Info) isSingleFile() bool { // PieceCount returns count of full pieces and size of last piece func (i Info) PieceCount() (int, int) { - if i.Length == 0 { - for _, f := range i.Files { - i.Length += f.Length - } - } - return i.Length / i.PieceLength, i.Length % i.PieceLength + length := i.GetLength() + return length / i.PieceLength, length % i.PieceLength } func (i Info) String() string { diff --git a/meta/torrent_test.go b/meta/torrent_test.go index df629d3..b2443af 100644 --- a/meta/torrent_test.go +++ b/meta/torrent_test.go @@ -43,7 +43,7 @@ func TestPieces2(t *testing.T) { if err != nil { t.Error(err) } - for i, p := range tor.Info.pieces() { + for i, p := range tor.Info.GetPieces() { t.Logf("%d %x\n", i, p) } } -- cgit v1.2.3