From d883268c1d4ff9aca76d47b99e471e3581970e7a Mon Sep 17 00:00:00 2001 From: zhangyue Date: Thu, 20 Jun 2019 16:28:51 +0800 Subject: [PATCH] fix: not copy parent dir when path contains dot Signed-off-by: zhangyue --- daemon/mgr/container_copy.go | 6 ++++-- test/cli_container_cp_test.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/daemon/mgr/container_copy.go b/daemon/mgr/container_copy.go index 0eb68536b1..0fcc7f3c3d 100644 --- a/daemon/mgr/container_copy.go +++ b/daemon/mgr/container_copy.go @@ -240,9 +240,11 @@ func (c *Container) getResolvedPath(path string, running bool) (resolvedPath, ab } // get the real path on the host - resolvedPath = filepath.Join(rootfs, absPath) - resolvedPath = filepath.Clean(resolvedPath) + resolvedPath = rootfs + string(os.PathSeparator) + path + cleanedPath := filepath.Clean(resolvedPath) + resolvedPath = archive.PreserveTrailingDotOrSeparator(cleanedPath, resolvedPath, os.PathSeparator) + logrus.Debugf("resolved path: %s", resolvedPath) return resolvedPath, absPath } diff --git a/test/cli_container_cp_test.go b/test/cli_container_cp_test.go index 43ea6d93bc..ec2dd6c2ef 100644 --- a/test/cli_container_cp_test.go +++ b/test/cli_container_cp_test.go @@ -132,3 +132,24 @@ func (suite *PouchContainerCopySuite) TestStopContainerCopy(c *check.C) { // test stopped container can start after cp command.PouchRun("start", name).Assert(c, icmd.Success) } + +// Test pouch cp, where path contains dot +func (suite *PouchContainerCopySuite) TestCopyPathDot(c *check.C) { + testDataPath := "testdata/cp/test-copy-path-dot" + c.Assert(os.MkdirAll(testDataPath, 0755), check.IsNil) + defer os.RemoveAll(testDataPath) + + name := "TestCopyPathDot" + command.PouchRun("run", "-d", + "--name", name, + busyboxImage, + "sh", "-c", + "mkdir -p test && echo 'test pouch cp' >> test/data.txt && top").Assert(c, icmd.Success) + defer DelContainerForceMultyTime(c, name) + + // don't copy test dir under testDataPath + localTestPath := fmt.Sprintf("%s/%s", testDataPath, "data.txt") + containerTestPath := fmt.Sprintf("%s:%s", name, "test/.") + command.PouchRun("cp", containerTestPath, testDataPath).Assert(c, icmd.Success) + checkFileContains(c, localTestPath, "test pouch cp") +}