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()
58 torrentUsualPieceSize int64,
59 fileFirstPieceIndex int,
60 fileEndPieceIndex int,
61 fileTorrentOffset int64,
63 torrentCompletedPieces bitmap.Bitmap,
66 bitmap.Flip(torrentCompletedPieces, fileFirstPieceIndex+1, fileEndPieceIndex).IterTyped(func(piece int) bool {
67 if piece >= fileEndPieceIndex {
70 if piece > fileFirstPieceIndex {
71 left += torrentUsualPieceSize
75 if !torrentCompletedPieces.Get(fileFirstPieceIndex) {
76 left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
78 if !torrentCompletedPieces.Get(fileEndPieceIndex) {
79 left += (fileTorrentOffset + fileLength) % torrentUsualPieceSize
84 func (f *File) bytesLeft() (left int64) {
85 return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
88 // The relative file path for a multi-file torrent, and the torrent name for a
89 // single-file torrent.
90 func (f *File) DisplayPath() string {
91 fip := f.FileInfo().Path
95 return strings.Join(fip, "/")
99 // The download status of a piece that comprises part of a File.
100 type FilePieceState struct {
101 Bytes int64 // Bytes within the piece that are part of this File.
105 // Returns the state of pieces in this file.
106 func (f *File) State() (ret []FilePieceState) {
108 defer f.t.cl.rUnlock()
109 pieceSize := int64(f.t.usualPieceSize())
110 off := f.offset % pieceSize
111 remaining := f.length
112 for i := pieceIndex(f.offset / pieceSize); ; i++ {
116 len1 := pieceSize - off
117 if len1 > remaining {
120 ps := f.t.pieceState(i)
121 ret = append(ret, FilePieceState{len1, ps})
128 // Requests that all pieces containing data in the file be downloaded.
129 func (f *File) Download() {
130 f.SetPriority(PiecePriorityNormal)
133 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
134 begin = int((off + pieceSize - 1) / pieceSize)
135 end = int((off + size) / pieceSize)
139 // Deprecated: Use File.SetPriority.
140 func (f *File) Cancel() {
141 f.SetPriority(PiecePriorityNone)
144 func (f *File) NewReader() Reader {
148 readahead: 5 * 1024 * 1024,
156 // Sets the minimum priority for pieces in the File.
157 func (f *File) SetPriority(prio piecePriority) {
159 defer f.t.cl.unlock()
164 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
167 // Returns the priority per File.SetPriority.
168 func (f *File) Priority() piecePriority {
170 defer f.t.cl.unlock()
174 // Returns the index of the first piece containing data for the file.
175 func (f *File) firstPieceIndex() pieceIndex {
176 if f.t.usualPieceSize() == 0 {
179 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
182 // Returns the index of the piece after the last one containing data for the file.
183 func (f *File) endPieceIndex() pieceIndex {
184 if f.t.usualPieceSize() == 0 {
187 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))