Browse code

Fix copy from a "created" container. Fixes #14420

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/07/08 12:15:09
Showing 3 changed files
... ...
@@ -23,6 +23,7 @@ import (
23 23
 	"github.com/docker/docker/image"
24 24
 	"github.com/docker/docker/pkg/archive"
25 25
 	"github.com/docker/docker/pkg/broadcastwriter"
26
+	"github.com/docker/docker/pkg/fileutils"
26 27
 	"github.com/docker/docker/pkg/ioutils"
27 28
 	"github.com/docker/docker/pkg/jsonlog"
28 29
 	"github.com/docker/docker/pkg/mount"
... ...
@@ -627,6 +628,14 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
627 627
 		if err != nil {
628 628
 			return nil, err
629 629
 		}
630
+		var stat os.FileInfo
631
+		stat, err = os.Stat(m.Source)
632
+		if err != nil {
633
+			return nil, err
634
+		}
635
+		if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
636
+			return nil, err
637
+		}
630 638
 		if err = mount.Mount(m.Source, dest, "bind", "rbind,ro"); err != nil {
631 639
 			return nil, err
632 640
 		}
... ...
@@ -632,3 +632,20 @@ func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
632 632
 		c.Fatalf("expected %q but got %q", expectedMsg, msg)
633 633
 	}
634 634
 }
635
+
636
+func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
637
+	out, err := exec.Command(dockerBinary, "create", "--name", "test_cp", "-v", "/test", "busybox").CombinedOutput()
638
+	if err != nil {
639
+		c.Fatalf(string(out), err)
640
+	}
641
+
642
+	tmpDir, err := ioutil.TempDir("", "test")
643
+	if err != nil {
644
+		c.Fatalf("unable to make temporary directory: %s", err)
645
+	}
646
+	defer os.RemoveAll(tmpDir)
647
+	out, err = exec.Command(dockerBinary, "cp", "test_cp:/bin/sh", tmpDir).CombinedOutput()
648
+	if err != nil {
649
+		c.Fatalf(string(out), err)
650
+	}
651
+}
... ...
@@ -167,3 +167,23 @@ func ReadSymlinkedDirectory(path string) (string, error) {
167 167
 	}
168 168
 	return realPath, nil
169 169
 }
170
+
171
+// CreateIfNotExists creates a file or a directory only if it does not already exist.
172
+func CreateIfNotExists(path string, isDir bool) error {
173
+	if _, err := os.Stat(path); err != nil {
174
+		if os.IsNotExist(err) {
175
+			if isDir {
176
+				return os.MkdirAll(path, 0755)
177
+			}
178
+			if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
179
+				return err
180
+			}
181
+			f, err := os.OpenFile(path, os.O_CREATE, 0755)
182
+			if err != nil {
183
+				return err
184
+			}
185
+			f.Close()
186
+		}
187
+	}
188
+	return nil
189
+}