]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Fix bytesLeft for files (#348)
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/missinggo/bitmap"
7
8         "github.com/anacrolix/torrent/metainfo"
9 )
10
11 // Provides access to regions of torrent data that correspond to its files.
12 type File struct {
13         t      *Torrent
14         path   string
15         offset int64
16         length int64
17         fi     metainfo.FileInfo
18         prio   piecePriority
19 }
20
21 func (f *File) Torrent() *Torrent {
22         return f.t
23 }
24
25 // Data for this file begins this many bytes into the Torrent.
26 func (f *File) Offset() int64 {
27         return f.offset
28 }
29
30 // The FileInfo from the metainfo.Info to which this file corresponds.
31 func (f File) FileInfo() metainfo.FileInfo {
32         return f.fi
33 }
34
35 // The file's path components joined by '/'.
36 func (f File) Path() string {
37         return f.path
38 }
39
40 // The file's length in bytes.
41 func (f *File) Length() int64 {
42         return f.length
43 }
44
45 // Number of bytes of the entire file we have completed. This is the sum of
46 // completed pieces, and dirtied chunks of incomplete pieces.
47 func (f *File) BytesCompleted() int64 {
48         f.t.cl.rLock()
49         defer f.t.cl.rUnlock()
50         return f.bytesCompleted()
51 }
52
53 func (f *File) bytesCompleted() int64 {
54         return f.length - f.bytesLeft()
55 }
56
57 func (f *File) bytesLeft() (left int64) {
58         pieceSize := int64(f.t.usualPieceSize())
59         firstPieceIndex := f.firstPieceIndex()
60         endPieceIndex := f.endPieceIndex() - 1
61         bitmap.Flip(f.t.completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
62                 if piece >= endPieceIndex {
63                         return false
64                 }
65                 if piece > firstPieceIndex {
66                         left += pieceSize
67                 }
68                 return true
69         })
70         if !f.t.pieceComplete(firstPieceIndex) {
71                 left += pieceSize - (f.offset % pieceSize)
72         }
73         if !f.t.pieceComplete(endPieceIndex) {
74                 left += (f.offset + f.length) % pieceSize
75         }
76         return
77 }
78
79 // The relative file path for a multi-file torrent, and the torrent name for a
80 // single-file torrent.
81 func (f *File) DisplayPath() string {
82         fip := f.FileInfo().Path
83         if len(fip) == 0 {
84                 return f.t.info.Name
85         }
86         return strings.Join(fip, "/")
87
88 }
89
90 // The download status of a piece that comprises part of a File.
91 type FilePieceState struct {
92         Bytes int64 // Bytes within the piece that are part of this File.
93         PieceState
94 }
95
96 // Returns the state of pieces in this file.
97 func (f *File) State() (ret []FilePieceState) {
98         f.t.cl.rLock()
99         defer f.t.cl.rUnlock()
100         pieceSize := int64(f.t.usualPieceSize())
101         off := f.offset % pieceSize
102         remaining := f.length
103         for i := pieceIndex(f.offset / pieceSize); ; i++ {
104                 if remaining == 0 {
105                         break
106                 }
107                 len1 := pieceSize - off
108                 if len1 > remaining {
109                         len1 = remaining
110                 }
111                 ps := f.t.pieceState(i)
112                 ret = append(ret, FilePieceState{len1, ps})
113                 off = 0
114                 remaining -= len1
115         }
116         return
117 }
118
119 // Requests that all pieces containing data in the file be downloaded.
120 func (f *File) Download() {
121         f.SetPriority(PiecePriorityNormal)
122 }
123
124 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
125         begin = int((off + pieceSize - 1) / pieceSize)
126         end = int((off + size) / pieceSize)
127         return
128 }
129
130 // Deprecated: Use File.SetPriority.
131 func (f *File) Cancel() {
132         f.SetPriority(PiecePriorityNone)
133 }
134
135 func (f *File) NewReader() Reader {
136         tr := reader{
137                 mu:        f.t.cl.locker(),
138                 t:         f.t,
139                 readahead: 5 * 1024 * 1024,
140                 offset:    f.Offset(),
141                 length:    f.Length(),
142         }
143         f.t.addReader(&tr)
144         return &tr
145 }
146
147 // Sets the minimum priority for pieces in the File.
148 func (f *File) SetPriority(prio piecePriority) {
149         f.t.cl.lock()
150         defer f.t.cl.unlock()
151         if prio == f.prio {
152                 return
153         }
154         f.prio = prio
155         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
156 }
157
158 // Returns the priority per File.SetPriority.
159 func (f *File) Priority() piecePriority {
160         f.t.cl.lock()
161         defer f.t.cl.unlock()
162         return f.prio
163 }
164
165 // Returns the index of the first piece containing data for the file.
166 func (f *File) firstPieceIndex() pieceIndex {
167         if f.t.usualPieceSize() == 0 {
168                 return 0
169         }
170         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
171 }
172
173 // Returns the index of the piece after the last one containing data for the file.
174 func (f *File) endPieceIndex() pieceIndex {
175         if f.t.usualPieceSize() == 0 {
176                 return 0
177         }
178         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
179 }