From 660a4a9d988f663c00c78f41e2a941d8dde64f78 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 19 Jul 2016 07:41:00 -0700 Subject: [PATCH] mfs: fix copying into directory with no given filename License: MIT Signed-off-by: Jeromy --- core/commands/files/files.go | 6 ++++++ mfs/ops.go | 3 +++ test/sharness/t0250-files-api.sh | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/core/commands/files/files.go b/core/commands/files/files.go index f6f94a3ea43..750cddaa94e 100644 --- a/core/commands/files/files.go +++ b/core/commands/files/files.go @@ -217,12 +217,18 @@ var FilesCpCmd = &cmds.Command{ res.SetError(err, cmds.ErrNormal) return } + src = strings.TrimRight(src, "/") + dst, err := checkPath(req.Arguments()[1]) if err != nil { res.SetError(err, cmds.ErrNormal) return } + if dst[len(dst)-1] == '/' { + dst += gopath.Base(src) + } + nd, err := getNodeFromPath(req.Context(), node, src) if err != nil { res.SetError(err, cmds.ErrNormal) diff --git a/mfs/ops.go b/mfs/ops.go index 7cf9ed9f30a..94c6c30df8b 100644 --- a/mfs/ops.go +++ b/mfs/ops.go @@ -89,6 +89,9 @@ func lookupDir(r *Root, path string) (*Directory, error) { // PutNode inserts 'nd' at 'path' in the given mfs func PutNode(r *Root, path string, nd *dag.Node) error { dirp, filename := gopath.Split(path) + if filename == "" { + return fmt.Errorf("cannot create file with empty name") + } pdir, err := lookupDir(r, dirp) if err != nil { diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 31e91c04e96..5c9f1332d51 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -460,6 +460,24 @@ test_files_api() { ipfs files rm -r /test_dir && ipfs files rm -r /test_file ' + + test_expect_success "make a directory and a file" ' + ipfs files mkdir /adir && + echo "blah" | ipfs files write --create /foobar + ' + + test_expect_success "copy a file into a directory" ' + ipfs files cp /foobar /adir/ + ' + + test_expect_success "file made it into directory" ' + ipfs files ls /adir | grep foobar + ' + + test_expect_success "clean up" ' + ipfs files rm -r /foobar && + ipfs files rm -r /adir + ' } # test offline and online