Skip to content

Commit 4d13d53

Browse files
authored
mutate: apply opaque-directory whiteouts (.wh..wh..opq) in Extract (#2372)
* mutate: apply opaque-directory whiteouts (.wh..wh..opq) in Extract Extract did not apply OCI opaque-directory whiteouts (.wh..wh..opq) when flattening layers, so files a lower layer placed in a directory opaqued by an upper layer wrongly reappeared in the merged output. The opaque marker shares the .wh. whiteout prefix, so it matched the generic per-file whiteout check and was mis-parsed as a whiteout of a phantom file named ".wh..opq", never marking the directory opaque. Match the marker exactly and handle it before the generic prefix strip; track opaqued directories per layer and promote them only after the layer is processed, so the opaque layer's own entries survive while lower layers are masked. Adds a regression test. Signed-off-by: Sadman Fahmid <[email protected]> Signed-off-by: Sadman Fahmid <[email protected]>
1 parent af2603f commit 4d13d53

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎pkg/v1/mutate/mutate.go‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import (
3737

3838
const whiteoutPrefix = ".wh."
3939

40+
// opaqueWhiteout marks a directory as opaque: all entries from lower layers under
41+
// its parent directory are hidden. It shares the whiteoutPrefix, so it must be
42+
// matched exactly and handled before the generic per-file whiteout logic.
43+
const opaqueWhiteout = ".wh..wh..opq"
44+
4045
// Addendum contains layers and history to be appended
4146
// to a base image
4247
type Addendum struct {
@@ -267,6 +272,9 @@ func extract(img v1.Image, w io.Writer) error {
267272
defer tarWriter.Close()
268273

269274
fileMap := map[string]bool{}
275+
// opaqueDirs holds directories opaqued by an upper layer; entries under them
276+
// from lower (later-iterated) layers are hidden.
277+
opaqueDirs := map[string]bool{}
270278

271279
layers, err := img.Layers()
272280
if err != nil {
@@ -277,14 +285,18 @@ func extract(img v1.Image, w io.Writer) error {
277285
// whiteout layers more efficient, since we can just keep track of the removed
278286
// files as we see .wh. layers and ignore those in previous layers.
279287
for i := len(layers) - 1; i >= 0; i-- {
280-
if err := extractLayer(tarWriter, fileMap, layers[i]); err != nil {
288+
if err := extractLayer(tarWriter, fileMap, opaqueDirs, layers[i]); err != nil {
281289
return err
282290
}
283291
}
284292
return nil
285293
}
286294

287-
func extractLayer(tarWriter *tar.Writer, fileMap map[string]bool, layer v1.Layer) error {
295+
func extractLayer(tarWriter *tar.Writer, fileMap, opaqueDirs map[string]bool, layer v1.Layer) error {
296+
// Opaque markers in this layer hide only lower layers, so stage them and
297+
// promote to opaqueDirs after the whole layer is processed.
298+
layerOpaque := map[string]bool{}
299+
288300
layerReader, err := layer.Uncompressed()
289301
if err != nil {
290302
return fmt.Errorf("reading layer contents: %w", err)
@@ -328,6 +340,14 @@ func extractLayer(tarWriter *tar.Writer, fileMap map[string]bool, layer v1.Layer
328340

329341
basename := path.Base(header.Name)
330342
dirname := path.Dir(header.Name)
343+
344+
// An opaque marker hides all lower-layer entries under dirname. It shares
345+
// the whiteout prefix, so handle it before the generic per-file logic.
346+
if basename == opaqueWhiteout {
347+
layerOpaque[dirname] = true
348+
continue
349+
}
350+
331351
tombstone := strings.HasPrefix(basename, whiteoutPrefix)
332352
if tombstone {
333353
basename = basename[len(whiteoutPrefix):]
@@ -351,6 +371,11 @@ func extractLayer(tarWriter *tar.Writer, fileMap map[string]bool, layer v1.Layer
351371
continue
352372
}
353373

374+
// check for a parent directory opaqued by an upper layer
375+
if inOpaqueDir(opaqueDirs, name) {
376+
continue
377+
}
378+
354379
// mark file as handled. non-directory implicitly tombstones
355380
// any entries with a matching (or child) name
356381
fileMap[name] = tombstone || (header.Typeflag != tar.TypeDir)
@@ -366,6 +391,11 @@ func extractLayer(tarWriter *tar.Writer, fileMap map[string]bool, layer v1.Layer
366391
}
367392
}
368393

394+
// Opaque dirs found in this layer now hide entries in lower layers.
395+
for d := range layerOpaque {
396+
opaqueDirs[d] = true
397+
}
398+
369399
// Drain any bytes the tar.Reader did not consume (trailing data after the
370400
// end-of-archive marker) so the underlying verifying reader reaches io.EOF
371401
// and the layer's digest is verified. Without this, a layer whose contents
@@ -377,6 +407,20 @@ func extractLayer(tarWriter *tar.Writer, fileMap map[string]bool, layer v1.Layer
377407
return nil
378408
}
379409

410+
func inOpaqueDir(opaqueDirs map[string]bool, file string) bool {
411+
for file != "" {
412+
dirname := path.Dir(file)
413+
if file == dirname {
414+
break
415+
}
416+
if opaqueDirs[dirname] {
417+
return true
418+
}
419+
file = dirname
420+
}
421+
return false
422+
}
423+
380424
func inWhiteoutDir(fileMap map[string]bool, file string) bool {
381425
for file != "" {
382426
dirname := path.Dir(file)

‎pkg/v1/mutate/mutate_test.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,63 @@ func TestExtractOverwrittenFile(t *testing.T) {
105105
}
106106
}
107107

108+
func TestExtractOpaqueWhiteout(t *testing.T) {
109+
mkLayer := func(files map[string][]byte, opaqueDirs ...string) v1.Layer {
110+
var buf bytes.Buffer
111+
tw := tar.NewWriter(&buf)
112+
for name, body := range files {
113+
if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o644, Size: int64(len(body))}); err != nil {
114+
t.Fatal(err)
115+
}
116+
if _, err := tw.Write(body); err != nil {
117+
t.Fatal(err)
118+
}
119+
}
120+
for _, d := range opaqueDirs {
121+
if err := tw.WriteHeader(&tar.Header{Name: d + "/.wh..wh..opq", Mode: 0o644}); err != nil {
122+
t.Fatal(err)
123+
}
124+
}
125+
if err := tw.Close(); err != nil {
126+
t.Fatal(err)
127+
}
128+
b := buf.Bytes()
129+
l, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
130+
return io.NopCloser(bytes.NewReader(b)), nil
131+
})
132+
if err != nil {
133+
t.Fatal(err)
134+
}
135+
return l
136+
}
137+
138+
base := mkLayer(map[string][]byte{"d/a.txt": []byte("a"), "d/b.txt": []byte("b")})
139+
upper := mkLayer(map[string][]byte{"d/c.txt": []byte("c")}, "d")
140+
img, err := mutate.AppendLayers(empty.Image, base, upper)
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
145+
got := map[string]bool{}
146+
tr := tar.NewReader(mutate.Extract(img))
147+
for {
148+
h, err := tr.Next()
149+
if errors.Is(err, io.EOF) {
150+
break
151+
}
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
got[h.Name] = true
156+
}
157+
if !got["d/c.txt"] {
158+
t.Error("d/c.txt (upper layer, under opaque dir) should be present")
159+
}
160+
if got["d/a.txt"] || got["d/b.txt"] {
161+
t.Error("opaque dir must hide lower-layer files d/a.txt and d/b.txt")
162+
}
163+
}
164+
108165
func TestExtractClosesLayerBeforeOpeningNext(t *testing.T) {
109166
tokens := make(chan struct{}, 4)
110167
layers := make([]v1.Layer, cap(tokens)+1)

0 commit comments

Comments
 (0)