From cd329d062bddaffcfea3ac0cd3245c0be5bf5542 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Tue, 23 Sep 2014 12:08:42 -0700 Subject: [PATCH] Make sure COPY/ADD on dirs doesn't grab too many files Add check for / first - per LK4D4's comment. Add a comment to explain why we're adding a / Signed-off-by: Doug Davis --- builder/internals.go | 10 +++++++- integration-cli/docker_cli_build_test.go | 31 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/builder/internals.go b/builder/internals.go index 7539f26c0a..7b17d9a9ca 100644 --- a/builder/internals.go +++ b/builder/internals.go @@ -293,9 +293,17 @@ func calcCopyInfo(b *Builder, cmdName string, ci *copyInfo, allowRemote bool, al return err } else if fi.IsDir() { var subfiles []string + absOrigPath := path.Join(b.contextPath, ci.origPath) + + // Add a trailing / to make sure we only + // pick up nested files under the dir and + // not sibling files of the dir that just + // happen to start with the same chars + if !strings.HasSuffix(absOrigPath, "/") { + absOrigPath += "/" + } for _, fileInfo := range sums { absFile := path.Join(b.contextPath, fileInfo.Name()) - absOrigPath := path.Join(b.contextPath, ci.origPath) if strings.HasPrefix(absFile, absOrigPath) { subfiles = append(subfiles, fileInfo.Sum()) } diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 1ecaeb8e1f..ba91840c04 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -1175,6 +1175,37 @@ func TestBuildADDLocalFileWithoutCache(t *testing.T) { logDone("build - add local file without cache") } +func TestBuildCopyDirButNotFile(t *testing.T) { + name := "testbuildcopydirbutnotfile" + defer deleteImages(name) + dockerfile := ` + FROM scratch + COPY dir /tmp/` + ctx, err := fakeContext(dockerfile, map[string]string{ + "dir/foo": "hello", + }) + defer ctx.Close() + if err != nil { + t.Fatal(err) + } + id1, err := buildImageFromContext(name, ctx, true) + if err != nil { + t.Fatal(err) + } + // Check that adding file with similar name doesn't mess with cache + if err := ctx.Add("dir_file", "hello2"); err != nil { + t.Fatal(err) + } + id2, err := buildImageFromContext(name, ctx, true) + if err != nil { + t.Fatal(err) + } + if id1 != id2 { + t.Fatal("The cache should have been used but wasn't") + } + logDone("build - add current directory but not file") +} + func TestBuildADDCurrentDirWithCache(t *testing.T) { name := "testbuildaddcurrentdirwithcache" defer deleteImages(name)