]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Merge remote-tracking branch 'libtorgo/master' into HEAD
[btrtrc.git] / file.go
1 package torrent
2
3 import "github.com/anacrolix/libtorgo/metainfo"
4
5 // Provides access to regions of torrent data that correspond to its files.
6 type File struct {
7         t      Torrent
8         path   string
9         offset int64
10         length int64
11         fi     metainfo.FileInfo
12 }
13
14 // Data for this file begins this far into the torrent.
15 func (f *File) Offset() int64 {
16         return f.offset
17 }
18
19 func (f File) FileInfo() metainfo.FileInfo {
20         return f.fi
21 }
22
23 func (f File) Path() string {
24         return f.path
25 }
26
27 func (f *File) Length() int64 {
28         return f.length
29 }
30
31 type FilePieceState struct {
32         Length int64
33         State  byte
34 }
35
36 func (f *File) Progress() (ret []FilePieceState) {
37         pieceSize := int64(f.t.usualPieceSize())
38         off := f.offset % pieceSize
39         remaining := f.length
40         for i := int(f.offset / pieceSize); ; i++ {
41                 if remaining == 0 {
42                         break
43                 }
44                 len1 := pieceSize - off
45                 if len1 > remaining {
46                         len1 = remaining
47                 }
48                 ret = append(ret, FilePieceState{len1, f.t.pieceStatusChar(i)})
49                 off = 0
50                 remaining -= len1
51         }
52         return
53 }
54
55 func (f *File) PrioritizeRegion(off, len int64) {
56         if off < 0 || off >= f.length {
57                 return
58         }
59         if off+len > f.length {
60                 len = f.length - off
61         }
62         off += f.offset
63         f.t.SetRegionPriority(off, len)
64 }