}
}
-func (me FileInfo) Offset(info *Info) (ret int64) {
- for _, fi := range info.UpvertedFiles() {
- if me.DisplayPath(info) == fi.DisplayPath(info) {
- return
- }
- ret += fi.Length
- }
- panic("not found")
-}
-
-func (fi FileInfo) BestPath() []string {
+func (fi *FileInfo) BestPath() []string {
if len(fi.PathUtf8) != 0 {
return fi.PathUtf8
}
}
func (info *Info) Piece(index int) Piece {
- return Piece{info, pieceIndex(index)}
+ return Piece{info, index}
}
func (info *Info) BestName() string {
type pieceIndex = int
func (p Piece) Length() int64 {
- if int(p.i) == p.Info.NumPieces()-1 {
+ if p.Info.HasV2() {
+ var offset int64
+ pieceLength := p.Info.PieceLength
+ lastFileEnd := int64(0)
+ done := false
+ p.Info.FileTree.UpvertedFiles(nil, func(fi FileInfo) {
+ if done {
+ return
+ }
+ fileStartPiece := int(offset / pieceLength)
+ if fileStartPiece > p.i {
+ done = true
+ return
+ }
+ lastFileEnd = offset + fi.Length
+ offset = (lastFileEnd + pieceLength - 1) / pieceLength * pieceLength
+ })
+ ret := min(lastFileEnd-int64(p.i)*pieceLength, pieceLength)
+ if ret <= 0 {
+ panic(ret)
+ }
+ return ret
+ }
+ if p.i == p.Info.NumPieces()-1 {
return p.Info.TotalLength() - int64(p.i)*p.Info.PieceLength
}
return p.Info.PieceLength
return
}
-func (p Piece) Index() pieceIndex {
+func (p Piece) Index() int {
return p.i
}