]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Improve various File related doc comments
[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 many bytes into the Torrent.
23 func (f *File) Offset() int64 {
24         return f.offset
25 }
26
27 // The FileInfo from the metainfo.Info to which this file corresponds.
28 func (f File) FileInfo() metainfo.FileInfo {
29         return f.fi
30 }
31
32 // The file's path components joined by '/'.
33 func (f File) Path() string {
34         return f.path
35 }
36
37 // The file's length in bytes.
38 func (f *File) Length() int64 {
39         return f.length
40 }
41
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
46         if len(fip) == 0 {
47                 return f.t.Info().Name
48         }
49         return strings.Join(fip, "/")
50
51 }
52
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.
56         PieceState
57 }
58
59 // Returns the state of pieces in this file.
60 func (f *File) State() (ret []FilePieceState) {
61         f.t.cl.mu.RLock()
62         defer f.t.cl.mu.RUnlock()
63         pieceSize := int64(f.t.usualPieceSize())
64         off := f.offset % pieceSize
65         remaining := f.length
66         for i := int(f.offset / pieceSize); ; i++ {
67                 if remaining == 0 {
68                         break
69                 }
70                 len1 := pieceSize - off
71                 if len1 > remaining {
72                         len1 = remaining
73                 }
74                 ps := f.t.pieceState(i)
75                 ret = append(ret, FilePieceState{len1, ps})
76                 off = 0
77                 remaining -= len1
78         }
79         return
80 }
81
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))
85 }
86
87 // Deprecated: Use File.DownloadRegion.
88 func (f *File) PrioritizeRegion(off, len int64) {
89         f.DownloadRegion(off, len)
90 }
91
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))
96 }
97
98 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
99         begin = int((off + pieceSize - 1) / pieceSize)
100         end = int((off + size) / pieceSize)
101         return
102 }
103
104 func (f *File) exclusivePieces() (begin, end int) {
105         return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
106 }
107
108 func (f *File) Cancel() {
109         f.t.CancelPieces(f.exclusivePieces())
110 }
111
112 func (f *File) NewReader() Reader {
113         tr := reader{
114                 mu:        &f.t.cl.mu,
115                 t:         f.t,
116                 readahead: 5 * 1024 * 1024,
117                 offset:    f.Offset(),
118                 length:    f.Length(),
119         }
120         f.t.addReader(&tr)
121         return &tr
122 }