aboutsummaryrefslogtreecommitdiff
path: root/meta
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-07-12 21:06:07 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-07-12 21:06:07 +0200
commit5fdbc767bc12cdaf7d969a598e6a2eb4b5e66b7c (patch)
tree73ae9c1462c18ae6c20efb1169070090fe455e89 /meta
parentd8de4ab1572293d6582491447b01436cfa27fc38 (diff)
Good pieces
Diffstat (limited to 'meta')
-rw-r--r--meta/info.go4
-rw-r--r--meta/piece.go12
2 files changed, 14 insertions, 2 deletions
diff --git a/meta/info.go b/meta/info.go
index eba5318..da117a5 100644
--- a/meta/info.go
+++ b/meta/info.go
@@ -57,12 +57,12 @@ func (i Info) FileLength(n int) int64 {
return i.Files[n].Length
}
-func (i Info) GetPieces() []Piece {
+func (i Info) GetPieces() Pieces {
n, last := i.Full(), i.Last()
if last > 0 {
n++
}
- p := make([]Piece, n)
+ p := make(Pieces, n)
for k := 0; k < n; k++ {
p[k].Length = i.PieceLength
if k+1 == n {
diff --git a/meta/piece.go b/meta/piece.go
index a3119e9..def03fb 100644
--- a/meta/piece.go
+++ b/meta/piece.go
@@ -2,6 +2,8 @@ package meta
import "crypto/sha1"
+type Pieces []Piece
+
type Piece struct {
Offset int64
Length int
@@ -13,3 +15,13 @@ func (p *Piece) Check(b []byte) bool {
p.Ok = sha1.Sum(b) == p.Sum
return p.Ok
}
+
+func (p Pieces) Good() float64 {
+ var good int
+ for _, v := range p {
+ if v.Ok {
+ good++
+ }
+ }
+ return float64(good) * 100.0 / float64(len(p))
+}