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,
65 pieceSizeCompletedFn func(pieceIndex int) int64,
71 noCompletedMiddlePieces := roaring.New()
72 noCompletedMiddlePieces.AddRange(bitmap.BitRange(fileFirstPieceIndex), bitmap.BitRange(fileEndPieceIndex))
73 noCompletedMiddlePieces.AndNot(torrentCompletedPieces)
74 noCompletedMiddlePieces.Iterate(func(pieceIndex uint32) bool {
76 pieceSizeCompleted := pieceSizeCompletedFn(i)
77 if i == fileFirstPieceIndex {
78 beginOffset := fileTorrentOffset % torrentUsualPieceSize
79 beginSize := torrentUsualPieceSize - beginOffset
80 beginDownLoaded := pieceSizeCompleted - beginOffset
81 if beginDownLoaded < 0 {
84 left += beginSize - beginDownLoaded
85 } else if i == fileEndPieceIndex-1 {
86 endSize := (fileTorrentOffset + fileLength) % torrentUsualPieceSize
88 endSize = torrentUsualPieceSize
90 endDownloaded := pieceSizeCompleted
91 if endDownloaded > endSize {
92 endDownloaded = endSize
94 left += endSize - endDownloaded
96 left += torrentUsualPieceSize - pieceSizeCompleted
101 if left > fileLength {
105 //numPiecesSpanned := f.EndPieceIndex() - f.BeginPieceIndex()
106 //completedMiddlePieces := f.t._completedPieces.Clone()
107 //completedMiddlePieces.RemoveRange(0, bitmap.BitRange(f.BeginPieceIndex()+1))
108 //completedMiddlePieces.RemoveRange(bitmap.BitRange(f.EndPieceIndex()-1), bitmap.ToEnd)
109 //left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.GetCardinality())) * torrentUsualPieceSize
113 func (f *File) bytesLeft() (left int64) {
114 return fileBytesLeft(int64(f.t.usualPieceSize()), f.BeginPieceIndex(), f.EndPieceIndex(), f.offset, f.length, &f.t._completedPieces, func(pieceIndex int) int64 {
115 return int64(f.t.piece(pieceIndex).numDirtyBytes())
119 // The relative file path for a multi-file torrent, and the torrent name for a
120 // single-file torrent. Dir separators are '/'.
121 func (f *File) DisplayPath() string {
125 // The download status of a piece that comprises part of a File.
126 type FilePieceState struct {
127 Bytes int64 // Bytes within the piece that are part of this File.
131 // Returns the state of pieces in this file.
132 func (f *File) State() (ret []FilePieceState) {
134 defer f.t.cl.rUnlock()
135 pieceSize := int64(f.t.usualPieceSize())
136 off := f.offset % pieceSize
137 remaining := f.length
138 for i := pieceIndex(f.offset / pieceSize); ; i++ {
142 len1 := pieceSize - off
143 if len1 > remaining {
146 ps := f.t.pieceState(i)
147 ret = append(ret, FilePieceState{len1, ps})
154 // Requests that all pieces containing data in the file be downloaded.
155 func (f *File) Download() {
156 f.SetPriority(PiecePriorityNormal)
159 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
160 begin = int((off + pieceSize - 1) / pieceSize)
161 end = int((off + size) / pieceSize)
165 // Deprecated: Use File.SetPriority.
166 func (f *File) Cancel() {
167 f.SetPriority(PiecePriorityNone)
170 func (f *File) NewReader() Reader {
171 return f.t.newReader(f.Offset(), f.Length())
174 // Sets the minimum priority for pieces in the File.
175 func (f *File) SetPriority(prio piecePriority) {
179 f.t.updatePiecePriorities(f.BeginPieceIndex(), f.EndPieceIndex(), "File.SetPriority")
184 // Returns the priority per File.SetPriority.
185 func (f *File) Priority() (prio piecePriority) {
192 // Returns the index of the first piece containing data for the file.
193 func (f *File) BeginPieceIndex() int {
194 if f.t.usualPieceSize() == 0 {
197 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
200 // Returns the index of the piece after the last one containing data for the file.
201 func (f *File) EndPieceIndex() int {
202 if f.t.usualPieceSize() == 0 {
205 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))