6 "github.com/anacrolix/torrent/metainfo"
7 pwp "github.com/anacrolix/torrent/peer_protocol"
10 // Provides access to regions of torrent data that correspond to its files.
20 func (f *File) Torrent() *Torrent {
24 // Data for this file begins this many bytes into the Torrent.
25 func (f *File) Offset() int64 {
29 // The FileInfo from the metainfo.Info to which this file corresponds.
30 func (f File) FileInfo() metainfo.FileInfo {
34 // The file's path components joined by '/'.
35 func (f File) Path() string {
39 // The file's length in bytes.
40 func (f *File) Length() int64 {
44 // The relative file path for a multi-file torrent, and the torrent name for a
45 // single-file torrent.
46 func (f *File) DisplayPath() string {
47 fip := f.FileInfo().Path
51 return strings.Join(fip, "/")
55 // The download status of a piece that comprises part of a File.
56 type FilePieceState struct {
57 Bytes int64 // Bytes within the piece that are part of this File.
61 // Returns the state of pieces in this file.
62 func (f *File) State() (ret []FilePieceState) {
64 defer f.t.cl.mu.RUnlock()
65 pieceSize := int64(f.t.usualPieceSize())
66 off := f.offset % pieceSize
68 for i := pieceIndex(f.offset / pieceSize); ; i++ {
72 len1 := pieceSize - off
76 ps := f.t.pieceState(i)
77 ret = append(ret, FilePieceState{len1, ps})
84 // Requests that all pieces containing data in the file be downloaded.
85 func (f *File) Download() {
86 f.SetPriority(PiecePriorityNormal)
89 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
90 begin = int((off + pieceSize - 1) / pieceSize)
91 end = int((off + size) / pieceSize)
95 func (f *File) exclusivePieces() (begin, end int) {
96 return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
99 // Deprecated: Use File.SetPriority.
100 func (f *File) Cancel() {
101 f.SetPriority(PiecePriorityNone)
104 func (f *File) NewReader() Reader {
108 readahead: 5 * 1024 * 1024,
116 // Sets the minimum priority for pieces in the File.
117 func (f *File) SetPriority(prio piecePriority) {
119 defer f.t.cl.mu.Unlock()
124 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
127 // Returns the priority per File.SetPriority.
128 func (f *File) Priority() piecePriority {
130 defer f.t.cl.mu.Unlock()
134 func (f *File) firstPieceIndex() pwp.Integer {
135 if f.t.usualPieceSize() == 0 {
138 return pwp.Integer(f.offset / int64(f.t.usualPieceSize()))
141 func (f *File) endPieceIndex() pwp.Integer {
142 if f.t.usualPieceSize() == 0 {
145 return pwp.Integer((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1