]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Switch pieceIndex back to an int
[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         prio   piecePriority
17 }
18
19 func (f *File) Torrent() *Torrent {
20         return f.t
21 }
22
23 // Data for this file begins this many bytes into the Torrent.
24 func (f *File) Offset() int64 {
25         return f.offset
26 }
27
28 // The FileInfo from the metainfo.Info to which this file corresponds.
29 func (f File) FileInfo() metainfo.FileInfo {
30         return f.fi
31 }
32
33 // The file's path components joined by '/'.
34 func (f File) Path() string {
35         return f.path
36 }
37
38 // The file's length in bytes.
39 func (f *File) Length() int64 {
40         return f.length
41 }
42
43 // The relative file path for a multi-file torrent, and the torrent name for a
44 // single-file torrent.
45 func (f *File) DisplayPath() string {
46         fip := f.FileInfo().Path
47         if len(fip) == 0 {
48                 return f.t.info.Name
49         }
50         return strings.Join(fip, "/")
51
52 }
53
54 // The download status of a piece that comprises part of a File.
55 type FilePieceState struct {
56         Bytes int64 // Bytes within the piece that are part of this File.
57         PieceState
58 }
59
60 // Returns the state of pieces in this file.
61 func (f *File) State() (ret []FilePieceState) {
62         f.t.cl.mu.RLock()
63         defer f.t.cl.mu.RUnlock()
64         pieceSize := int64(f.t.usualPieceSize())
65         off := f.offset % pieceSize
66         remaining := f.length
67         for i := pieceIndex(f.offset / pieceSize); ; i++ {
68                 if remaining == 0 {
69                         break
70                 }
71                 len1 := pieceSize - off
72                 if len1 > remaining {
73                         len1 = remaining
74                 }
75                 ps := f.t.pieceState(i)
76                 ret = append(ret, FilePieceState{len1, ps})
77                 off = 0
78                 remaining -= len1
79         }
80         return
81 }
82
83 // Requests that all pieces containing data in the file be downloaded.
84 func (f *File) Download() {
85         f.SetPriority(PiecePriorityNormal)
86 }
87
88 func byteRegionExclusivePieces(off, size, pieceSize int64) (begin, end int) {
89         begin = int((off + pieceSize - 1) / pieceSize)
90         end = int((off + size) / pieceSize)
91         return
92 }
93
94 func (f *File) exclusivePieces() (begin, end int) {
95         return byteRegionExclusivePieces(f.offset, f.length, int64(f.t.usualPieceSize()))
96 }
97
98 // Deprecated: Use File.SetPriority.
99 func (f *File) Cancel() {
100         f.SetPriority(PiecePriorityNone)
101 }
102
103 func (f *File) NewReader() Reader {
104         tr := reader{
105                 mu:        &f.t.cl.mu,
106                 t:         f.t,
107                 readahead: 5 * 1024 * 1024,
108                 offset:    f.Offset(),
109                 length:    f.Length(),
110         }
111         f.t.addReader(&tr)
112         return &tr
113 }
114
115 // Sets the minimum priority for pieces in the File.
116 func (f *File) SetPriority(prio piecePriority) {
117         f.t.cl.mu.Lock()
118         defer f.t.cl.mu.Unlock()
119         if prio == f.prio {
120                 return
121         }
122         f.prio = prio
123         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
124 }
125
126 // Returns the priority per File.SetPriority.
127 func (f *File) Priority() piecePriority {
128         f.t.cl.mu.Lock()
129         defer f.t.cl.mu.Unlock()
130         return f.prio
131 }
132
133 func (f *File) firstPieceIndex() pieceIndex {
134         if f.t.usualPieceSize() == 0 {
135                 return 0
136         }
137         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
138 }
139
140 func (f *File) endPieceIndex() pieceIndex {
141         if f.t.usualPieceSize() == 0 {
142                 return 0
143         }
144         return pieceIndex((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
145 }