2014-03-17 22:44:22 +08:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
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"
|
2015-02-06 13:03:33 +08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
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 (
|
|
|
|
torrentfsReadRequests = expvar.NewInt("torrentfsReadRequests")
|
|
|
|
torrentfsDelayedReadRequests = expvar.NewInt("torrentfsDelayedReadRequests")
|
|
|
|
interruptedReads = expvar.NewInt("interruptedReads")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
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 {
|
2016-08-01 21:56:56 +08:00
|
|
|
if len(parent) == 0 {
|
|
|
|
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):]
|
|
|
|
if len(extra) == 0 {
|
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 {
|
2015-08-23 17:25:33 +08:00
|
|
|
if !isSubPath(dn.path, strings.Join(fi.Path, "/")) {
|
2014-03-17 22:44:22 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := fi.Path[len(dn.path)]
|
|
|
|
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
|
|
|
|
fullPath := dn.path + "/" + name
|
|
|
|
for _, f := range dn.t.Files() {
|
|
|
|
if f.DisplayPath() == fullPath {
|
|
|
|
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-06 13:37:13 +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
|
|
|
}
|