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