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() (n int64) {
49 n = f.bytesCompletedLocked()
54 func (f *File) bytesCompletedLocked() int64 {
55 return f.length - f.bytesLeft()
59 torrentUsualPieceSize int64,
60 fileFirstPieceIndex int,
61 fileEndPieceIndex int,
62 fileTorrentOffset int64,
64 torrentCompletedPieces *roaring.Bitmap,
66 numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
67 switch numPiecesSpanned {
70 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
74 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
75 left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
77 if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileEndPieceIndex - 1)) {
78 left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
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
88 func (f *File) bytesLeft() (left int64) {
89 return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, &f.t._completedPieces)
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 {
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.
104 // Returns the state of pieces in this file.
105 func (f *File) State() (ret []FilePieceState) {
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++ {
115 len1 := pieceSize - off
116 if len1 > remaining {
119 ps := f.t.pieceState(i)
120 ret = append(ret, FilePieceState{len1, ps})
127 // Requests that all pieces containing data in the file be downloaded.
128 func (f *File) Download() {
129 f.SetPriority(PiecePriorityNormal)
132 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
133 begin = int((off + pieceSize - 1) / pieceSize)
134 end = int((off + size) / pieceSize)
138 // Deprecated: Use File.SetPriority.
139 func (f *File) Cancel() {
140 f.SetPriority(PiecePriorityNone)
143 func (f *File) NewReader() Reader {
144 return f.t.newReader(f.Offset(), f.Length())
147 // Sets the minimum priority for pieces in the File.
148 func (f *File) SetPriority(prio piecePriority) {
152 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex(), "File.SetPriority")
157 // Returns the priority per File.SetPriority.
158 func (f *File) Priority() (prio piecePriority) {
165 // Returns the index of the first piece containing data for the file.
166 func (f *File) firstPieceIndex() pieceIndex {
167 if f.t.usualPieceSize() == 0 {
170 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
173 // Returns the index of the piece after the last one containing data for the file.
174 func (f *File) endPieceIndex() pieceIndex {
175 if f.t.usualPieceSize() == 0 {
178 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))