aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-07-05 02:45:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-07-05 02:45:58 +0200
commit8c8dc09615ebb4467be7d357b1e3ae70625b286c (patch)
tree4bb1325688011d15ab68ccb2173f58de00a45cf5
parenta2a73d6845f6a58e58b46e8c6f198808791c65b3 (diff)
cleanup
-rw-r--r--meta/torrent.go35
-rw-r--r--meta/torrent_test.go2
2 files changed, 20 insertions, 17 deletions
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)
}
}