4 "bitbucket.org/anacrolix/go.torrent/mmap_span"
7 metainfo "github.com/nsf/libtorgo/torrent"
13 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
14 "launchpad.net/gommap"
18 pieceHash = crypto.SHA1
19 maxRequests = 250 // Maximum pending requests we allow peers to send us.
20 chunkSize = 0x4000 // 16KiB
21 BEP20 = "-GT0000-" // Peer ID client identifier prefix
22 dialTimeout = time.Second * 15
25 type InfoHash [20]byte
27 type pieceSum [20]byte
29 func copyHashSum(dst, src []byte) {
30 if len(dst) != len(src) || copy(dst, src) != len(dst) {
31 panic("hash sum sizes differ")
35 func BytesInfoHash(b []byte) (ih InfoHash) {
36 if len(b) != len(ih) || copy(ih[:], b) != len(ih) {
37 panic("bad infohash bytes")
44 PendingChunkSpecs map[chunkSpec]struct{}
50 func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
51 css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
52 for cs := range p.PendingChunkSpecs {
57 css[i], css[j] = css[j], css[i]
62 func (p *piece) Complete() bool {
63 return len(p.PendingChunkSpecs) == 0 && p.EverHashed
66 func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
67 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
68 cs.Length = pieceLength - cs.Begin
72 type chunkSpec struct {
73 Begin, Length peer_protocol.Integer
77 Index peer_protocol.Integer
81 func newRequest(index, begin, length peer_protocol.Integer) request {
82 return request{index, chunkSpec{begin, length}}
85 type pieceByBytesPendingSlice struct {
86 Pending, Indices []peer_protocol.Integer
89 func (pcs pieceByBytesPendingSlice) Len() int {
90 return len(pcs.Indices)
93 func (me pieceByBytesPendingSlice) Less(i, j int) bool {
94 return me.Pending[me.Indices[i]] < me.Pending[me.Indices[j]]
97 func (me pieceByBytesPendingSlice) Swap(i, j int) {
98 me.Indices[i], me.Indices[j] = me.Indices[j], me.Indices[i]
102 // Requested data not yet available.
103 ErrDataNotReady = errors.New("data not ready")
106 type metaInfoMetaData struct {
107 mi *metainfo.MetaInfo
110 func (me metaInfoMetaData) Files() []metainfo.FileInfo { return me.mi.Files }
111 func (me metaInfoMetaData) Name() string { return me.mi.Name }
112 func (me metaInfoMetaData) PieceHashes() []string {
115 func (me metaInfoMetaData) PieceLength() int64 { return me.mi.PieceLength }
116 func (me metaInfoMetaData) PieceCount() int {
117 return len(me.mi.Pieces) / pieceHash.Size()
120 func NewMetaDataFromMetaInfo(mi *metainfo.MetaInfo) MetaData {
121 return metaInfoMetaData{mi}
124 type MetaData interface {
125 PieceHashes() []string
126 Files() []metainfo.FileInfo
132 func mmapTorrentData(md MetaData, location string) (mms mmap_span.MMapSpan, err error) {
139 for _, miFile := range md.Files() {
140 fileName := filepath.Join(append([]string{location, md.Name()}, miFile.Path...)...)
141 err = os.MkdirAll(filepath.Dir(fileName), 0777)
146 file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
153 fi, err = file.Stat()
157 if fi.Size() < miFile.Length {
158 err = file.Truncate(miFile.Length)
164 mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
168 if int64(len(mMap)) != miFile.Length {
169 panic("mmap has wrong length")
171 mms = append(mms, mMap)
180 func metadataPieceSize(totalSize int, piece int) int {
181 ret := totalSize - piece*(1<<14)