2020-04-27 16:46:23 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2020-05-18 17:42:23 +00:00
|
|
|
"fmt"
|
2020-04-27 16:46:23 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ajnavarro/distribyted/config"
|
|
|
|
"github.com/ajnavarro/distribyted/node"
|
2020-05-02 12:06:18 +00:00
|
|
|
"github.com/ajnavarro/distribyted/stats"
|
2020-04-27 16:46:23 +00:00
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2020-07-14 11:55:08 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-27 16:46:23 +00:00
|
|
|
)
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
type Handler struct {
|
2020-04-27 16:46:23 +00:00
|
|
|
c *torrent.Client
|
2020-05-02 12:06:18 +00:00
|
|
|
s *stats.Torrent
|
2020-04-27 16:46:23 +00:00
|
|
|
opts *fs.Options
|
|
|
|
|
|
|
|
servers map[string]*fuse.Server
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:15:01 +00:00
|
|
|
func NewHandler(c *torrent.Client, s *stats.Torrent) *Handler {
|
2020-05-18 17:42:23 +00:00
|
|
|
return &Handler{
|
2020-04-27 16:46:23 +00:00
|
|
|
c: c,
|
2020-05-02 12:06:18 +00:00
|
|
|
s: s,
|
2020-04-27 16:46:23 +00:00
|
|
|
opts: &fs.Options{},
|
|
|
|
servers: make(map[string]*fuse.Server),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
func (s *Handler) Mount(mpc *config.MountPoint) error {
|
2020-04-27 16:46:23 +00:00
|
|
|
var torrents []*torrent.Torrent
|
2020-05-18 17:42:23 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-04-27 16:46:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-18 17:42:23 +00:00
|
|
|
|
2020-06-07 10:52:00 +00:00
|
|
|
// only get info if name is not available
|
|
|
|
if t.Name() == "" {
|
2020-07-14 11:55:08 +00:00
|
|
|
log.WithField("hash", t.InfoHash()).Info("getting torrent info")
|
2020-06-07 10:52:00 +00:00
|
|
|
<-t.GotInfo()
|
|
|
|
}
|
2020-05-02 12:06:18 +00:00
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
s.s.Add(mpc.Path, t)
|
2020-08-02 19:38:53 +00:00
|
|
|
log.WithField("name", t.Name()).WithField("path", mpc.Path).Info("torrent added to mountpoint")
|
2020-05-02 12:06:18 +00:00
|
|
|
|
2020-04-27 16:46:23 +00:00
|
|
|
torrents = append(torrents, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO change permissions
|
2020-05-02 12:06:18 +00:00
|
|
|
if err := os.MkdirAll(mpc.Path, 0770); err != nil && !os.IsExist(err) {
|
2020-04-27 16:46:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:15:01 +00:00
|
|
|
node := node.NewRoot(torrents)
|
2020-04-27 16:46:23 +00:00
|
|
|
server, err := fs.Mount(mpc.Path, node, s.opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.servers[mpc.Path] = server
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
func (s *Handler) Close() {
|
2020-04-27 16:46:23 +00:00
|
|
|
for path, server := range s.servers {
|
2020-07-14 11:55:08 +00:00
|
|
|
log.WithField("path", path).Info("unmounting")
|
2020-04-27 16:46:23 +00:00
|
|
|
err := server.Unmount()
|
|
|
|
if err != nil {
|
2020-07-14 11:55:08 +00:00
|
|
|
//TODO try to force unmount if possible
|
|
|
|
log.WithError(err).WithField("path", path).Error("unmount failed")
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|