Skip to content

container/cp: don't print success message when copy fails - #7515

Open
xh123637 wants to merge 2 commits into
docker:masterfrom
xh123637:fix/7514-no-success-on-copy-failure
Open

xh123637 wants to merge 2 commits into
docker:masterfrom
xh123637:fix/7514-no-success-on-copy-failure

Conversation

@xh123637

Copy link
Copy Markdown

Fixes: #7514

Summary

docker cp printed the Successfully copied ... summary unconditionally in
the non-quiet paths of both copyFromContainer and copyToContainer, even
when the copy itself failed. Copying a directory from a container onto an
existing local regular file therefore printed
Successfully copied 0B to ... before reporting cannot copy directory
and exiting with code 1.

This change only prints the summary when the copy succeeded, in both
directions. The quiet paths already returned the error without printing a
summary and are unchanged.

Release notes (optional)

Fix `docker cp` printing a "Successfully copied" message when the copy fails.

Testing

New regression tests in cli/command/container/cp_test.go:

  • TestCopyFromContainerDoesNotReportSuccessOnFailure: copying a directory
    onto an existing regular file returns cannot copy directory, leaves the
    destination unchanged, and prints no success message.
  • TestCopyToContainerDoesNotReportSuccessOnFailure: a failed upload prints
    no success message.

Both tests failed before this change (the success message was printed) and
pass after it. The existing TestCopy* / TestRunCopy* tests in
cli/command/container still pass, and gofmt and go vet are clean for
the changed package.

Note: TestContainerExportOutputToFile fails in this environment both with
and without this change (gid expectation mismatch), so it is unrelated to
this change. No end-to-end run against a real daemon was done.

The non-quiet copy paths printed the "Successfully copied ..." summary
unconditionally, even when the copy failed. Copying a directory onto an
existing regular file therefore printed "Successfully copied 0B" before
reporting "cannot copy directory". Only print the summary when the copy
succeeded, in both the from-container and to-container directions.

Fixes: docker#7514

Signed-off-by: xh123637 <3324733895@qq.com>
Comment thread cli/command/container/cp.go Outdated
Comment on lines +337 to +342
reportedSize := copiedSize
if !cpRes.Stat.Mode.IsDir() {
reportedSize = cpRes.Stat.Size
if res == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should have a look at this code in general, because it's rather convoluted (definitely for a follow-up), but perhaps as part of this PR, we should consider renaming res to err - looking at the code didn't make it clear that res is just an error, not some other "result" return type.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done — renamed res to err in copyFromContainer (commit 317a13c). Since err was already declared in this function, the CopyTo call now assigns to it directly. I left the broader cleanup of this code for a follow-up, as you suggested.

The variable only holds the error returned by archive.CopyTo, so name
it err to make that clear instead of suggesting some other "result"
return type.

Signed-off-by: xh123637 <3324733895@qq.com>
@thaJeztah

Copy link
Copy Markdown
Member

Thanks for updating; could you;

Use a known identity (sorry, no anonymous contributions.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker cp prints “Successfully copied” before reporting “cannot copy directory”

2 participants