summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangguanzhang <zhangguanzhang@qq.com>2020-06-14 19:45:52 +0800
committerzhangguanzhang <zhangguanzhang@qq.com>2020-06-15 23:56:45 +0800
commit9d293bd2de60f101b3712d2e099584221516776d (patch)
treecf429356c7b7b9420f3608b2eb38d7f6994c66e3
parent3218736cff4b718b8fe855759687cb66f19d6e1e (diff)
downloadpodman-9d293bd2de60f101b3712d2e099584221516776d.tar.gz
podman-9d293bd2de60f101b3712d2e099584221516776d.tar.bz2
podman-9d293bd2de60f101b3712d2e099584221516776d.zip
fix podman cp can create an extra directory level
Signed-off-by: zhangguanzhang <zhangguanzhang@qq.com>
-rw-r--r--pkg/domain/infra/abi/cp.go14
-rw-r--r--test/e2e/build/Dockerfile.test-cp-root-dir2
-rw-r--r--test/e2e/cp_test.go38
3 files changed, 53 insertions, 1 deletions
diff --git a/pkg/domain/infra/abi/cp.go b/pkg/domain/infra/abi/cp.go
index 9fc1e3bee..542813394 100644
--- a/pkg/domain/infra/abi/cp.go
+++ b/pkg/domain/infra/abi/cp.go
@@ -260,7 +260,19 @@ func containerCopy(srcPath, destPath, src, dest string, idMappingOpts storage.ID
if srcfi.IsDir() {
logrus.Debugf("copying %q to %q", srcPath+string(os.PathSeparator)+"*", dest+string(os.PathSeparator)+"*")
if destDirIsExist && !strings.HasSuffix(src, fmt.Sprintf("%s.", string(os.PathSeparator))) {
- destPath = filepath.Join(destPath, filepath.Base(srcPath))
+ srcPathBase := filepath.Base(srcPath)
+ if !isFromHostToCtr {
+ pathArr := strings.SplitN(src, ":", 2)
+ if len(pathArr) != 2 {
+ return errors.Errorf("invalid arguments %s, you must specify source path", src)
+ }
+ if pathArr[1] == "/" {
+ // If `srcPath` is the root directory of the container,
+ // `srcPath` will be `.../${sha256_ID}/merged/`, so do not join it
+ srcPathBase = ""
+ }
+ }
+ destPath = filepath.Join(destPath, srcPathBase)
}
if err = copyWithTar(srcPath, destPath); err != nil {
return errors.Wrapf(err, "error copying %q to %q", srcPath, dest)
diff --git a/test/e2e/build/Dockerfile.test-cp-root-dir b/test/e2e/build/Dockerfile.test-cp-root-dir
new file mode 100644
index 000000000..9f7de7c32
--- /dev/null
+++ b/test/e2e/build/Dockerfile.test-cp-root-dir
@@ -0,0 +1,2 @@
+FROM scratch
+COPY Dockerfile.test-cp-root-dir /
diff --git a/test/e2e/cp_test.go b/test/e2e/cp_test.go
index f95f8646c..2982ab04a 100644
--- a/test/e2e/cp_test.go
+++ b/test/e2e/cp_test.go
@@ -294,4 +294,42 @@ var _ = Describe("Podman cp", func() {
os.Remove("testfile1")
})
+ It("podman cp the root directory from the ctr to an existing directory on the host ", func() {
+ imgName := "test-cp-root-dir:latest"
+ DockerfileName := "Dockerfile.test-cp-root-dir"
+ ctrName := "test-container-cp-root"
+
+ session := podmanTest.PodmanNoCache([]string{"build", "-f", "build/" + DockerfileName, "-t", imgName, "build/"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ testDirPath := filepath.Join(podmanTest.RunRoot, "TestDirForCp")
+
+ session = podmanTest.Podman([]string{"create", "--name", ctrName, imgName, "dummy"})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ err := os.Mkdir(testDirPath, 0755)
+ Expect(err).To(BeNil())
+ defer os.RemoveAll(testDirPath)
+
+ // Copy the root directory of the container to an existing directory
+ session = podmanTest.Podman([]string{"cp", ctrName + ":/", testDirPath})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ // The file should be in the directory,
+ // not one layer too much of the directory called merged
+ checkFile := filepath.Join(testDirPath, DockerfileName)
+ _, err = os.Stat(checkFile)
+ Expect(err).To(BeNil())
+
+ session = podmanTest.Podman([]string{"container", "rm", ctrName})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+
+ session = podmanTest.PodmanNoCache([]string{"rmi", "-f", imgName})
+ session.WaitWithDefaultTimeout()
+ Expect(session.ExitCode()).To(Equal(0))
+ })
})