]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Add BytesCompleted method for files (#347)
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/missinggo/bitmap"
7
8         "github.com/anacrolix/torrent/metainfo"
9         pp "github.com/anacrolix/torrent/peer_protocol"
10 )
11
12 // Provides access to regions of torrent data that correspond to its files.
13 type File struct {
14         t      *Torrent
15         path   string
16         offset int64
17         length int64
18         fi     metainfo.FileInfo
19         prio   piecePriority
20 }
21
22 func (f *File) Torrent() *Torrent {
23         return f.t
24 }
25
26 // Data for this file begins this many bytes into the Torrent.
27 func (f *File) Offset() int64 {
28         return f.offset
29 }
30
31 // The FileInfo from the metainfo.Info to which this file corresponds.
32 func (f File) FileInfo() metainfo.FileInfo {
33         return f.fi
34 }
35
36 // The file's path components joined by '/'.
37 func (f File) Path() string {
38         return f.path
39 }
40
41 // The file's length in bytes.
42 func (f *File) Length() int64 {
43         return f.length
44 }
45
46 // Number of bytes of the entire file we have completed. This is the sum of
47 // completed pieces, and dirtied chunks of incomplete pieces.
48 func (f *File) BytesCompleted() int64 {
49         f.t.cl.rLock()
50         defer f.t.cl.rUnlock()
51         return f.bytesCompleted()
52 }
53
54 func (f *File) bytesCompleted() int64 {
55         return f.length - f.bytesLeft()
56 }
57
58 func (f *File) bytesLeft() (left int64) {
59         firstPieceIndex := f.firstPieceIndex()
60         endPieceIndex := f.endPieceIndex()
61         bitmap.Flip(f.t.completedPieces, firstPieceIndex, endPieceIndex+1).IterTyped(func(piece int) bool {
62                 p := &f.t.pieces[piece]
63                 left += int64(p.length() - p.numDirtyBytes())
64                 return true
65         })
66         startPiece := f.t.piece(firstPieceIndex)
67         endChunk := int(f.offset%f.t.info.PieceLength) * int(startPiece.numChunks()) / int(startPiece.length())
68         bitmap.Flip(startPiece.dirtyChunks, 0, endChunk).IterTyped(func(chunk int) bool {
69                 left -= int64(startPiece.chunkSize())
70                 return true
71         })
72         endPiece := f.t.piece(endPieceIndex)
73         startChunk := int((f.offset+f.length)%f.t.info.PieceLength) * int(endPiece.numChunks()) / int(endPiece.length())
74         lastChunkIndex := int(endPiece.lastChunkIndex())
75         bitmap.Flip(endPiece.dirtyChunks, startChunk, int(endPiece.numChunks())).IterTyped(func(chunk int) bool {
76                 if chunk == lastChunkIndex {
77                         left -= int64(endPiece.chunkIndexSpec(pp.Integer(chunk)).Length)
78                 } else {
79                         left -= int64(endPiece.chunkSize())
80                 }
81                 return true
82         })
83         return
84 }
85
86 // The relative file path for a multi-file torrent, and the torrent name for a
87 // single-file torrent.
88 func (f *File) DisplayPath() string {
89         fip := f.FileInfo().Path
90         if len(fip) == 0 {
91                 return f.t.info.Name
92         }
93         return strings.Join(fip, "/")
94
95 }
96
97 // The download status of a piece that comprises part of a File.
98 type FilePieceState struct {
99         Bytes int64 // Bytes within the piece that are part of this File.
100         PieceState
101 }
102
103 // Returns the state of pieces in this file.
104 func (f *File) State() (ret []FilePieceState) {
105         f.t.cl.rLock()
106         defer f.t.cl.rUnlock()
107         pieceSize := int64(f.t.usualPieceSize())
108         off := f.offset % pieceSize
109         remaining := f.length
110         for i := pieceIndex(f.offset / pieceSize); ; i++ {
111                 if remaining == 0 {
112                         break
113                 }
114                 len1 := pieceSize - off
115                 if len1 > remaining {
116                         len1 = remaining
117                 }
118                 ps := f.t.pieceState(i)
119                 ret = append(ret, FilePieceState{len1, ps})
120                 off = 0
121                 remaining -= len1
122         }
123         return
124 }
125
126 // Requests that all pieces containing data in the file be downloaded.
127 func (f *File) Download() {
128         f.SetPriority(PiecePriorityNormal)
129 }
130
131 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
132         begin = int((off + pieceSize - 1) / pieceSize)
133         end = int((off + size) / pieceSize)
134         return
135 }
136
137 // Deprecated: Use File.SetPriority.
138 func (f *File) Cancel() {
139         f.SetPriority(PiecePriorityNone)
140 }
141
142 func (f *File) NewReader() Reader {
143         tr := reader{
144                 mu:        f.t.cl.locker(),
145                 t:         f.t,
146                 readahead: 5 * 1024 * 1024,
147                 offset:    f.Offset(),
148                 length:    f.Length(),
149         }
150         f.t.addReader(&tr)
151         return &tr
152 }
153
154 // Sets the minimum priority for pieces in the File.
155 func (f *File) SetPriority(prio piecePriority) {
156         f.t.cl.lock()
157         defer f.t.cl.unlock()
158         if prio == f.prio {
159                 return
160         }
161         f.prio = prio
162         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
163 }
164
165 // Returns the priority per File.SetPriority.
166 func (f *File) Priority() piecePriority {
167         f.t.cl.lock()
168         defer f.t.cl.unlock()
169         return f.prio
170 }
171
172 func (f *File) firstPieceIndex() pieceIndex {
173         if f.t.usualPieceSize() == 0 {
174                 return 0
175         }
176         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
177 }
178
179 func (f *File) endPieceIndex() pieceIndex {
180         if f.t.usualPieceSize() == 0 {
181                 return 0
182         }
183         return pieceIndex((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
184 }