2020-09-27 19:23:47 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2021-11-29 10:07:54 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2021-11-16 12:13:58 +00:00
|
|
|
"sync"
|
2021-11-29 10:07:54 +00:00
|
|
|
"time"
|
2021-11-16 12:13:58 +00:00
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Filesystem = &Torrent{}
|
|
|
|
|
|
|
|
type Torrent struct {
|
2021-11-29 10:07:54 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
ts map[string]*torrent.Torrent
|
|
|
|
s *storage
|
|
|
|
loaded bool
|
|
|
|
readTimeout int
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
func NewTorrent(readTimeout int) *Torrent {
|
2020-09-27 19:23:47 +00:00
|
|
|
return &Torrent{
|
2021-11-29 10:07:54 +00:00
|
|
|
s: newStorage(SupportedFactories),
|
|
|
|
ts: make(map[string]*torrent.Torrent),
|
|
|
|
readTimeout: readTimeout,
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
func (fs *Torrent) load() {
|
2021-11-16 12:13:58 +00:00
|
|
|
if fs.loaded {
|
|
|
|
return
|
|
|
|
}
|
2021-11-29 10:07:54 +00:00
|
|
|
fs.mu.RLock()
|
|
|
|
defer fs.mu.RUnlock()
|
2021-11-16 12:13:58 +00:00
|
|
|
|
2021-03-06 22:08:15 +00:00
|
|
|
for _, t := range fs.ts {
|
|
|
|
<-t.GotInfo()
|
|
|
|
for _, file := range t.Files() {
|
2021-11-29 10:07:54 +00:00
|
|
|
fs.s.Add(&torrentFile{
|
|
|
|
reader: file.NewReader(),
|
|
|
|
len: file.Length(),
|
|
|
|
timeout: fs.readTimeout,
|
|
|
|
}, file.Path())
|
2021-03-06 22:08:15 +00:00
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
2021-03-06 22:08:15 +00:00
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
fs.loaded = true
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2021-11-29 10:07:54 +00:00
|
|
|
return fs.s.Children(path)
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ File = &torrentFile{}
|
|
|
|
|
|
|
|
type torrentFile struct {
|
2021-11-29 10:07:54 +00:00
|
|
|
mu sync.Mutex
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
reader torrent.Reader
|
|
|
|
len int64
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
timeout int
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *torrentFile) Size() int64 {
|
|
|
|
return d.len
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *torrentFile) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *torrentFile) Close() error {
|
2021-11-29 10:07:54 +00:00
|
|
|
return d.reader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *torrentFile) Read(p []byte) (n int, err error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
timer := time.AfterFunc(
|
|
|
|
time.Duration(d.timeout)*time.Second,
|
|
|
|
func() {
|
|
|
|
cancel()
|
|
|
|
},
|
|
|
|
)
|
2021-03-01 18:04:59 +00:00
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
defer timer.Stop()
|
2021-03-01 18:04:59 +00:00
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
return d.reader.ReadContext(ctx, p)
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
func (d *torrentFile) ReadAt(p []byte, off int64) (int, error) {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
_, err := d.reader.Seek(off, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
i, err := d.readAtLeast(p, len(p))
|
|
|
|
return i, err
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:07:54 +00:00
|
|
|
func (d *torrentFile) readAtLeast(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(d.timeout)*time.Second,
|
|
|
|
func() {
|
|
|
|
cancel()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
nn, err = d.reader.ReadContext(ctx, buf[n:])
|
|
|
|
n += nn
|
|
|
|
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
if n >= min {
|
|
|
|
err = nil
|
|
|
|
} else if n > 0 && err == io.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
return
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|