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{}
type Torrent struct {
t *torrent.Torrent
ts []*torrent.Torrent
s *storage
}
func NewTorrent(t *torrent.Torrent) *Torrent {
func NewTorrent(ts []*torrent.Torrent) *Torrent {
return &Torrent{
t: t,
ts: ts,
s: newStorage(SupportedFactories),
}
}
func (fs *Torrent) load() {
<-fs.t.GotInfo()
for _, file := range fs.t.Files() {
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) {

View file

@ -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)

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 {
folder := p
// On windows, the folder must don't exist

View file

@ -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,15 +163,13 @@ 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)
files, err := fh.fs.ReadDir(path)
if err != nil {
return nil, err
}
for p := range files {
out = append(out, p)
}
}
return out, nil
}
@ -233,11 +231,7 @@ 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
}
file, err := fh.fs.Open(path)
if err != nil {
return nil, err
}
@ -245,7 +239,6 @@ func (fh *fileHandler) lookupFile(path string) (fs.File, error) {
if file != nil {
return file, nil
}
}
return nil, os.ErrNotExist
}

View file

@ -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
}

View file

@ -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) {

View file

@ -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),

View file

@ -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))
}