6 "github.com/anacrolix/missinggo/bitmap"
8 "github.com/anacrolix/torrent/metainfo"
9 pp "github.com/anacrolix/torrent/peer_protocol"
12 // Provides access to regions of torrent data that correspond to its files.
22 func (f *File) Torrent() *Torrent {
26 // Data for this file begins this many bytes into the Torrent.
27 func (f *File) Offset() int64 {
31 // The FileInfo from the metainfo.Info to which this file corresponds.
32 func (f File) FileInfo() metainfo.FileInfo {
36 // The file's path components joined by '/'.
37 func (f File) Path() string {
41 // The file's length in bytes.
42 func (f *File) Length() int64 {
46 // Number of bytes of the entire file we have completed. This is the sum of
47 // completed pieces, and dirtied chunks of incomplete pieces.
48 func (f *File) BytesCompleted() int64 {
50 defer f.t.cl.rUnlock()
51 return f.bytesCompleted()
54 func (f *File) bytesCompleted() int64 {
55 return f.length - f.bytesLeft()
58 func (f *File) bytesLeft() (left int64) {
59 firstPieceIndex := f.firstPieceIndex()
60 endPieceIndex := f.endPieceIndex()
61 bitmap.Flip(f.t.completedPieces, firstPieceIndex, endPieceIndex+1).IterTyped(func(piece int) bool {
62 p := &f.t.pieces[piece]
63 left += int64(p.length() - p.numDirtyBytes())
66 startPiece := f.t.piece(firstPieceIndex)
67 endChunk := int(f.offset%f.t.info.PieceLength) * int(startPiece.numChunks()) / int(startPiece.length())
68 bitmap.Flip(startPiece.dirtyChunks, 0, endChunk).IterTyped(func(chunk int) bool {
69 left -= int64(startPiece.chunkSize())
72 endPiece := f.t.piece(endPieceIndex)
73 startChunk := int((f.offset+f.length)%f.t.info.PieceLength) * int(endPiece.numChunks()) / int(endPiece.length())
74 lastChunkIndex := int(endPiece.lastChunkIndex())
75 bitmap.Flip(endPiece.dirtyChunks, startChunk, int(endPiece.numChunks())).IterTyped(func(chunk int) bool {
76 if chunk == lastChunkIndex {
77 left -= int64(endPiece.chunkIndexSpec(pp.Integer(chunk)).Length)
79 left -= int64(endPiece.chunkSize())
86 // The relative file path for a multi-file torrent, and the torrent name for a
87 // single-file torrent.
88 func (f *File) DisplayPath() string {
89 fip := f.FileInfo().Path
93 return strings.Join(fip, "/")
97 // The download status of a piece that comprises part of a File.
98 type FilePieceState struct {
99 Bytes int64 // Bytes within the piece that are part of this File.
103 // Returns the state of pieces in this file.
104 func (f *File) State() (ret []FilePieceState) {
106 defer f.t.cl.rUnlock()
107 pieceSize := int64(f.t.usualPieceSize())
108 off := f.offset % pieceSize
109 remaining := f.length
110 for i := pieceIndex(f.offset / pieceSize); ; i++ {
114 len1 := pieceSize - off
115 if len1 > remaining {
118 ps := f.t.pieceState(i)
119 ret = append(ret, FilePieceState{len1, ps})
126 // Requests that all pieces containing data in the file be downloaded.
127 func (f *File) Download() {
128 f.SetPriority(PiecePriorityNormal)
131 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
132 begin = int((off + pieceSize - 1) / pieceSize)
133 end = int((off + size) / pieceSize)
137 // Deprecated: Use File.SetPriority.
138 func (f *File) Cancel() {
139 f.SetPriority(PiecePriorityNone)
142 func (f *File) NewReader() Reader {
146 readahead: 5 * 1024 * 1024,
154 // Sets the minimum priority for pieces in the File.
155 func (f *File) SetPriority(prio piecePriority) {
157 defer f.t.cl.unlock()
162 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
165 // Returns the priority per File.SetPriority.
166 func (f *File) Priority() piecePriority {
168 defer f.t.cl.unlock()
172 func (f *File) firstPieceIndex() pieceIndex {
173 if f.t.usualPieceSize() == 0 {
176 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
179 func (f *File) endPieceIndex() pieceIndex {
180 if f.t.usualPieceSize() == 0 {
183 return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))