"expvar"
"fmt"
"os"
+ "path"
"strings"
"sync"
}
type node struct {
- path []string
+ path string
metadata *metainfo.Info
FS *TorrentFS
t torrent.Torrent
}
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) {
_ 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)]
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
}
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,
"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"
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))
+ }
+}