]> Sergey Matveev's repositories - btrtrc.git/blob - file.go
Add comment doc for File.PrioritizeRegion
[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 }
17
18 // Data for this file begins this far into the torrent.
19 func (f *File) Offset() int64 {
20         return f.offset
21 }
22
23 func (f File) FileInfo() metainfo.FileInfo {
24         return f.fi
25 }
26
27 func (f File) Path() string {
28         return f.path
29 }
30
31 func (f *File) Length() int64 {
32         return f.length
33 }
34
35 // The relative file path for a multi-file torrent, and the torrent name for a
36 // single-file torrent.
37 func (f *File) DisplayPath() string {
38         fip := f.FileInfo().Path
39         if len(fip) == 0 {
40                 return f.t.Info().Name
41         }
42         return strings.Join(fip, "/")
43
44 }
45
46 type FilePieceState struct {
47         Bytes int64 // Bytes within the piece that are part of this File.
48         PieceState
49 }
50
51 // Returns the state of pieces in this file.
52 func (f *File) State() (ret []FilePieceState) {
53         pieceSize := int64(f.t.usualPieceSize())
54         off := f.offset % pieceSize
55         remaining := f.length
56         for i := int(f.offset / pieceSize); ; i++ {
57                 if remaining == 0 {
58                         break
59                 }
60                 len1 := pieceSize - off
61                 if len1 > remaining {
62                         len1 = remaining
63                 }
64                 f.t.cl.mu.RLock()
65                 ps := f.t.pieceState(i)
66                 f.t.cl.mu.RUnlock()
67                 ret = append(ret, FilePieceState{len1, ps})
68                 off = 0
69                 remaining -= len1
70         }
71         return
72 }
73
74 // Marks pieces in the region of the file for download. This is a helper
75 // wrapping Torrent.SetRegionPriority.
76 func (f *File) PrioritizeRegion(off, len int64) {
77         if off < 0 || off >= f.length {
78                 return
79         }
80         if off+len > f.length {
81                 len = f.length - off
82         }
83         off += f.offset
84         f.t.SetRegionPriority(off, len)
85 }