2014-03-17 22:44:22 +08:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
2018-01-31 13:42:26 +08:00
|
|
|
"context"
|
2014-08-28 06:08:09 +08:00
|
|
|
"expvar"
|
2014-08-21 19:08:56 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2015-03-26 14:18:08 +08:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent"
|
2015-04-28 13:24:17 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2014-03-17 22:44:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMode = 0555
|
|
|
|
)
|
|
|
|
|
2014-08-28 06:08:09 +08:00
|
|
|
var (
|
2020-02-20 08:09:57 +08:00
|
|
|
torrentfsReadRequests = expvar.NewInt("torrentfsReadRequests")
|
2014-08-28 06:08:09 +08:00
|
|
|
)
|
|
|
|
|
2014-12-02 04:30:50 +08:00
|
|
|
type TorrentFS struct {
|
2014-12-04 02:53:10 +08:00
|
|
|
Client *torrent.Client
|
|
|
|
destroyed chan struct{}
|
|
|
|
mu sync.Mutex
|
|
|
|
blockedReads int
|
|
|
|
event sync.Cond
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
2014-08-28 06:08:59 +08:00
|
|
|
var (
|
2014-12-02 04:30:50 +08:00
|
|
|
_ fusefs.FSDestroyer = &TorrentFS{}
|
2014-08-28 06:08:59 +08:00
|
|
|
|
2015-04-01 09:15:44 +08:00
|
|
|
_ fusefs.NodeForgetter = rootNode{}
|
|
|
|
_ fusefs.HandleReadDirAller = rootNode{}
|
|
|
|
_ fusefs.HandleReadDirAller = dirNode{}
|
|
|
|
)
|
2014-03-18 19:39:33 +08:00
|
|
|
|
2018-01-06 13:37:40 +08:00
|
|
|
// Is a directory node that lists all torrents and handles destruction of the
|
|
|
|
// filesystem.
|
2014-03-17 22:44:22 +08:00
|
|
|
type rootNode struct {
|
2014-12-02 04:30:50 +08:00
|
|
|
fs *TorrentFS
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
2015-08-23 17:25:33 +08:00
|
|
|
path string
|
2016-08-26 18:29:05 +08:00
|
|
|
metadata *metainfo.Info
|
2014-12-02 04:30:50 +08:00
|
|
|
FS *TorrentFS
|
2016-04-03 16:40:43 +08:00
|
|
|
t *torrent.Torrent
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirNode struct {
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-02-06 13:03:33 +08:00
|
|
|
_ fusefs.HandleReadDirAller = dirNode{}
|
2014-03-17 22:44:22 +08:00
|
|
|
)
|
|
|
|
|
2015-08-23 17:25:33 +08:00
|
|
|
func isSubPath(parent, child string) bool {
|
2021-01-16 18:59:36 +08:00
|
|
|
if parent == "" {
|
2016-08-01 21:56:56 +08:00
|
|
|
return len(child) > 0
|
|
|
|
}
|
2015-08-23 17:25:33 +08:00
|
|
|
if !strings.HasPrefix(child, parent) {
|
2014-03-17 22:44:22 +08:00
|
|
|
return false
|
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
extra := child[len(parent):]
|
2021-01-16 18:59:36 +08:00
|
|
|
if extra == "" {
|
2015-08-23 17:25:33 +08:00
|
|
|
return false
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
// Not just a file with more stuff on the end.
|
|
|
|
return extra[0] == '/'
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:03:33 +08:00
|
|
|
func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error) {
|
2014-03-17 22:44:22 +08:00
|
|
|
names := map[string]bool{}
|
2014-06-28 17:38:31 +08:00
|
|
|
for _, fi := range dn.metadata.Files {
|
2019-04-17 04:28:10 +08:00
|
|
|
filePathname := strings.Join(fi.Path, "/")
|
|
|
|
if !isSubPath(dn.path, filePathname) {
|
2014-03-17 22:44:22 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-04-17 04:28:10 +08:00
|
|
|
var name string
|
|
|
|
if dn.path == "" {
|
|
|
|
name = fi.Path[0]
|
|
|
|
} else {
|
|
|
|
dirPathname := strings.Split(dn.path, "/")
|
|
|
|
name = fi.Path[len(dirPathname)]
|
|
|
|
}
|
2014-03-17 22:44:22 +08:00
|
|
|
if names[name] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names[name] = true
|
|
|
|
de := fuse.Dirent{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
if len(fi.Path) == len(dn.path)+1 {
|
|
|
|
de.Type = fuse.DT_File
|
|
|
|
} else {
|
|
|
|
de.Type = fuse.DT_Dir
|
|
|
|
}
|
|
|
|
des = append(des, de)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-06 13:37:13 +08:00
|
|
|
func (dn dirNode) Lookup(_ context.Context, name string) (fusefs.Node, error) {
|
|
|
|
dir := false
|
|
|
|
var file *torrent.File
|
2019-04-17 04:23:03 +08:00
|
|
|
var fullPath string
|
|
|
|
if dn.path != "" {
|
|
|
|
fullPath = dn.path + "/" + name
|
|
|
|
} else {
|
|
|
|
fullPath = name
|
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
for _, f := range dn.t.Files() {
|
|
|
|
if f.DisplayPath() == fullPath {
|
2018-01-21 19:49:12 +08:00
|
|
|
file = f
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
if isSubPath(fullPath, f.DisplayPath()) {
|
|
|
|
dir = true
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
n := dn.node
|
|
|
|
n.path = fullPath
|
|
|
|
if dir && file != nil {
|
|
|
|
panic("both dir and file")
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
if file != nil {
|
|
|
|
return fileNode{n, file}, nil
|
|
|
|
}
|
|
|
|
if dir {
|
|
|
|
return dirNode{n}, nil
|
|
|
|
}
|
|
|
|
return nil, fuse.ENOENT
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-02 21:59:25 +08:00
|
|
|
func (dn dirNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2014-03-17 22:44:22 +08:00
|
|
|
attr.Mode = os.ModeDir | defaultMode
|
2015-06-02 21:59:25 +08:00
|
|
|
return nil
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (rn rootNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, err error) {
|
|
|
|
for _, t := range rn.fs.Client.Torrents() {
|
2015-04-28 13:24:17 +08:00
|
|
|
info := t.Info()
|
2016-01-06 09:19:49 +08:00
|
|
|
if t.Name() != name || info == nil {
|
2014-06-28 17:38:31 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
__node := node{
|
2015-04-28 13:24:17 +08:00
|
|
|
metadata: info,
|
2016-04-19 12:11:11 +08:00
|
|
|
FS: rn.fs,
|
2014-12-03 15:07:50 +08:00
|
|
|
t: t,
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2015-04-28 13:24:17 +08:00
|
|
|
if !info.IsDir() {
|
2018-01-21 19:49:12 +08:00
|
|
|
_node = fileNode{__node, t.Files()[0]}
|
2014-06-28 17:38:31 +08:00
|
|
|
} else {
|
|
|
|
_node = dirNode{__node}
|
|
|
|
}
|
|
|
|
break
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
if _node == nil {
|
|
|
|
err = fuse.ENOENT
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (rn rootNode) ReadDirAll(ctx context.Context) (dirents []fuse.Dirent, err error) {
|
|
|
|
for _, t := range rn.fs.Client.Torrents() {
|
2015-04-28 13:24:17 +08:00
|
|
|
info := t.Info()
|
|
|
|
if info == nil {
|
2014-07-23 12:55:38 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-03-17 22:44:22 +08:00
|
|
|
dirents = append(dirents, fuse.Dirent{
|
2015-04-28 13:24:17 +08:00
|
|
|
Name: info.Name,
|
2014-03-17 22:44:22 +08:00
|
|
|
Type: func() fuse.DirentType {
|
2015-04-28 13:24:17 +08:00
|
|
|
if !info.IsDir() {
|
2014-03-17 22:44:22 +08:00
|
|
|
return fuse.DT_File
|
|
|
|
} else {
|
|
|
|
return fuse.DT_Dir
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (rn rootNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
2015-03-25 14:25:24 +08:00
|
|
|
attr.Mode = os.ModeDir
|
2015-06-02 21:59:25 +08:00
|
|
|
return nil
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:15:39 +08:00
|
|
|
// TODO(anacrolix): Why should rootNode implement this?
|
2016-04-19 12:11:11 +08:00
|
|
|
func (rn rootNode) Forget() {
|
|
|
|
rn.fs.Destroy()
|
2014-03-18 19:39:33 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:03:33 +08:00
|
|
|
func (tfs *TorrentFS) Root() (fusefs.Node, error) {
|
2014-03-17 22:44:22 +08:00
|
|
|
return rootNode{tfs}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (tfs *TorrentFS) Destroy() {
|
|
|
|
tfs.mu.Lock()
|
2014-04-17 14:37:54 +08:00
|
|
|
select {
|
2016-04-19 12:11:11 +08:00
|
|
|
case <-tfs.destroyed:
|
2014-04-17 14:37:54 +08:00
|
|
|
default:
|
2016-04-19 12:11:11 +08:00
|
|
|
close(tfs.destroyed)
|
2014-04-17 14:37:54 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
tfs.mu.Unlock()
|
2014-04-17 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 04:30:50 +08:00
|
|
|
func New(cl *torrent.Client) *TorrentFS {
|
|
|
|
fs := &TorrentFS{
|
2014-04-17 14:37:54 +08:00
|
|
|
Client: cl,
|
|
|
|
destroyed: make(chan struct{}),
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2014-12-04 02:53:10 +08:00
|
|
|
fs.event.L = &fs.mu
|
2014-03-18 19:39:33 +08:00
|
|
|
return fs
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|