6 "github.com/anacrolix/torrent/metainfo"
9 // Provides access to regions of torrent data that correspond to its files.
18 func (f *File) Torrent() *Torrent {
22 // Data for this file begins this far into the torrent.
23 func (f *File) Offset() int64 {
27 func (f File) FileInfo() metainfo.FileInfo {
31 func (f File) Path() string {
35 func (f *File) Length() int64 {
39 // The relative file path for a multi-file torrent, and the torrent name for a
40 // single-file torrent.
41 func (f *File) DisplayPath() string {
42 fip := f.FileInfo().Path
44 return f.t.Info().Name
46 return strings.Join(fip, "/")
50 type FilePieceState struct {
51 Bytes int64 // Bytes within the piece that are part of this File.
55 // Returns the state of pieces in this file.
56 func (f *File) State() (ret []FilePieceState) {
58 defer f.t.cl.mu.RUnlock()
59 pieceSize := int64(f.t.usualPieceSize())
60 off := f.offset % pieceSize
62 for i := int(f.offset / pieceSize); ; i++ {
66 len1 := pieceSize - off
70 ps := f.t.pieceState(i)
71 ret = append(ret, FilePieceState{len1, ps})
78 // Requests that all pieces containing data in the file be downloaded.
79 func (f *File) Download() {
80 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset, f.length))
83 // Deprecated: Use File.DownloadRegion.
84 func (f *File) PrioritizeRegion(off, len int64) {
85 f.DownloadRegion(off, len)
88 // Requests that torrent pieces containing bytes in the given region of the
89 // file be downloaded.
90 func (f *File) DownloadRegion(off, len int64) {
91 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset+off, len))
94 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
95 begin = int((off + pieceSize - 1) / pieceSize)
96 end = int((off + size) / pieceSize)
100 func (f *File) exclusivePieces() (begin, end int) {
101 return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
104 func (f *File) Cancel() {
105 f.t.CancelPieces(f.exclusivePieces())
108 func (f *File) NewReader() Reader {
112 readahead: 5 * 1024 * 1024,