From: Matthias Kruk Date: Wed, 26 May 2021 04:31:15 +0000 (+0900) Subject: include/queue: queue_foreach: Return failure if callback fails X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=9069bf91b58280cd67634e4c3f0af0b5e435a8d8;p=toolbox include/queue: queue_foreach: Return failure if callback fails The queue_foreach() function always returns success, even if one of the callbacks failed. This makes it hard to handle errors that occur inside the callbacks. This commit changes the function to return failure if a callback has failed. --- diff --git a/include/queue.sh b/include/queue.sh index b5bc03e..868d61b 100644 --- a/include/queue.sh +++ b/include/queue.sh @@ -500,9 +500,11 @@ queue_foreach() { local data local mutex local item + local err data=$(_queue_get_data "$name") mutex=$(_queue_get_mutex "$name") + err=0 if ! mutex_lock "$mutex"; then return 1 @@ -511,6 +513,7 @@ queue_foreach() { if [ -f "$data" ]; then while read -r item; do if ! "$func" "$item" "${args[@]}"; then + err=1 break fi done < "$data" @@ -518,5 +521,5 @@ queue_foreach() { mutex_unlock "$mutex" - return 0 + return "$err" }