tstor/fuse/handler.go
Antonio Navarro Perez 45f10e2f81 Refactoring and first steps to make multi OS compatible.
- Using cgofuse to be compatible with multiple OSes
- Refactor to make possible better testing
- Add a bunch of tests
- Add code coverage

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
2020-10-18 11:39:59 +02:00

90 lines
1.9 KiB
Go

package fuse
import (
"fmt"
"github.com/ajnavarro/distribyted/config"
"github.com/ajnavarro/distribyted/fs"
"github.com/ajnavarro/distribyted/stats"
"github.com/anacrolix/torrent"
"github.com/billziss-gh/cgofuse/fuse"
log "github.com/sirupsen/logrus"
)
type Handler struct {
c *torrent.Client
s *stats.Torrent
hosts map[string]*fuse.FileSystemHost
}
func NewHandler(c *torrent.Client, s *stats.Torrent) *Handler {
return &Handler{
c: c,
s: s,
hosts: make(map[string]*fuse.FileSystemHost),
}
}
func (s *Handler) Mount(mpc *config.MountPoint) error {
var torrents []fs.Filesystem
for _, mpcTorrent := range mpc.Torrents {
var t *torrent.Torrent
var err error
switch {
case mpcTorrent.MagnetURI != "":
t, err = s.c.AddMagnet(mpcTorrent.MagnetURI)
break
case mpcTorrent.TorrentPath != "":
t, err = s.c.AddTorrentFromFile(mpcTorrent.TorrentPath)
break
default:
err = fmt.Errorf("no magnet URI or torrent path provided")
}
if err != nil {
return err
}
// only get info if name is not available
if t.Name() == "" {
log.WithField("hash", t.InfoHash()).Info("getting torrent info")
<-t.GotInfo()
}
s.s.Add(mpc.Path, t)
torrents = append(torrents, fs.NewTorrent(t))
log.WithField("name", t.Name()).WithField("path", mpc.Path).Info("torrent added to mountpoint")
}
// TODO change permissions
// if err := os.MkdirAll(mpc.Path, 0770); err != nil && !os.IsExist(err) {
// return err
// }
host := fuse.NewFileSystemHost(&FS{FSS: torrents})
go func() {
ok := host.Mount(mpc.Path, nil)
if !ok {
log.WithField("path", mpc.Path).Error("error trying to mount filesystem")
}
}()
s.hosts[mpc.Path] = host
return nil
}
func (s *Handler) Close() {
for path, server := range s.hosts {
log.WithField("path", path).Info("unmounting")
ok := server.Unmount()
if !ok {
//TODO try to force unmount if possible
log.WithField("path", path).Error("unmount failed")
}
}
}