]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
use path.utf8 first for some torrent (#350)
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "strconv"
5         "strings"
6
7         "github.com/anacrolix/missinggo/pubsub"
8
9         "github.com/anacrolix/torrent/metainfo"
10 )
11
12 // The torrent's infohash. This is fixed and cannot change. It uniquely
13 // identifies a torrent.
14 func (t *Torrent) InfoHash() metainfo.Hash {
15         return t.infoHash
16 }
17
18 // Returns a channel that is closed when the info (.Info()) for the torrent
19 // has become available.
20 func (t *Torrent) GotInfo() <-chan struct{} {
21         t.cl.lock()
22         defer t.cl.unlock()
23         return t.gotMetainfo.C()
24 }
25
26 // Returns the metainfo info dictionary, or nil if it's not yet available.
27 func (t *Torrent) Info() *metainfo.Info {
28         t.cl.lock()
29         defer t.cl.unlock()
30         return t.info
31 }
32
33 // Returns a Reader bound to the torrent's data. All read calls block until
34 // the data requested is actually available.
35 func (t *Torrent) NewReader() Reader {
36         r := reader{
37                 mu:        t.cl.locker(),
38                 t:         t,
39                 readahead: 5 * 1024 * 1024,
40                 length:    *t.length,
41         }
42         t.addReader(&r)
43         return &r
44 }
45
46 // Returns the state of pieces of the torrent. They are grouped into runs of
47 // same state. The sum of the state run lengths is the number of pieces
48 // in the torrent.
49 func (t *Torrent) PieceStateRuns() []PieceStateRun {
50         t.cl.rLock()
51         defer t.cl.rUnlock()
52         return t.pieceStateRuns()
53 }
54
55 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
56         t.cl.rLock()
57         defer t.cl.rUnlock()
58         return t.pieceState(piece)
59 }
60
61 // The number of pieces in the torrent. This requires that the info has been
62 // obtained first.
63 func (t *Torrent) NumPieces() pieceIndex {
64         return t.numPieces()
65 }
66
67 // Get missing bytes count for specific piece.
68 func (t *Torrent) PieceBytesMissing(piece int) int64 {
69         t.cl.lock()
70         defer t.cl.unlock()
71
72         return int64(t.pieces[piece].bytesLeft())
73 }
74
75 // Drop the torrent from the client, and close it. It's always safe to do
76 // this. No data corruption can, or should occur to either the torrent's data,
77 // or connected peers.
78 func (t *Torrent) Drop() {
79         t.cl.lock()
80         t.cl.dropTorrent(t.infoHash)
81         t.cl.unlock()
82 }
83
84 // Number of bytes of the entire torrent we have completed. This is the sum of
85 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
86 // for download rate, as it can go down when pieces are lost or fail checks.
87 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
88 func (t *Torrent) BytesCompleted() int64 {
89         t.cl.rLock()
90         defer t.cl.rUnlock()
91         return t.bytesCompleted()
92 }
93
94 // The subscription emits as (int) the index of pieces as their state changes.
95 // A state change is when the PieceState for a piece alters in value.
96 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
97         return t.pieceStateChanges.Subscribe()
98 }
99
100 // Returns true if the torrent is currently being seeded. This occurs when the
101 // client is willing to upload without wanting anything in return.
102 func (t *Torrent) Seeding() bool {
103         t.cl.lock()
104         defer t.cl.unlock()
105         return t.seeding()
106 }
107
108 // Clobbers the torrent display name. The display name is used as the torrent
109 // name if the metainfo is not available.
110 func (t *Torrent) SetDisplayName(dn string) {
111         t.nameMu.Lock()
112         defer t.nameMu.Unlock()
113         if t.haveInfo() {
114                 return
115         }
116         t.displayName = dn
117 }
118
119 // The current working name for the torrent. Either the name in the info dict,
120 // or a display name given such as by the dn value in a magnet link, or "".
121 func (t *Torrent) Name() string {
122         return t.name()
123 }
124
125 // The completed length of all the torrent data, in all its files. This is
126 // derived from the torrent info, when it is available.
127 func (t *Torrent) Length() int64 {
128         return *t.length
129 }
130
131 // Returns a run-time generated metainfo for the torrent that includes the
132 // info bytes and announce-list as currently known to the client.
133 func (t *Torrent) Metainfo() metainfo.MetaInfo {
134         t.cl.lock()
135         defer t.cl.unlock()
136         return t.newMetaInfo()
137 }
138
139 func (t *Torrent) addReader(r *reader) {
140         t.cl.lock()
141         defer t.cl.unlock()
142         if t.readers == nil {
143                 t.readers = make(map[*reader]struct{})
144         }
145         t.readers[r] = struct{}{}
146         r.posChanged()
147 }
148
149 func (t *Torrent) deleteReader(r *reader) {
150         delete(t.readers, r)
151         t.readersChanged()
152 }
153
154 // Raise the priorities of pieces in the range [begin, end) to at least Normal
155 // priority. Piece indexes are not the same as bytes. Requires that the info
156 // has been obtained, see Torrent.Info and Torrent.GotInfo.
157 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
158         t.cl.lock()
159         defer t.cl.unlock()
160         t.downloadPiecesLocked(begin, end)
161 }
162
163 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
164         for i := begin; i < end; i++ {
165                 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
166                         t.updatePiecePriority(i)
167                 }
168         }
169 }
170
171 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
172         t.cl.lock()
173         defer t.cl.unlock()
174         t.cancelPiecesLocked(begin, end)
175 }
176
177 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
178         for i := begin; i < end; i++ {
179                 p := &t.pieces[i]
180                 if p.priority == PiecePriorityNone {
181                         continue
182                 }
183                 p.priority = PiecePriorityNone
184                 t.updatePiecePriority(i)
185         }
186 }
187
188 func (t *Torrent) initFiles() {
189         var offset int64
190         t.files = new([]*File)
191         for _, fi := range t.info.UpvertedFiles() {
192                 var path []string
193                 if len(fi.PathUTF8) != 0 {
194                         path = fi.PathUTF8
195                 } else {
196                         path = fi.Path
197                 }
198                 *t.files = append(*t.files, &File{
199                         t,
200                         strings.Join(append([]string{t.info.Name}, path...), "/"),
201                         offset,
202                         fi.Length,
203                         fi,
204                         PiecePriorityNone,
205                 })
206                 offset += fi.Length
207         }
208
209 }
210
211 // Returns handles to the files in the torrent. This requires that the Info is
212 // available first.
213 func (t *Torrent) Files() []*File {
214         return *t.files
215 }
216
217 func (t *Torrent) AddPeers(pp []Peer) {
218         cl := t.cl
219         cl.lock()
220         defer cl.unlock()
221         t.addPeers(pp)
222 }
223
224 // Marks the entire torrent for download. Requires the info first, see
225 // GotInfo. Sets piece priorities for historical reasons.
226 func (t *Torrent) DownloadAll() {
227         t.DownloadPieces(0, t.numPieces())
228 }
229
230 func (t *Torrent) String() string {
231         s := t.name()
232         if s == "" {
233                 return t.infoHash.HexString()
234         } else {
235                 return strconv.Quote(s)
236         }
237 }
238
239 func (t *Torrent) AddTrackers(announceList [][]string) {
240         t.cl.lock()
241         defer t.cl.unlock()
242         t.addTrackers(announceList)
243 }
244
245 func (t *Torrent) Piece(i pieceIndex) *Piece {
246         t.cl.lock()
247         defer t.cl.unlock()
248         return &t.pieces[i]
249 }