6 "github.com/anacrolix/torrent/metainfo"
9 // Provides access to regions of torrent data that correspond to its files.
19 func (f *File) Torrent() *Torrent {
23 // Data for this file begins this many bytes into the Torrent.
24 func (f *File) Offset() int64 {
28 // The FileInfo from the metainfo.Info to which this file corresponds.
29 func (f File) FileInfo() metainfo.FileInfo {
33 // The file's path components joined by '/'.
34 func (f File) Path() string {
38 // The file's length in bytes.
39 func (f *File) Length() int64 {
43 // The relative file path for a multi-file torrent, and the torrent name for a
44 // single-file torrent.
45 func (f *File) DisplayPath() string {
46 fip := f.FileInfo().Path
50 return strings.Join(fip, "/")
54 // The download status of a piece that comprises part of a File.
55 type FilePieceState struct {
56 Bytes int64 // Bytes within the piece that are part of this File.
60 // Returns the state of pieces in this file.
61 func (f *File) State() (ret []FilePieceState) {
63 defer f.t.cl.rUnlock()
64 pieceSize := int64(f.t.usualPieceSize())
65 off := f.offset % pieceSize
67 for i := pieceIndex(f.offset / pieceSize); ; i++ {
71 len1 := pieceSize - off
75 ps := f.t.pieceState(i)
76 ret = append(ret, FilePieceState{len1, ps})
83 // Requests that all pieces containing data in the file be downloaded.
84 func (f *File) Download() {
85 f.SetPriority(PiecePriorityNormal)
88 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
89 begin = int((off + pieceSize - 1) / pieceSize)
90 end = int((off + size) / pieceSize)
94 func (f *File) exclusivePieces() (begin, end int) {
95 return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
98 // Deprecated: Use File.SetPriority.
99 func (f *File) Cancel() {
100 f.SetPriority(PiecePriorityNone)
103 func (f *File) NewReader() Reader {
107 readahead: 5 * 1024 * 1024,
115 // Sets the minimum priority for pieces in the File.
116 func (f *File) SetPriority(prio piecePriority) {
118 defer f.t.cl.unlock()
123 f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
126 // Returns the priority per File.SetPriority.
127 func (f *File) Priority() piecePriority {
129 defer f.t.cl.unlock()
133 func (f *File) firstPieceIndex() pieceIndex {
134 if f.t.usualPieceSize() == 0 {
137 return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
140 func (f *File) endPieceIndex() pieceIndex {
141 if f.t.usualPieceSize() == 0 {
144 return pieceIndex((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1