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

Add flag to create parent directories in files cp command #8340

Merged
merged 6 commits into from
Aug 14, 2021
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
16 changes: 16 additions & 0 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/sharness/t0250-files-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" '
Copy link
Member

Choose a reason for hiding this comment

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

Can we make sure the file was actually created? I.e., try reading it.

(trying to avoid regressions)

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
'
Expand Down