From 5e8ce7d073b387066d2d73cd63592b8f68a4205b Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Mon, 4 Sep 2023 20:26:04 +0100 Subject: [PATCH] lib.cli-std: fix two shell errors when AUTOMAKE is false If `AUTOMAKE` is set to `no` in the config file, it is normalized to the empty string. This leads to two errors if `find` is provided by Busybox. There is a conditional where `$AUTOMAKE` is not quoted when compared to `recursive` leading to the following error: /usr/share/shorewall/lib.cli-std: line 398: [: =: unary operator expected In contrast to the non-Busybox case, we don't check for an empty `$AUTOMAKE` before passing it as an argument to `-maxdepth`, leading to: /usr/bin/find: Expected a positive decimal integer argument to -maxdepth, but got -type Refactor the conditionals to eliminate code duplication and fix these two bugs. Link: https://gitlab.com/shorewall/code/-/issues/10 Signed-off-by: Jeremy Sowden --- Shorewall/lib.cli-std | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/Shorewall/lib.cli-std b/Shorewall/lib.cli-std index a5a497674..4dd0e8149 100644 --- a/Shorewall/lib.cli-std +++ b/Shorewall/lib.cli-std @@ -381,36 +381,33 @@ uptodate() { [ -x $1 ] || return 1 local dir - local busybox local find + local quit + local maxdepth find=$(mywhich find) [ -n "${find}" ] || return 1 - [ -h "${find}" ] && busybox=Yes - find="${find} -L" + + if [ -h "${find}" ]; then + # + # 'Find' is provided by Busybox and doesn't support -quit. + # + quit= + else + quit=-quit + fi + + if [ "$AUTOMAKE" = recursive ]; then + maxdepth= + elif [ -z "$AUTOMAKE" ]; then + maxdepth="-maxdepth 1" + else + maxdepth="-maxdepth $AUTOMAKE" + fi for dir in $g_shorewalldir $(split $CONFIG_PATH); do - if [ -n "${busybox}" ]; then - # - # Busybox 'find' doesn't support -quit. - # - if [ $AUTOMAKE = recursive ]; then - if [ -n "$(${find} ${dir} -newer $1 -print)" ]; then - return 1; - fi - elif [ -n "$(${find} ${dir} -maxdepth $AUTOMAKE -type f -newer $1 -print)" ]; then - return 1; - fi - elif [ "$AUTOMAKE" = recursive ]; then - if [ -n "$(${find} ${dir} -newer $1 -print -quit)" ]; then - return 1; - fi - elif [ -z "$AUTOMAKE" ]; then - if [ -n "$(${find} ${dir} -maxdepth 1 -type f -newer $1 -print -quit)" ]; then - return 1; - fi - elif [ -n "$(${find} ${dir} -maxdepth $AUTOMAKE -type f -newer $1 -print -quit)" ]; then + if [ -n "$(${find} -L ${dir} ${maxdepth} -newer $1 -print ${quit})" ]; then return 1; fi done