Skip to content

Commit

Permalink
Merge pull request #2977 from ipfs/fix/files-cp
Browse files Browse the repository at this point in the history
mfs: fix copying into directory with no given filename
  • Loading branch information
whyrusleeping authored Jul 19, 2016
2 parents 0e4c195 + 660a4a9 commit 8165f76
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions mfs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions test/sharness/t0250-files-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8165f76

Please sign in to comment.