]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/fileinfo.go
Drop support for go 1.20
[btrtrc.git] / metainfo / fileinfo.go
1 package metainfo
2
3 import "strings"
4
5 // Information specific to a single file inside the MetaInfo structure.
6 type FileInfo struct {
7         Length   int64    `bencode:"length"` // BEP3
8         Path     []string `bencode:"path"`   // BEP3
9         PathUtf8 []string `bencode:"path.utf-8,omitempty"`
10 }
11
12 func (fi *FileInfo) DisplayPath(info *Info) string {
13         if info.IsDir() {
14                 return strings.Join(fi.BestPath(), "/")
15         } else {
16                 return info.BestName()
17         }
18 }
19
20 func (me FileInfo) Offset(info *Info) (ret int64) {
21         for _, fi := range info.UpvertedFiles() {
22                 if me.DisplayPath(info) == fi.DisplayPath(info) {
23                         return
24                 }
25                 ret += fi.Length
26         }
27         panic("not found")
28 }
29
30 func (fi FileInfo) BestPath() []string {
31         if len(fi.PathUtf8) != 0 {
32                 return fi.PathUtf8
33         }
34         return fi.Path
35 }