#!/bin/sh # Keep the tracked overlay/rootfs snapshot in sync with prebuilt/ artifacts. # prebuilt/ is the single source of truth; this script mirrors every artifact # that also exists in overlay/rootfs back into the overlay. set -eu BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)" CHECK_ONLY=0 for arg in "$@"; do case "$arg" in --check|-c) CHECK_ONLY=1 ;; *) echo "Usage: $0 [--check]" >&2 exit 1 ;; esac done python3 - "$BASE_DIR" "$CHECK_ONLY" <<'PYEOF' import hashlib import os import shutil import sys base = sys.argv[1] check_only = sys.argv[2] == "1" prebuilt_root = os.path.join(base, "prebuilt") overlay_root = os.path.join(base, "overlay", "rootfs") def digest(path): with open(path, "rb") as handle: return hashlib.sha256(handle.read()).hexdigest() def regular_files(root): result = [] for parent, _, names in os.walk(root): for name in names: path = os.path.join(parent, name) if not os.path.islink(path) and os.path.isfile(path): result.append(path) return result def is_elf(path): try: with open(path, "rb") as handle: return handle.read(4) == b"\x7fELF" except OSError: return False by_name = {} for path in regular_files(prebuilt_root): by_name.setdefault(os.path.basename(path), []).append(path) drift = [] matched = 0 for target in regular_files(overlay_root): rel = os.path.relpath(target, overlay_root) candidates = by_name.get(os.path.basename(target), []) source = None for candidate in candidates: stripped = os.path.relpath(candidate, prebuilt_root).split(os.sep, 1)[-1] if stripped == rel: source = candidate break if source is None and candidates: bin_dir = os.path.join(overlay_root, "usr", "bin") + os.sep local_bin_dir = os.path.join(overlay_root, "usr", "local", "bin") + os.sep for candidate in candidates: if is_elf(candidate) and not target.startswith(bin_dir) and \ not target.startswith(local_bin_dir): continue source = candidate break if source is None: continue matched += 1 if digest(source) == digest(target): continue drift.append((source, target)) if not check_only: shutil.copy2(source, target) if check_only: for source, target in drift: print("out of sync: " + os.path.relpath(target, base)) print("artifact drift: %d of %d mirrored file(s)" % (len(drift), matched)) sys.exit(1 if drift else 0) print("synced %d artifact(s) into overlay/rootfs; %d already in sync" % ( len(drift), matched - len(drift))) PYEOF