]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Merge branch 'master' of github.com:anacrolix/torrent
[btrtrc.git] / file.go
1 package torrent
2
3 import "github.com/anacrolix/torrent/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         Bytes int64 // Bytes within the piece that are part of this File.
33         PieceState
34 }
35
36 // Returns the state of pieces in this file.
37 func (f *File) State() (ret []FilePieceState) {
38         f.t.cl.mu.Lock()
39         defer f.t.cl.mu.Unlock()
40         pieceSize := int64(f.t.usualPieceSize())
41         off := f.offset % pieceSize
42         remaining := f.length
43         for i := int(f.offset / pieceSize); ; i++ {
44                 if remaining == 0 {
45                         break
46                 }
47                 len1 := pieceSize - off
48                 if len1 > remaining {
49                         len1 = remaining
50                 }
51                 ret = append(ret, FilePieceState{len1, f.t.pieceState(i)})
52                 off = 0
53                 remaining -= len1
54         }
55         return
56 }
57
58 func (f *File) PrioritizeRegion(off, len int64) {
59         if off < 0 || off >= f.length {
60                 return
61         }
62         if off+len > f.length {
63                 len = f.length - off
64         }
65         off += f.offset
66         f.t.SetRegionPriority(off, len)
67 }