From e735aeeee381426631e3fd8b01a30203236e9417 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 23 Aug 2015 19:25:33 +1000 Subject: [PATCH] fs: Update for changes in bazil.org/fuse --- fs/torrentfs.go | 24 ++++++++++++------------ fs/torrentfs_test.go | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/fs/torrentfs.go b/fs/torrentfs.go index ea2ae4e9..9b3f3958 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -4,6 +4,7 @@ import ( "expvar" "fmt" "os" + "path" "strings" "sync" @@ -46,7 +47,7 @@ type rootNode struct { } type node struct { - path []string + path string metadata *metainfo.Info FS *TorrentFS t torrent.Torrent @@ -65,7 +66,7 @@ func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error { } func (n *node) fsPath() string { - return "/" + strings.Join(append([]string{n.metadata.Name}, n.path...), "/") + return "/" + n.metadata.Name + "/" + n.path } func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) { @@ -151,22 +152,21 @@ var ( _ fusefs.HandleReader = fileNode{} ) -func isSubPath(parent, child []string) bool { - if len(child) <= len(parent) { +func isSubPath(parent, child string) bool { + if !strings.HasPrefix(child, parent) { return false } - for i := range parent { - if parent[i] != child[i] { - return false - } + s := child[len(parent):] + if len(s) == 0 { + return false } - return true + return s[0] == '/' } func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error) { names := map[string]bool{} for _, fi := range dn.metadata.Files { - if !isSubPath(dn.path, fi.Path) { + if !isSubPath(dn.path, strings.Join(fi.Path, "/")) { continue } name := fi.Path[len(dn.path)] @@ -190,7 +190,7 @@ func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error) func (dn dirNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, err error) { var torrentOffset int64 for _, fi := range dn.metadata.Files { - if !isSubPath(dn.path, fi.Path) { + if !isSubPath(dn.path, strings.Join(fi.Path, "/")) { torrentOffset += fi.Length continue } @@ -199,7 +199,7 @@ func (dn dirNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, e continue } __node := dn.node - __node.path = append(__node.path, name) + __node.path = path.Join(__node.path, name) if len(fi.Path) == len(dn.path)+1 { _node = fileNode{ node: __node, diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index a83a0cd5..8ae89e1c 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -15,9 +15,12 @@ import ( "testing" "time" + _ "github.com/anacrolix/envpprof" + "bazil.org/fuse" fusefs "bazil.org/fuse/fs" "github.com/anacrolix/missinggo" + "github.com/stretchr/testify/assert" netContext "golang.org/x/net/context" "github.com/anacrolix/torrent" @@ -253,3 +256,19 @@ func TestDownloadOnDemand(t *testing.T) { t.Fatalf("%q != %q", string(content), testutil.GreetingFileContents) } } + +func TestIsSubPath(t *testing.T) { + for _, case_ := range []struct { + parent, child string + is bool + }{ + {"", "", false}, + {"", "/", true}, + {"a/b", "a/bc", false}, + {"a/b", "a/b", false}, + {"a/b", "a/b/c", true}, + {"a/b", "a//b", false}, + } { + assert.Equal(t, case_.is, isSubPath(case_.parent, case_.child)) + } +} -- 2.48.1