file-paths.go 1.0 KB

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