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