]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Remove unused code
[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.rLock()
63         defer f.t.cl.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 // Deprecated: Use File.SetPriority.
95 func (f *File) Cancel() {
96         f.SetPriority(PiecePriorityNone)
97 }
98
99 func (f *File) NewReader() Reader {
100         tr := reader{
101                 mu:        f.t.cl.locker(),
102                 t:         f.t,
103                 readahead: 5 * 1024 * 1024,
104                 offset:    f.Offset(),
105                 length:    f.Length(),
106         }
107         f.t.addReader(&tr)
108         return &tr
109 }
110
111 // Sets the minimum priority for pieces in the File.
112 func (f *File) SetPriority(prio piecePriority) {
113         f.t.cl.lock()
114         defer f.t.cl.unlock()
115         if prio == f.prio {
116                 return
117         }
118         f.prio = prio
119         f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex())
120 }
121
122 // Returns the priority per File.SetPriority.
123 func (f *File) Priority() piecePriority {
124         f.t.cl.lock()
125         defer f.t.cl.unlock()
126         return f.prio
127 }
128
129 func (f *File) firstPieceIndex() pieceIndex {
130         if f.t.usualPieceSize() == 0 {
131                 return 0
132         }
133         return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
134 }
135
136 func (f *File) endPieceIndex() pieceIndex {
137         if f.t.usualPieceSize() == 0 {
138                 return 0
139         }
140         return pieceIndex((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
141 }