Several torrents per filesystem (#38)

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
This commit is contained in:
Antonio Navarro Perez 2021-03-06 23:08:15 +01:00 committed by GitHub
parent ed8bd64017
commit 2a38efbb03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 46 deletions

View file

@ -8,22 +8,25 @@ import (
var _ Filesystem = &Torrent{} var _ Filesystem = &Torrent{}
type Torrent struct { type Torrent struct {
t *torrent.Torrent ts []*torrent.Torrent
s *storage s *storage
} }
func NewTorrent(t *torrent.Torrent) *Torrent { func NewTorrent(ts []*torrent.Torrent) *Torrent {
return &Torrent{ return &Torrent{
t: t, ts: ts,
s: newStorage(SupportedFactories), s: newStorage(SupportedFactories),
} }
} }
func (fs *Torrent) load() { func (fs *Torrent) load() {
<-fs.t.GotInfo() for _, t := range fs.ts {
for _, file := range fs.t.Files() { <-t.GotInfo()
for _, file := range t.Files() {
fs.s.Add(&torrentFile{readerFunc: file.NewReader, len: file.Length()}, file.Path()) fs.s.Add(&torrentFile{readerFunc: file.NewReader, len: file.Length()}, file.Path())
} }
}
} }
func (fs *Torrent) Open(filename string) (File, error) { func (fs *Torrent) Open(filename string) (File, error) {

View file

@ -22,10 +22,10 @@ func TestTorrentFilesystem(t *testing.T) {
client, err := torrent.NewClient(cfg) client, err := torrent.NewClient(cfg)
require.NoError(err) require.NoError(err)
torrent, err := client.AddMagnet(testMagnet) to, err := client.AddMagnet(testMagnet)
require.NoError(err) require.NoError(err)
tfs := NewTorrent(torrent) tfs := NewTorrent([]*torrent.Torrent{to})
files, err := tfs.ReadDir("/") files, err := tfs.ReadDir("/")
require.NoError(err) require.NoError(err)

View file

@ -24,7 +24,7 @@ func NewHandler(fuseAllowOther bool) *Handler {
} }
} }
func (s *Handler) MountAll(fss map[string][]fs.Filesystem, ef config.EventFunc) error { func (s *Handler) MountAll(fss map[string]fs.Filesystem, ef config.EventFunc) error {
for p, fss := range fss { for p, fss := range fss {
folder := p folder := p
// On windows, the folder must don't exist // On windows, the folder must don't exist

View file

@ -17,9 +17,9 @@ type FS struct {
fh *fileHandler fh *fileHandler
} }
func NewFS(fss []fs.Filesystem) fuse.FileSystemInterface { func NewFS(fs fs.Filesystem) fuse.FileSystemInterface {
return &FS{ return &FS{
fh: &fileHandler{fss: fss}, fh: &fileHandler{fs: fs},
} }
} }
@ -144,7 +144,7 @@ var ErrBadHolderIndex = errors.New("holder index too big")
type fileHandler struct { type fileHandler struct {
mu sync.Mutex mu sync.Mutex
opened []fs.File opened []fs.File
fss []fs.Filesystem fs fs.Filesystem
} }
func (fh *fileHandler) GetFile(path string, fhi uint64) (fs.File, error) { func (fh *fileHandler) GetFile(path string, fhi uint64) (fs.File, error) {
@ -163,15 +163,13 @@ func (fh *fileHandler) ListDir(path string) ([]string, error) {
defer fh.mu.Unlock() defer fh.mu.Unlock()
var out []string var out []string
for _, ifs := range fh.fss { files, err := fh.fs.ReadDir(path)
files, err := ifs.ReadDir(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for p := range files { for p := range files {
out = append(out, p) out = append(out, p)
} }
}
return out, nil return out, nil
} }
@ -233,11 +231,7 @@ func (fh *fileHandler) Remove(fhi uint64) error {
} }
func (fh *fileHandler) lookupFile(path string) (fs.File, error) { func (fh *fileHandler) lookupFile(path string) (fs.File, error) {
for _, f := range fh.fss { file, err := fh.fs.Open(path)
file, err := f.Open(path)
if err == os.ErrNotExist {
continue
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -245,7 +239,6 @@ func (fh *fileHandler) lookupFile(path string) (fs.File, error) {
if file != nil { if file != nil {
return file, nil return file, nil
} }
}
return nil, os.ErrNotExist return nil, os.ErrNotExist
} }

View file

@ -16,19 +16,19 @@ type Handler struct {
s *stats.Torrent s *stats.Torrent
fssMu sync.Mutex fssMu sync.Mutex
fss map[string][]fs.Filesystem fss map[string]fs.Filesystem
} }
func NewHandler(c *torrent.Client, s *stats.Torrent) *Handler { func NewHandler(c *torrent.Client, s *stats.Torrent) *Handler {
return &Handler{ return &Handler{
c: c, c: c,
s: s, s: s,
fss: make(map[string][]fs.Filesystem), fss: make(map[string]fs.Filesystem),
} }
} }
func (s *Handler) Load(path string, ts []*config.Torrent) error { func (s *Handler) Load(path string, ts []*config.Torrent) error {
var torrents []fs.Filesystem var torrents []*torrent.Torrent
for _, mpcTorrent := range ts { for _, mpcTorrent := range ts {
var t *torrent.Torrent var t *torrent.Torrent
var err error var err error
@ -54,7 +54,7 @@ func (s *Handler) Load(path string, ts []*config.Torrent) error {
} }
s.s.Add(path, t) s.s.Add(path, t)
torrents = append(torrents, fs.NewTorrent(t)) torrents = append(torrents, t)
log.WithField("name", t.Name()).WithField("path", path).Info("torrent added to mountpoint") log.WithField("name", t.Name()).WithField("path", path).Info("torrent added to mountpoint")
} }
@ -63,12 +63,12 @@ func (s *Handler) Load(path string, ts []*config.Torrent) error {
s.fssMu.Lock() s.fssMu.Lock()
defer s.fssMu.Unlock() defer s.fssMu.Unlock()
s.fss[folder] = torrents s.fss[folder] = fs.NewTorrent(torrents)
return nil return nil
} }
func (s *Handler) Fileststems() map[string][]fs.Filesystem { func (s *Handler) Fileststems() map[string]fs.Filesystem {
return s.fss return s.fss
} }
@ -76,7 +76,7 @@ func (s *Handler) RemoveAll() error {
s.fssMu.Lock() s.fssMu.Lock()
defer s.fssMu.Unlock() defer s.fssMu.Unlock()
s.fss = make(map[string][]fs.Filesystem) s.fss = make(map[string]fs.Filesystem)
s.s.RemoveAll() s.s.RemoveAll()
return nil return nil
} }

View file

@ -19,12 +19,13 @@ type WebDAV struct {
fss []fs.Filesystem fss []fs.Filesystem
} }
func newFS(mFss map[string][]fs.Filesystem) *WebDAV { func newFS(mFss map[string]fs.Filesystem) *WebDAV {
for _, fss := range mFss { var fss []fs.Filesystem
return &WebDAV{fss: fss} for _, fs := range mFss {
fss = append(fss, fs)
} }
return nil return &WebDAV{fss: fss}
} }
func (wd *WebDAV) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) { func (wd *WebDAV) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {

View file

@ -5,7 +5,7 @@ import (
"golang.org/x/net/webdav" "golang.org/x/net/webdav"
) )
func newHandler(fss map[string][]fs.Filesystem) *webdav.Handler { func newHandler(fss map[string]fs.Filesystem) *webdav.Handler {
return &webdav.Handler{ return &webdav.Handler{
Prefix: "/", Prefix: "/",
FileSystem: newFS(fss), FileSystem: newFS(fss),

View file

@ -8,7 +8,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func NewWebDAVServer(fss map[string][]fs.Filesystem, port int) error { func NewWebDAVServer(fss map[string]fs.Filesystem, port int) error {
logrus.WithField("host", fmt.Sprintf("0.0.0.0:%d", port)).Info("starting webDAV server") logrus.WithField("host", fmt.Sprintf("0.0.0.0:%d", port)).Info("starting webDAV server")
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), newHandler(fss)) return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), newHandler(fss))
} }