From cfc24769a26e825e4267cbfdd59af807e27508b9 Mon Sep 17 00:00:00 2001 From: Arnaud Porterie Date: Wed, 10 Dec 2014 11:09:03 -0800 Subject: [PATCH] Fix permissions on ADD/COPY Fix a regression introduced in PR#9467 when a single file was added or copied. Signed-off-by: Arnaud Porterie --- builder/internals.go | 10 ++++++++- integration-cli/docker_cli_build_test.go | 28 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/builder/internals.go b/builder/internals.go index fe2eb57859..6935f4f1c8 100644 --- a/builder/internals.go +++ b/builder/internals.go @@ -660,11 +660,19 @@ func copyAsDirectory(source, destination string) error { } func fixPermissions(source, destination string, uid, gid int) error { + // The copied root permission should not be changed for previously existing + // directories. + s, err := os.Stat(destination) + if err != nil && !os.IsNotExist(err) { + return err + } + fixRootPermission := (err != nil) || !s.IsDir() + // We Walk on the source rather than on the destination because we don't // want to change permissions on things we haven't created or modified. return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error { // Do not alter the walk root itself as it potentially existed before. - if source == fullpath { + if !fixRootPermission && (source == fullpath) { return nil } // Path is prefixed by source: substitute with destination instead. diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index bb11431cd6..785b3368ca 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -3564,3 +3564,31 @@ func TestBuildStderr(t *testing.T) { } logDone("build - testing stderr") } + +func TestBuildChownSingleFile(t *testing.T) { + name := "testbuildchownsinglefile" + defer deleteImages(name) + + ctx, err := fakeContext(` +FROM busybox +COPY test / +RUN ls -l /test +RUN [ $(ls -l /test | awk '{print $3":"$4}') = 'root:root' ] +`, map[string]string{ + "test": "test", + }) + if err != nil { + t.Fatal(err) + } + defer ctx.Close() + + if err := os.Chown(filepath.Join(ctx.Dir, "test"), 4242, 4242); err != nil { + t.Fatal(err) + } + + if _, err := buildImageFromContext(name, ctx, true); err != nil { + t.Fatal(err) + } + + logDone("build - change permission on single file") +}