Expose metainfo.GeneratePieces
This commit is contained in:
parent
533fec840a
commit
ccc71463b9
|
@ -1,7 +1,6 @@
|
||||||
package metainfo
|
package metainfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"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
|
// Sets Pieces (the block of piece hashes in the Info) by using the passed
|
||||||
// function to get at the torrent data.
|
// 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 {
|
if info.PieceLength == 0 {
|
||||||
return errors.New("piece length must be non-zero")
|
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)
|
pw.CloseWithError(err)
|
||||||
}()
|
}()
|
||||||
defer pr.Close()
|
defer pr.Close()
|
||||||
var pieces []byte
|
info.Pieces, err = GeneratePieces(pr, info.PieceLength, nil)
|
||||||
for {
|
return
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (info *Info) TotalLength() (ret int64) {
|
func (info *Info) TotalLength() (ret int64) {
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue