4 "github.com/RoaringBitmap/roaring"
5 "github.com/anacrolix/missinggo/v2/bitmap"
7 "github.com/anacrolix/torrent/metainfo"
10 // Provides access to regions of torrent data that correspond to its files.
21 func (f *File) Torrent() *Torrent {
25 // Data for this file begins this many bytes into the Torrent.
26 func (f *File) Offset() int64 {
30 // The FileInfo from the metainfo.Info to which this file corresponds.
31 func (f File) FileInfo() metainfo.FileInfo {
35 // The file's path components joined by '/'.
36 func (f File) Path() string {
40 // The file's length in bytes.
41 func (f *File) Length() int64 {
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 {
49 defer f.t.cl.rUnlock()
50 return f.bytesCompleted()
53 func (f *File) bytesCompleted() int64 {
54 return f.length - f.bytesLeft()
58 torrentUsualPieceSize int64,
59 fileFirstPieceIndex int,
60 fileEndPieceIndex int,
61 fileTorrentOffset int64,
63 torrentCompletedPieces *roaring.Bitmap,
65 numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
66 switch numPiecesSpanned {
69 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
73 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
74 left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
76 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileEndPieceIndex - 1)) {
77 left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
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
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)
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 {
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.
103 // Returns the state of pieces in this file.
104 func (f *File) State() (ret []FilePieceState) {
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++ {
114 len1 := pieceSize - off
115 if len1 > remaining {
118 ps := f.t.pieceState(i)
119 ret = append(ret, FilePieceState{len1, ps})
126 // Requests that all pieces containing data in the file be downloaded.
127 func (f *File) Download() {
128 f.SetPriority(PiecePriorityNormal)
131 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
132 begin = int((off + pieceSize - 1) / pieceSize)
133 end = int((off + size) / pieceSize)
137 // Deprecated: Use File.SetPriority.
138 func (f *File) Cancel() {
139 f.SetPriority(PiecePriorityNone)
142 func (f *File) NewReader() Reader {
143 return f.t.newReader(f.Offset(), f.Length())
146 // Sets the minimum priority for pieces in the File.
147 func (f *File) SetPriority(prio piecePriority) {
151 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
156 // Returns the priority per File.SetPriority.
157 func (f *File) Priority() (prio piecePriority) {
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 {
169 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
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 {
177 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))