6 "github.com/anacrolix/missinggo/v2/bitmap"
8 "github.com/anacrolix/torrent/metainfo"
11 // 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()
57 func fileBytesLeft(pieceSize int64, firstPieceIndex int, endPieceIndex int, fileOffset int64, fileLength int64, completedPieces bitmap.Bitmap) (left int64) {
59 bitmap.Flip(completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
60 if piece >= endPieceIndex {
63 if piece > firstPieceIndex {
68 if !completedPieces.Get(firstPieceIndex) {
69 left += pieceSize - (fileOffset % pieceSize)
71 if !completedPieces.Get(endPieceIndex) {
72 left += (fileOffset + fileLength) % pieceSize
77 func (f *File) bytesLeft() (left int64) {
78 return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
81 // The relative file path for a multi-file torrent, and the torrent name for a
82 // single-file torrent.
83 func (f *File) DisplayPath() string {
84 fip := f.FileInfo().Path
88 return strings.Join(fip, "/")
92 // The download status of a piece that comprises part of a File.
93 type FilePieceState struct {
94 Bytes int64 // Bytes within the piece that are part of this File.
98 // Returns the state of pieces in this file.
99 func (f *File) State() (ret []FilePieceState) {
101 defer f.t.cl.rUnlock()
102 pieceSize := int64(f.t.usualPieceSize())
103 off := f.offset % pieceSize
104 remaining := f.length
105 for i := pieceIndex(f.offset / pieceSize); ; i++ {
109 len1 := pieceSize - off
110 if len1 > remaining {
113 ps := f.t.pieceState(i)
114 ret = append(ret, FilePieceState{len1, ps})
121 // Requests that all pieces containing data in the file be downloaded.
122 func (f *File) Download() {
123 f.SetPriority(PiecePriorityNormal)
126 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
127 begin = int((off + pieceSize - 1) / pieceSize)
128 end = int((off + size) / pieceSize)
132 // Deprecated: Use File.SetPriority.
133 func (f *File) Cancel() {
134 f.SetPriority(PiecePriorityNone)
137 func (f *File) NewReader() Reader {
141 readahead: 5 * 1024 * 1024,
149 // Sets the minimum priority for pieces in the File.
150 func (f *File) SetPriority(prio piecePriority) {
152 defer f.t.cl.unlock()
157 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
160 // Returns the priority per File.SetPriority.
161 func (f *File) Priority() piecePriority {
163 defer f.t.cl.unlock()
167 // Returns the index of the first piece containing data for the file.
168 func (f *File) firstPieceIndex() pieceIndex {
169 if f.t.usualPieceSize() == 0 {
172 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
175 // Returns the index of the piece after the last one containing data for the file.
176 func (f *File) endPieceIndex() pieceIndex {
177 if f.t.usualPieceSize() == 0 {
180 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))