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

mfs: fix copying into directory with no given filename #2977

Merged
merged 1 commit into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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/
'
Copy link
Member

Choose a reason for hiding this comment

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

This test case could've been created from the get go. Let's test more thoroughly from the beginning.


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