6 "github.com/anacrolix/missinggo"
7 "github.com/anacrolix/torrent/metainfo"
10 // 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 far into the torrent.
24 func (f *File) Offset() int64 {
28 func (f File) FileInfo() metainfo.FileInfo {
32 func (f File) Path() string {
36 func (f *File) Length() int64 {
40 // The relative file path for a multi-file torrent, and the torrent name for a
41 // single-file torrent.
42 func (f *File) DisplayPath() string {
43 fip := f.FileInfo().Path
45 return f.t.Info().Name
47 return strings.Join(fip, "/")
51 type FilePieceState struct {
52 Bytes int64 // Bytes within the piece that are part of this File.
56 // Returns the state of pieces in this file.
57 func (f *File) State() (ret []FilePieceState) {
59 defer f.t.cl.mu.RUnlock()
60 pieceSize := int64(f.t.usualPieceSize())
61 off := f.offset % pieceSize
63 for i := int(f.offset / pieceSize); ; i++ {
67 len1 := pieceSize - off
71 ps := f.t.pieceState(i)
72 ret = append(ret, FilePieceState{len1, ps})
79 // Requests that all pieces containing data in the file be downloaded.
80 func (f *File) Download() {
81 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset, f.length))
84 // Requests that torrent pieces containing bytes in the given region of the
85 // file be downloaded.
86 func (f *File) PrioritizeRegion(off, len int64) {
87 f.t.DownloadPieces(f.t.byteRegionPieces(f.offset+off, len))
90 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
91 begin = int((off + pieceSize - 1) / pieceSize)
92 end = int((off + size) / pieceSize)
96 func (f *File) exclusivePieces() (begin, end int) {
97 return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
100 func (f *File) Cancel() {
101 f.t.CancelPieces(f.exclusivePieces())
104 func (f *File) NewReader() Reader {
105 tr := f.t.NewReader()
106 return fileReader{missinggo.NewSectionReadSeeker(tr, f.Offset(), f.Length()), tr}