Expose metainfo.GeneratePieces

This commit is contained in:
Matt Joiner 2020-05-13 14:32:20 +10:00
parent 533fec840a
commit ccc71463b9
2 changed files with 25 additions and 22 deletions

View File

@ -1,7 +1,6 @@
package metainfo
import (
"crypto/sha1"
"errors"
"fmt"
"io"
@ -85,7 +84,7 @@ func (info *Info) writeFiles(w io.Writer, open func(fi FileInfo) (io.ReadCloser,
// Sets Pieces (the block of piece hashes in the Info) by using the passed
// function to get at the torrent data.
func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) error {
func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) (err error) {
if info.PieceLength == 0 {
return errors.New("piece length must be non-zero")
}
@ -95,26 +94,8 @@ func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error))
pw.CloseWithError(err)
}()
defer pr.Close()
var pieces []byte
for {
hasher := sha1.New()
wn, err := io.CopyN(hasher, pr, info.PieceLength)
if err == io.EOF {
err = nil
}
if err != nil {
return err
}
if wn == 0 {
break
}
pieces = hasher.Sum(pieces)
if wn < info.PieceLength {
break
}
}
info.Pieces = pieces
return nil
info.Pieces, err = GeneratePieces(pr, info.PieceLength, nil)
return
}
func (info *Info) TotalLength() (ret int64) {

22
metainfo/pieces.go Normal file
View File

@ -0,0 +1,22 @@
package metainfo
import (
"crypto/sha1"
"io"
)
func GeneratePieces(r io.Reader, pieceLength int64, b []byte) ([]byte, error) {
for {
h := sha1.New()
written, err := io.CopyN(h, r, pieceLength)
if written > 0 {
b = h.Sum(b)
}
if err == io.EOF {
return b, nil
}
if err != nil {
return b, err
}
}
}