Close() error
}
-// Interacts with torrent piece data. Optional interfaces to implement include io.WriterTo, such as
-// when a piece supports a more efficient way to write out incomplete chunks
+// Interacts with torrent piece data. Optional interfaces to implement include:
+// io.WriterTo, such as when a piece supports a more efficient way to write out incomplete chunks.
+// SelfHashing, such as when a piece supports a more efficient way to hash its contents.
type PieceImpl interface {
// These interfaces are not as strict as normally required. They can
// assume that the parameters are appropriate for the dimensions of the
Complete bool
Ok bool
}
+
+// Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
+type SelfHashing interface {
+ SelfHash() (metainfo.Hash, error)
+}
}
func (t *Torrent) hashPiece(piece pieceIndex) (ret metainfo.Hash, err error) {
- hash := pieceHash.New()
p := t.piece(piece)
p.waitNoPendingWrites()
storagePiece := t.pieces[piece].Storage()
+
+ //Does the backend want to do its own hashing?
+ if i, ok := storagePiece.PieceImpl.(storage.SelfHashing); ok {
+ var sum metainfo.Hash
+ //log.Printf("A piece decided to self-hash: %d", piece)
+ sum, err = i.SelfHash()
+ missinggo.CopyExact(&ret, sum)
+ return
+ }
+
+ hash := pieceHash.New()
const logPieceContents = false
if logPieceContents {
var examineBuf bytes.Buffer