7 "github.com/anacrolix/chansync/events"
8 "github.com/anacrolix/missinggo/v2/pubsub"
9 "github.com/anacrolix/sync"
11 "github.com/anacrolix/torrent/metainfo"
14 // The Torrent's infohash. This is fixed and cannot change. It uniquely identifies a torrent.
15 func (t *Torrent) InfoHash() metainfo.Hash {
16 return *t.canonicalShortInfohash()
19 // Returns a channel that is closed when the info (.Info()) for the torrent has become available.
20 func (t *Torrent) GotInfo() events.Done {
24 // Returns the metainfo info dictionary, or nil if it's not yet available.
25 func (t *Torrent) Info() (info *metainfo.Info) {
32 // Returns a Reader bound to the torrent's data. All read calls block until the data requested is
33 // actually available. Note that you probably want to ensure the Torrent Info is available first.
34 func (t *Torrent) NewReader() Reader {
35 return t.newReader(0, t.length())
38 func (t *Torrent) newReader(offset, length int64) Reader {
45 r.readaheadFunc = defaultReadaheadFunc
50 type PieceStateRuns []PieceStateRun
52 func (me PieceStateRuns) String() (s string) {
54 var sb strings.Builder
55 sb.WriteString(me[0].String())
56 for i := 1; i < len(me); i += 1 {
58 sb.WriteString(me[i].String())
65 // Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of
66 // the state run-lengths is the number of pieces in the torrent.
67 func (t *Torrent) PieceStateRuns() (runs PieceStateRuns) {
69 runs = t.pieceStateRuns()
74 func (t *Torrent) PieceState(piece pieceIndex) (ps PieceState) {
76 ps = t.pieceState(piece)
81 // The number of pieces in the torrent. This requires that the info has been
83 func (t *Torrent) NumPieces() pieceIndex {
87 // Get missing bytes count for specific piece.
88 func (t *Torrent) PieceBytesMissing(piece int) int64 {
92 return int64(t.pieces[piece].bytesLeft())
95 // Drop the torrent from the client, and close it. It's always safe to do
96 // this. No data corruption can, or should occur to either the torrent's data,
97 // or connected peers.
98 func (t *Torrent) Drop() {
103 err := t.cl.dropTorrent(t, &wg)
109 // Number of bytes of the entire torrent we have completed. This is the sum of
110 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
111 // for download rate, as it can go down when pieces are lost or fail checks.
112 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
113 func (t *Torrent) BytesCompleted() int64 {
116 return t.bytesCompleted()
119 // The subscription emits as (int) the index of pieces as their state changes.
120 // A state change is when the PieceState for a piece alters in value.
121 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription[PieceStateChange] {
122 return t.pieceStateChanges.Subscribe()
125 // Returns true if the torrent is currently being seeded. This occurs when the
126 // client is willing to upload without wanting anything in return.
127 func (t *Torrent) Seeding() (ret bool) {
134 // Clobbers the torrent display name if metainfo is unavailable.
135 // The display name is used as the torrent name while the metainfo is unavailable.
136 func (t *Torrent) SetDisplayName(dn string) {
144 // The current working name for the torrent. Either the name in the info dict,
145 // or a display name given such as by the dn value in a magnet link, or "".
146 func (t *Torrent) Name() string {
150 // The completed length of all the torrent data, in all its files. This is
151 // derived from the torrent info, when it is available.
152 func (t *Torrent) Length() int64 {
153 return t._length.Value
156 // Returns a run-time generated metainfo for the torrent that includes the
157 // info bytes and announce-list as currently known to the client.
158 func (t *Torrent) Metainfo() metainfo.MetaInfo {
161 return t.newMetaInfo()
164 func (t *Torrent) addReader(r *reader) {
167 if t.readers == nil {
168 t.readers = make(map[*reader]struct{})
170 t.readers[r] = struct{}{}
174 func (t *Torrent) deleteReader(r *reader) {
179 // Raise the priorities of pieces in the range [begin, end) to at least Normal
180 // priority. Piece indexes are not the same as bytes. Requires that the info
181 // has been obtained, see Torrent.Info and Torrent.GotInfo.
182 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
184 t.downloadPiecesLocked(begin, end)
188 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
189 for i := begin; i < end; i++ {
190 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
191 t.updatePiecePriority(i, "Torrent.DownloadPieces")
196 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
198 t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces")
202 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) {
203 for i := begin; i < end; i++ {
205 if p.priority == PiecePriorityNone {
208 p.priority = PiecePriorityNone
209 t.updatePiecePriority(i, reason)
213 func (t *Torrent) initFiles() {
216 t.files = new([]*File)
217 for _, fi := range t.info.UpvertedFiles() {
218 *t.files = append(*t.files, &File{
220 strings.Join(append([]string{info.BestName()}, fi.BestPath()...), "/"),
224 fi.DisplayPath(info),
229 if info.FilesArePieceAligned() {
230 offset = (offset + info.PieceLength - 1) / info.PieceLength * info.PieceLength
235 // Returns handles to the files in the torrent. This requires that the Info is
237 func (t *Torrent) Files() []*File {
241 func (t *Torrent) AddPeers(pp []PeerInfo) (n int) {
248 // Marks the entire torrent for download. Requires the info first, see
249 // GotInfo. Sets piece priorities for historical reasons.
250 func (t *Torrent) DownloadAll() {
251 t.DownloadPieces(0, t.numPieces())
254 func (t *Torrent) String() string {
257 return t.canonicalShortInfohash().HexString()
259 return strconv.Quote(s)
263 func (t *Torrent) AddTrackers(announceList [][]string) {
266 t.addTrackers(announceList)
269 func (t *Torrent) Piece(i pieceIndex) *Piece {
273 func (t *Torrent) PeerConns() []*PeerConn {
276 ret := make([]*PeerConn, 0, len(t.conns))
277 for c := range t.conns {
283 func (t *Torrent) WebseedPeerConns() []*Peer {
286 ret := make([]*Peer, 0, len(t.conns))
287 for _, c := range t.webSeeds {