commit:     0581fbc0421e88d3ef01b55ab864803070be3d96
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Aug  7 17:02:06 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri May 30 07:30:36 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=0581fbc0

helper-functions.sh: drop the __redirect_alloc_fd() function

Given a target of bash-4.2, the __redirect_alloc_fd() function is no
longer needed. Get rid of it.

Refrain from potentially invoking the similarly obsoleted __bashpid()
function.

Use -- to signify end of options for a few commands.

Don't perform arithmetic by combining the true builtin with arithmetic
expansion. To use (( … )) is more idiomatic in bash.

Inject mj_write_fd safely into the EXIT trap code, fixing an instance of
SC2086 into the bargain.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/helper-functions.sh | 77 +++++++++++++++++--------------------------------
 1 file changed, 26 insertions(+), 51 deletions(-)

diff --git a/bin/helper-functions.sh b/bin/helper-functions.sh
index a7f99197a5..1ba3630a56 100644
--- a/bin/helper-functions.sh
+++ b/bin/helper-functions.sh
@@ -11,6 +11,8 @@ source "${PORTAGE_BIN_PATH}"/isolated-functions.sh || exit 1
 # API functions for doing parallel processing
 #
 __multijob_init() {
+       local pipe
+
        # Setup a pipe for children to write their pids to when they finish.
        # We have to allocate two fd's because POSIX has undefined behavior
        # when using one single fd for both read and write. #487056
@@ -19,13 +21,13 @@ __multijob_init() {
        # read and write to not block ourselve, but use it for reading only.
        # The second fd really is opened for write only, as Cygwin supports
        # just one single read fd per FIFO. #583962
-       local pipe
-       pipe=$(mktemp -t multijob.XXXXXX) || die
-       rm -f "${pipe}"
-       mkfifo -m 600 "${pipe}" || die
-       __redirect_alloc_fd mj_read_fd "${pipe}"
-       __redirect_alloc_fd mj_write_fd "${pipe}" '>'
-       rm -f "${pipe}"
+       pipe=$(mktemp -t multijob.XXXXXX) \
+       && rm -f -- "${pipe}" \
+       && mkfifo -m 600 -- "${pipe}" \
+       && exec {mj_read_fd}<>"${pipe}" \
+       && exec {mj_write_fd}>"${pipe}" \
+       && rm -f -- "${pipe}" \
+       || die "__multijob_init: failed to set up the pipes"
 
        # See how many children we can fork based on the user's settings.
        mj_max_jobs=$(___makeopts_jobs "$@") || die
@@ -33,64 +35,37 @@ __multijob_init() {
 }
 
 __multijob_child_init() {
-       trap 'echo ${BASHPID:-$(__bashpid)} $? >&'${mj_write_fd} EXIT
+       local exit_routine
+
+       # shellcheck disable=SC2016
+       printf -v exit_routine 'echo "${BASHPID} $?" >&%q' "${mj_write_fd}"
+       # shellcheck disable=SC2064
+       trap "${exit_routine}" EXIT
        trap 'exit 1' INT TERM
 }
 
 __multijob_finish_one() {
-       local pid ret
-       read -r -u ${mj_read_fd} pid ret
-       : $(( --mj_num_jobs ))
-       return ${ret}
+       local ret
+
+       read -r -u "${mj_read_fd}" _ ret
+       (( --mj_num_jobs ))
+       return "${ret}"
 }
 
 __multijob_finish() {
-       local ret=0
-       while [[ ${mj_num_jobs} -gt 0 ]] ; do
+       local ret
+
+       while (( mj_num_jobs > 0 )); do
                __multijob_finish_one
-               : $(( ret |= $? ))
+               (( ret |= $? ))
        done
        # Let bash clean up its internal child tracking state.
        wait
-       return ${ret}
+       return "${ret}"
 }
 
 __multijob_post_fork() {
-       : $(( ++mj_num_jobs ))
-       if [[ ${mj_num_jobs} -ge ${mj_max_jobs} ]] ; then
+       if (( ++mj_num_jobs >= mj_max_jobs )); then
                __multijob_finish_one
        fi
-       return $?
-}
-
-# @FUNCTION: __redirect_alloc_fd
-# @USAGE: <var> <file> [redirection]
-# @DESCRIPTION:
-# Find a free fd and redirect the specified file via it.  Store the new
-# fd in the specified variable.  Useful for the cases where we don't care
-# about the exact fd #.
-__redirect_alloc_fd() {
-       local var=$1 file=$2 redir=${3:-"<>"}
-
-       if [[ $(( (BASH_VERSINFO[0] << 8) + BASH_VERSINFO[1] )) -ge $(( (4 << 
8) + 1 )) ]] ; then
-               # Newer bash provides this functionality.
-               eval "exec {${var}}${redir}'${file}'"
-       else
-               # Need to provide the functionality ourselves.
-               local fd=10
-               local fddir=/dev/fd
-               # Prefer /proc/self/fd if available (/dev/fd
-               # doesn't work on solaris, see bug #474536).
-               [[ -d /proc/self/fd ]] && fddir=/proc/self/fd
-               while :; do
-                       # Make sure the fd isn't open.  It could be a char 
device,
-                       # or a symlink (possibly broken) to something else.
-                       if [[ ! -e ${fddir}/${fd} ]] && [[ ! -L ${fddir}/${fd} 
]] ; then
-                               eval "exec ${fd}${redir}'${file}'" && break
-                       fi
-                       [[ ${fd} -gt 1024 ]] && die 'could not locate a free 
temp fd !?'
-                       : $(( ++fd ))
-               done
-               : $(( ${var} = fd ))
-       fi
 }

Reply via email to