]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Readers obtained from File.NewReader should not readahead into other Files
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/torrent/metainfo"
7 )
8
9 // Provides access to regions of torrent data that correspond to its files.
10 type File struct {
11         t      *Torrent
12         path   string
13         offset int64
14         length int64
15         fi     metainfo.FileInfo
16 }
17
18 func (f *File) Torrent() *Torrent {
19         return f.t
20 }
21
22 // Data for this file begins this far into the torrent.
23 func (f *File) Offset() int64 {
24         return f.offset
25 }
26
27 func (f File) FileInfo() metainfo.FileInfo {
28         return f.fi
29 }
30
31 func (f File) Path() string {
32         return f.path
33 }
34
35 func (f *File) Length() int64 {
36         return f.length
37 }
38
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
43         if len(fip) == 0 {
44                 return f.t.Info().Name
45         }
46         return strings.Join(fip, "/")
47
48 }
49
50 type FilePieceState struct {
51         Bytes int64 // Bytes within the piece that are part of this File.
52         PieceState
53 }
54
55 // Returns the state of pieces in this file.
56 func (f *File) State() (ret []FilePieceState) {
57         f.t.cl.mu.RLock()
58         defer f.t.cl.mu.RUnlock()
59         pieceSize := int64(f.t.usualPieceSize())
60         off := f.offset % pieceSize
61         remaining := f.length
62         for i := int(f.offset / pieceSize); ; i++ {
63                 if remaining == 0 {
64                         break
65                 }
66                 len1 := pieceSize - off
67                 if len1 > remaining {
68                         len1 = remaining
69                 }
70                 ps := f.t.pieceState(i)
71                 ret = append(ret, FilePieceState{len1, ps})
72                 off = 0
73                 remaining -= len1
74         }
75         return
76 }
77
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))
81 }
82
83 // Deprecated: Use File.DownloadRegion.
84 func (f *File) PrioritizeRegion(off, len int64) {
85         f.DownloadRegion(off, len)
86 }
87
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))
92 }
93
94 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
95         begin = int((off + pieceSize - 1) / pieceSize)
96         end = int((off + size) / pieceSize)
97         return
98 }
99
100 func (f *File) exclusivePieces() (begin, end int) {
101         return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
102 }
103
104 func (f *File) Cancel() {
105         f.t.CancelPieces(f.exclusivePieces())
106 }
107
108 func (f *File) NewReader() Reader {
109         tr := reader{
110                 mu:        &f.t.cl.mu,
111                 t:         f.t,
112                 readahead: 5 * 1024 * 1024,
113                 offset:    f.Offset(),
114                 length:    f.Length(),
115         }
116         f.t.addReader(&tr)
117         return &tr
118 }