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