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