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) {
57 pieceSize := int64(f.t.usualPieceSize())
58 off := f.offset % pieceSize
60 for i := int(f.offset / pieceSize); ; i++ {
64 len1 := pieceSize - off
69 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 // Requests that torrent pieces containing bytes in the given region of the
84 // file be downloaded.
85 func (f *File) PrioritizeRegion(off, len int64) {
86 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset+off, len))
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 func (f *File) Cancel() {
100 f.t.CancelPieces(f.exclusivePieces())