diff --git a/commands/http/parse.go b/commands/http/parse.go index 3d972db73..c579a0394 100644 --- a/commands/http/parse.go +++ b/commands/http/parse.go @@ -9,6 +9,7 @@ import ( cmds "github.com/ipfs/go-ipfs/commands" files "github.com/ipfs/go-ipfs/commands/files" + path "github.com/ipfs/go-ipfs/path" ) // Parse parses the data in a http.Request and returns a command Request object @@ -16,32 +17,33 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { if !strings.HasPrefix(r.URL.Path, ApiPath) { return nil, errors.New("Unexpected path prefix") } - path := strings.Split(strings.TrimPrefix(r.URL.Path, ApiPath+"/"), "/") + pth := path.SplitList(strings.TrimPrefix(r.URL.Path, ApiPath+"/")) stringArgs := make([]string, 0) if err := apiVersionMatches(r); err != nil { - if path[0] != "version" { // compatibility with previous version check + if pth[0] != "version" { // compatibility with previous version check return nil, err } } - cmd, err := root.Get(path[:len(path)-1]) + cmd, err := root.Get(pth[:len(pth)-1]) if err != nil { // 404 if there is no command at that path return nil, ErrNotFound } - if sub := cmd.Subcommand(path[len(path)-1]); sub == nil { - if len(path) <= 1 { + if sub := cmd.Subcommand(pth[len(pth)-1]); sub == nil { + if len(pth) <= 1 { return nil, ErrNotFound } // if the last string in the path isn't a subcommand, use it as an argument // e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command) - stringArgs = append(stringArgs, path[len(path)-1]) - path = path[:len(path)-1] + stringArgs = append(stringArgs, pth[len(pth)-1]) + pth = pth[:len(pth)-1] + } else { cmd = sub } @@ -93,7 +95,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { } } - optDefs, err := root.GetOptions(path) + optDefs, err := root.GetOptions(pth) if err != nil { return nil, err } @@ -116,7 +118,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { return nil, fmt.Errorf("File argument '%s' is required", requiredFile) } - req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs) + req, err := cmds.NewRequest(pth, opts, args, f, cmd, optDefs) if err != nil { return nil, err } diff --git a/core/commands/dht.go b/core/commands/dht.go index c5c413ee2..3cf710949 100644 --- a/core/commands/dht.go +++ b/core/commands/dht.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "strings" "time" key "github.com/ipfs/go-ipfs/blocks/key" @@ -600,7 +599,7 @@ PutValue will store the given key value pair in the dht. } func escapeDhtKey(s string) (key.Key, error) { - parts := strings.Split(s, "/") + parts := path.SplitList(s) switch len(parts) { case 1: return key.B58KeyDecode(s), nil diff --git a/core/commands/files/files.go b/core/commands/files/files.go index cffb6f2d0..e01fae5a8 100644 --- a/core/commands/files/files.go +++ b/core/commands/files/files.go @@ -245,8 +245,7 @@ Examples: res.SetOutput(&FilesLsOutput{listing}) return case *mfs.File: - parts := strings.Split(path, "/") - name := parts[len(parts)-1] + _, name := gopath.Split(path) out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}} res.SetOutput(out) return diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 24fedd1fd..1bb03ec00 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -246,7 +246,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request var backLink string = prefix + urlPath // don't go further up than /ipfs/$hash/ - pathSplit := strings.Split(backLink, "/") + pathSplit := path.SplitList(backLink) switch { // keep backlink case len(pathSplit) == 3: // url: /ipfs/$hash diff --git a/merkledag/utils/utils.go b/merkledag/utils/utils.go index 1f19e3380..97e2ebb4e 100644 --- a/merkledag/utils/utils.go +++ b/merkledag/utils/utils.go @@ -2,7 +2,6 @@ package dagutils import ( "errors" - "strings" ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" @@ -12,6 +11,7 @@ import ( bserv "github.com/ipfs/go-ipfs/blockservice" offline "github.com/ipfs/go-ipfs/exchange/offline" dag "github.com/ipfs/go-ipfs/merkledag" + path "github.com/ipfs/go-ipfs/path" ) type Editor struct { @@ -76,8 +76,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s return root, nil } -func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *dag.Node, create func() *dag.Node) error { - splpath := strings.Split(path, "/") +func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert *dag.Node, create func() *dag.Node) error { + splpath := path.SplitList(pth) nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create) if err != nil { return err @@ -130,8 +130,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []st return root, nil } -func (e *Editor) RmLink(ctx context.Context, path string) error { - splpath := strings.Split(path, "/") +func (e *Editor) RmLink(ctx context.Context, pth string) error { + splpath := path.SplitList(pth) nd, err := e.rmLink(ctx, e.root, splpath) if err != nil { return err diff --git a/merkledag/utils/utils_test.go b/merkledag/utils/utils_test.go index 498f676b2..d4b2af5f3 100644 --- a/merkledag/utils/utils_test.go +++ b/merkledag/utils/utils_test.go @@ -1,12 +1,12 @@ package dagutils import ( - "strings" "testing" key "github.com/ipfs/go-ipfs/blocks/key" dag "github.com/ipfs/go-ipfs/merkledag" mdtest "github.com/ipfs/go-ipfs/merkledag/test" + path "github.com/ipfs/go-ipfs/path" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" ) @@ -43,8 +43,8 @@ func TestAddLink(t *testing.T) { } } -func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path string, exp key.Key) { - parts := strings.Split(path, "/") +func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, pth string, exp key.Key) { + parts := path.SplitList(pth) cur := root for _, e := range parts { nxt, err := cur.GetLinkedNode(context.Background(), ds, e) diff --git a/mfs/mfs_test.go b/mfs/mfs_test.go index 609d81a29..13797c460 100644 --- a/mfs/mfs_test.go +++ b/mfs/mfs_test.go @@ -8,12 +8,12 @@ import ( "io/ioutil" "os" "sort" - "strings" "testing" ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" + "github.com/ipfs/go-ipfs/path" bstore "github.com/ipfs/go-ipfs/blocks/blockstore" key "github.com/ipfs/go-ipfs/blocks/key" @@ -43,8 +43,8 @@ func getRandFile(t *testing.T, ds dag.DAGService, size int64) *dag.Node { return nd } -func mkdirP(t *testing.T, root *Directory, path string) *Directory { - dirs := strings.Split(path, "/") +func mkdirP(t *testing.T, root *Directory, pth string) *Directory { + dirs := path.SplitList(pth) cur := root for _, d := range dirs { n, err := cur.Mkdir(d) @@ -69,15 +69,15 @@ func mkdirP(t *testing.T, root *Directory, path string) *Directory { return cur } -func assertDirAtPath(root *Directory, path string, children []string) error { - fsn, err := DirLookup(root, path) +func assertDirAtPath(root *Directory, pth string, children []string) error { + fsn, err := DirLookup(root, pth) if err != nil { return err } dir, ok := fsn.(*Directory) if !ok { - return fmt.Errorf("%s was not a directory", path) + return fmt.Errorf("%s was not a directory", pth) } listing, err := dir.List() @@ -113,13 +113,13 @@ func compStrArrs(a, b []string) bool { return true } -func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path string) error { - parts := strings.Split(path, "/") +func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, pth string) error { + parts := path.SplitList(pth) cur := root for i, d := range parts[:len(parts)-1] { next, err := cur.Child(d) if err != nil { - return fmt.Errorf("looking for %s failed: %s", path, err) + return fmt.Errorf("looking for %s failed: %s", pth, err) } nextDir, ok := next.(*Directory) @@ -138,7 +138,7 @@ func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path st file, ok := finaln.(*File) if !ok { - return fmt.Errorf("%s was not a file!", path) + return fmt.Errorf("%s was not a file!", pth) } out, err := ioutil.ReadAll(file) diff --git a/mfs/ops.go b/mfs/ops.go index 9e8ec1674..c7309a31d 100644 --- a/mfs/ops.go +++ b/mfs/ops.go @@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd *dag.Node) error { // Mkdir creates a directory at 'path' under the directory 'd', creating // intermediary directories as needed if 'parents' is set to true func Mkdir(r *Root, pth string, parents bool) error { - parts := strings.Split(pth, "/") + parts := path.SplitList(pth) if parts[0] == "" { parts = parts[1:] } @@ -159,7 +159,7 @@ func Lookup(r *Root, path string) (FSNode, error) { // under the directory 'd' func DirLookup(d *Directory, pth string) (FSNode, error) { pth = strings.Trim(pth, "/") - parts := strings.Split(pth, "/") + parts := path.SplitList(pth) if len(parts) == 1 && parts[0] == "" { return d, nil } diff --git a/path/path.go b/path/path.go index b6aa187b9..6f14f9016 100644 --- a/path/path.go +++ b/path/path.go @@ -106,3 +106,7 @@ func (p *Path) IsValid() error { func Join(pths []string) string { return strings.Join(pths, "/") } + +func SplitList(pth string) []string { + return strings.Split(pth, "/") +} diff --git a/routing/record/selection.go b/routing/record/selection.go index e90ebcd39..8e68006c1 100644 --- a/routing/record/selection.go +++ b/routing/record/selection.go @@ -2,9 +2,9 @@ package record import ( "errors" - "strings" key "github.com/ipfs/go-ipfs/blocks/key" + path "github.com/ipfs/go-ipfs/path" ) // A SelectorFunc selects the best value for the given key from @@ -18,7 +18,7 @@ func (s Selector) BestRecord(k key.Key, recs [][]byte) (int, error) { return 0, errors.New("no records given!") } - parts := strings.Split(string(k), "/") + parts := path.SplitList(string(k)) if len(parts) < 3 { log.Infof("Record key does not have selectorfunc: %s", k) return 0, errors.New("record key does not have selectorfunc") diff --git a/routing/record/validation.go b/routing/record/validation.go index f186bea90..a2afc0dfa 100644 --- a/routing/record/validation.go +++ b/routing/record/validation.go @@ -3,10 +3,10 @@ package record import ( "bytes" "errors" - "strings" key "github.com/ipfs/go-ipfs/blocks/key" ci "github.com/ipfs/go-ipfs/p2p/crypto" + path "github.com/ipfs/go-ipfs/path" pb "github.com/ipfs/go-ipfs/routing/dht/pb" u "github.com/ipfs/go-ipfs/util" ) @@ -37,7 +37,7 @@ type ValidChecker struct { // It runs needed validators func (v Validator) VerifyRecord(r *pb.Record) error { // Now, check validity func - parts := strings.Split(r.GetKey(), "/") + parts := path.SplitList(r.GetKey()) if len(parts) < 3 { log.Infof("Record key does not have validator: %s", key.Key(r.GetKey())) return nil @@ -54,7 +54,7 @@ func (v Validator) VerifyRecord(r *pb.Record) error { func (v Validator) IsSigned(k key.Key) (bool, error) { // Now, check validity func - parts := strings.Split(string(k), "/") + parts := path.SplitList(string(k)) if len(parts) < 3 { log.Infof("Record key does not have validator: %s", k) return false, nil diff --git a/tar/format.go b/tar/format.go index 547e77c87..3fab02b6e 100644 --- a/tar/format.go +++ b/tar/format.go @@ -98,7 +98,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) { // adds a '-' to the beginning of each path element so we can use 'data' as a // special link in the structure without having to worry about func escapePath(pth string) string { - elems := strings.Split(strings.Trim(pth, "/"), "/") + elems := path.SplitList(strings.Trim(pth, "/")) for i, e := range elems { elems[i] = "-" + e } diff --git a/util/ipfsaddr/ipfsaddr.go b/util/ipfsaddr/ipfsaddr.go index dec09303f..1a911d4e8 100644 --- a/util/ipfsaddr/ipfsaddr.go +++ b/util/ipfsaddr/ipfsaddr.go @@ -2,11 +2,11 @@ package ipfsaddr import ( "errors" - "strings" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" peer "github.com/ipfs/go-ipfs/p2p/peer" + path "github.com/ipfs/go-ipfs/path" logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log" ) @@ -94,7 +94,7 @@ func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) { } // make sure ipfs id parses as a peer.ID - peerIdParts := strings.Split(ipfspart.String(), "/") + peerIdParts := path.SplitList(ipfspart.String()) peerIdStr := peerIdParts[len(peerIdParts)-1] id, err := peer.IDB58Decode(peerIdStr) if err != nil { diff --git a/util/ipfsaddr/ipfsaddr_test.go b/util/ipfsaddr/ipfsaddr_test.go index ecdb0f32a..aca4ae238 100644 --- a/util/ipfsaddr/ipfsaddr_test.go +++ b/util/ipfsaddr/ipfsaddr_test.go @@ -1,11 +1,11 @@ package ipfsaddr import ( - "strings" "testing" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" peer "github.com/ipfs/go-ipfs/p2p/peer" + path "github.com/ipfs/go-ipfs/path" ) var good = []string{ @@ -87,7 +87,7 @@ func TestIDMatches(t *testing.T) { continue } - sp := strings.Split(g, "/") + sp := path.SplitList(g) sid := sp[len(sp)-1] id, err := peer.IDB58Decode(sid) if err != nil {