1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix files ownership when ADD is used

Signed-off-by: Mauricio Garavaglia <mauriciogaravaglia@gmail.com>
This commit is contained in:
Mauricio Garavaglia 2015-11-23 02:22:22 -03:00
parent a3065fa48f
commit b638bc6f17
2 changed files with 43 additions and 1 deletions

View file

@ -127,6 +127,7 @@ func (d Docker) Release(sessionID string, activeImages []string) {
func (d Docker) Copy(c *daemon.Container, destPath string, src builder.FileInfo, decompress bool) error {
srcPath := src.Path()
destExists := true
destDir := false
rootUID, rootGID := d.Daemon.GetRemappedUIDGID()
// Work in daemon-local OS specific file paths
@ -140,6 +141,7 @@ func (d Docker) Copy(c *daemon.Container, destPath string, src builder.FileInfo,
// Preserve the trailing slash
// TODO: why are we appending another path separator if there was already one?
if strings.HasSuffix(destPath, string(os.PathSeparator)) || destPath == "." {
destDir = true
dest += string(os.PathSeparator)
}
@ -182,7 +184,7 @@ func (d Docker) Copy(c *daemon.Container, destPath string, src builder.FileInfo,
}
// only needed for fixPermissions, but might as well put it before CopyFileWithTar
if destExists && destStat.IsDir() {
if destDir || (destExists && destStat.IsDir()) {
destPath = filepath.Join(destPath, src.Name())
}

View file

@ -3252,6 +3252,46 @@ func (s *DockerSuite) TestBuildAddFileNotFound(c *check.C) {
}
}
func (s *DockerSuite) TestBuildAddChangeOwnership(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "testbuildaddown"
ctx := func() *FakeContext {
dockerfile := `
FROM busybox
ADD foo /bar/
RUN [ $(stat -c %U:%G "/bar") = 'root:root' ]
RUN [ $(stat -c %U:%G "/bar/foo") = 'root:root' ]
`
tmpDir, err := ioutil.TempDir("", "fake-context")
c.Assert(err, check.IsNil)
testFile, err := os.Create(filepath.Join(tmpDir, "foo"))
if err != nil {
c.Fatalf("failed to create foo file: %v", err)
}
defer testFile.Close()
chownCmd := exec.Command("chown", "daemon:daemon", "foo")
chownCmd.Dir = tmpDir
out, _, err := runCommandWithOutput(chownCmd)
if err != nil {
c.Fatal(err, out)
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
c.Fatalf("failed to open destination dockerfile: %v", err)
}
return fakeContextFromDir(tmpDir)
}()
defer ctx.Close()
if _, err := buildImageFromContext(name, ctx, true); err != nil {
c.Fatalf("build failed to complete for TestBuildAddChangeOwnership: %v", err)
}
}
func (s *DockerSuite) TestBuildInheritance(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "testbuildinheritance"