Explorar el Código

Merge pull request #34271 from tonistiigi/build-symlink-hash

builder: fix build cache hash for broken symlink
Yong Tang hace 7 años
padre
commit
3ab20a87fa

+ 0 - 3
builder/remotecontext/archive.go

@@ -122,8 +122,5 @@ func normalize(path string, root containerfs.ContainerFS) (cleanPath, fullPath s
 	if err != nil {
 		return "", "", errors.Wrapf(err, "forbidden path outside the build context: %s (%s)", path, cleanPath)
 	}
-	if _, err := root.Lstat(fullPath); err != nil {
-		return "", "", errors.WithStack(convertPathError(err, path))
-	}
 	return
 }

+ 6 - 4
builder/remotecontext/lazycontext.go

@@ -40,14 +40,16 @@ func (c *lazySource) Hash(path string) (string, error) {
 		return "", err
 	}
 
-	fi, err := c.root.Lstat(fullPath)
+	relPath, err := Rel(c.root, fullPath)
 	if err != nil {
-		return "", errors.WithStack(err)
+		return "", errors.WithStack(convertPathError(err, cleanPath))
 	}
 
-	relPath, err := Rel(c.root, fullPath)
+	fi, err := os.Lstat(fullPath)
 	if err != nil {
-		return "", errors.WithStack(convertPathError(err, cleanPath))
+		// Backwards compatibility: a missing file returns a path as hash.
+		// This is reached in the case of a broken symlink.
+		return relPath, nil
 	}
 
 	sum, ok := c.sums[relPath]

+ 7 - 15
builder/remotecontext/tarsum_test.go

@@ -104,17 +104,6 @@ func TestHashSubdir(t *testing.T) {
 	}
 }
 
-func TestStatNotExisting(t *testing.T) {
-	contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
-	defer cleanup()
-
-	src := makeTestArchiveContext(t, contextDir)
-	_, err := src.Hash("not-existing")
-	if !os.IsNotExist(errors.Cause(err)) {
-		t.Fatalf("This file should not exist: %s", err)
-	}
-}
-
 func TestRemoveDirectory(t *testing.T) {
 	contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
 	defer cleanup()
@@ -129,17 +118,20 @@ func TestRemoveDirectory(t *testing.T) {
 
 	src := makeTestArchiveContext(t, contextDir)
 
-	tarSum := src.(modifiableContext)
+	_, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath))
+	if err != nil {
+		t.Fatalf("Statting %s shouldn't fail: %+v", relativePath, err)
+	}
 
+	tarSum := src.(modifiableContext)
 	err = tarSum.Remove(relativePath)
 	if err != nil {
 		t.Fatalf("Error when executing Remove: %s", err)
 	}
 
-	_, err = src.Hash(contextSubdir)
-
+	_, err = src.Root().Stat(src.Root().Join(src.Root().Path(), relativePath))
 	if !os.IsNotExist(errors.Cause(err)) {
-		t.Fatal("Directory should not exist at this point")
+		t.Fatalf("Directory should not exist at this point: %+v ", err)
 	}
 }