6 "github.com/anacrolix/missinggo/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 (f *File) bytesLeft() (left int64) {
58 pieceSize := int64(f.t.usualPieceSize())
59 firstPieceIndex := f.firstPieceIndex()
60 endPieceIndex := f.endPieceIndex() - 1
61 bitmap.Flip(f.t.completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
62 if piece >= endPieceIndex {
65 if piece > firstPieceIndex {
70 if !f.t.pieceComplete(firstPieceIndex) {
71 left += pieceSize - (f.offset % pieceSize)
73 if !f.t.pieceComplete(endPieceIndex) {
74 left += (f.offset + f.length) % pieceSize
79 // The relative file path for a multi-file torrent, and the torrent name for a
80 // single-file torrent.
81 func (f *File) DisplayPath() string {
82 fip := f.FileInfo().Path
86 return strings.Join(fip, "/")
90 // The download status of a piece that comprises part of a File.
91 type FilePieceState struct {
92 Bytes int64 // Bytes within the piece that are part of this File.
96 // Returns the state of pieces in this file.
97 func (f *File) State() (ret []FilePieceState) {
99 defer f.t.cl.rUnlock()
100 pieceSize := int64(f.t.usualPieceSize())
101 off := f.offset % pieceSize
102 remaining := f.length
103 for i := pieceIndex(f.offset / pieceSize); ; i++ {
107 len1 := pieceSize - off
108 if len1 > remaining {
111 ps := f.t.pieceState(i)
112 ret = append(ret, FilePieceState{len1, ps})
119 // Requests that all pieces containing data in the file be downloaded.
120 func (f *File) Download() {
121 f.SetPriority(PiecePriorityNormal)
124 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
125 begin = int((off + pieceSize - 1) / pieceSize)
126 end = int((off + size) / pieceSize)
130 // Deprecated: Use File.SetPriority.
131 func (f *File) Cancel() {
132 f.SetPriority(PiecePriorityNone)
135 func (f *File) NewReader() Reader {
139 readahead: 5 * 1024 * 1024,
147 // Sets the minimum priority for pieces in the File.
148 func (f *File) SetPriority(prio piecePriority) {
150 defer f.t.cl.unlock()
155 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
158 // Returns the priority per File.SetPriority.
159 func (f *File) Priority() piecePriority {
161 defer f.t.cl.unlock()
165 // Returns the index of the first piece containing data for the file.
166 func (f *File) firstPieceIndex() pieceIndex {
167 if f.t.usualPieceSize() == 0 {
170 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
173 // Returns the index of the piece after the last one containing data for the file.
174 func (f *File) endPieceIndex() pieceIndex {
175 if f.t.usualPieceSize() == 0 {
178 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))