]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Rename variables in fileBytesLeft
[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         fileEndPieceIndex--
66         bitmap.Flip(torrentCompletedPieces, fileFirstPieceIndex+1, fileEndPieceIndex).IterTyped(func(piece int) bool {
67                 if piece >= fileEndPieceIndex {
68                         return false
69                 }
70                 if piece > fileFirstPieceIndex {
71                         left += torrentUsualPieceSize
72                 }
73                 return true
74         })
75         if !torrentCompletedPieces.Get(fileFirstPieceIndex) {
76                 left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
77         }
78         if !torrentCompletedPieces.Get(fileEndPieceIndex) {
79                 left += (fileTorrentOffset + fileLength) % torrentUsualPieceSize
80         }
81         return
82 }
83
84 func (f *File) bytesLeft() (left int64) {
85         return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
86 }
87
88 // The relative file path for a multi-file torrent, and the torrent name for a
89 // single-file torrent.
90 func (f *File) DisplayPath() string {
91         fip := f.FileInfo().Path
92         if len(fip) == 0 {
93                 return f.t.info.Name
94         }
95         return strings.Join(fip, "/")
96
97 }
98
99 // The download status of a piece that comprises part of a File.
100 type FilePieceState struct {
101         Bytes int64 // Bytes within the piece that are part of this File.
102         PieceState
103 }
104
105 // Returns the state of pieces in this file.
106 func (f *File) State() (ret []FilePieceState) {
107         f.t.cl.rLock()
108         defer f.t.cl.rUnlock()
109         pieceSize := int64(f.t.usualPieceSize())
110         off := f.offset % pieceSize
111         remaining := f.length
112         for i := pieceIndex(f.offset / pieceSize); ; i++ {
113                 if remaining == 0 {
114                         break
115                 }
116                 len1 := pieceSize - off
117                 if len1 > remaining {
118                         len1 = remaining
119                 }
120                 ps := f.t.pieceState(i)
121                 ret = append(ret, FilePieceState{len1, ps})
122                 off = 0
123                 remaining -= len1
124         }
125         return
126 }
127
128 // Requests that all pieces containing data in the file be downloaded.
129 func (f *File) Download() {
130         f.SetPriority(PiecePriorityNormal)
131 }
132
133 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
134         begin = int((off + pieceSize - 1) / pieceSize)
135         end = int((off + size) / pieceSize)
136         return
137 }
138
139 // Deprecated: Use File.SetPriority.
140 func (f *File) Cancel() {
141         f.SetPriority(PiecePriorityNone)
142 }
143
144 func (f *File) NewReader() Reader {
145         tr := reader{
146                 mu:        f.t.cl.locker(),
147                 t:         f.t,
148                 readahead: 5 * 1024 * 1024,
149                 offset:    f.Offset(),
150                 length:    f.Length(),
151         }
152         f.t.addReader(&tr)
153         return &tr
154 }
155
156 // Sets the minimum priority for pieces in the File.
157 func (f *File) SetPriority(prio piecePriority) {
158         f.t.cl.lock()
159         defer f.t.cl.unlock()
160         if prio == f.prio {
161                 return
162         }
163         f.prio = prio
164         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
165 }
166
167 // Returns the priority per File.SetPriority.
168 func (f *File) Priority() piecePriority {
169         f.t.cl.lock()
170         defer f.t.cl.unlock()
171         return f.prio
172 }
173
174 // Returns the index of the first piece containing data for the file.
175 func (f *File) firstPieceIndex() pieceIndex {
176         if f.t.usualPieceSize() == 0 {
177                 return 0
178         }
179         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
180 }
181
182 // Returns the index of the piece after the last one containing data for the file.
183 func (f *File) endPieceIndex() pieceIndex {
184         if f.t.usualPieceSize() == 0 {
185                 return 0
186         }
187         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
188 }