Browse code

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 <dug@us.ibm.com>

Doug Davis authored on 2014/09/24 04:08:42
Showing 2 changed files
... ...
@@ -293,9 +293,17 @@ func calcCopyInfo(b *Builder, cmdName string, ci *copyInfo, allowRemote bool, al
293 293
 			return err
294 294
 		} else if fi.IsDir() {
295 295
 			var subfiles []string
296
+			absOrigPath := path.Join(b.contextPath, ci.origPath)
297
+
298
+			// Add a trailing / to make sure we only
299
+			// pick up nested files under the dir and
300
+			// not sibling files of the dir that just
301
+			// happen to start with the same chars
302
+			if !strings.HasSuffix(absOrigPath, "/") {
303
+				absOrigPath += "/"
304
+			}
296 305
 			for _, fileInfo := range sums {
297 306
 				absFile := path.Join(b.contextPath, fileInfo.Name())
298
-				absOrigPath := path.Join(b.contextPath, ci.origPath)
299 307
 				if strings.HasPrefix(absFile, absOrigPath) {
300 308
 					subfiles = append(subfiles, fileInfo.Sum())
301 309
 				}
... ...
@@ -1175,6 +1175,37 @@ func TestBuildADDLocalFileWithoutCache(t *testing.T) {
1175 1175
 	logDone("build - add local file without cache")
1176 1176
 }
1177 1177
 
1178
+func TestBuildCopyDirButNotFile(t *testing.T) {
1179
+	name := "testbuildcopydirbutnotfile"
1180
+	defer deleteImages(name)
1181
+	dockerfile := `
1182
+        FROM scratch
1183
+        COPY dir /tmp/`
1184
+	ctx, err := fakeContext(dockerfile, map[string]string{
1185
+		"dir/foo": "hello",
1186
+	})
1187
+	defer ctx.Close()
1188
+	if err != nil {
1189
+		t.Fatal(err)
1190
+	}
1191
+	id1, err := buildImageFromContext(name, ctx, true)
1192
+	if err != nil {
1193
+		t.Fatal(err)
1194
+	}
1195
+	// Check that adding file with similar name doesn't mess with cache
1196
+	if err := ctx.Add("dir_file", "hello2"); err != nil {
1197
+		t.Fatal(err)
1198
+	}
1199
+	id2, err := buildImageFromContext(name, ctx, true)
1200
+	if err != nil {
1201
+		t.Fatal(err)
1202
+	}
1203
+	if id1 != id2 {
1204
+		t.Fatal("The cache should have been used but wasn't")
1205
+	}
1206
+	logDone("build - add current directory but not file")
1207
+}
1208
+
1178 1209
 func TestBuildADDCurrentDirWithCache(t *testing.T) {
1179 1210
 	name := "testbuildaddcurrentdirwithcache"
1180 1211
 	defer deleteImages(name)