Files
lotherkandClaude Sonnet 5.5 8429ae7e8a ext-extract: sha256 through cksum(1) where there is no sha256sum
NetBSD fails the extract of every fork with 'xargs: sha256sum: No such file'
(MacBook, 2026-11-24).  Same line format, '<hex>  <path>'.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 06:36:55 +00:00

108 lines
4.4 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# ext-extract.sh -- fill a pkgsrc work directory from the source tree of a fork
#
# usage: ext-extract.sh SRCDIR WRKSRC
#
# A fork of ext/ builds from its own tree, not from an archive: no DISTFILES,
# no distinfo, and the recipe's do-extract calls this script (ext/AGENTS.md).
#
# Why this exists instead of a `cp -R' in the recipe: pkgsrc extracts once.
# The work directory then holds a copy that a later commit does not reach, so
# a release can ship the binary of an older commit while the tree says
# otherwise (measured 2026-11-23: the recipe of mcde 288d1383 kept the time
# stamp of 2026-11-22 and the release carried that day's build).
#
# What it does, and why in this order:
#
# 1. hash the source tree, one pass (SHA256 and path per file, .git left out);
# 2. a file that left the tree since the last extract: remove the whole copy
# and start from nothing. A refresh that kept it would build a file that
# no longer exists -- a leftover that stays invisible until the binary
# differs from the source it claims to be built from;
# 3. copy with -p, so an unchanged file keeps its time stamp and make keeps
# the object files that depend on it (MCDE is 27 minutes from nothing);
# 4. touch every file whose hash changed. A time stamp alone cannot be
# trusted here -- exactly that went wrong on 2026-11-23 -- and hashing
# costs one read of the tree, not one process per file.
#
# The hash list lives beside WRKSRC (in the work directory, never in the copy).
set -eu
[ $# -eq 2 ] || { echo "usage: $0 SRCDIR WRKSRC" >&2; exit 2; }
src=${1%/}
wrksrc=${2%/}
case $src in /*) ;; *) echo "ext-extract: SRCDIR must be absolute: $src" >&2; exit 2 ;; esac
case $wrksrc in /*) ;; *) echo "ext-extract: WRKSRC must be absolute: $wrksrc" >&2; exit 2 ;; esac
[ -d "$src" ] || { echo "ext-extract: no source tree at $src" >&2; exit 1; }
wrkdir=$(dirname "$wrksrc")
mkdir -p "$wrkdir"
list=$wrkdir/.makronix-srcfiles
tmpd=$wrkdir/.makronix-srcfiles.tmp
rm -rf "$tmpd"
mkdir -p "$tmpd"
# One line per file: "<sha256> <path>", the path relative to the tree and
# without the git metadata (.git is a directory in a clone, a file in a
# submodule checkout).
# sha256sum is GNU; NetBSD has cksum(1) -a sha256 ("SHA256 (path) = hex"),
# turned into the same "<hex> <path>" line.
if command -v sha256sum > /dev/null 2>&1; then
sha256_files() { xargs -0 -r sha256sum; }
else
sha256_files() {
xargs -0 -r cksum -a sha256 | sed 's/^SHA256 (\(.*\)) = \(.*\)$/\2 \1/'
}
fi
(cd "$src" && find . \( -name .git -o -path './.git/*' \) -prune -o \
-type f -print0 | sha256_files) > "$tmpd/hashes"
full=0
[ -f "$list" ] && [ -d "$wrksrc" ] || full=1
if [ $full -eq 0 ]; then
cut -c67- "$list" | LC_ALL=C sort > "$tmpd/paths.old"
cut -c67- "$tmpd/hashes" | LC_ALL=C sort > "$tmpd/paths.new"
# a name in the old list that the new one does not have: the copy holds a
# file the source no longer has, and patching it up is not worth a rule
if comm -23 "$tmpd/paths.old" "$tmpd/paths.new" | grep -q .; then
echo "ext-extract: a file left the source tree -- the work directory starts from nothing"
full=1
fi
fi
if [ $full -eq 1 ]; then
# from nothing, not over what is there: a file that left the source tree
# must not survive in the copy (that is the case this branch exists for)
rm -rf "$wrksrc"
mkdir -p "$wrksrc"
cp -Rp "$src"/. "$wrksrc"/
else
# Only what is new or changed is copied, and each file keeps the time
# stamp of the source: make then rebuilds exactly that and keeps every
# object file that does not depend on it (MCDE is 27 minutes from
# nothing). Copying the whole tree instead would give every file a new
# time stamp and rebuild everything.
LC_ALL=C sort -k2 "$list" > "$tmpd/a"
LC_ALL=C sort -k2 "$tmpd/hashes" > "$tmpd/b"
join -j 2 -o 1.2,1.1,2.1 "$tmpd/a" "$tmpd/b" > "$tmpd/common"
while read -r path was now; do
[ "$was" = "$now" ] && continue
mkdir -p "$wrksrc/$(dirname "$path")"
cp -p "$src/$path" "$wrksrc/$path"
done < "$tmpd/common"
# a path the old list does not have at all: join drops it, so it is
# copied here
comm -13 "$tmpd/paths.old" "$tmpd/paths.new" > "$tmpd/new"
while read -r path; do
mkdir -p "$wrksrc/$(dirname "$path")"
cp -p "$src/$path" "$wrksrc/$path"
done < "$tmpd/new"
fi
rm -rf "$wrksrc/.git"
mv "$tmpd/hashes" "$list"
rm -rf "$tmpd"