Skip to content

Commit

Permalink
fix bug dest path of copying tar
Browse files Browse the repository at this point in the history
when podman cp tar without --extract flag, if the destination already exists, or ends with path seprator, cp the tar under the directory, otherwise copy the tar named with the destination

Signed-off-by: Qi Wang <qiwan@redhat.com>
QiWang19 committed May 24, 2019
1 parent 579fd01 commit bb7b0aa
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/podman/cp.go
Original file line number Diff line number Diff line change
@@ -272,6 +272,10 @@ func copy(src, destPath, dest string, idMappingOpts storage.IDMappingOptions, ch
}
return nil
}

if destDirIsExist || strings.HasSuffix(dest, string(os.PathSeparator)) {
destPath = filepath.Join(destPath, filepath.Base(srcPath))
}
// Copy the file, preserving attributes.
logrus.Debugf("copying %q to %q", srcPath, destPath)
if err = copyFileWithTar(srcPath, destPath); err != nil {
27 changes: 27 additions & 0 deletions test/e2e/cp_test.go
Original file line number Diff line number Diff line change
@@ -143,4 +143,31 @@ var _ = Describe("Podman cp", func() {
os.Remove("file.tar.gz")
os.RemoveAll(testDirPath)
})

It("podman cp tar", func() {
path, err := os.Getwd()
Expect(err).To(BeNil())
testDirPath := filepath.Join(path, "TestDir")
err = os.Mkdir(testDirPath, 0777)
Expect(err).To(BeNil())
cmd := exec.Command("tar", "-cvf", "file.tar", testDirPath)
_, err = cmd.Output()
Expect(err).To(BeNil())

session := podmanTest.Podman([]string{"create", "--name", "testctr", ALPINE, "ls", "-l", "foo"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

session = podmanTest.Podman([]string{"cp", "file.tar", "testctr:/foo/"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

session = podmanTest.Podman([]string{"start", "-a", "testctr"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.OutputToString()).To(ContainSubstring("file.tar"))

os.Remove("file.tar")
os.RemoveAll(testDirPath)
})
})

0 comments on commit bb7b0aa

Please sign in to comment.