/
torrent.go
197 lines (159 loc) · 3.36 KB
/
torrent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package fs
import (
"context"
"io"
"sync"
"time"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent"
"github.com/distribyted/distribyted/iio"
)
var _ Filesystem = &Torrent{}
type Torrent struct {
mu sync.RWMutex
ts map[string]*torrent.Torrent
s *storage
loaded bool
readTimeout int
}
func NewTorrent(readTimeout int) *Torrent {
return &Torrent{
s: newStorage(SupportedFactories),
ts: make(map[string]*torrent.Torrent),
readTimeout: readTimeout,
}
}
func (fs *Torrent) AddTorrent(t *torrent.Torrent) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.loaded = false
fs.ts[t.InfoHash().HexString()] = t
}
func (fs *Torrent) RemoveTorrent(h string) {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.s.Clear()
fs.loaded = false
delete(fs.ts, h)
}
func (fs *Torrent) load() {
if fs.loaded {
return
}
fs.mu.RLock()
defer fs.mu.RUnlock()
for _, t := range fs.ts {
<-t.GotInfo()
for _, file := range t.Files() {
fs.s.Add(&torrentFile{
readerFunc: file.NewReader,
len: file.Length(),
timeout: fs.readTimeout,
}, file.Path())
}
}
fs.loaded = true
}
func (fs *Torrent) Open(filename string) (File, error) {
fs.load()
return fs.s.Get(filename)
}
func (fs *Torrent) ReadDir(path string) (map[string]File, error) {
fs.load()
return fs.s.Children(path)
}
type reader interface {
iio.Reader
missinggo.ReadContexter
}
type readAtWrapper struct {
timeout int
mu sync.Mutex
torrent.Reader
io.ReaderAt
io.Closer
}
func newReadAtWrapper(r torrent.Reader, timeout int) reader {
return &readAtWrapper{Reader: r, timeout: timeout}
}
func (rw *readAtWrapper) ReadAt(p []byte, off int64) (int, error) {
rw.mu.Lock()
defer rw.mu.Unlock()
_, err := rw.Seek(off, io.SeekStart)
if err != nil {
return 0, err
}
return readAtLeast(rw, rw.timeout, p, len(p))
}
func readAtLeast(r missinggo.ReadContexter, timeout int, buf []byte, min int) (n int, err error) {
if len(buf) < min {
return 0, io.ErrShortBuffer
}
for n < min && err == nil {
var nn int
ctx, cancel := context.WithCancel(context.Background())
timer := time.AfterFunc(
time.Duration(timeout)*time.Second,
func() {
cancel()
},
)
nn, err = r.ReadContext(ctx, buf[n:])
n += nn
timer.Stop()
}
if n >= min {
err = nil
} else if n > 0 && err == io.EOF {
err = io.ErrUnexpectedEOF
}
return
}
func (rw *readAtWrapper) Close() error {
rw.mu.Lock()
defer rw.mu.Unlock()
return rw.Reader.Close()
}
var _ File = &torrentFile{}
type torrentFile struct {
readerFunc func() torrent.Reader
reader reader
len int64
timeout int
}
func (d *torrentFile) load() {
if d.reader != nil {
return
}
d.reader = newReadAtWrapper(d.readerFunc(), d.timeout)
}
func (d *torrentFile) Size() int64 {
return d.len
}
func (d *torrentFile) IsDir() bool {
return false
}
func (d *torrentFile) Close() error {
var err error
if d.reader != nil {
err = d.reader.Close()
}
d.reader = nil
return err
}
func (d *torrentFile) Read(p []byte) (n int, err error) {
d.load()
ctx, cancel := context.WithCancel(context.Background())
timer := time.AfterFunc(
time.Duration(d.timeout)*time.Second,
func() {
cancel()
},
)
defer timer.Stop()
return d.reader.ReadContext(ctx, p)
}
func (d *torrentFile) ReadAt(p []byte, off int64) (n int, err error) {
d.load()
return d.reader.ReadAt(p, off)
}