Fix file piece index offset calculations

This commit is contained in:
Matt Joiner 2018-01-25 17:01:29 +11:00
parent 29aa07f1a9
commit 21108bf6ec
2 changed files with 21 additions and 8 deletions

12
file.go
View File

@ -121,7 +121,7 @@ func (f *File) SetPriority(prio piecePriority) {
return
}
f.prio = prio
f.t.updatePiecePriorities(f.firstPieceIndex().Int(), f.lastPieceIndex().Int()+1)
f.t.updatePiecePriorities(f.firstPieceIndex().Int(), f.endPieceIndex().Int())
}
// Returns the priority per File.SetPriority.
@ -132,9 +132,15 @@ func (f *File) Priority() piecePriority {
}
func (f *File) firstPieceIndex() pwp.Integer {
if f.t.usualPieceSize() == 0 {
return 0
}
return pwp.Integer(f.offset / int64(f.t.usualPieceSize()))
}
func (f *File) lastPieceIndex() pwp.Integer {
return pwp.Integer((f.offset + f.length) / int64(f.t.usualPieceSize()))
func (f *File) endPieceIndex() pwp.Integer {
if f.t.usualPieceSize() == 0 {
return 0
}
return pwp.Integer((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
}

View File

@ -305,26 +305,33 @@ func (t *Torrent) makePieces() {
piece.index = i
piece.noPendingWrites.L = &piece.pendingWritesMutex
missinggo.CopyExact(piece.hash[:], hash)
piece.files = (*t.files)[pieceFirstFileIndex(piece.torrentBeginOffset(), *t.files):pieceLastFileIndex(piece.torrentEndOffset(), *t.files)]
files := *t.files
beginFile := pieceFirstFileIndex(piece.torrentBeginOffset(), files)
endFile := pieceEndFileIndex(piece.torrentEndOffset(), files)
piece.files = files[beginFile:endFile]
}
}
// Returns the index of the first file containing the piece. files must be
// ordered by offset.
func pieceFirstFileIndex(pieceOffset int64, files []*File) int {
for i, f := range files {
if f.offset+f.length > pieceOffset {
return i
}
}
return -1
return 0
}
func pieceLastFileIndex(pieceEndOffset int64, files []*File) int {
// Returns the index after the last file containing the piece. files must be
// ordered by offset.
func pieceEndFileIndex(pieceEndOffset int64, files []*File) int {
for i, f := range files {
if f.offset+f.length >= pieceEndOffset {
return i
return i + 1
}
}
return -1
return 0
}
func (t *Torrent) cacheLength(info *metainfo.Info) {