Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded "/" #1984

Merged
merged 3 commits into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"fmt"
"io"
"reflect"
"strings"

"github.com/ipfs/go-ipfs/path"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
)

Expand Down Expand Up @@ -143,16 +143,16 @@ func (c *Command) Call(req Request) Response {
}

// Resolve gets the subcommands at the given path
func (c *Command) Resolve(path []string) ([]*Command, error) {
cmds := make([]*Command, len(path)+1)
func (c *Command) Resolve(pth []string) ([]*Command, error) {
cmds := make([]*Command, len(pth)+1)
cmds[0] = c

cmd := c
for i, name := range path {
for i, name := range pth {
cmd = cmd.Subcommand(name)

if cmd == nil {
pathS := strings.Join(path[0:i], "/")
pathS := path.Join(pth[0:i])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you didnt add it, but that [0:i] not being just [:i] is bothering me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 6 occurences of '[0:'. Purging them once for all.

return nil, fmt.Errorf("Undefined command: '%s'", pathS)
}

Expand Down
5 changes: 3 additions & 2 deletions commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
path "github.com/ipfs/go-ipfs/path"
config "github.com/ipfs/go-ipfs/repo/config"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Expand Down Expand Up @@ -85,8 +86,8 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
reader = strings.NewReader("")
}

path := strings.Join(req.Path(), "/")
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)
pth := path.Join(req.Path())
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, pth, query)

httpReq, err := http.NewRequest("POST", url, reader)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions commands/http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ 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
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)

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

} else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
if len(path) <= 1 {
} else 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
Expand Down Expand Up @@ -86,7 +87,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
}
Expand All @@ -109,7 +110,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
}
Expand Down
6 changes: 3 additions & 3 deletions core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"errors"
"fmt"
"io"
"strings"
"time"

key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
notif "github.com/ipfs/go-ipfs/notifications"
peer "github.com/ipfs/go-ipfs/p2p/peer"
path "github.com/ipfs/go-ipfs/path"
ipdht "github.com/ipfs/go-ipfs/routing/dht"
u "github.com/ipfs/go-ipfs/util"
)
Expand Down Expand Up @@ -599,13 +599,13 @@ 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
case 3:
k := key.B58KeyDecode(parts[2])
return key.Key(strings.Join(append(parts[:2], string(k)), "/")), nil
return key.Key(path.Join(append(parts[:2], k.String()))), nil
default:
return "", errors.New("invalid key")
}
Expand Down
3 changes: 1 addition & 2 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
var backLink string = 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
Expand All @@ -236,7 +236,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
if len(pathSplit) > 5 {
// also strip the trailing segment, because it's a backlink
backLinkParts := pathSplit[3 : len(pathSplit)-2]
backLink += strings.Join(backLinkParts, "/") + "/"
backLink += path.Join(backLinkParts) + "/"
}
}

Expand Down Expand Up @@ -304,7 +304,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {

var newPath string
if len(rsegs) > 1 {
newPath = strings.Join(rsegs[2:], "/")
newPath = path.Join(rsegs[2:])
}

var newkey key.Key
Expand Down Expand Up @@ -429,7 +429,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", key.String())
http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components[:len(components)-1], "/"), http.StatusCreated)
http.Redirect(w, r, gopath.Join(ipfsPathPrefix+key.String(), path.Join(components[:len(components)-1])), http.StatusCreated)
}

func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
Expand Down
3 changes: 1 addition & 2 deletions fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"strings"

fuse "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
fs "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
Expand Down Expand Up @@ -194,7 +193,7 @@ func (s *Root) Lookup(ctx context.Context, name string) (fs.Node, error) {

segments := resolved.Segments()
if segments[0] == "ipfs" {
p := strings.Join(resolved.Segments()[1:], "/")
p := path.Join(resolved.Segments()[1:])
return &Link{s.IpfsRoot + "/" + p}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions merkledag/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions merkledag/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions mfs/mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions mfs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
)

// Mv moves the file or directory at 'src' to 'dst'
Expand Down Expand Up @@ -99,8 +100,8 @@ 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, path string, parents bool) error {
parts := strings.Split(path, "/")
func Mkdir(r *Root, pth string, parents bool) error {
parts := path.SplitList(pth)
if parts[0] == "" {
parts = parts[1:]
}
Expand All @@ -112,7 +113,7 @@ func Mkdir(r *Root, path string, parents bool) error {

if len(parts) == 0 {
// this will only happen on 'mkdir /'
return fmt.Errorf("cannot mkdir '%s'", path)
return fmt.Errorf("cannot mkdir '%s'", pth)
}

cur := r.GetValue().(*Directory)
Expand All @@ -130,7 +131,7 @@ func Mkdir(r *Root, path string, parents bool) error {

next, ok := fsn.(*Directory)
if !ok {
return fmt.Errorf("%s was not a directory", strings.Join(parts[:i], "/"))
return fmt.Errorf("%s was not a directory", path.Join(parts[:i]))
}
cur = next
}
Expand All @@ -156,9 +157,9 @@ func Lookup(r *Root, path string) (FSNode, error) {

// DirLookup will look up a file or directory at the given path
// under the directory 'd'
func DirLookup(d *Directory, path string) (FSNode, error) {
path = strings.Trim(path, "/")
parts := strings.Split(path, "/")
func DirLookup(d *Directory, pth string) (FSNode, error) {
pth = strings.Trim(pth, "/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be path.Clean no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if the path comes from cli, it should have already been cleaned in commands/cli/parse.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, thats true. carry on

parts := path.SplitList(pth)
if len(parts) == 1 && parts[0] == "" {
return d, nil
}
Expand All @@ -168,7 +169,7 @@ func DirLookup(d *Directory, path string) (FSNode, error) {
for i, p := range parts {
chdir, ok := cur.(*Directory)
if !ok {
return nil, fmt.Errorf("cannot access %s: Not a directory", strings.Join(parts[:i+1], "/"))
return nil, fmt.Errorf("cannot access %s: Not a directory", path.Join(parts[:i+1]))
}

child, err := chdir.Child(p)
Expand Down
Loading