Skip to content

Commit

Permalink
Merge pull request moby#14429 from coolljt0725/fix_cp_created_container
Browse files Browse the repository at this point in the history
Fix copy from a "created" container. Fixes moby#14420
  • Loading branch information
LK4D4 committed Jul 8, 2015
2 parents 159e830 + 289ee90 commit f5ebcfe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions daemon/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/mount"
Expand Down Expand Up @@ -627,6 +628,14 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
var stat os.FileInfo
stat, err = os.Stat(m.Source)
if err != nil {
return nil, err
}
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
return nil, err
}
if err = mount.Mount(m.Source, dest, "bind", "rbind,ro"); err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions integration-cli/docker_cli_cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,20 @@ func (s *DockerSuite) TestCopyAndRestart(c *check.C) {
c.Fatalf("expected %q but got %q", expectedMsg, msg)
}
}

func (s *DockerSuite) TestCopyCreatedContainer(c *check.C) {
out, err := exec.Command(dockerBinary, "create", "--name", "test_cp", "-v", "/test", "busybox").CombinedOutput()
if err != nil {
c.Fatalf(string(out), err)
}

tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
c.Fatalf("unable to make temporary directory: %s", err)
}
defer os.RemoveAll(tmpDir)
out, err = exec.Command(dockerBinary, "cp", "test_cp:/bin/sh", tmpDir).CombinedOutput()
if err != nil {
c.Fatalf(string(out), err)
}
}
20 changes: 20 additions & 0 deletions pkg/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,23 @@ func ReadSymlinkedDirectory(path string) (string, error) {
}
return realPath, nil
}

// CreateIfNotExists creates a file or a directory only if it does not already exist.
func CreateIfNotExists(path string, isDir bool) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
if isDir {
return os.MkdirAll(path, 0755)
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
f, err := os.OpenFile(path, os.O_CREATE, 0755)
if err != nil {
return err
}
f.Close()
}
}
return nil
}

0 comments on commit f5ebcfe

Please sign in to comment.