Fix file piece index offset calculations
This commit is contained in:
parent
29aa07f1a9
commit
21108bf6ec
12
file.go
12
file.go
|
@ -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
|
||||
}
|
||||
|
|
17
torrent.go
17
torrent.go
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue