Jelajahi Sumber

Merge pull request #12278 from duglin/12267-Wildcards

Fix for Daemon crashing when wildcards are used for COPY/ADD
Jessie Frazelle 10 tahun lalu
induk
melakukan
d0bc0153c6

+ 6 - 3
builder/internals.go

@@ -155,6 +155,7 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecomp
 			dest,
 			dest,
 			allowRemote,
 			allowRemote,
 			allowDecompression,
 			allowDecompression,
+			true,
 		); err != nil {
 		); err != nil {
 			return err
 			return err
 		}
 		}
@@ -225,7 +226,7 @@ func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecomp
 	return nil
 	return nil
 }
 }
 
 
-func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath string, destPath string, allowRemote bool, allowDecompression bool) error {
+func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath string, destPath string, allowRemote bool, allowDecompression bool, allowWildcards bool) error {
 
 
 	if origPath != "" && origPath[0] == '/' && len(origPath) > 1 {
 	if origPath != "" && origPath[0] == '/' && len(origPath) > 1 {
 		origPath = origPath[1:]
 		origPath = origPath[1:]
@@ -350,7 +351,7 @@ func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath stri
 	}
 	}
 
 
 	// Deal with wildcards
 	// Deal with wildcards
-	if ContainsWildcards(origPath) {
+	if allowWildcards && ContainsWildcards(origPath) {
 		for _, fileInfo := range b.context.GetSums() {
 		for _, fileInfo := range b.context.GetSums() {
 			if fileInfo.Name() == "" {
 			if fileInfo.Name() == "" {
 				continue
 				continue
@@ -360,7 +361,9 @@ func calcCopyInfo(b *Builder, cmdName string, cInfos *[]*copyInfo, origPath stri
 				continue
 				continue
 			}
 			}
 
 
-			calcCopyInfo(b, cmdName, cInfos, fileInfo.Name(), destPath, allowRemote, allowDecompression)
+			// Note we set allowWildcards to false in case the name has
+			// a * in it
+			calcCopyInfo(b, cmdName, cInfos, fileInfo.Name(), destPath, allowRemote, allowDecompression, false)
 		}
 		}
 		return nil
 		return nil
 	}
 	}

+ 26 - 1
integration-cli/docker_cli_build_test.go

@@ -1112,10 +1112,10 @@ func (s *DockerSuite) TestBuildCopyWildcard(c *check.C) {
 			"dir/nested_dir/nest_nest_file": "2 times nested",
 			"dir/nested_dir/nest_nest_file": "2 times nested",
 			"dirt": "dirty",
 			"dirt": "dirty",
 		})
 		})
-	defer ctx.Close()
 	if err != nil {
 	if err != nil {
 		c.Fatal(err)
 		c.Fatal(err)
 	}
 	}
+	defer ctx.Close()
 
 
 	id1, err := buildImageFromContext(name, ctx, true)
 	id1, err := buildImageFromContext(name, ctx, true)
 	if err != nil {
 	if err != nil {
@@ -1154,6 +1154,31 @@ func (s *DockerSuite) TestBuildCopyWildcardNoFind(c *check.C) {
 
 
 }
 }
 
 
+func (s *DockerSuite) TestBuildCopyWildcardInName(c *check.C) {
+	name := "testcopywildcardinname"
+	defer deleteImages(name)
+	ctx, err := fakeContext(`FROM busybox
+	COPY *.txt /tmp/
+	RUN [ "$(cat /tmp/\*.txt)" = 'hi there' ]
+	`, map[string]string{"*.txt": "hi there"})
+
+	if err != nil {
+		// Normally we would do c.Fatal(err) here but given that
+		// the odds of this failing are so rare, it must be because
+		// the OS we're running the client on doesn't support * in
+		// filenames (like windows).  So, instead of failing the test
+		// just let it pass. Then we don't need to explicitly
+		// say which OSs this works on or not.
+		return
+	}
+	defer ctx.Close()
+
+	_, err = buildImageFromContext(name, ctx, true)
+	if err != nil {
+		c.Fatalf("should have built: %q", err)
+	}
+}
+
 func (s *DockerSuite) TestBuildCopyWildcardCache(c *check.C) {
 func (s *DockerSuite) TestBuildCopyWildcardCache(c *check.C) {
 	name := "testcopywildcardcache"
 	name := "testcopywildcardcache"
 	ctx, err := fakeContext(`FROM busybox
 	ctx, err := fakeContext(`FROM busybox

+ 0 - 1
integration-cli/docker_utils.go

@@ -687,7 +687,6 @@ func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
 func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
 func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
 	ctx, err := fakeContextWithFiles(files)
 	ctx, err := fakeContextWithFiles(files)
 	if err != nil {
 	if err != nil {
-		ctx.Close()
 		return nil, err
 		return nil, err
 	}
 	}
 	if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
 	if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {