summaryrefslogtreecommitdiffstats
path: root/bin/helper-functions.sh
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2012-06-04 15:22:21 -0700
committerZac Medico <zmedico@gentoo.org>2012-06-04 15:22:21 -0700
commit2c50bd9a82c3bb6dfbc63466ae8bfbd401fb3235 (patch)
treeadd4c0e63e776ae6ef22b6fed64d7a2332e0507f /bin/helper-functions.sh
parentee3bf6f9773c0afbd494c657d253241f79d98044 (diff)
downloadportage-2c50bd9a82c3bb6dfbc63466ae8bfbd401fb3235.tar.gz
portage-2c50bd9a82c3bb6dfbc63466ae8bfbd401fb3235.tar.bz2
portage-2c50bd9a82c3bb6dfbc63466ae8bfbd401fb3235.zip
helper-functions.sh: multijob support bash <4.1v2.2.0_alpha110
The redirect_alloc_fd() compatibility function is borrowed from Mike Frysinger's proposed multiprocessing.eclass: http://archives.gentoo.org/gentoo-dev/msg_5ecd3b1dd0720522807c136d8fd2cd5f.xml
Diffstat (limited to 'bin/helper-functions.sh')
-rw-r--r--bin/helper-functions.sh30
1 files changed, 29 insertions, 1 deletions
diff --git a/bin/helper-functions.sh b/bin/helper-functions.sh
index afe14af91..c7400fa4b 100644
--- a/bin/helper-functions.sh
+++ b/bin/helper-functions.sh
@@ -22,7 +22,7 @@ multijob_init() {
mj_control_pipe=$(mktemp -t multijob.XXXXXX)
rm "${mj_control_pipe}"
mkfifo "${mj_control_pipe}"
- exec {mj_control_fd}<>${mj_control_pipe}
+ redirect_alloc_fd mj_control_fd "${mj_control_pipe}"
rm -f "${mj_control_pipe}"
# See how many children we can fork based on the user's settings.
@@ -60,3 +60,31 @@ multijob_post_fork() {
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
+ 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 /dev/fd/${fd} ]] && [[ ! -L /dev/fd/${fd} ]] ; then
+ eval "exec ${fd}${redir}'${file}'" && break
+ fi
+ [[ ${fd} -gt 1024 ]] && die "redirect_alloc_fd failed"
+ : $(( ++fd ))
+ done
+ : $(( ${var} = fd ))
+ fi
+}