Skip to content

Commit

Permalink
executor: stubs cleaner should remove empty directory mounts
Browse files Browse the repository at this point in the history
On Linux, an empty directory is usually 4096 bytes, not 0, so we need an
additional explicit check here.

Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Nov 23, 2022
1 parent 54e3957 commit 6778973
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
47 changes: 47 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func TestIntegration(t *testing.T) {
testSBOMScan,
testSBOMScanSingleRef,
testMultipleCacheExports,
testMountStubsDirectory,
testMountStubsTimestamp,
)
}
Expand Down Expand Up @@ -8106,6 +8107,52 @@ func testMultipleCacheExports(t *testing.T, sb integration.Sandbox) {
ensureFileContents(t, filepath.Join(destDir, "unique"), string(uniqueFile))
}

func testMountStubsDirectory(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

st := llb.Image("busybox:latest").Run(
llb.Args([]string{"/bin/echo", "dummy"}),
llb.AddMount("/foo/bar", llb.Scratch(), llb.Tmpfs()),
)
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

tmpDir := t.TempDir()
tarFile := filepath.Join(tmpDir, "out.tar")
tarFileW, err := os.Create(tarFile)
require.NoError(t, err)
defer tarFileW.Close()

_, err = c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterTar,
Output: fixedWriteCloser(tarFileW),
},
},
}, nil)
require.NoError(t, err)
tarFileW.Close()

tarFileR, err := os.Open(tarFile)
require.NoError(t, err)
defer tarFileR.Close()
tarR := tar.NewReader(tarFileR)

for {
hd, err := tarR.Next()
if errors.Is(err, io.EOF) {
break
}
require.NoError(t, err)
if hd.Name == "foo/bar/" {
require.Fail(t, "foo/bar/ should not be in the tar")
}
}
}

// /~https://github.com/moby/buildkit/issues/3148
func testMountStubsTimestamp(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
Expand Down
11 changes: 10 additions & 1 deletion executor/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ func MountStubsCleaner(dir string, mounts []Mount) func() {
if err != nil {
continue
}
if st.Size() != 0 {
if st.IsDir() {
entries, err := os.ReadDir(p)
if err != nil {
continue
}
if len(entries) != 0 {
continue
}
} else if st.Size() != 0 {
continue
}

// Back up the timestamps of the dir for reproducible builds
// /~https://github.com/moby/buildkit/issues/3148
dir := filepath.Dir(p)
Expand Down

0 comments on commit 6778973

Please sign in to comment.