]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Rewrite file.bytesLeft
[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(fileFirstPieceIndex) {
70                         left += fileLength
71                 }
72         default:
73                 if !torrentCompletedPieces.Get(fileFirstPieceIndex) {
74                         left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
75                 }
76                 if !torrentCompletedPieces.Get(fileEndPieceIndex - 1) {
77                         left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
78                 }
79                 completedMiddlePieces := torrentCompletedPieces.Copy()
80                 completedMiddlePieces.RemoveRange(0, fileFirstPieceIndex+1)
81                 completedMiddlePieces.RemoveRange(fileEndPieceIndex-1, bitmap.ToEnd)
82                 left += int64(numPiecesSpanned-2-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
102 // The download status of a piece that comprises part of a File.
103 type FilePieceState struct {
104         Bytes int64 // Bytes within the piece that are part of this File.
105         PieceState
106 }
107
108 // Returns the state of pieces in this file.
109 func (f *File) State() (ret []FilePieceState) {
110         f.t.cl.rLock()
111         defer f.t.cl.rUnlock()
112         pieceSize := int64(f.t.usualPieceSize())
113         off := f.offset % pieceSize
114         remaining := f.length
115         for i := pieceIndex(f.offset / pieceSize); ; i++ {
116                 if remaining == 0 {
117                         break
118                 }
119                 len1 := pieceSize - off
120                 if len1 > remaining {
121                         len1 = remaining
122                 }
123                 ps := f.t.pieceState(i)
124                 ret = append(ret, FilePieceState{len1, ps})
125                 off = 0
126                 remaining -= len1
127         }
128         return
129 }
130
131 // Requests that all pieces containing data in the file be downloaded.
132 func (f *File) Download() {
133         f.SetPriority(PiecePriorityNormal)
134 }
135
136 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
137         begin = int((off + pieceSize - 1) / pieceSize)
138         end = int((off + size) / pieceSize)
139         return
140 }
141
142 // Deprecated: Use File.SetPriority.
143 func (f *File) Cancel() {
144         f.SetPriority(PiecePriorityNone)
145 }
146
147 func (f *File) NewReader() Reader {
148         tr := reader{
149                 mu:        f.t.cl.locker(),
150                 t:         f.t,
151                 readahead: 5 * 1024 * 1024,
152                 offset:    f.Offset(),
153                 length:    f.Length(),
154         }
155         f.t.addReader(&tr)
156         return &tr
157 }
158
159 // Sets the minimum priority for pieces in the File.
160 func (f *File) SetPriority(prio piecePriority) {
161         f.t.cl.lock()
162         defer f.t.cl.unlock()
163         if prio == f.prio {
164                 return
165         }
166         f.prio = prio
167         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
168 }
169
170 // Returns the priority per File.SetPriority.
171 func (f *File) Priority() piecePriority {
172         f.t.cl.lock()
173         defer f.t.cl.unlock()
174         return f.prio
175 }
176
177 // Returns the index of the first piece containing data for the file.
178 func (f *File) firstPieceIndex() pieceIndex {
179         if f.t.usualPieceSize() == 0 {
180                 return 0
181         }
182         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
183 }
184
185 // Returns the index of the piece after the last one containing data for the file.
186 func (f *File) endPieceIndex() pieceIndex {
187         if f.t.usualPieceSize() == 0 {
188                 return 0
189         }
190         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
191 }