piece-completion.go 846 B

1234567891011121314151617181920212223242526272829303132
  1. package storage
  2. import (
  3. "os"
  4. "github.com/anacrolix/log"
  5. "github.com/anacrolix/torrent/metainfo"
  6. )
  7. type PieceCompletionGetSetter interface {
  8. Get(metainfo.PieceKey) (Completion, error)
  9. Set(_ metainfo.PieceKey, complete bool) error
  10. }
  11. // Implementations track the completion of pieces. It must be concurrent-safe.
  12. type PieceCompletion interface {
  13. PieceCompletionGetSetter
  14. Close() error
  15. }
  16. func pieceCompletionForDir(dir string) (ret PieceCompletion) {
  17. // This should be happening before sqlite attempts to open a database in the intended directory.
  18. os.MkdirAll(dir, 0o700)
  19. ret, err := NewDefaultPieceCompletionForDir(dir)
  20. if err != nil {
  21. // This kinda sux using the global logger. This code is ancient.
  22. log.Levelf(log.Warning, "couldn't open piece completion db in %q: %s", dir, err)
  23. ret = NewMapPieceCompletion()
  24. }
  25. return
  26. }