]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Print peer ID in ASCII-only
[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() (n int64) {
48         f.t.cl.rLock()
49         n = f.bytesCompletedLocked()
50         f.t.cl.rUnlock()
51         return
52 }
53
54 func (f *File) bytesCompletedLocked() int64 {
55         return f.length - f.bytesLeft()
56 }
57
58 func fileBytesLeft(
59         torrentUsualPieceSize int64,
60         fileFirstPieceIndex int,
61         fileEndPieceIndex int,
62         fileTorrentOffset int64,
63         fileLength int64,
64         torrentCompletedPieces *roaring.Bitmap,
65 ) (left int64) {
66         numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
67         switch numPiecesSpanned {
68         case 0:
69         case 1:
70                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
71                         left += fileLength
72                 }
73         default:
74                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
75                         left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
76                 }
77                 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileEndPieceIndex - 1)) {
78                         left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
79                 }
80                 completedMiddlePieces := torrentCompletedPieces.Clone()
81                 completedMiddlePieces.RemoveRange(0, bitmap.BitRange(fileFirstPieceIndex+1))
82                 completedMiddlePieces.RemoveRange(bitmap.BitRange(fileEndPieceIndex-1), bitmap.ToEnd)
83                 left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.GetCardinality())) * torrentUsualPieceSize
84         }
85         return
86 }
87
88 func (f *File) bytesLeft() (left int64) {
89         return fileBytesLeft(int64(f.t.usualPieceSize()), f.BeginPieceIndex(), f.EndPieceIndex(), f.offset, f.length, &f.t._completedPieces)
90 }
91
92 // The relative file path for a multi-file torrent, and the torrent name for a
93 // single-file torrent. Dir separators are '/'.
94 func (f *File) DisplayPath() string {
95         return f.displayPath
96 }
97
98 // The download status of a piece that comprises part of a File.
99 type FilePieceState struct {
100         Bytes int64 // Bytes within the piece that are part of this File.
101         PieceState
102 }
103
104 // Returns the state of pieces in this file.
105 func (f *File) State() (ret []FilePieceState) {
106         f.t.cl.rLock()
107         defer f.t.cl.rUnlock()
108         pieceSize := int64(f.t.usualPieceSize())
109         off := f.offset % pieceSize
110         remaining := f.length
111         for i := pieceIndex(f.offset / pieceSize); ; i++ {
112                 if remaining == 0 {
113                         break
114                 }
115                 len1 := pieceSize - off
116                 if len1 > remaining {
117                         len1 = remaining
118                 }
119                 ps := f.t.pieceState(i)
120                 ret = append(ret, FilePieceState{len1, ps})
121                 off = 0
122                 remaining -= len1
123         }
124         return
125 }
126
127 // Requests that all pieces containing data in the file be downloaded.
128 func (f *File) Download() {
129         f.SetPriority(PiecePriorityNormal)
130 }
131
132 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
133         begin = int((off + pieceSize - 1) / pieceSize)
134         end = int((off + size) / pieceSize)
135         return
136 }
137
138 // Deprecated: Use File.SetPriority.
139 func (f *File) Cancel() {
140         f.SetPriority(PiecePriorityNone)
141 }
142
143 func (f *File) NewReader() Reader {
144         return f.t.newReader(f.Offset(), f.Length())
145 }
146
147 // Sets the minimum priority for pieces in the File.
148 func (f *File) SetPriority(prio piecePriority) {
149         f.t.cl.lock()
150         if prio != f.prio {
151                 f.prio = prio
152                 f.t.updatePiecePriorities(f.BeginPieceIndex(), f.EndPieceIndex(), "File.SetPriority")
153         }
154         f.t.cl.unlock()
155 }
156
157 // Returns the priority per File.SetPriority.
158 func (f *File) Priority() (prio piecePriority) {
159         f.t.cl.rLock()
160         prio = f.prio
161         f.t.cl.rUnlock()
162         return
163 }
164
165 // Returns the index of the first piece containing data for the file.
166 func (f *File) BeginPieceIndex() int {
167         if f.t.usualPieceSize() == 0 {
168                 return 0
169         }
170         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
171 }
172
173 // Returns the index of the piece after the last one containing data for the file.
174 func (f *File) EndPieceIndex() int {
175         if f.t.usualPieceSize() == 0 {
176                 return 0
177         }
178         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
179 }