interface.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package storage
  2. import (
  3. "context"
  4. "io"
  5. g "github.com/anacrolix/generics"
  6. "github.com/anacrolix/torrent/metainfo"
  7. )
  8. type ClientImplCloser interface {
  9. ClientImpl
  10. Close() error
  11. }
  12. // Represents data storage for an unspecified torrent.
  13. type ClientImpl interface {
  14. OpenTorrent(ctx context.Context, info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error)
  15. }
  16. type TorrentCapacity *func() (cap int64, capped bool)
  17. // Data storage bound to a torrent.
  18. type TorrentImpl struct {
  19. // v2 infos might not have the piece hash available even if we have the info. The
  20. // metainfo.Piece.Hash method was removed to enforce this.
  21. Piece func(p metainfo.Piece) PieceImpl
  22. // Preferred over PieceWithHash. Called with the piece hash if it's available.
  23. PieceWithHash func(p metainfo.Piece, pieceHash g.Option[[]byte]) PieceImpl
  24. Close func() error
  25. Flush func() error
  26. // Storages that share the same space, will provide equal pointers. The function is called once
  27. // to determine the storage for torrents sharing the same function pointer, and mutated in
  28. // place.
  29. Capacity TorrentCapacity
  30. }
  31. // Interacts with torrent piece data. Optional interfaces to implement include:
  32. //
  33. // io.WriterTo, such as when a piece supports a more efficient way to write out incomplete chunks.
  34. // SelfHashing, such as when a piece supports a more efficient way to hash its contents.
  35. type PieceImpl interface {
  36. // These interfaces are not as strict as normally required. They can
  37. // assume that the parameters are appropriate for the dimensions of the
  38. // piece.
  39. io.ReaderAt
  40. io.WriterAt
  41. // Called when the client believes the piece data will pass a hash check.
  42. // The storage can move or mark the piece data as read-only as it sees
  43. // fit.
  44. MarkComplete() error
  45. MarkNotComplete() error
  46. // Returns true if the piece is complete.
  47. Completion() Completion
  48. }
  49. type Completion struct {
  50. Complete bool
  51. Ok bool
  52. Err error
  53. }
  54. // Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
  55. type SelfHashing interface {
  56. SelfHash() (metainfo.Hash, error)
  57. }