瀏覽代碼

Fix issue with file caching + prevent wrong cache hit

Docker-DCO-1.0-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
Guillaume J. Charmes 11 年之前
父節點
當前提交
f3103e5c91
共有 2 個文件被更改,包括 26 次插入8 次删除
  1. 11 8
      buildfile.go
  2. 15 0
      integration/buildfile_test.go

+ 11 - 8
buildfile.go

@@ -407,18 +407,20 @@ func (b *buildFile) CmdAdd(args string) error {
 			hash string
 			hash string
 			sums = b.context.GetSums()
 			sums = b.context.GetSums()
 		)
 		)
+
+		// Has tarsum strips the '.' and './', we put it back for comparaison.
+		for file, sum := range sums {
+			if len(file) == 0 || file[0] != '.' && file[0] != '/' {
+				delete(sums, file)
+				sums["./"+file] = sum
+			}
+		}
+
 		if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
 		if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
 			return err
 			return err
 		} else if fi.IsDir() {
 		} else if fi.IsDir() {
 			var subfiles []string
 			var subfiles []string
 			for file, sum := range sums {
 			for file, sum := range sums {
-				// Has tarsum stips the '.' and './', we put it back for comparaison.
-				if len(file) == 0 {
-					file = "./"
-				}
-				if file[0] != '.' && file[0] != '/' {
-					file = "./" + file
-				}
 				if strings.HasPrefix(file, origPath) {
 				if strings.HasPrefix(file, origPath) {
 					subfiles = append(subfiles, sum)
 					subfiles = append(subfiles, sum)
 				}
 				}
@@ -435,7 +437,8 @@ func (b *buildFile) CmdAdd(args string) error {
 		if err != nil {
 		if err != nil {
 			return err
 			return err
 		}
 		}
-		if hit {
+		// If we do not have a hash, never use the cache
+		if hit && hash != "" {
 			return nil
 			return nil
 		}
 		}
 	}
 	}

+ 15 - 0
integration/buildfile_test.go

@@ -532,6 +532,21 @@ func TestBuildADDLocalFileWithCache(t *testing.T) {
 	if id5 == id6 {
 	if id5 == id6 {
 		t.Fatal("The cache should have been invalided but hasn't.")
 		t.Fatal("The cache should have been invalided but hasn't.")
 	}
 	}
+
+	template.dockerfile += `
+	add bar /src2/bar2
+	add /bar /src2/bar3
+	run ls /src2/bar2 /src2/bar3
+	`
+	id7 := checkCacheBehaviorFromEngime(t, template, true, eng)
+	if id6 == id7 {
+		t.Fatal("The cache should have been invalided but hasn't.")
+	}
+	template.files[1][1] = "hello5"
+	id8 := checkCacheBehaviorFromEngime(t, template, true, eng)
+	if id7 == id8 {
+		t.Fatal("The cache should have been invalided but hasn't.")
+	}
 }
 }
 
 
 func TestBuildADDLocalFileWithoutCache(t *testing.T) {
 func TestBuildADDLocalFileWithoutCache(t *testing.T) {