2014-03-17 14:44:22 +00:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
2014-04-08 09:39:34 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-04-17 06:37:54 +00:00
|
|
|
"sync"
|
2014-04-08 09:39:34 +00:00
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
|
|
|
metainfo "github.com/nsf/libtorgo/torrent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMode = 0555
|
|
|
|
)
|
|
|
|
|
|
|
|
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-05-22 14:38:07 +00:00
|
|
|
var _ fusefs.FSDestroyer = &torrentFS{}
|
|
|
|
|
2014-03-18 11:39:33 +00:00
|
|
|
var _ fusefs.NodeForgetter = rootNode{}
|
|
|
|
|
2014-03-17 14:44:22 +00:00
|
|
|
type rootNode struct {
|
|
|
|
fs *torrentFS
|
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
path []string
|
|
|
|
metaInfo *metainfo.MetaInfo
|
|
|
|
FS *torrentFS
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fn fileNode) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fusefs.Intr) fuse.Error {
|
|
|
|
if req.Dir {
|
|
|
|
panic("hodor")
|
|
|
|
}
|
2014-05-22 14:34:18 +00:00
|
|
|
size := req.Size
|
|
|
|
if int64(fn.size)-req.Offset < int64(size) {
|
|
|
|
size = int(int64(fn.size) - req.Offset)
|
|
|
|
}
|
2014-06-25 15:36:25 +00:00
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
2014-03-17 14:44:22 +00:00
|
|
|
}
|
|
|
|
infoHash := torrent.BytesInfoHash(fn.metaInfo.InfoHash)
|
|
|
|
torrentOff := fn.TorrentOffset + req.Offset
|
2014-06-26 08:06:33 +00:00
|
|
|
// log.Print(torrentOff, size, fn.TorrentOffset)
|
2014-05-22 14:34:18 +00:00
|
|
|
if err := fn.FS.Client.PrioritizeDataRegion(infoHash, torrentOff, int64(size)); err != nil {
|
2014-04-08 15:15:39 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-22 14:34:18 +00:00
|
|
|
resp.Data = resp.Data[:size]
|
2014-03-17 14:44:22 +00:00
|
|
|
for {
|
2014-03-19 17:30:08 +00:00
|
|
|
dataWaiter := fn.FS.Client.DataWaiter()
|
2014-05-22 14:34:18 +00:00
|
|
|
n, err := fn.FS.Client.TorrentReadAt(infoHash, torrentOff, resp.Data)
|
2014-03-17 14:44:22 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
2014-05-22 14:34:18 +00:00
|
|
|
resp.Data = resp.Data[:n]
|
2014-03-17 14:44:22 +00:00
|
|
|
return nil
|
|
|
|
case torrent.ErrDataNotReady:
|
|
|
|
select {
|
2014-03-19 17:30:08 +00:00
|
|
|
case <-dataWaiter:
|
2014-04-17 06:37:54 +00:00
|
|
|
case <-fn.FS.destroyed:
|
|
|
|
return fuse.EIO
|
2014-03-17 14:44:22 +00:00
|
|
|
case <-intr:
|
|
|
|
return fuse.EINTR
|
|
|
|
}
|
|
|
|
default:
|
2014-03-19 17:30:08 +00:00
|
|
|
log.Print(err)
|
2014-03-17 14:44:22 +00:00
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dirNode struct {
|
|
|
|
node
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ fusefs.HandleReadDirer = dirNode{}
|
|
|
|
|
|
|
|
_ fusefs.HandleReader = fileNode{}
|
|
|
|
)
|
|
|
|
|
|
|
|
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{}
|
|
|
|
for _, fi := range dn.metaInfo.Files {
|
|
|
|
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
|
|
|
|
for _, fi := range dn.metaInfo.Files {
|
|
|
|
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 isSingleFileTorrent(mi *metainfo.MetaInfo) bool {
|
|
|
|
return len(mi.Files) == 1 && mi.Files[0].Path == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) {
|
|
|
|
for _, _torrent := range me.fs.Client.Torrents() {
|
|
|
|
metaInfo := _torrent.MetaInfo
|
|
|
|
if metaInfo.Name == name {
|
|
|
|
__node := node{
|
|
|
|
metaInfo: metaInfo,
|
|
|
|
FS: me.fs,
|
|
|
|
InfoHash: torrent.BytesInfoHash(metaInfo.InfoHash),
|
|
|
|
}
|
|
|
|
if isSingleFileTorrent(metaInfo) {
|
|
|
|
_node = fileNode{__node, uint64(metaInfo.Files[0].Length), 0}
|
|
|
|
} else {
|
|
|
|
_node = dirNode{__node}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _node == nil {
|
|
|
|
err = fuse.ENOENT
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me rootNode) ReadDir(intr fusefs.Intr) (dirents []fuse.Dirent, err fuse.Error) {
|
|
|
|
for _, _torrent := range me.fs.Client.Torrents() {
|
|
|
|
metaInfo := _torrent.MetaInfo
|
|
|
|
dirents = append(dirents, fuse.Dirent{
|
|
|
|
Name: metaInfo.Name,
|
|
|
|
Type: func() fuse.DirentType {
|
|
|
|
if isSingleFileTorrent(metaInfo) {
|
|
|
|
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-03-17 14:44:22 +00:00
|
|
|
func (tfs *torrentFS) Root() (fusefs.Node, fuse.Error) {
|
|
|
|
return rootNode{tfs}, nil
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:37:54 +00:00
|
|
|
func (me *torrentFS) Destroy() {
|
|
|
|
me.mu.Lock()
|
|
|
|
select {
|
|
|
|
case <-me.destroyed:
|
|
|
|
default:
|
|
|
|
close(me.destroyed)
|
|
|
|
}
|
|
|
|
me.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-03-18 11:39:33 +00:00
|
|
|
func New(cl *torrent.Client) *torrentFS {
|
2014-03-17 14:44:22 +00:00
|
|
|
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
|
|
|
}
|