]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Add abstraction and tests for #387
[btrtrc.git] / file.go
1 package torrent
2
3 import (
4         "strings"
5
6         "github.com/anacrolix/missinggo/v2/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 fileBytesLeft(pieceSize int64, firstPieceIndex int, endPieceIndex int, fileOffset int64, fileLength int64, completedPieces bitmap.Bitmap) (left int64) {
58         endPieceIndex--
59         bitmap.Flip(completedPieces, firstPieceIndex+1, endPieceIndex).IterTyped(func(piece int) bool {
60                 if piece >= endPieceIndex {
61                         return false
62                 }
63                 if piece > firstPieceIndex {
64                         left += pieceSize
65                 }
66                 return true
67         })
68         if !completedPieces.Get(firstPieceIndex) {
69                 left += pieceSize - (fileOffset % pieceSize)
70         }
71         if !completedPieces.Get(endPieceIndex) {
72                 left += (fileOffset + fileLength) % pieceSize
73         }
74         return
75 }
76
77 func (f *File) bytesLeft() (left int64) {
78         return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
79 }
80
81 // The relative file path for a multi-file torrent, and the torrent name for a
82 // single-file torrent.
83 func (f *File) DisplayPath() string {
84         fip := f.FileInfo().Path
85         if len(fip) == 0 {
86                 return f.t.info.Name
87         }
88         return strings.Join(fip, "/")
89
90 }
91
92 // The download status of a piece that comprises part of a File.
93 type FilePieceState struct {
94         Bytes int64 // Bytes within the piece that are part of this File.
95         PieceState
96 }
97
98 // Returns the state of pieces in this file.
99 func (f *File) State() (ret []FilePieceState) {
100         f.t.cl.rLock()
101         defer f.t.cl.rUnlock()
102         pieceSize := int64(f.t.usualPieceSize())
103         off := f.offset % pieceSize
104         remaining := f.length
105         for i := pieceIndex(f.offset / pieceSize); ; i++ {
106                 if remaining == 0 {
107                         break
108                 }
109                 len1 := pieceSize - off
110                 if len1 > remaining {
111                         len1 = remaining
112                 }
113                 ps := f.t.pieceState(i)
114                 ret = append(ret, FilePieceState{len1, ps})
115                 off = 0
116                 remaining -= len1
117         }
118         return
119 }
120
121 // Requests that all pieces containing data in the file be downloaded.
122 func (f *File) Download() {
123         f.SetPriority(PiecePriorityNormal)
124 }
125
126 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
127         begin = int((off + pieceSize - 1) / pieceSize)
128         end = int((off + size) / pieceSize)
129         return
130 }
131
132 // Deprecated: Use File.SetPriority.
133 func (f *File) Cancel() {
134         f.SetPriority(PiecePriorityNone)
135 }
136
137 func (f *File) NewReader() Reader {
138         tr := reader{
139                 mu:        f.t.cl.locker(),
140                 t:         f.t,
141                 readahead: 5 * 1024 * 1024,
142                 offset:    f.Offset(),
143                 length:    f.Length(),
144         }
145         f.t.addReader(&tr)
146         return &tr
147 }
148
149 // Sets the minimum priority for pieces in the File.
150 func (f *File) SetPriority(prio piecePriority) {
151         f.t.cl.lock()
152         defer f.t.cl.unlock()
153         if prio == f.prio {
154                 return
155         }
156         f.prio = prio
157         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
158 }
159
160 // Returns the priority per File.SetPriority.
161 func (f *File) Priority() piecePriority {
162         f.t.cl.lock()
163         defer f.t.cl.unlock()
164         return f.prio
165 }
166
167 // Returns the index of the first piece containing data for the file.
168 func (f *File) firstPieceIndex() pieceIndex {
169         if f.t.usualPieceSize() == 0 {
170                 return 0
171         }
172         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
173 }
174
175 // Returns the index of the piece after the last one containing data for the file.
176 func (f *File) endPieceIndex() pieceIndex {
177         if f.t.usualPieceSize() == 0 {
178                 return 0
179         }
180         return pieceIndex((f.offset + f.length + int64(f.t.usualPieceSize()) - 1) / int64(f.t.usualPieceSize()))
181 }