*: Do not use RETURN traps

It has some weird semantics that seem to trip us up after updating
bash to 5.1. We tried to use it inside functions to clean up some
stuff after function returns. This can be emulated with an EXIT trap
within a subshell. Fortunately all the users of the RETURN trap were
not setting any global variables - modifications of such variables are
local to the subshell and are lost when the subshell exits.
This commit is contained in:
Krzesimir Nowak 2021-09-27 20:03:54 +02:00
parent fbaa248898
commit eee6b50aa5
5 changed files with 25 additions and 7 deletions

View File

@ -124,13 +124,16 @@ sign_and_upload_files() {
local suffix="$3" local suffix="$3"
shift 3 shift 3
# run a subshell to possibly clean the temporary directory with
# signatures without clobbering the global EXIT trap
(
# Create simple GPG detached signature for all uploads. # Create simple GPG detached signature for all uploads.
local sigs=() local sigs=()
if [[ -n "${FLAGS_sign}" ]]; then if [[ -n "${FLAGS_sign}" ]]; then
local file local file
local sigfile local sigfile
local sigdir=$(mktemp --directory) local sigdir=$(mktemp --directory)
trap "rm -rf ${sigdir}" RETURN trap "rm -rf ${sigdir}" EXIT
for file in "$@"; do for file in "$@"; do
if [[ "${file}" =~ \.(asc|gpg|sig)$ ]]; then if [[ "${file}" =~ \.(asc|gpg|sig)$ ]]; then
continue continue
@ -150,6 +153,7 @@ sign_and_upload_files() {
fi fi
upload_files "${msg}" "${path}" "${suffix}" "$@" "${sigs[@]}" upload_files "${msg}" "${path}" "${suffix}" "$@" "${sigs[@]}"
)
} }
upload_packages() { upload_packages() {

View File

@ -114,13 +114,16 @@ function torcx_package() {
local name=${name##*/} local name=${name##*/}
local version=${version%%-r*} local version=${version%%-r*}
# Run in a subshell to clean tmproot and tmppkgroot up without
# clobbering this shell's EXIT trap.
(
# Set up the base package layout to dump everything into /bin and /lib. # Set up the base package layout to dump everything into /bin and /lib.
# tmproot is what the packages are installed into. # tmproot is what the packages are installed into.
# A subset of the files from tmproot are then moved into tmppkgroot, # A subset of the files from tmproot are then moved into tmppkgroot,
# which is then archived and uploaded. # which is then archived and uploaded.
tmproot=$(sudo mktemp --tmpdir="${BUILD_DIR}" -d) tmproot=$(sudo mktemp --tmpdir="${BUILD_DIR}" -d)
tmppkgroot=$(sudo mktemp --tmpdir="${BUILD_DIR}" -d) tmppkgroot=$(sudo mktemp --tmpdir="${BUILD_DIR}" -d)
trap "sudo rm -rf '${tmproot}' '${tmppkgroot}'" EXIT RETURN trap "sudo rm -rf '${tmproot}' '${tmppkgroot}'" EXIT
sudo chmod 0755 "${tmproot}" "${tmppkgroot}" sudo chmod 0755 "${tmproot}" "${tmppkgroot}"
sudo mkdir -p "${tmproot}"/{.torcx,bin,lib,usr} sudo mkdir -p "${tmproot}"/{.torcx,bin,lib,usr}
sudo ln -fns ../bin "${tmproot}/usr/bin" sudo ln -fns ../bin "${tmproot}/usr/bin"
@ -228,8 +231,7 @@ function torcx_package() {
"${source_pkg}" \ "${source_pkg}" \
"${update_default}" \ "${update_default}" \
"${pkg_locations[@]}" "${pkg_locations[@]}"
)
trap - EXIT
} }
# This list defines every torcx image that goes into the vendor store for the # This list defines every torcx image that goes into the vendor store for the

View File

@ -43,7 +43,10 @@ sudo rm -rf chroot/build src/build torcx
enter() { enter() {
local verify_key= local verify_key=
trap 'sudo rm -f chroot/etc/portage/gangue.*' RETURN # Run in a subshell to clean some gangue files on exit without
# possibly clobbering the global EXIT trap.
(
trap 'sudo rm -f chroot/etc/portage/gangue.*' EXIT
[ -s verify.asc ] && [ -s verify.asc ] &&
sudo ln -f verify.asc chroot/etc/portage/gangue.asc && sudo ln -f verify.asc chroot/etc/portage/gangue.asc &&
verify_key=--verify-key=/etc/portage/gangue.asc verify_key=--verify-key=/etc/portage/gangue.asc
@ -55,6 +58,7 @@ enter() {
--json-key=/etc/portage/gangue.json $verify_key \ --json-key=/etc/portage/gangue.json $verify_key \
"'"${URI}" "${DISTDIR}/${FILE}"' \ "'"${URI}" "${DISTDIR}/${FILE}"' \
"$@" "$@"
)
} }
script() { script() {

View File

@ -47,7 +47,10 @@ bin/cork update \
enter() { enter() {
local verify_key= local verify_key=
trap 'sudo rm -f chroot/etc/portage/gangue.*' RETURN # Run in a subshell to clean some gangue files on exit without
# possibly clobbering the global EXIT trap.
(
trap 'sudo rm -f chroot/etc/portage/gangue.*' EXIT
[ -s verify.asc ] && [ -s verify.asc ] &&
sudo ln -f verify.asc chroot/etc/portage/gangue.asc && sudo ln -f verify.asc chroot/etc/portage/gangue.asc &&
verify_key=--verify-key=/etc/portage/gangue.asc verify_key=--verify-key=/etc/portage/gangue.asc
@ -60,6 +63,7 @@ enter() {
--json-key=/etc/portage/gangue.json $verify_key \ --json-key=/etc/portage/gangue.json $verify_key \
"'"${URI}" "${DISTDIR}/${FILE}"' \ "'"${URI}" "${DISTDIR}/${FILE}"' \
"$@" "$@"
)
} }
script() { script() {

View File

@ -43,7 +43,10 @@ sudo rm -rf chroot/build tmp
enter() { enter() {
local verify_key= local verify_key=
trap 'sudo rm -f chroot/etc/portage/gangue.*' RETURN # Run in a subshell to clean some gangue files on exit without
# possibly clobbering the global EXIT trap.
(
trap 'sudo rm -f chroot/etc/portage/gangue.*' EXIT
[ -s verify.asc ] && [ -s verify.asc ] &&
sudo ln -f verify.asc chroot/etc/portage/gangue.asc && sudo ln -f verify.asc chroot/etc/portage/gangue.asc &&
verify_key=--verify-key=/etc/portage/gangue.asc verify_key=--verify-key=/etc/portage/gangue.asc
@ -55,6 +58,7 @@ enter() {
--json-key=/etc/portage/gangue.json $verify_key \ --json-key=/etc/portage/gangue.json $verify_key \
"'"${URI}" "${DISTDIR}/${FILE}"' \ "'"${URI}" "${DISTDIR}/${FILE}"' \
"$@" "$@"
)
} }
script() { script() {