From 5103c6279fd0fbb9680033dd14e3858f5081eca0 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 26 Jul 2017 15:26:55 -0700 Subject: [PATCH 1/2] builder: fix build cache hash for broken symlink Signed-off-by: Tonis Tiigi Upstream-commit: 793ebdbf4b187b3680aed0073643040ddbeef523 Component: engine --- components/engine/builder/remotecontext/archive.go | 3 --- components/engine/builder/remotecontext/lazycontext.go | 10 +++++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/components/engine/builder/remotecontext/archive.go b/components/engine/builder/remotecontext/archive.go index fc18c5da31..b62d9dd0b7 100644 --- a/components/engine/builder/remotecontext/archive.go +++ b/components/engine/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 } diff --git a/components/engine/builder/remotecontext/lazycontext.go b/components/engine/builder/remotecontext/lazycontext.go index 66f36defd4..14848baa13 100644 --- a/components/engine/builder/remotecontext/lazycontext.go +++ b/components/engine/builder/remotecontext/lazycontext.go @@ -40,16 +40,16 @@ func (c *lazySource) Hash(path string) (string, error) { return "", err } - fi, err := c.root.Lstat(fullPath) - if err != nil { - return "", errors.WithStack(err) - } - relPath, err := Rel(c.root, fullPath) if err != nil { return "", errors.WithStack(convertPathError(err, cleanPath)) } + fi, err := os.Lstat(fullPath) + if err != nil { + return relPath, nil + } + sum, ok := c.sums[relPath] if !ok { sum, err = c.prepareHash(relPath, fi) From a2e7813d7e48bcd9d4a39c3286906f6a02985670 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Wed, 26 Jul 2017 16:23:31 -0700 Subject: [PATCH 2/2] Fix tests after Hash() behavior change Signed-off-by: Tonis Tiigi Upstream-commit: d5b15f0d3ee6b9575a919ca5ba568631957425e0 Component: engine --- .../builder/remotecontext/lazycontext.go | 2 ++ .../builder/remotecontext/tarsum_test.go | 22 ++++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/components/engine/builder/remotecontext/lazycontext.go b/components/engine/builder/remotecontext/lazycontext.go index 14848baa13..08b8058549 100644 --- a/components/engine/builder/remotecontext/lazycontext.go +++ b/components/engine/builder/remotecontext/lazycontext.go @@ -47,6 +47,8 @@ func (c *lazySource) Hash(path string) (string, error) { fi, err := os.Lstat(fullPath) if err != nil { + // Backwards compatibility: a missing file returns a path as hash. + // This is reached in the case of a broken symlink. return relPath, nil } diff --git a/components/engine/builder/remotecontext/tarsum_test.go b/components/engine/builder/remotecontext/tarsum_test.go index 6d2b41d3d4..9395460916 100644 --- a/components/engine/builder/remotecontext/tarsum_test.go +++ b/components/engine/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) } }