[arch-projects] [mkinitcpio][PATCH 04/19] refactor error tracking in build hooks

Dave Reisner dreisner at archlinux.org
Sun May 13 13:57:07 EDT 2012


Rather than catching errors solely from parse_hook via an ERR trap,
implement a RETURN trap which catches all errors from the core add_*
functions.

In the future, this may mean that support for MODULES/FILES/BINARIES
within hooks goes away, and those variables remain only in the config.

Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
 functions  |    9 +++++++--
 mkinitcpio |   13 +++++++------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/functions b/functions
index 5956dc3..bca31b4 100644
--- a/functions
+++ b/functions
@@ -160,6 +160,7 @@ warning() {
 error() {
     local mesg=$1; shift
     printf "$RED==> ERROR:$NC$BOLD $mesg$NC\n" "$@" >&2
+    return 1
 }
 
 die() {
@@ -320,7 +321,10 @@ add_dir() {
 
     local path=$1 mode=${2:-755}
 
-    [[ -e $BUILDROOT$1 ]] && return 0 # file exists
+    if [[ -d $BUILDROOT$1 ]]; then
+        # ignore dir already exists
+        return 0
+    fi
 
     (( QUIET )) || plain "adding dir: %s" "$path"
     command install -dm$mode "$BUILDROOT$path"
@@ -398,7 +402,8 @@ add_binary() {
     # always add the binary itself
     add_file "$binary" "$dest" "$mode"
 
-    lddout=$(ldd "$binary" 2>/dev/null) || return 0 # not a binary!
+    # negate this so that the RETURN trap is not fired on non-binaries
+    ! lddout=$(ldd "$binary" 2>/dev/null) && return 0
 
     # resolve sodeps
     regex='(/.+) \(0x[a-fA-F0-9]+\)'
diff --git a/mkinitcpio b/mkinitcpio
index 74de0c2..ce40b22 100755
--- a/mkinitcpio
+++ b/mkinitcpio
@@ -351,10 +351,10 @@ else
     msg "Starting build: %s" "$KERNELVERSION"
 fi
 
-# set errtrace and a trap to catch errors in parse_hook
+# set functrace and trap to catch errors in add_* functions
 declare -i builderrors=0
-set -E
-trap '[[ $FUNCNAME = parse_hook ]] && (( ++builderrors ))' ERR
+set -o functrace
+trap '(( $? )) && [[ $FUNCNAME = add_* ]] && (( ++builderrors ))' RETURN
 
 # save vars from $CONFIG; they will be parsed last
 for var in MODULES BINARIES FILES; do
@@ -402,7 +402,8 @@ for var in cfg_{MODULES,BINARIES,FILES}; do
 done
 parse_hook
 
-# reset the trap to catch all errors
+# switch out the error handler to catch all errors
+trap -- RETURN
 trap '(( ++builderrors ))' ERR
 
 if (( ${#ADDED_MODULES[*]} )); then
@@ -425,8 +426,8 @@ else
 fi
 
 # unset errtrace and trap
-set +E
-trap ERR
+set +o functrace
+trap -- ERR
 
 declare -i status=0
 declare -a pipesave
-- 
1.7.10.2



More information about the arch-projects mailing list