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 many bytes into the Torrent.
23 func (f *File) Offset() int64 {
27 // The FileInfo from the metainfo.Info to which this file corresponds.
28 func (f File) FileInfo() metainfo.FileInfo {
32 // The file's path components joined by '/'.
33 func (f File) Path() string {
37 // The file's length in bytes.
38 func (f *File) Length() int64 {
42 // The relative file path for a multi-file torrent, and the torrent name for a
43 // single-file torrent.
44 func (f *File) DisplayPath() string {
45 fip := f.FileInfo().Path
47 return f.t.Info().Name
49 return strings.Join(fip, "/")
53 // The download status of a piece that comprises part of a File.
54 type FilePieceState struct {
55 Bytes int64 // Bytes within the piece that are part of this File.
59 // Returns the state of pieces in this file.
60 func (f *File) State() (ret []FilePieceState) {
62 defer f.t.cl.mu.RUnlock()
63 pieceSize := int64(f.t.usualPieceSize())
64 off := f.offset % pieceSize
66 for i := int(f.offset / pieceSize); ; i++ {
70 len1 := pieceSize - off
74 ps := f.t.pieceState(i)
75 ret = append(ret, FilePieceState{len1, ps})
82 // Requests that all pieces containing data in the file be downloaded.
83 func (f *File) Download() {
84 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset, f.length))
87 // Deprecated: Use File.DownloadRegion.
88 func (f *File) PrioritizeRegion(off, len int64) {
89 f.DownloadRegion(off, len)
92 // Requests that torrent pieces containing bytes in the given region of the
93 // file be downloaded.
94 func (f *File) DownloadRegion(off, len int64) {
95 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset+off, len))
98 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
99 begin = int((off + pieceSize - 1) / pieceSize)
100 end = int((off + size) / pieceSize)
104 func (f *File) exclusivePieces() (begin, end int) {
105 return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
108 func (f *File) Cancel() {
109 f.t.CancelPieces(f.exclusivePieces())
112 func (f *File) NewReader() Reader {
116 readahead: 5 * 1024 * 1024,