"""A tiny OCI whiteout teaching model, not a Docker/OverlayFS implementation."""
import io
import tarfile

def layer(name, data):
    out = io.BytesIO()
    with tarfile.open(fileobj=out, mode="w") as t:
        h = tarfile.TarInfo(name)
        h.size = len(data)
        t.addfile(h, io.BytesIO(data))
    return out.getvalue()

first = layer("token.txt", b"FAKE_TOKEN_FOR_DEMO")
second = layer(".wh.token.txt", b"")
view = {}
for blob in (first, second):
    with tarfile.open(fileobj=io.BytesIO(blob)) as t:
        for h in t:
            if h.name.startswith(".wh."):
                view.pop(h.name[4:], None)
            else:
                view[h.name] = t.extractfile(h).read()
assert "token.txt" not in view
assert b"FAKE_TOKEN_FOR_DEMO" in first
print("merged view contains token:", "token.txt" in view)
print("old layer still contains bytes:", b"FAKE_TOKEN_FOR_DEMO" in first)
