4 "bitbucket.org/anacrolix/go.torrent/mmap_span"
11 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
12 metainfo "github.com/nsf/libtorgo/torrent"
13 "launchpad.net/gommap"
17 pieceHash = crypto.SHA1
19 chunkSize = 0x4000 // 16KiB
20 BEP20 = "-GT0000-" // Peer ID client identifier prefix
21 dialTimeout = time.Second * 15
24 type InfoHash [20]byte
26 type pieceSum [20]byte
28 func copyHashSum(dst, src []byte) {
29 if len(dst) != len(src) || copy(dst, src) != len(dst) {
30 panic("hash sum sizes differ")
34 func BytesInfoHash(b []byte) (ih InfoHash) {
35 if len(b) != len(ih) || copy(ih[:], b) != len(ih) {
36 panic("bad infohash bytes")
43 PendingChunkSpecs map[chunkSpec]struct{}
49 func (p *piece) Complete() bool {
50 return len(p.PendingChunkSpecs) == 0 && p.EverHashed
53 func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
54 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
55 cs.Length = pieceLength - cs.Begin
59 type chunkSpec struct {
60 Begin, Length peer_protocol.Integer
64 Index peer_protocol.Integer
68 type pieceByBytesPendingSlice struct {
69 Pending, Indices []peer_protocol.Integer
72 func (pcs pieceByBytesPendingSlice) Len() int {
73 return len(pcs.Indices)
76 func (me pieceByBytesPendingSlice) Less(i, j int) bool {
77 return me.Pending[me.Indices[i]] < me.Pending[me.Indices[j]]
80 func (me pieceByBytesPendingSlice) Swap(i, j int) {
81 me.Indices[i], me.Indices[j] = me.Indices[j], me.Indices[i]
85 // Requested data not yet available.
86 ErrDataNotReady = errors.New("data not ready")
89 func mmapTorrentData(metaInfo *metainfo.MetaInfo, location string) (mms mmap_span.MMapSpan, err error) {
96 for _, miFile := range metaInfo.Files {
97 fileName := filepath.Join(append([]string{location, metaInfo.Name}, miFile.Path...)...)
98 err = os.MkdirAll(filepath.Dir(fileName), 0777)
103 file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
110 fi, err = file.Stat()
114 if fi.Size() < miFile.Length {
115 err = file.Truncate(miFile.Length)
121 mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
125 if int64(len(mMap)) != miFile.Length {
126 panic("mmap has wrong length")
128 mms = append(mms, mMap)