Skip to content

Commit 299fc0b

Browse files
authored
Merge pull request #8340 from jbouwman/files-cp-parent
Add flag to create parent directories in files cp command
2 parents 7c76118 + 0af19ab commit 299fc0b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

core/commands/files.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,21 @@ GC'ed.
356356
cmds.StringArg("source", true, false, "Source IPFS or MFS path to copy."),
357357
cmds.StringArg("dest", true, false, "Destination within MFS."),
358358
},
359+
Options: []cmds.Option{
360+
cmds.BoolOption(filesParentsOptionName, "p", "Make parent directories as needed."),
361+
},
359362
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
363+
mkParents, _ := req.Options[filesParentsOptionName].(bool)
360364
nd, err := cmdenv.GetNode(env)
361365
if err != nil {
362366
return err
363367
}
364368

369+
prefix, err := getPrefixNew(req)
370+
if err != nil {
371+
return err
372+
}
373+
365374
api, err := cmdenv.GetApi(env, req)
366375
if err != nil {
367376
return err
@@ -389,6 +398,13 @@ GC'ed.
389398
return fmt.Errorf("cp: cannot get node from path %s: %s", src, err)
390399
}
391400

401+
if mkParents {
402+
err := ensureContainingDirectoryExists(nd.FilesRoot, dst, prefix)
403+
if err != nil {
404+
return err
405+
}
406+
}
407+
392408
err = mfs.PutNode(nd.FilesRoot, dst, node)
393409
if err != nil {
394410
return fmt.Errorf("cp: cannot put node in path %s: %s", dst, err)

test/sharness/t0250-files-api.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ test_files_api() {
296296
ipfs files cp /ipfs/$FILE3 /cats/this/is/a/dir/file3
297297
'
298298

299+
test_expect_success "can copy file into deep dir using -p flag $EXTRA" '
300+
ipfs files cp -p /ipfs/$FILE3 /cats/some/other/dir/file3
301+
'
302+
303+
test_expect_success "file copied into deep dir exists $EXTRA" '
304+
ipfs files read /cats/some/other/dir/file3 > file_out &&
305+
echo "baz" > file_exp &&
306+
test_cmp file_out file_exp
307+
'
308+
309+
test_expect_success "cleanup deep cp -p test $EXTRA" '
310+
ipfs files rm -r /cats/some
311+
'
312+
299313
test_expect_success "can read file $EXTRA" '
300314
ipfs files read /cats/this/is/a/dir/file3 > output
301315
'

0 commit comments

Comments
 (0)