da5459
From 6d7ab38f33edb9ab87a290a0c68cfd27b55b061f Mon Sep 17 00:00:00 2001
da5459
From: Nalin Dahyabhai <nalin@redhat.com>
da5459
Date: Wed, 8 Jan 2020 11:02:05 -0500
da5459
Subject: [PATCH 1/2] Check for .dockerignore specifically
da5459
da5459
When generating the list of exclusions to process .dockerignore
da5459
contents, don't include .dockerignore if we don't have a .dockerignore
da5459
file in the context directory.  That way, if the file doesn't exist, and
da5459
the caller didn't pass in any patterns, we get no patterns instead of
da5459
just one ".dockerignore" pattern, and we can hit the faster copy path.
da5459
da5459
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
da5459
da5459
Closes: #2072
da5459
Approved by: giuseppe
da5459
---
da5459
 add.go | 10 ++++++++--
da5459
 1 file changed, 8 insertions(+), 2 deletions(-)
da5459
da5459
diff --git a/add.go b/add.go
da5459
index b5119e369..e82a5ef9a 100644
da5459
--- a/add.go
da5459
+++ b/add.go
da5459
@@ -215,7 +215,12 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM
da5459
 	if contextDir == "" {
da5459
 		return nil, nil
da5459
 	}
da5459
-	patterns := []string{".dockerignore"}
da5459
+	// If there's no .dockerignore file, then we don't have to add a
da5459
+	// pattern to tell copy logic to ignore it later.
da5459
+	var patterns []string
da5459
+	if _, err := os.Stat(filepath.Join(contextDir, ".dockerignore")); err == nil || !os.IsNotExist(err) {
da5459
+		patterns = []string{".dockerignore"}
da5459
+	}
da5459
 	for _, ignoreSpec := range lines {
da5459
 		ignoreSpec = strings.TrimSpace(ignoreSpec)
da5459
 		// ignore comments passed back from .dockerignore
da5459
@@ -224,7 +229,8 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM
da5459
 		}
da5459
 		// if the spec starts with '!' it means the pattern
da5459
 		// should be included. make a note so that we can move
da5459
-		// it to the front of the updated pattern
da5459
+		// it to the front of the updated pattern, and insert
da5459
+		// the context dir's path in between
da5459
 		includeFlag := ""
da5459
 		if strings.HasPrefix(ignoreSpec, "!") {
da5459
 			includeFlag = "!"
da5459
da5459
From f999964084ce75c833b0cffd17fb09b947dad506 Mon Sep 17 00:00:00 2001
da5459
From: Nalin Dahyabhai <nalin@redhat.com>
da5459
Date: Wed, 8 Jan 2020 11:04:57 -0500
da5459
Subject: [PATCH 2/2] copyFileWithTar: close source files at the right time
da5459
da5459
Close source files after we've finished reading from them, rather than
da5459
leaving it for later.
da5459
da5459
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
da5459
da5459
Closes: #2072
da5459
Approved by: giuseppe
da5459
---
da5459
 util.go | 9 +++------
da5459
 1 file changed, 3 insertions(+), 6 deletions(-)
da5459
da5459
diff --git a/util.go b/util.go
da5459
index b4670e41c..2f923357c 100644
da5459
--- a/util.go
da5459
+++ b/util.go
da5459
@@ -165,11 +165,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
da5459
 				if err != nil {
da5459
 					return errors.Wrapf(err, "error opening %q to copy its contents", src)
da5459
 				}
da5459
-				defer func() {
da5459
-					if err := f.Close(); err != nil {
da5459
-						logrus.Debugf("error closing %s: %v", fi.Name(), err)
da5459
-					}
da5459
-				}()
da5459
 			}
da5459
 		}
da5459
 
da5459
@@ -200,6 +195,9 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
da5459
 					logrus.Debugf("error copying contents of %s: %v", fi.Name(), err)
da5459
 					copyErr = err
da5459
 				}
da5459
+				if err = srcFile.Close(); err != nil {
da5459
+					logrus.Debugf("error closing %s: %v", fi.Name(), err)
da5459
+				}
da5459
 			}
da5459
 			if err = writer.Close(); err != nil {
da5459
 				logrus.Debugf("error closing write pipe for %s: %v", hdr.Name, err)
da5459
@@ -213,7 +211,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
da5459
 		if err == nil {
da5459
 			err = copyErr
da5459
 		}
da5459
-		f = nil
da5459
 		if pipeWriter != nil {
da5459
 			pipeWriter.Close()
da5459
 		}