Fix bugs in the new WriteSectionTo hashing implementation

This commit is contained in:
Matt Joiner 2013-10-07 18:58:33 +11:00
parent a44accadbe
commit 43418e9f65
2 changed files with 4 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
"crypto/rand"
"encoding"
"errors"
"fmt"
metainfo "github.com/nsf/libtorgo/torrent"
"io"
"launchpad.net/gommap"
@ -231,12 +232,12 @@ func (t *Torrent) PieceSize(piece int) (size int64) {
func (t *Torrent) HashPiece(piece int) (ps pieceSum) {
hash := PieceHash.New()
n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, int64(piece)*t.MetaInfo.PieceLength)
n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
if err != nil {
panic(err)
}
if n != t.PieceSize(piece) {
panic("hashed wrong number of bytes")
panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceSize(piece), n, piece))
}
copyHashSum(ps[:], hash.Sum(nil))
return

View File

@ -61,7 +61,7 @@ func (me MMapSpan) WriteSectionTo(w io.Writer, off, n int64) (written int64, err
if err != nil {
return true
}
return n != 0
return n == 0
})
return
}