2014-03-17 14:44:22 +00:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
2014-08-27 22:08:09 +00:00
|
|
|
"expvar"
|
2014-09-13 17:44:07 +00:00
|
|
|
"fmt"
|
2014-08-21 11:08:56 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-08-27 22:08:09 +00:00
|
|
|
"time"
|
2014-08-21 11:08:56 +00:00
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMode = 0555
|
|
|
|
)
|
|
|
|
|
2014-08-27 22:08:09 +00:00
|
|
|
var (
|
|
|
|
torrentfsReadRequests = expvar.NewInt("torrentfsReadRequests")
|
|
|
|
torrentfsDelayedReadRequests = expvar.NewInt("torrentfsDelayedReadRequests")
|
|
|
|
interruptedReads = expvar.NewInt("interruptedReads")
|
|
|
|
)
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
type TorrentFS struct {
|
2014-04-17 06:37:54 +00:00
|
|
|
Client *torrent.Client
|
|
|
|
destroyed chan struct{}
|
|
|
|
mu sync.Mutex
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 22:08:59 +00:00
|
|
|
var (
|
2014-12-01 20:30:50 +00:00
|
|
|
_ fusefs.FSDestroyer = &TorrentFS{}
|
|
|
|
_ fusefs.FSIniter = &TorrentFS{}
|
2014-08-27 22:08:59 +00:00
|
|
|
)
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func (fs *TorrentFS) Init(req *fuse.InitRequest, resp *fuse.InitResponse, intr fusefs.Intr) fuse.Error {
|
2014-08-27 22:08:59 +00:00
|
|
|
log.Print(req)
|
|
|
|
log.Print(resp)
|
|
|
|
resp.MaxReadahead = req.MaxReadahead
|
|
|
|
resp.Flags |= fuse.InitAsyncRead
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-22 14:38:07 +00:00
|
|
|
|
2014-03-18 11:39:33 +00:00
|
|
|
var _ fusefs.NodeForgetter = rootNode{}
|
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
type rootNode struct {
|
2014-12-01 20:30:50 +00:00
|
|
|
fs *TorrentFS
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
path []string
|
2014-11-18 20:36:27 +00:00
|
|
|
metadata *torrent.MetaInfo
|
2014-12-01 20:30:50 +00:00
|
|
|
FS *TorrentFS
|
2014-03-17 14:44:22 +00:00
|
|
|
InfoHash torrent.InfoHash
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileNode struct {
|
|
|
|
node
|
|
|
|
size uint64
|
|
|
|
TorrentOffset int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fn fileNode) Attr() (attr fuse.Attr) {
|
|
|
|
attr.Size = fn.size
|
|
|
|
attr.Mode = defaultMode
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-21 15:37:34 +00:00
|
|
|
func (n *node) fsPath() string {
|
|
|
|
return "/" + strings.Join(append([]string{n.metadata.Name}, n.path...), "/")
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func blockingRead(fs *TorrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) {
|
2014-09-13 17:44:07 +00:00
|
|
|
dataWaiter := fs.Client.DataWaiter(ih, off)
|
|
|
|
select {
|
|
|
|
case <-dataWaiter:
|
|
|
|
case <-fs.destroyed:
|
|
|
|
err = fuse.EIO
|
|
|
|
return
|
|
|
|
case <-intr:
|
|
|
|
err = fuse.EINTR
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err = fs.Client.TorrentReadAt(ih, off, p)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func readFull(fs *TorrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) {
|
2014-09-13 17:44:07 +00:00
|
|
|
for len(p) != 0 {
|
|
|
|
var nn int
|
|
|
|
nn, err = blockingRead(fs, ih, off, p, intr)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n += nn
|
|
|
|
off += int64(nn)
|
|
|
|
p = p[nn:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
func (fn fileNode) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fusefs.Intr) fuse.Error {
|
2014-08-27 22:08:09 +00:00
|
|
|
torrentfsReadRequests.Add(1)
|
|
|
|
started := time.Now()
|
2014-03-17 14:44:22 +00:00
|
|
|
if req.Dir {
|
2014-08-25 12:14:10 +00:00
|
|
|
panic("read on directory")
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
2014-08-27 22:08:09 +00:00
|
|
|
defer func() {
|
|
|
|
ms := time.Now().Sub(started).Nanoseconds() / 1000000
|
|
|
|
if ms < 20 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("torrentfs read took %dms", ms)
|
|
|
|
}()
|
2014-05-22 14:34:18 +00:00
|
|
|
size := req.Size
|
2014-08-25 12:15:18 +00:00
|
|
|
fileLeft := int64(fn.size) - req.Offset
|
2014-08-27 22:09:41 +00:00
|
|
|
if fileLeft < 0 {
|
|
|
|
fileLeft = 0
|
|
|
|
}
|
2014-08-25 12:15:18 +00:00
|
|
|
if fileLeft < int64(size) {
|
|
|
|
size = int(fileLeft)
|
2014-05-22 14:34:18 +00:00
|
|
|
}
|
2014-08-25 12:15:18 +00:00
|
|
|
resp.Data = resp.Data[:size]
|
|
|
|
if len(resp.Data) == 0 {
|
|
|
|
return nil
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
infoHash := fn.InfoHash
|
2014-03-17 14:44:22 +00:00
|
|
|
torrentOff := fn.TorrentOffset + req.Offset
|
2014-08-25 12:15:18 +00:00
|
|
|
go func() {
|
|
|
|
if err := fn.FS.Client.PrioritizeDataRegion(infoHash, torrentOff, int64(size)); err != nil {
|
2014-08-27 22:09:41 +00:00
|
|
|
log.Printf("error prioritizing %s: %s", fn.fsPath(), err)
|
2014-08-25 12:15:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2014-09-13 17:44:07 +00:00
|
|
|
n, err := readFull(fn.FS, infoHash, torrentOff, resp.Data, intr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != size {
|
|
|
|
panic(fmt.Sprintf("%d < %d", n, size))
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
2014-09-13 17:44:07 +00:00
|
|
|
return nil
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirNode struct {
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ fusefs.HandleReadDirer = dirNode{}
|
2014-11-20 18:50:53 +00:00
|
|
|
_ fusefs.HandleReader = fileNode{}
|
2014-03-17 14:44:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func isSubPath(parent, child []string) bool {
|
|
|
|
if len(child) <= len(parent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range parent {
|
|
|
|
if parent[i] != child[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dn dirNode) ReadDir(intr fusefs.Intr) (des []fuse.Dirent, err fuse.Error) {
|
|
|
|
names := map[string]bool{}
|
2014-06-28 09:38:31 +00:00
|
|
|
for _, fi := range dn.metadata.Files {
|
2014-03-17 14:44:22 +00:00
|
|
|
if !isSubPath(dn.path, fi.Path) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dn dirNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) {
|
|
|
|
var torrentOffset int64
|
2014-06-28 09:38:31 +00:00
|
|
|
for _, fi := range dn.metadata.Files {
|
2014-03-17 14:44:22 +00:00
|
|
|
if !isSubPath(dn.path, fi.Path) {
|
|
|
|
torrentOffset += fi.Length
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.Path[len(dn.path)] != name {
|
|
|
|
torrentOffset += fi.Length
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
__node := dn.node
|
|
|
|
__node.path = append(__node.path, name)
|
|
|
|
if len(fi.Path) == len(dn.path)+1 {
|
|
|
|
_node = fileNode{
|
|
|
|
node: __node,
|
|
|
|
size: uint64(fi.Length),
|
|
|
|
TorrentOffset: torrentOffset,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_node = dirNode{__node}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if _node == nil {
|
|
|
|
err = fuse.ENOENT
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dn dirNode) Attr() (attr fuse.Attr) {
|
|
|
|
attr.Mode = os.ModeDir | defaultMode
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) {
|
2014-06-28 09:38:31 +00:00
|
|
|
for _, t := range me.fs.Client.Torrents() {
|
2014-07-23 04:55:38 +00:00
|
|
|
if t.Name() != name || t.Info == nil {
|
2014-06-28 09:38:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
__node := node{
|
|
|
|
metadata: t.Info,
|
|
|
|
FS: me.fs,
|
|
|
|
InfoHash: t.InfoHash,
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
2014-11-18 20:36:27 +00:00
|
|
|
if t.Info.SingleFile() {
|
2014-06-28 09:38:31 +00:00
|
|
|
_node = fileNode{__node, uint64(t.Info.Length), 0}
|
|
|
|
} else {
|
|
|
|
_node = dirNode{__node}
|
|
|
|
}
|
|
|
|
break
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
|
|
|
if _node == nil {
|
|
|
|
err = fuse.ENOENT
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me rootNode) ReadDir(intr fusefs.Intr) (dirents []fuse.Dirent, err fuse.Error) {
|
2014-11-18 20:36:27 +00:00
|
|
|
for _, t := range me.fs.Client.Torrents() {
|
|
|
|
if t.Info == nil {
|
2014-07-23 04:55:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-03-17 14:44:22 +00:00
|
|
|
dirents = append(dirents, fuse.Dirent{
|
2014-11-18 20:36:27 +00:00
|
|
|
Name: t.Info.Name,
|
2014-03-17 14:44:22 +00:00
|
|
|
Type: func() fuse.DirentType {
|
2014-11-18 20:36:27 +00:00
|
|
|
if t.Info.SingleFile() {
|
2014-03-17 14:44:22 +00:00
|
|
|
return fuse.DT_File
|
|
|
|
} else {
|
|
|
|
return fuse.DT_Dir
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rootNode) Attr() fuse.Attr {
|
|
|
|
return fuse.Attr{
|
|
|
|
Mode: os.ModeDir,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:15:39 +00:00
|
|
|
// TODO(anacrolix): Why should rootNode implement this?
|
2014-04-17 06:37:54 +00:00
|
|
|
func (me rootNode) Forget() {
|
|
|
|
me.fs.Destroy()
|
2014-03-18 11:39:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func (tfs *TorrentFS) Root() (fusefs.Node, fuse.Error) {
|
2014-03-17 14:44:22 +00:00
|
|
|
return rootNode{tfs}, nil
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func (me *TorrentFS) Destroy() {
|
2014-04-17 06:37:54 +00:00
|
|
|
me.mu.Lock()
|
|
|
|
select {
|
|
|
|
case <-me.destroyed:
|
|
|
|
default:
|
|
|
|
close(me.destroyed)
|
|
|
|
}
|
|
|
|
me.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:30:50 +00:00
|
|
|
func New(cl *torrent.Client) *TorrentFS {
|
|
|
|
fs := &TorrentFS{
|
2014-04-17 06:37:54 +00:00
|
|
|
Client: cl,
|
|
|
|
destroyed: make(chan struct{}),
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
2014-03-18 11:39:33 +00:00
|
|
|
return fs
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|