diff --git a/fs/torrent.go b/fs/torrent.go index 9e2e1ec..00ce5fc 100644 --- a/fs/torrent.go +++ b/fs/torrent.go @@ -8,22 +8,25 @@ import ( var _ Filesystem = &Torrent{} type Torrent struct { - t *torrent.Torrent - s *storage + ts []*torrent.Torrent + s *storage } -func NewTorrent(t *torrent.Torrent) *Torrent { +func NewTorrent(ts []*torrent.Torrent) *Torrent { return &Torrent{ - t: t, - s: newStorage(SupportedFactories), + ts: ts, + s: newStorage(SupportedFactories), } } func (fs *Torrent) load() { - <-fs.t.GotInfo() - for _, file := range fs.t.Files() { - fs.s.Add(&torrentFile{readerFunc: file.NewReader, len: file.Length()}, file.Path()) + for _, t := range fs.ts { + <-t.GotInfo() + for _, file := range t.Files() { + fs.s.Add(&torrentFile{readerFunc: file.NewReader, len: file.Length()}, file.Path()) + } } + } func (fs *Torrent) Open(filename string) (File, error) { diff --git a/fs/torrent_test.go b/fs/torrent_test.go index 8c02c61..5d6bbc5 100644 --- a/fs/torrent_test.go +++ b/fs/torrent_test.go @@ -22,10 +22,10 @@ func TestTorrentFilesystem(t *testing.T) { client, err := torrent.NewClient(cfg) require.NoError(err) - torrent, err := client.AddMagnet(testMagnet) + to, err := client.AddMagnet(testMagnet) require.NoError(err) - tfs := NewTorrent(torrent) + tfs := NewTorrent([]*torrent.Torrent{to}) files, err := tfs.ReadDir("/") require.NoError(err) diff --git a/fuse/handler.go b/fuse/handler.go index c08723d..121b95d 100644 --- a/fuse/handler.go +++ b/fuse/handler.go @@ -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 { folder := p // On windows, the folder must don't exist diff --git a/fuse/mount.go b/fuse/mount.go index 6f68787..3cd04e5 100644 --- a/fuse/mount.go +++ b/fuse/mount.go @@ -17,9 +17,9 @@ type FS struct { fh *fileHandler } -func NewFS(fss []fs.Filesystem) fuse.FileSystemInterface { +func NewFS(fs fs.Filesystem) fuse.FileSystemInterface { 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 { mu sync.Mutex opened []fs.File - fss []fs.Filesystem + fs fs.Filesystem } func (fh *fileHandler) GetFile(path string, fhi uint64) (fs.File, error) { @@ -163,14 +163,12 @@ func (fh *fileHandler) ListDir(path string) ([]string, error) { defer fh.mu.Unlock() var out []string - for _, ifs := range fh.fss { - files, err := ifs.ReadDir(path) - if err != nil { - return nil, err - } - for p := range files { - out = append(out, p) - } + files, err := fh.fs.ReadDir(path) + if err != nil { + return nil, err + } + for p := range files { + out = append(out, p) } return out, nil @@ -233,18 +231,13 @@ func (fh *fileHandler) Remove(fhi uint64) error { } func (fh *fileHandler) lookupFile(path string) (fs.File, error) { - for _, f := range fh.fss { - file, err := f.Open(path) - if err == os.ErrNotExist { - continue - } - if err != nil { - return nil, err - } + file, err := fh.fs.Open(path) + if err != nil { + return nil, err + } - if file != nil { - return file, nil - } + if file != nil { + return file, nil } return nil, os.ErrNotExist diff --git a/torrent/handler.go b/torrent/handler.go index 57f2209..e6c958e 100644 --- a/torrent/handler.go +++ b/torrent/handler.go @@ -16,19 +16,19 @@ type Handler struct { s *stats.Torrent fssMu sync.Mutex - fss map[string][]fs.Filesystem + fss map[string]fs.Filesystem } func NewHandler(c *torrent.Client, s *stats.Torrent) *Handler { return &Handler{ c: c, s: s, - fss: make(map[string][]fs.Filesystem), + fss: make(map[string]fs.Filesystem), } } func (s *Handler) Load(path string, ts []*config.Torrent) error { - var torrents []fs.Filesystem + var torrents []*torrent.Torrent for _, mpcTorrent := range ts { var t *torrent.Torrent var err error @@ -54,7 +54,7 @@ func (s *Handler) Load(path string, ts []*config.Torrent) error { } 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") } @@ -63,12 +63,12 @@ func (s *Handler) Load(path string, ts []*config.Torrent) error { s.fssMu.Lock() defer s.fssMu.Unlock() - s.fss[folder] = torrents + s.fss[folder] = fs.NewTorrent(torrents) return nil } -func (s *Handler) Fileststems() map[string][]fs.Filesystem { +func (s *Handler) Fileststems() map[string]fs.Filesystem { return s.fss } @@ -76,7 +76,7 @@ func (s *Handler) RemoveAll() error { s.fssMu.Lock() defer s.fssMu.Unlock() - s.fss = make(map[string][]fs.Filesystem) + s.fss = make(map[string]fs.Filesystem) s.s.RemoveAll() return nil } diff --git a/webdav/fs.go b/webdav/fs.go index 48d7d47..ed3de52 100644 --- a/webdav/fs.go +++ b/webdav/fs.go @@ -19,12 +19,13 @@ type WebDAV struct { fss []fs.Filesystem } -func newFS(mFss map[string][]fs.Filesystem) *WebDAV { - for _, fss := range mFss { - return &WebDAV{fss: fss} +func newFS(mFss map[string]fs.Filesystem) *WebDAV { + var fss []fs.Filesystem + 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) { diff --git a/webdav/handler.go b/webdav/handler.go index 86be2c3..5662fe0 100644 --- a/webdav/handler.go +++ b/webdav/handler.go @@ -5,7 +5,7 @@ import ( "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{ Prefix: "/", FileSystem: newFS(fss), diff --git a/webdav/http.go b/webdav/http.go index b2b3f06..ff9eb28 100644 --- a/webdav/http.go +++ b/webdav/http.go @@ -8,7 +8,7 @@ import ( "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") return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), newHandler(fss)) }