]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Merge branch 'request-strategy-rewrite'
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/missinggo/v2/bitmap"
7
8         "github.com/anacrolix/torrent/metainfo"
9 )
10
11 // Provides access to regions of torrent data that correspond to its files.
12 type File struct {
13         t      *Torrent
14         path   string
15         offset int64
16         length int64
17         fi     metainfo.FileInfo
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 bitmap.Bitmap,
64 ) (left int64) {
65         numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
66         switch numPiecesSpanned {
67         case 0:
68         case 1:
69                 if !torrentCompletedPieces.Get(bitmap.BitIndex(fileFirstPieceIndex)) {
70                         left += fileLength
71                 }
72         default:
73                 if !torrentCompletedPieces.Get(bitmap.BitIndex(fileFirstPieceIndex)) {
74                         left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
75                 }
76                 if !torrentCompletedPieces.Get(bitmap.BitIndex(fileEndPieceIndex - 1)) {
77                         left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
78                 }
79                 completedMiddlePieces := torrentCompletedPieces.Copy()
80                 completedMiddlePieces.RemoveRange(0, bitmap.BitRange(fileFirstPieceIndex+1))
81                 completedMiddlePieces.RemoveRange(bitmap.BitRange(fileEndPieceIndex-1), bitmap.ToEnd)
82                 left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.Len())) * 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.
93 func (f *File) DisplayPath() string {
94         fip := f.FileInfo().Path
95         if len(fip) == 0 {
96                 return f.t.info.Name
97         }
98         return strings.Join(fip, "/")
99 }
100
101 // The download status of a piece that comprises part of a File.
102 type FilePieceState struct {
103         Bytes int64 // Bytes within the piece that are part of this File.
104         PieceState
105 }
106
107 // Returns the state of pieces in this file.
108 func (f *File) State() (ret []FilePieceState) {
109         f.t.cl.rLock()
110         defer f.t.cl.rUnlock()
111         pieceSize := int64(f.t.usualPieceSize())
112         off := f.offset % pieceSize
113         remaining := f.length
114         for i := pieceIndex(f.offset / pieceSize); ; i++ {
115                 if remaining == 0 {
116                         break
117                 }
118                 len1 := pieceSize - off
119                 if len1 > remaining {
120                         len1 = remaining
121                 }
122                 ps := f.t.pieceState(i)
123                 ret = append(ret, FilePieceState{len1, ps})
124                 off = 0
125                 remaining -= len1
126         }
127         return
128 }
129
130 // Requests that all pieces containing data in the file be downloaded.
131 func (f *File) Download() {
132         f.SetPriority(PiecePriorityNormal)
133 }
134
135 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
136         begin = int((off + pieceSize - 1) / pieceSize)
137         end = int((off + size) / pieceSize)
138         return
139 }
140
141 // Deprecated: Use File.SetPriority.
142 func (f *File) Cancel() {
143         f.SetPriority(PiecePriorityNone)
144 }
145
146 func (f *File) NewReader() Reader {
147         tr := reader{
148                 mu:        f.t.cl.locker(),
149                 t:         f.t,
150                 readahead: 5 * 1024 * 1024,
151                 offset:    f.Offset(),
152                 length:    f.Length(),
153         }
154         f.t.addReader(&tr)
155         return &tr
156 }
157
158 // Sets the minimum priority for pieces in the File.
159 func (f *File) SetPriority(prio piecePriority) {
160         f.t.cl.lock()
161         defer f.t.cl.unlock()
162         if prio == f.prio {
163                 return
164         }
165         f.prio = prio
166         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
167 }
168
169 // Returns the priority per File.SetPriority.
170 func (f *File) Priority() piecePriority {
171         f.t.cl.lock()
172         defer f.t.cl.unlock()
173         return f.prio
174 }
175
176 // Returns the index of the first piece containing data for the file.
177 func (f *File) firstPieceIndex() pieceIndex {
178         if f.t.usualPieceSize() == 0 {
179                 return 0
180         }
181         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
182 }
183
184 // Returns the index of the piece after the last one containing data for the file.
185 func (f *File) endPieceIndex() pieceIndex {
186         if f.t.usualPieceSize() == 0 {
187                 return 0
188         }
189         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
190 }