github.com/anacrolix/torrent@v1.61.0/storage/file-paths.go (about) 1 package storage 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 "github.com/anacrolix/torrent/metainfo" 9 ) 10 11 // Determines the filepath to be used for each file in a torrent. 12 type FilePathMaker func(opts FilePathMakerOpts) string 13 14 // Determines the directory for a given torrent within a storage client. 15 type TorrentDirFilePathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string 16 17 // Info passed to a FilePathMaker. 18 type FilePathMakerOpts struct { 19 Info *metainfo.Info 20 File *metainfo.FileInfo 21 } 22 23 // defaultPathMaker just returns the storage client's base directory. 24 func defaultPathMaker(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string { 25 return baseDir 26 } 27 28 func infoHashPathMaker(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string { 29 return filepath.Join(baseDir, infoHash.HexString()) 30 } 31 32 func isSubFilepath(base, sub string) bool { 33 rel, err := filepath.Rel(base, sub) 34 if err != nil { 35 return false 36 } 37 return rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) 38 }