]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Inlineable methods to access File piecePriority (#601)
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "github.com/RoaringBitmap/roaring"
5         "github.com/anacrolix/missinggo/v2/bitmap"
6
7         "github.com/anacrolix/torrent/metainfo"
8 )
9
10 // Provides access to regions of torrent data that correspond to its files.
11 type File struct {
12         t           *Torrent
13         path        string
14         offset      int64
15         length      int64
16         fi          metainfo.FileInfo
17         displayPath string
18         prio        piecePriority
19 }
20
21 func (f *File) Torrent() *Torrent {
22         return f.t
23 }
24
25 // Data for this file begins this many bytes into the Torrent.
26 func (f *File) Offset() int64 {
27         return f.offset
28 }
29
30 // The FileInfo from the metainfo.Info to which this file corresponds.
31 func (f File) FileInfo() metainfo.FileInfo {
32         return f.fi
33 }
34
35 // The file's path components joined by '/'.
36 func (f File) Path() string {
37         return f.path
38 }
39
40 // The file's length in bytes.
41 func (f *File) Length() int64 {
42         return f.length
43 }
44
45 // Number of bytes of the entire file we have completed. This is the sum of
46 // completed pieces, and dirtied chunks of incomplete pieces.
47 func (f *File) BytesCompleted() int64 {
48         f.t.cl.rLock()
49         defer f.t.cl.rUnlock()
50         return f.bytesCompleted()
51 }
52
53 func (f *File) bytesCompleted() int64 {
54         return f.length - f.bytesLeft()
55 }
56
57 func fileBytesLeft(
58         torrentUsualPieceSize int64,
59         fileFirstPieceIndex int,
60         fileEndPieceIndex int,
61         fileTorrentOffset int64,
62         fileLength int64,
63         torrentCompletedPieces *roaring.Bitmap,
64 ) (left int64) {
65         numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
66         switch numPiecesSpanned {
67         case 0:
68         case 1:
69                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
70                         left += fileLength
71                 }
72         default:
73                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
74                         left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
75                 }
76                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileEndPieceIndex - 1)) {
77                         left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
78                 }
79                 completedMiddlePieces := torrentCompletedPieces.Clone()
80                 completedMiddlePieces.RemoveRange(0, bitmap.BitRange(fileFirstPieceIndex+1))
81                 completedMiddlePieces.RemoveRange(bitmap.BitRange(fileEndPieceIndex-1), bitmap.ToEnd)
82                 left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.GetCardinality())) * torrentUsualPieceSize
83         }
84         return
85 }
86
87 func (f *File) bytesLeft() (left int64) {
88         return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, &f.t._completedPieces)
89 }
90
91 // The relative file path for a multi-file torrent, and the torrent name for a
92 // single-file torrent. Dir separators are '/'.
93 func (f *File) DisplayPath() string {
94         return f.displayPath
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         return f.t.newReader(f.Offset(), f.Length())
144 }
145
146 // Sets the minimum priority for pieces in the File.
147 func (f *File) SetPriority(prio piecePriority) {
148         f.t.cl.lock()
149         if prio != f.prio {
150                 f.prio = prio
151                 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
152         }
153         f.t.cl.unlock()
154 }
155
156 // Returns the priority per File.SetPriority.
157 func (f *File) Priority() (prio piecePriority) {
158         f.t.cl.lock()
159         prio = f.prio
160         f.t.cl.unlock()
161         return
162 }
163
164 // Returns the index of the first piece containing data for the file.
165 func (f *File) firstPieceIndex() pieceIndex {
166         if f.t.usualPieceSize() == 0 {
167                 return 0
168         }
169         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
170 }
171
172 // Returns the index of the piece after the last one containing data for the file.
173 func (f *File) endPieceIndex() pieceIndex {
174         if f.t.usualPieceSize() == 0 {
175                 return 0
176         }
177         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
178 }