Skip to content

Commit

Permalink
fix cp none exists dest path ends with '/'
Browse files Browse the repository at this point in the history
close containers#3894
This patch let podman cp return 'no such file or directory' error if DEST_PATH does not exist and ends with / when copying file.

Signed-off-by: Qi Wang <qiwan@redhat.com>
  • Loading branch information
QiWang19 committed Sep 24, 2019
1 parent b300b98 commit 23c3962
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/podman/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
}

destdir := destPath
if !srcfi.IsDir() && !strings.HasSuffix(dest, string(os.PathSeparator)) {
if !srcfi.IsDir() {
destdir = filepath.Dir(destPath)
}
_, err = os.Stat(destdir)
Expand Down Expand Up @@ -329,7 +329,7 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch

destfi, err := os.Stat(destPath)
if err != nil {
if !os.IsNotExist(err) {
if !os.IsNotExist(err) || strings.HasSuffix(dest, string(os.PathSeparator)) {
return errors.Wrapf(err, "failed to get stat of dest path %s", destPath)
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/podman-cp.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Assuming a path separator of /, a first argument of **src_path** and second argu
- **dest_path** does not exist
- the file is saved to a file created at **dest_path**
- **dest_path** does not exist and ends with /
- **dest_path** is created as a directory and the file is copied into this directory using the basename from **src_path**
- Error condition: the destination directory must exist.
- **dest_path** exists and is a file
- the destination is overwritten with the source file's contents
- **dest_path** exists and is a directory
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ var _ = Describe("Podman cp", func() {
err := ioutil.WriteFile(srcPath, fromHostToContainer, 0644)
Expect(err).To(BeNil())

session = podmanTest.Podman([]string{"cp", srcPath, name + ":foo/"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Not(Equal(0)))

session = podmanTest.Podman([]string{"cp", srcPath, name + ":foo"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expand Down Expand Up @@ -187,6 +191,15 @@ var _ = Describe("Podman cp", func() {

_, err = os.Stat("/tmp/cp_test.txt")
Expect(err).To(Not(BeNil()))

session = podmanTest.Podman([]string{"exec", name, "ln", "-s", "/tmp/nonesuch", "/test1"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

session = podmanTest.Podman([]string{"cp", "--pause=false", srcPath, name + ":/test1/"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Not(Equal(0)))

})
It("podman cp volume", func() {
session := podmanTest.Podman([]string{"volume", "create", "data"})
Expand Down
6 changes: 3 additions & 3 deletions test/system/065-cp.bats
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ load helpers
is "$output" "" "output from podman cp 1"

run_podman cp --pause=false $srcdir/$rand_filename2 cpcontainer:/tmp/d2/x/
is "$output" "" "output from podman cp 3"
is "$output" "Error: failed to get stat of dest path .*stat.* no such file or directory" "cp returns error no such file or directory"

run_podman cp --pause=false $srcdir/$rand_filename3 cpcontainer:/tmp/d3/x
is "$output" "" "output from podman cp 3"
Expand All @@ -164,7 +164,7 @@ load helpers
# In the second case, podman creates a directory nonesuch2, then
# creates a file with the same name as the input file. THIS IS WEIRD!
run_podman exec cpcontainer cat /tmp/nonesuch2/$rand_filename2
is "$output" "$rand_content2" "cp creates destination dir and file"
is "$output" ".*No such file or directory" "podman returns error no such file or directory"

# In the third case, podman (correctly imo) creates a file named 'x'
run_podman exec cpcontainer cat /tmp/d3/x
Expand Down Expand Up @@ -221,4 +221,4 @@ function teardown() {
basic_teardown
}

# vim: filetype=sh
# vim: filetype=sh

0 comments on commit 23c3962

Please sign in to comment.