From 155435cb35aa783574c4c9b420fdd0ead8e85022 Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Thu, 12 Aug 2021 09:23:39 -0700 Subject: [PATCH 1/5] Add flag to create parent directories in files cp command Closes #8289 --- core/commands/files.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/commands/files.go b/core/commands/files.go index 927ce51e2..6a1c88706 100644 --- a/core/commands/files.go +++ b/core/commands/files.go @@ -356,8 +356,14 @@ 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) + + prefix, err := getPrefixNew(req) if err != nil { return err } @@ -389,6 +395,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) From c2e6a22bba886aa494765f6b647aaa3d18f0f3d6 Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Thu, 12 Aug 2021 09:40:24 -0700 Subject: [PATCH 2/5] Restore dropped error condition check --- core/commands/files.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/commands/files.go b/core/commands/files.go index 6a1c88706..f081af799 100644 --- a/core/commands/files.go +++ b/core/commands/files.go @@ -362,6 +362,9 @@ GC'ed. 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 { From 0d87b474c1017166d9f5b36435166f629cb1980b Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Fri, 13 Aug 2021 17:21:30 -0700 Subject: [PATCH 3/5] Add test for ipfs files cp -p --- test/sharness/t0250-files-api.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 7d6c85dc9..54abb267b 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -296,6 +296,10 @@ 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 "can read file $EXTRA" ' ipfs files read /cats/this/is/a/dir/file3 > output ' From ff3fff107bae6d134ef7155d4efa940a11a6c92d Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Sat, 14 Aug 2021 09:34:47 -0700 Subject: [PATCH 4/5] Clean up after cp -p test --- test/sharness/t0250-files-api.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 54abb267b..75f049464 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -300,6 +300,10 @@ test_files_api() { ipfs files cp -p /ipfs/$FILE3 /cats/some/other/dir/file3 ' + 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 ' From 0af19abe6b6e767c2966bfb8a38095eff48448d2 Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Sat, 14 Aug 2021 09:38:33 -0700 Subject: [PATCH 5/5] Verify creation of file created by cp -p --- test/sharness/t0250-files-api.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/sharness/t0250-files-api.sh b/test/sharness/t0250-files-api.sh index 75f049464..e2162cdf7 100755 --- a/test/sharness/t0250-files-api.sh +++ b/test/sharness/t0250-files-api.sh @@ -300,6 +300,12 @@ test_files_api() { 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 '