2015-04-14 13:59:41 +00:00
|
|
|
package torrent
|
|
|
|
|
2015-04-28 05:24:17 +00:00
|
|
|
import (
|
2019-02-16 07:33:14 +00:00
|
|
|
"strconv"
|
2016-05-03 06:47:11 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-07 00:50:30 +00:00
|
|
|
"github.com/anacrolix/chansync/events"
|
2022-03-17 03:55:55 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2/pubsub"
|
2021-09-10 04:14:57 +00:00
|
|
|
"github.com/anacrolix/sync"
|
2019-08-21 10:58:40 +00:00
|
|
|
|
2015-04-28 05:24:17 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
2020-02-21 00:07:50 +00:00
|
|
|
// The Torrent's infohash. This is fixed and cannot change. It uniquely identifies a torrent.
|
2016-04-04 03:01:31 +00:00
|
|
|
func (t *Torrent) InfoHash() metainfo.Hash {
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.infoHash
|
2015-08-01 17:55:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:49:15 +00:00
|
|
|
// Returns a channel that is closed when the info (.Info()) for the torrent has become available.
|
2021-10-07 00:50:30 +00:00
|
|
|
func (t *Torrent) GotInfo() events.Done {
|
|
|
|
return t.gotMetainfoC
|
2015-04-28 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:49:34 +00:00
|
|
|
// Returns the metainfo info dictionary, or nil if it's not yet available.
|
2021-09-13 01:41:11 +00:00
|
|
|
func (t *Torrent) Info() (info *metainfo.Info) {
|
|
|
|
t.nameMu.RLock()
|
|
|
|
info = t.info
|
|
|
|
t.nameMu.RUnlock()
|
|
|
|
return
|
2015-04-14 13:59:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 03:12:44 +00:00
|
|
|
// Returns a Reader bound to the torrent's data. All read calls block until the data requested is
|
|
|
|
// actually available. Note that you probably want to ensure the Torrent Info is available first.
|
2018-01-06 05:37:13 +00:00
|
|
|
func (t *Torrent) NewReader() Reader {
|
2022-05-12 03:47:12 +00:00
|
|
|
return t.newReader(0, t.length())
|
2021-09-09 07:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) newReader(offset, length int64) Reader {
|
2018-01-06 05:37:13 +00:00
|
|
|
r := reader{
|
2021-09-09 10:55:09 +00:00
|
|
|
mu: t.cl.locker(),
|
|
|
|
t: t,
|
|
|
|
offset: offset,
|
|
|
|
length: length,
|
2015-04-14 13:59:41 +00:00
|
|
|
}
|
2021-11-14 02:52:05 +00:00
|
|
|
r.readaheadFunc = defaultReadaheadFunc
|
2018-01-06 05:37:13 +00:00
|
|
|
t.addReader(&r)
|
|
|
|
return &r
|
2015-04-14 13:59:41 +00:00
|
|
|
}
|
2015-06-01 08:22:12 +00:00
|
|
|
|
2020-02-27 05:42:33 +00:00
|
|
|
type PieceStateRuns []PieceStateRun
|
|
|
|
|
2021-09-01 04:06:25 +00:00
|
|
|
func (me PieceStateRuns) String() (s string) {
|
|
|
|
if len(me) > 0 {
|
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteString(me[0].String())
|
|
|
|
for i := 1; i < len(me); i += 1 {
|
|
|
|
sb.WriteByte(' ')
|
|
|
|
sb.WriteString(me[i].String())
|
|
|
|
}
|
|
|
|
return sb.String()
|
2020-02-27 05:42:33 +00:00
|
|
|
}
|
2021-09-01 04:06:25 +00:00
|
|
|
return
|
2020-02-27 05:42:33 +00: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.
|
2021-09-01 04:06:25 +00:00
|
|
|
func (t *Torrent) PieceStateRuns() (runs PieceStateRuns) {
|
2019-01-08 04:46:03 +00:00
|
|
|
t.cl.rLock()
|
2021-09-01 04:06:25 +00:00
|
|
|
runs = t.pieceStateRuns()
|
|
|
|
t.cl.rUnlock()
|
|
|
|
return
|
2015-06-01 08:22:12 +00:00
|
|
|
}
|
2015-06-22 16:02:22 +00:00
|
|
|
|
2021-09-15 00:14:28 +00:00
|
|
|
func (t *Torrent) PieceState(piece pieceIndex) (ps PieceState) {
|
2019-01-08 04:46:03 +00:00
|
|
|
t.cl.rLock()
|
2021-09-15 00:14:28 +00:00
|
|
|
ps = t.pieceState(piece)
|
|
|
|
t.cl.rUnlock()
|
2021-10-05 06:48:52 +00:00
|
|
|
return
|
2016-02-07 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 14:49:34 +00:00
|
|
|
// The number of pieces in the torrent. This requires that the info has been
|
|
|
|
// obtained first.
|
2018-07-11 23:15:15 +00:00
|
|
|
func (t *Torrent) NumPieces() pieceIndex {
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.numPieces()
|
2015-06-22 16:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 04:46:28 +00:00
|
|
|
// Get missing bytes count for specific piece.
|
|
|
|
func (t *Torrent) PieceBytesMissing(piece int) int64 {
|
2022-07-13 10:04:03 +00:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2017-06-02 04:46:28 +00:00
|
|
|
|
|
|
|
return int64(t.pieces[piece].bytesLeft())
|
|
|
|
}
|
|
|
|
|
2016-04-18 11:52:30 +00: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 08:40:43 +00:00
|
|
|
func (t *Torrent) Drop() {
|
2021-09-10 04:14:57 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.lock()
|
2021-05-09 07:31:45 +00:00
|
|
|
defer t.cl.unlock()
|
2023-05-27 04:47:25 +00:00
|
|
|
err := t.cl.dropTorrent(t.infoHash, &wg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-06-22 16:02:22 +00:00
|
|
|
}
|
2015-07-21 12:54:02 +00:00
|
|
|
|
2018-01-27 01:01:09 +00: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 08:40:43 +00:00
|
|
|
func (t *Torrent) BytesCompleted() int64 {
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.bytesCompleted()
|
2015-07-21 12:54:02 +00:00
|
|
|
}
|
2015-09-06 02:33:22 +00:00
|
|
|
|
2015-12-12 03:00:07 +00: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.
|
2022-03-17 03:55:55 +00:00
|
|
|
func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription[PieceStateChange] {
|
2016-04-03 08:40:43 +00:00
|
|
|
return t.pieceStateChanges.Subscribe()
|
2015-09-06 02:33:22 +00:00
|
|
|
}
|
2015-11-22 07:44:33 +00:00
|
|
|
|
2015-12-12 03:00:07 +00:00
|
|
|
// Returns true if the torrent is currently being seeded. This occurs when the
|
|
|
|
// client is willing to upload without wanting anything in return.
|
2021-09-15 00:28:14 +00:00
|
|
|
func (t *Torrent) Seeding() (ret bool) {
|
2022-07-13 10:04:03 +00:00
|
|
|
t.cl.rLock()
|
2021-09-15 00:28:14 +00:00
|
|
|
ret = t.seeding()
|
2022-07-13 10:04:03 +00:00
|
|
|
t.cl.rUnlock()
|
2021-09-15 00:28:14 +00:00
|
|
|
return
|
2015-11-22 07:44:33 +00:00
|
|
|
}
|
2015-12-12 03:03:04 +00:00
|
|
|
|
2021-09-15 00:27:52 +00:00
|
|
|
// Clobbers the torrent display name if metainfo is unavailable.
|
|
|
|
// The display name is used as the torrent name while the metainfo is unavailable.
|
2016-04-03 08:40:43 +00:00
|
|
|
func (t *Torrent) SetDisplayName(dn string) {
|
2019-03-20 00:01:56 +00:00
|
|
|
t.nameMu.Lock()
|
2021-09-15 00:27:52 +00:00
|
|
|
if !t.haveInfo() {
|
|
|
|
t.displayName = dn
|
2019-03-20 00:01:56 +00:00
|
|
|
}
|
2021-09-15 00:27:52 +00:00
|
|
|
t.nameMu.Unlock()
|
2015-12-12 03:03:04 +00:00
|
|
|
}
|
2016-01-16 13:14:15 +00:00
|
|
|
|
2016-01-16 14:49:34 +00: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 08:40:43 +00:00
|
|
|
func (t *Torrent) Name() string {
|
|
|
|
return t.name()
|
2016-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:10:10 +00: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 08:40:43 +00:00
|
|
|
func (t *Torrent) Length() int64 {
|
2022-06-20 01:37:25 +00:00
|
|
|
return t._length.Value
|
2016-01-16 13:14:15 +00:00
|
|
|
}
|
2016-01-16 14:49:04 +00: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 10:29:05 +00:00
|
|
|
func (t *Torrent) Metainfo() metainfo.MetaInfo {
|
2022-07-13 10:04:03 +00:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2016-05-22 12:45:08 +00:00
|
|
|
return t.newMetaInfo()
|
2016-01-16 14:49:04 +00:00
|
|
|
}
|
2016-01-18 07:35:14 +00:00
|
|
|
|
2018-01-06 05:37:13 +00:00
|
|
|
func (t *Torrent) addReader(r *reader) {
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2016-04-03 08:40:43 +00:00
|
|
|
if t.readers == nil {
|
2018-01-06 05:37:13 +00:00
|
|
|
t.readers = make(map[*reader]struct{})
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
2016-04-03 08:40:43 +00:00
|
|
|
t.readers[r] = struct{}{}
|
2016-10-31 08:00:08 +00:00
|
|
|
r.posChanged()
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 05:37:13 +00:00
|
|
|
func (t *Torrent) deleteReader(r *reader) {
|
2016-04-03 08:40:43 +00:00
|
|
|
delete(t.readers, r)
|
|
|
|
t.readersChanged()
|
2016-01-18 07:35:14 +00:00
|
|
|
}
|
2016-01-18 14:28:56 +00:00
|
|
|
|
2018-05-18 04:06:28 +00: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-11 23:15:15 +00:00
|
|
|
func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.lock()
|
2018-01-27 03:31:31 +00:00
|
|
|
t.downloadPiecesLocked(begin, end)
|
2021-09-15 00:13:46 +00:00
|
|
|
t.cl.unlock()
|
2018-01-27 03:31:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 23:15:15 +00:00
|
|
|
func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
|
2018-01-25 06:18:36 +00:00
|
|
|
for i := begin; i < end; i++ {
|
|
|
|
if t.pieces[i].priority.Raise(PiecePriorityNormal) {
|
2021-10-08 22:14:57 +00:00
|
|
|
t.updatePiecePriority(i, "Torrent.DownloadPieces")
|
2018-01-25 06:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 00:52:28 +00:00
|
|
|
func (t *Torrent) CancelPieces(begin, end pieceIndex) {
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.lock()
|
2021-10-08 22:14:57 +00:00
|
|
|
t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces")
|
2021-09-15 00:13:46 +00:00
|
|
|
t.cl.unlock()
|
2018-01-27 03:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 22:14:57 +00:00
|
|
|
func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) {
|
2018-01-25 06:18:36 +00:00
|
|
|
for i := begin; i < end; i++ {
|
|
|
|
p := &t.pieces[i]
|
|
|
|
if p.priority == PiecePriorityNone {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.priority = PiecePriorityNone
|
2021-10-08 22:14:57 +00:00
|
|
|
t.updatePiecePriority(i, reason)
|
2018-01-25 06:18:36 +00:00
|
|
|
}
|
2016-02-20 03:39:56 +00:00
|
|
|
}
|
2016-05-03 06:47:11 +00:00
|
|
|
|
2018-01-21 11:49:12 +00:00
|
|
|
func (t *Torrent) initFiles() {
|
2016-05-03 06:47:11 +00:00
|
|
|
var offset int64
|
2018-01-21 11:49:12 +00:00
|
|
|
t.files = new([]*File)
|
|
|
|
for _, fi := range t.info.UpvertedFiles() {
|
|
|
|
*t.files = append(*t.files, &File{
|
2016-05-03 06:47:11 +00:00
|
|
|
t,
|
2022-03-17 04:07:10 +00:00
|
|
|
strings.Join(append([]string{t.info.BestName()}, fi.BestPath()...), "/"),
|
2016-05-03 06:47:11 +00:00
|
|
|
offset,
|
|
|
|
fi.Length,
|
|
|
|
fi,
|
2022-03-17 04:07:10 +00:00
|
|
|
fi.DisplayPath(t.info),
|
2018-01-21 11:49:12 +00:00
|
|
|
PiecePriorityNone,
|
2016-05-03 06:47:11 +00:00
|
|
|
})
|
|
|
|
offset += fi.Length
|
|
|
|
}
|
2018-01-21 11:49:12 +00: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 06:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:14:07 +00:00
|
|
|
func (t *Torrent) AddPeers(pp []PeerInfo) (n int) {
|
|
|
|
t.cl.lock()
|
2023-05-27 10:13:56 +00:00
|
|
|
defer t.cl.unlock()
|
2021-09-15 00:14:07 +00:00
|
|
|
n = t.addPeers(pp)
|
|
|
|
return
|
2016-05-03 06:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marks the entire torrent for download. Requires the info first, see
|
2018-01-25 06:18:36 +00:00
|
|
|
// GotInfo. Sets piece priorities for historical reasons.
|
2016-05-03 06:47:11 +00:00
|
|
|
func (t *Torrent) DownloadAll() {
|
2018-01-25 06:18:36 +00:00
|
|
|
t.DownloadPieces(0, t.numPieces())
|
2016-05-03 06:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) String() string {
|
|
|
|
s := t.name()
|
|
|
|
if s == "" {
|
2019-02-16 07:33:14 +00:00
|
|
|
return t.infoHash.HexString()
|
|
|
|
} else {
|
|
|
|
return strconv.Quote(s)
|
2016-05-03 06:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:18:58 +00:00
|
|
|
|
2017-02-15 07:40:30 +00:00
|
|
|
func (t *Torrent) AddTrackers(announceList [][]string) {
|
2018-07-25 03:41:50 +00:00
|
|
|
t.cl.lock()
|
|
|
|
defer t.cl.unlock()
|
2017-02-15 07:40:30 +00:00
|
|
|
t.addTrackers(announceList)
|
2016-05-23 00:18:58 +00:00
|
|
|
}
|
2017-09-15 09:22:32 +00:00
|
|
|
|
2018-07-11 23:15:15 +00:00
|
|
|
func (t *Torrent) Piece(i pieceIndex) *Piece {
|
2019-12-18 05:49:15 +00:00
|
|
|
return t.piece(i)
|
2017-09-15 09:22:32 +00:00
|
|
|
}
|
2020-02-21 00:07:50 +00:00
|
|
|
|
|
|
|
func (t *Torrent) PeerConns() []*PeerConn {
|
2020-04-16 07:21:15 +00:00
|
|
|
t.cl.rLock()
|
|
|
|
defer t.cl.rUnlock()
|
2020-02-21 00:07:50 +00:00
|
|
|
ret := make([]*PeerConn, 0, len(t.conns))
|
|
|
|
for c := range t.conns {
|
|
|
|
ret = append(ret, c)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|