6 "github.com/RoaringBitmap/roaring"
7 g "github.com/anacrolix/generics"
8 "github.com/anacrolix/missinggo/v2/bitmap"
10 "github.com/anacrolix/torrent/metainfo"
13 // Provides access to regions of torrent data that correspond to its files.
22 piecesRoot g.Option[[sha256.Size]byte]
25 func (f *File) String() string {
29 func (f *File) Torrent() *Torrent {
33 // Data for this file begins this many bytes into the Torrent.
34 func (f *File) Offset() int64 {
38 // The FileInfo from the metainfo.Info to which this file corresponds.
39 func (f *File) FileInfo() metainfo.FileInfo {
43 // The file's path components joined by '/'.
44 func (f *File) Path() string {
48 // The file's length in bytes.
49 func (f *File) Length() int64 {
53 // Number of bytes of the entire file we have completed. This is the sum of
54 // completed pieces, and dirtied chunks of incomplete pieces.
55 func (f *File) BytesCompleted() (n int64) {
57 n = f.bytesCompletedLocked()
62 func (f *File) bytesCompletedLocked() int64 {
63 return f.length - f.bytesLeft()
67 torrentUsualPieceSize int64,
68 fileFirstPieceIndex int,
69 fileEndPieceIndex int,
70 fileTorrentOffset int64,
72 torrentCompletedPieces *roaring.Bitmap,
73 pieceSizeCompletedFn func(pieceIndex int) int64,
79 noCompletedMiddlePieces := roaring.New()
80 noCompletedMiddlePieces.AddRange(bitmap.BitRange(fileFirstPieceIndex), bitmap.BitRange(fileEndPieceIndex))
81 noCompletedMiddlePieces.AndNot(torrentCompletedPieces)
82 noCompletedMiddlePieces.Iterate(func(pieceIndex uint32) bool {
84 pieceSizeCompleted := pieceSizeCompletedFn(i)
85 if i == fileFirstPieceIndex {
86 beginOffset := fileTorrentOffset % torrentUsualPieceSize
87 beginSize := torrentUsualPieceSize - beginOffset
88 beginDownLoaded := pieceSizeCompleted - beginOffset
89 if beginDownLoaded < 0 {
92 left += beginSize - beginDownLoaded
93 } else if i == fileEndPieceIndex-1 {
94 endSize := (fileTorrentOffset + fileLength) % torrentUsualPieceSize
96 endSize = torrentUsualPieceSize
98 endDownloaded := pieceSizeCompleted
99 if endDownloaded > endSize {
100 endDownloaded = endSize
102 left += endSize - endDownloaded
104 left += torrentUsualPieceSize - pieceSizeCompleted
109 if left > fileLength {
113 //numPiecesSpanned := f.EndPieceIndex() - f.BeginPieceIndex()
114 //completedMiddlePieces := f.t._completedPieces.Clone()
115 //completedMiddlePieces.RemoveRange(0, bitmap.BitRange(f.BeginPieceIndex()+1))
116 //completedMiddlePieces.RemoveRange(bitmap.BitRange(f.EndPieceIndex()-1), bitmap.ToEnd)
117 //left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.GetCardinality())) * torrentUsualPieceSize
121 func (f *File) bytesLeft() (left int64) {
122 return fileBytesLeft(int64(f.t.usualPieceSize()), f.BeginPieceIndex(), f.EndPieceIndex(), f.offset, f.length, &f.t._completedPieces, func(pieceIndex int) int64 {
123 return int64(f.t.piece(pieceIndex).numDirtyBytes())
127 // The relative file path for a multi-file torrent, and the torrent name for a
128 // single-file torrent. Dir separators are '/'.
129 func (f *File) DisplayPath() string {
133 // The download status of a piece that comprises part of a File.
134 type FilePieceState struct {
135 Bytes int64 // Bytes within the piece that are part of this File.
139 // Returns the state of pieces in this file.
140 func (f *File) State() (ret []FilePieceState) {
142 defer f.t.cl.rUnlock()
143 pieceSize := int64(f.t.usualPieceSize())
144 off := f.offset % pieceSize
145 remaining := f.length
146 for i := pieceIndex(f.offset / pieceSize); ; i++ {
150 len1 := pieceSize - off
151 if len1 > remaining {
154 ps := f.t.pieceState(i)
155 ret = append(ret, FilePieceState{len1, ps})
162 // Requests that all pieces containing data in the file be downloaded.
163 func (f *File) Download() {
164 f.SetPriority(PiecePriorityNormal)
167 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
168 begin = int((off + pieceSize - 1) / pieceSize)
169 end = int((off + size) / pieceSize)
173 // Deprecated: Use File.SetPriority.
174 func (f *File) Cancel() {
175 f.SetPriority(PiecePriorityNone)
178 func (f *File) NewReader() Reader {
179 return f.t.newReader(f.Offset(), f.Length())
182 // Sets the minimum priority for pieces in the File.
183 func (f *File) SetPriority(prio PiecePriority) {
187 f.t.updatePiecePriorities(f.BeginPieceIndex(), f.EndPieceIndex(), "File.SetPriority")
192 // Returns the priority per File.SetPriority.
193 func (f *File) Priority() (prio PiecePriority) {
200 // Returns the index of the first piece containing data for the file.
201 func (f *File) BeginPieceIndex() int {
202 if f.t.usualPieceSize() == 0 {
205 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
208 // Returns the index of the piece after the last one containing data for the file.
209 func (f *File) EndPieceIndex() int {
210 if f.t.usualPieceSize() == 0 {
213 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
216 func (f *File) numPieces() int {
217 return f.EndPieceIndex() - f.BeginPieceIndex()