diff --git a/core/commands/files.go b/core/commands/files.go index 927ce51e2cf..f081af799fb 100644 --- a/core/commands/files.go +++ b/core/commands/files.go @@ -356,12 +356,21 @@ GC'ed. cmds.StringArg("source", true, false, "Source IPFS or MFS path to copy."), cmds.StringArg("dest", true, false, "Destination within MFS."), }, + Options: []cmds.Option{ + cmds.BoolOption(filesParentsOptionName, "p", "Make parent directories as needed."), + }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + mkParents, _ := req.Options[filesParentsOptionName].(bool) nd, err := cmdenv.GetNode(env) if err != nil { return err } + prefix, err := getPrefixNew(req) + if err != nil { + return err + } + api, err := cmdenv.GetApi(env, req) if err != nil { return err @@ -389,6 +398,13 @@ GC'ed. return fmt.Errorf("cp: cannot get node from path %s: %s", src, err) } + if mkParents { + err := ensureContainingDirectoryExists(nd.FilesRoot, dst, prefix) + if err != nil { + return err + } + } + err = mfs.PutNode(nd.FilesRoot, dst, node) if err != nil { return fmt.Errorf("cp: cannot put node in path %s: %s", dst, err) diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 7d6c85dc984..e2162cdf79e 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -296,6 +296,20 @@ test_files_api() { ipfs files cp /ipfs/$FILE3 /cats/this/is/a/dir/file3 ' + test_expect_success "can copy file into deep dir using -p flag $EXTRA" ' + ipfs files cp -p /ipfs/$FILE3 /cats/some/other/dir/file3 + ' + + test_expect_success "file copied into deep dir exists $EXTRA" ' + ipfs files read /cats/some/other/dir/file3 > file_out && + echo "baz" > file_exp && + test_cmp file_out file_exp + ' + + test_expect_success "cleanup deep cp -p test $EXTRA" ' + ipfs files rm -r /cats/some + ' + test_expect_success "can read file $EXTRA" ' ipfs files read /cats/this/is/a/dir/file3 > output '