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 83b2348 commit 8e5406a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion 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,6 +329,9 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch

destfi, err := os.Stat(destPath)
if err != nil {
if strings.HasSuffix(dest, string(os.PathSeparator)) {
return errors.Wrapf(err, "failed to get stat of dest path %s", destPath)
}
if !os.IsNotExist(err) {
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

0 comments on commit 8e5406a

Please sign in to comment.