From e230668356e9f5fe6932f32e847a2328af326b98 Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 5 Jun 2025 23:58:42 +0700 Subject: [PATCH] audit-tmpdir.sh: optimize - make use of parallelMap --- .../build-support/setup-hooks/audit-tmpdir.sh | 65 ++++++------------- .../setup-hooks/tests/default.nix | 31 +++++---- 2 files changed, 36 insertions(+), 60 deletions(-) diff --git a/pkgs/build-support/setup-hooks/audit-tmpdir.sh b/pkgs/build-support/setup-hooks/audit-tmpdir.sh index 25fe203f813b..780f38bc1624 100644 --- a/pkgs/build-support/setup-hooks/audit-tmpdir.sh +++ b/pkgs/build-support/setup-hooks/audit-tmpdir.sh @@ -15,54 +15,27 @@ auditTmpdir() { echo "checking for references to $TMPDIR/ in $dir..." - local tmpdir elf_fifo script_fifo - tmpdir="$(mktemp -d)" - elf_fifo="$tmpdir/elf" - script_fifo="$tmpdir/script" - mkfifo "$elf_fifo" "$script_fifo" - - # Classifier: identify ELF and script files - ( - find "$dir" -type f -not -path '*/.build-id/*' -print0 \ - | while IFS= read -r -d $'\0' file; do - if isELF "$file"; then - printf '%s\0' "$file" >&3 - elif isScript "$file"; then - filename=${file##*/} - dir=${file%/*} - if [ -e "$dir/.$filename-wrapped" ]; then - printf '%s\0' "$file" >&4 + _processFile() { + local file="$1" + if isELF "$file"; then + if { printf :; patchelf --print-rpath "$file"; } | grep -q -F ":$TMPDIR/"; then + echo "RPATH of binary $file contains a forbidden reference to $TMPDIR/" + exit 1 + fi + elif isScript "$file"; then + filename=${i##*/} + dir=${i%/*} + if [ -e "$dir/.$filename-wrapped" ]; then + if grep -q -F "$TMPDIR/" "$file"; then + echo "wrapper script $file contains a forbidden reference to $TMPDIR/" + exit 1 fi fi - done - exec 3>&- 4>&- - ) 3> "$elf_fifo" 4> "$script_fifo" & + fi + } - # Handler: check RPATHs concurrently - ( - xargs -0 -r -P "$NIX_BUILD_CORES" -n 1 sh -c ' - if { printf :; patchelf --print-rpath "$1"; } | grep -q -F ":$TMPDIR/"; then - echo "RPATH of binary $1 contains a forbidden reference to $TMPDIR/" - exit 1 - fi - ' _ < "$elf_fifo" - ) & - local pid_elf=$! + find "$dir" -type f -not -path '*/.build-id/*' -print0 \ + | parallelMap _processFile - # Handler: check wrapper scripts concurrently - local pid_script - ( - xargs -0 -r -P "$NIX_BUILD_CORES" -n 1 sh -c ' - if grep -q -F "$TMPDIR/" "$1"; then - echo "wrapper script $1 contains a forbidden reference to $TMPDIR/" - exit 1 - fi - ' _ < "$script_fifo" - ) & - local pid_script=$! - - wait "$pid_elf" || { echo "Some binaries contain forbidden references to $TMPDIR/. Check the error above!"; exit 1; } - wait "$pid_script" || { echo "Some scripts contain forbidden references to $TMPDIR/. Check the error above!"; exit 1; } - - rm -r "$tmpdir" + unset -f _processFile } diff --git a/pkgs/build-support/setup-hooks/tests/default.nix b/pkgs/build-support/setup-hooks/tests/default.nix index ad4e284a9279..36cd73a1a9ee 100644 --- a/pkgs/build-support/setup-hooks/tests/default.nix +++ b/pkgs/build-support/setup-hooks/tests/default.nix @@ -3,18 +3,21 @@ }: { # test based on bootstrap tools to prevent rebuilding stdenv on each change - parallel = derivation { - name = "test-parallel-hook"; - system = stdenv.system; - builder = "${stdenv.bootstrapTools}/bin/bash"; - PATH = "${stdenv.bootstrapTools}/bin"; - args = [ - "-c" - '' - . ${../parallel.sh} - . ${./test-parallel.sh} - '' - ]; - meta = { }; - }; + parallel = + (derivation { + name = "test-parallel-hook"; + system = stdenv.system; + builder = "${stdenv.bootstrapTools}/bin/bash"; + PATH = "${stdenv.bootstrapTools}/bin"; + args = [ + "-c" + '' + . ${../parallel.sh} + . ${./test-parallel.sh} + '' + ]; + }) + // { + meta = { }; + }; }