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,
65 numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
66 switch numPiecesSpanned {
69 if !torrentCompletedPieces.Get(fileFirstPieceIndex) {
73 if !torrentCompletedPieces.Get(fileFirstPieceIndex) {
74 left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
76 if !torrentCompletedPieces.Get(fileEndPieceIndex - 1) {
77 left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
79 completedMiddlePieces := torrentCompletedPieces.Copy()
80 completedMiddlePieces.RemoveRange(0, fileFirstPieceIndex+1)
81 completedMiddlePieces.RemoveRange(fileEndPieceIndex-1, bitmap.ToEnd)
82 left += int64(numPiecesSpanned-2-completedMiddlePieces.Len()) * torrentUsualPieceSize
87 func (f *File) bytesLeft() (left int64) {
88 return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
91 // The relative file path for a multi-file torrent, and the torrent name for a
92 // single-file torrent.
93 func (f *File) DisplayPath() string {
94 fip := f.FileInfo().Path
98 return strings.Join(fip, "/")
101 // The download status of a piece that comprises part of a File.
102 type FilePieceState struct {
103 Bytes int64 // Bytes within the piece that are part of this File.
107 // Returns the state of pieces in this file.
108 func (f *File) State() (ret []FilePieceState) {
110 defer f.t.cl.rUnlock()
111 pieceSize := int64(f.t.usualPieceSize())
112 off := f.offset % pieceSize
113 remaining := f.length
114 for i := pieceIndex(f.offset / pieceSize); ; i++ {
118 len1 := pieceSize - off
119 if len1 > remaining {
122 ps := f.t.pieceState(i)
123 ret = append(ret, FilePieceState{len1, ps})
130 // Requests that all pieces containing data in the file be downloaded.
131 func (f *File) Download() {
132 f.SetPriority(PiecePriorityNormal)
135 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
136 begin = int((off + pieceSize - 1) / pieceSize)
137 end = int((off + size) / pieceSize)
141 // Deprecated: Use File.SetPriority.
142 func (f *File) Cancel() {
143 f.SetPriority(PiecePriorityNone)
146 func (f *File) NewReader() Reader {
150 readahead: 5 * 1024 * 1024,
158 // Sets the minimum priority for pieces in the File.
159 func (f *File) SetPriority(prio piecePriority) {
161 defer f.t.cl.unlock()
166 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
169 // Returns the priority per File.SetPriority.
170 func (f *File) Priority() piecePriority {
172 defer f.t.cl.unlock()
176 // Returns the index of the first piece containing data for the file.
177 func (f *File) firstPieceIndex() pieceIndex {
178 if f.t.usualPieceSize() == 0 {
181 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
184 // Returns the index of the piece after the last one containing data for the file.
185 func (f *File) endPieceIndex() pieceIndex {
186 if f.t.usualPieceSize() == 0 {
189 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))