2020-04-27 16:46:23 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ fs.NodeOnAdder = &Root{}
|
|
|
|
var _ fs.NodeGetattrer = &Root{}
|
|
|
|
|
|
|
|
type Root struct {
|
|
|
|
fs.Inode
|
|
|
|
torrents []*torrent.Torrent
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:15:01 +00:00
|
|
|
func NewRoot(torrents []*torrent.Torrent) *Root {
|
|
|
|
return &Root{torrents: torrents}
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (root *Root) OnAdd(ctx context.Context) {
|
|
|
|
for _, torrent := range root.torrents {
|
2020-06-03 09:15:01 +00:00
|
|
|
root.AddChild(
|
|
|
|
filepath.Clean(torrent.Name()),
|
|
|
|
root.NewPersistentInode(ctx, &Torrent{t: torrent}, fs.StableAttr{
|
|
|
|
Mode: syscall.S_IFDIR,
|
|
|
|
}), true)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (root *Root) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
|
|
|
out.Mode = syscall.S_IFDIR & 07777
|
|
|
|
|
|
|
|
return fs.OK
|
|
|
|
}
|