2015-04-14 21:59:41 +08:00
|
|
|
package torrent
|
|
|
|
|
2015-04-28 13:24:17 +08:00
|
|
|
import (
|
2019-02-16 15:33:14 +08:00
|
|
|
"strconv"
|
2016-05-03 14:47:11 +08:00
|
|
|
"strings"
|
|
|
|
|
2015-09-06 10:33:22 +08:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2015-04-28 13:24:17 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
2015-09-06 10:31:23 +08:00
|
|
|
// The torrent's infohash. This is fixed and cannot change. It uniquely
|
|
|
|
// identifies a torrent.
|
2016-04-04 11:01:31 +08:00
|
|
|
func (t *Torrent) InfoHash() metainfo.Hash {
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.infoHash
|
2015-08-02 01:55:48 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:10:10 +08:00
|
|
|
// Returns a channel that is closed when the info (.Info()) for the torrent
|
|
|
|
// has become available.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) GotInfo() <-chan struct{} {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-05-09 13:47:39 +08:00
|
|
|
return t.gotMetainfo.C()
|
2015-04-28 13:24:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:49:34 +08:00
|
|
|
// Returns the metainfo info dictionary, or nil if it's not yet available.
|
2016-08-26 18:29:05 +08:00
|
|
|
func (t *Torrent) Info() *metainfo.Info {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.info
|
2015-04-14 21:59:41 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 11:30:55 +08:00
|
|
|
// Returns a Reader bound to the torrent's data. All read calls block until
|
2016-01-28 14:22:10 +08:00
|
|
|
// the data requested is actually available.
|
2018-01-06 13:37:13 +08:00
|
|
|
func (t *Torrent) NewReader() Reader {
|
|
|
|
r := reader{
|
2018-07-25 11:41:50 +08:00
|
|
|
mu: t.cl.locker(),
|
2016-04-03 16:40:43 +08:00
|
|
|
t: t,
|
2015-04-14 21:59:41 +08:00
|
|
|
readahead: 5 * 1024 * 1024,
|
2018-01-06 20:15:41 +08:00
|
|
|
length: *t.length,
|
2015-04-14 21:59:41 +08:00
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
t.addReader(&r)
|
|
|
|
return &r
|
2015-04-14 21:59:41 +08:00
|
|
|
}
|
2015-06-01 16:22:12 +08:00
|
|
|
|
|
|
|
// Returns the state of pieces of the torrent. They are grouped into runs of
|
|
|
|
// same state. The sum of the state run lengths is the number of pieces
|
|
|
|
// in the torrent.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) PieceStateRuns() []PieceStateRun {
|
2019-01-08 12:46:03 +08:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.pieceStateRuns()
|
2015-06-01 16:22:12 +08:00
|
|
|
}
|
2015-06-23 00:02:22 +08:00
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) PieceState(piece pieceIndex) PieceState {
|
2019-01-08 12:46:03 +08:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.pieceState(piece)
|
2016-02-07 18:57:57 +08:00
|
|
|
}
|
|
|
|
|
2016-01-16 22:49:34 +08:00
|
|
|
// The number of pieces in the torrent. This requires that the info has been
|
|
|
|
// obtained first.
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) NumPieces() pieceIndex {
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.numPieces()
|
2015-06-23 00:02:22 +08:00
|
|
|
}
|
|
|
|
|
2017-06-02 12:46:28 +08:00
|
|
|
// Get missing bytes count for specific piece.
|
|
|
|
func (t *Torrent) PieceBytesMissing(piece int) int64 {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2017-06-02 12:46:28 +08:00
|
|
|
|
|
|
|
return int64(t.pieces[piece].bytesLeft())
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:52:30 +08:00
|
|
|
// Drop the torrent from the client, and close it. It's always safe to do
|
|
|
|
// this. No data corruption can, or should occur to either the torrent's data,
|
|
|
|
// or connected peers.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) Drop() {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
2016-04-03 16:40:43 +08:00
|
|
|
t.cl.dropTorrent(t.infoHash)
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.unlock()
|
2015-06-23 00:02:22 +08:00
|
|
|
}
|
2015-07-21 20:54:02 +08:00
|
|
|
|
2018-01-27 09:01:09 +08:00
|
|
|
// Number of bytes of the entire torrent we have completed. This is the sum of
|
|
|
|
// completed pieces, and dirtied chunks of incomplete pieces. Do not use this
|
|
|
|
// for download rate, as it can go down when pieces are lost or fail checks.
|
|
|
|
// Sample Torrent.Stats.DataBytesRead for actual file data download rate.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) BytesCompleted() int64 {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2016-04-03 16:40:43 +08:00
|
|
|
return t.bytesCompleted()
|
2015-07-21 20:54:02 +08:00
|
|
|
}
|
2015-09-06 10:33:22 +08:00
|
|
|
|
2015-12-12 11:00:07 +08:00
|
|
|
// The subscription emits as (int) the index of pieces as their state changes.
|
|
|
|
// A state change is when the PieceState for a piece alters in value.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
|
|
|
|
return t.pieceStateChanges.Subscribe()
|
2015-09-06 10:33:22 +08:00
|
|
|
}
|
2015-11-22 15:44:33 +08:00
|
|
|
|
2015-12-12 11:00:07 +08:00
|
|
|
// Returns true if the torrent is currently being seeded. This occurs when the
|
|
|
|
// client is willing to upload without wanting anything in return.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) Seeding() bool {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-05-11 19:44:55 +08:00
|
|
|
return t.seeding()
|
2015-11-22 15:44:33 +08:00
|
|
|
}
|
2015-12-12 11:03:04 +08:00
|
|
|
|
|
|
|
// Clobbers the torrent display name. The display name is used as the torrent
|
|
|
|
// name if the metainfo is not available.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) SetDisplayName(dn string) {
|
2019-03-20 08:01:56 +08:00
|
|
|
t.nameMu.Lock()
|
|
|
|
defer t.nameMu.Unlock()
|
|
|
|
if t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.displayName = dn
|
2015-12-12 11:03:04 +08:00
|
|
|
}
|
2016-01-16 21:14:15 +08:00
|
|
|
|
2016-01-16 22:49:34 +08:00
|
|
|
// The current working name for the torrent. Either the name in the info dict,
|
|
|
|
// or a display name given such as by the dn value in a magnet link, or "".
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) Name() string {
|
|
|
|
return t.name()
|
2016-01-16 21:14:15 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:10:10 +08:00
|
|
|
// The completed length of all the torrent data, in all its files. This is
|
|
|
|
// derived from the torrent info, when it is available.
|
2016-04-03 16:40:43 +08:00
|
|
|
func (t *Torrent) Length() int64 {
|
2018-01-06 20:15:41 +08:00
|
|
|
return *t.length
|
2016-01-16 21:14:15 +08:00
|
|
|
}
|
2016-01-16 22:49:04 +08:00
|
|
|
|
|
|
|
// Returns a run-time generated metainfo for the torrent that includes the
|
|
|
|
// info bytes and announce-list as currently known to the client.
|
2016-08-26 18:29:05 +08:00
|
|
|
func (t *Torrent) Metainfo() metainfo.MetaInfo {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-05-22 20:45:08 +08:00
|
|
|
return t.newMetaInfo()
|
2016-01-16 22:49:04 +08:00
|
|
|
}
|
2016-01-18 15:35:14 +08:00
|
|
|
|
2018-01-06 13:37:13 +08:00
|
|
|
func (t *Torrent) addReader(r *reader) {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-04-03 16:40:43 +08:00
|
|
|
if t.readers == nil {
|
2018-01-06 13:37:13 +08:00
|
|
|
t.readers = make(map[*reader]struct{})
|
2016-01-18 15:35:14 +08:00
|
|
|
}
|
2016-04-03 16:40:43 +08:00
|
|
|
t.readers[r] = struct{}{}
|
2016-10-31 16:00:08 +08:00
|
|
|
r.posChanged()
|
2016-01-18 15:35:14 +08:00
|
|
|
}
|
|
|
|
|
2018-01-06 13:37:13 +08:00
|
|
|
func (t *Torrent) deleteReader(r *reader) {
|
2016-04-03 16:40:43 +08:00
|
|
|
delete(t.readers, r)
|
|
|
|
t.readersChanged()
|
2016-01-18 15:35:14 +08:00
|
|
|
}
|
2016-01-18 22:28:56 +08:00
|
|
|
|
2018-05-18 12:06:28 +08:00
|
|
|
// Raise the priorities of pieces in the range [begin, end) to at least Normal
|
|
|
|
// priority. Piece indexes are not the same as bytes. Requires that the info
|
|
|
|
// has been obtained, see Torrent.Info and Torrent.GotInfo.
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2018-01-27 11:31:31 +08:00
|
|
|
t.downloadPiecesLocked(begin, end)
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
|
2018-01-25 14:18:36 +08:00
|
|
|
for i := begin; i < end; i++ {
|
|
|
|
if t.pieces[i].priority.Raise(PiecePriorityNormal) {
|
|
|
|
t.updatePiecePriority(i)
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 22:18:54 +08:00
|
|
|
}
|
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) CancelPieces(begin, end pieceIndex) {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2018-01-27 11:31:31 +08:00
|
|
|
t.cancelPiecesLocked(begin, end)
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
|
2018-01-25 14:18:36 +08:00
|
|
|
for i := begin; i < end; i++ {
|
|
|
|
p := &t.pieces[i]
|
|
|
|
if p.priority == PiecePriorityNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.priority = PiecePriorityNone
|
|
|
|
t.updatePiecePriority(i)
|
|
|
|
}
|
2016-02-20 11:39:56 +08:00
|
|
|
}
|
2016-05-03 14:47:11 +08:00
|
|
|
|
2018-01-21 19:49:12 +08:00
|
|
|
func (t *Torrent) initFiles() {
|
2016-05-03 14:47:11 +08:00
|
|
|
var offset int64
|
2018-01-21 19:49:12 +08:00
|
|
|
t.files = new([]*File)
|
|
|
|
for _, fi := range t.info.UpvertedFiles() {
|
|
|
|
*t.files = append(*t.files, &File{
|
2016-05-03 14:47:11 +08:00
|
|
|
t,
|
2018-01-21 19:49:12 +08:00
|
|
|
strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
|
2016-05-03 14:47:11 +08:00
|
|
|
offset,
|
|
|
|
fi.Length,
|
|
|
|
fi,
|
2018-01-21 19:49:12 +08:00
|
|
|
PiecePriorityNone,
|
2016-05-03 14:47:11 +08:00
|
|
|
})
|
|
|
|
offset += fi.Length
|
|
|
|
}
|
2018-01-21 19:49:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns handles to the files in the torrent. This requires that the Info is
|
|
|
|
// available first.
|
|
|
|
func (t *Torrent) Files() []*File {
|
|
|
|
return *t.files
|
2016-05-03 14:47:11 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:26:09 +08:00
|
|
|
func (t *Torrent) AddPeers(pp []Peer) {
|
2016-05-03 14:47:11 +08:00
|
|
|
cl := t.cl
|
2018-07-25 11:41:50 +08:00
|
|
|
cl.lock()
|
|
|
|
defer cl.unlock()
|
2016-06-15 13:29:47 +08:00
|
|
|
t.addPeers(pp)
|
2016-05-03 14:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marks the entire torrent for download. Requires the info first, see
|
2018-01-25 14:18:36 +08:00
|
|
|
// GotInfo. Sets piece priorities for historical reasons.
|
2016-05-03 14:47:11 +08:00
|
|
|
func (t *Torrent) DownloadAll() {
|
2018-01-25 14:18:36 +08:00
|
|
|
t.DownloadPieces(0, t.numPieces())
|
2016-05-03 14:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) String() string {
|
|
|
|
s := t.name()
|
|
|
|
if s == "" {
|
2019-02-16 15:33:14 +08:00
|
|
|
return t.infoHash.HexString()
|
|
|
|
} else {
|
|
|
|
return strconv.Quote(s)
|
2016-05-03 14:47:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 08:18:58 +08:00
|
|
|
|
2017-02-15 15:40:30 +08:00
|
|
|
func (t *Torrent) AddTrackers(announceList [][]string) {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2017-02-15 15:40:30 +08:00
|
|
|
t.addTrackers(announceList)
|
2016-05-23 08:18:58 +08:00
|
|
|
}
|
2017-09-15 17:22:32 +08:00
|
|
|
|
2018-07-12 07:15:15 +08:00
|
|
|
func (t *Torrent) Piece(i pieceIndex) *Piece {
|
2018-07-25 11:41:50 +08:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2017-09-15 17:22:32 +08:00
|
|
|
return &t.pieces[i]
|
|
|
|
}
|