From 91bb2e91bd8323fd616c6a5c503fe430bc61ef73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Sat, 22 May 2021 01:45:40 +0200 Subject: [PATCH] Turn checkbashisms into a make target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit make_gitrev.sh actually breaks checkbashisms' parser, which /insists/ that the end-of-line " is actually a string start Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia ZiemiaƄska Closes #12101 --- Makefile.am | 25 ------------------------- cmd/zpool/zpool.d/smart | 9 +++------ config/Shellcheck.am | 14 ++++++++++++++ config/always-shellcheck.m4 | 5 ++++- contrib/dracut/90zfs/Makefile.am | 3 +++ scripts/make_gitrev.sh | 4 ++-- scripts/paxcheck.sh | 3 +-- 7 files changed, 27 insertions(+), 36 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4fd3f9b3d6..8e4fffc5d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -135,31 +135,6 @@ checkabi: lib storeabi: lib $(MAKE) -C lib storeabi -PHONY += checkbashisms -checkbashisms: - @if type checkbashisms > /dev/null 2>&1; then \ - checkbashisms -n -p -x \ - $$(find ${top_srcdir} \ - -name '.git' -prune \ - -o -name 'build' -prune \ - -o -name 'tests' -prune \ - -o -name 'config' -prune \ - -o -name 'zed-functions.sh*' -prune \ - -o -name 'zfs-import*' -prune \ - -o -name 'zfs-mount*' -prune \ - -o -name 'zfs-zed*' -prune \ - -o -name 'smart' -prune \ - -o -name 'paxcheck.sh' -prune \ - -o -name 'make_gitrev.sh' -prune \ - -o -name '90zfs' -prune \ - -o -path '*initramfs/hooks' -prune \ - -o -type f ! -name 'config*' \ - ! -name 'libtool' \ - -exec awk 'FNR==1 && /^#!.*bin\/sh/ {print FILENAME}' {} \+); \ - else \ - echo "skipping checkbashisms because checkbashisms is not installed"; \ - fi - PHONY += mancheck mancheck: @if type mandoc > /dev/null 2>&1; then \ diff --git a/cmd/zpool/zpool.d/smart b/cmd/zpool/zpool.d/smart index b71591abbc..b95256d756 100755 --- a/cmd/zpool/zpool.d/smart +++ b/cmd/zpool/zpool.d/smart @@ -62,17 +62,14 @@ get_filename_from_dir() done } -script=$(basename "$0") +script="${0##*/}" if [ "$1" = "-h" ] ; then echo "$helpstr" | grep "$script:" | tr -s '\t' | cut -f 2- exit fi -smartctl_path=$(command -v smartctl) - -# shellcheck disable=SC2015 -if [ -b "$VDEV_UPATH" ] && [ -x "$smartctl_path" ] || [ -n "$samples" ] ; then +if [ -b "$VDEV_UPATH" ] && PATH="/usr/sbin:$PATH" command -v smartctl > /dev/null || [ -n "$samples" ] ; then if [ -n "$samples" ] ; then # cat a smartctl output text file instead of running smartctl # on a vdev (only used for developer testing). @@ -80,7 +77,7 @@ if [ -b "$VDEV_UPATH" ] && [ -x "$smartctl_path" ] || [ -n "$samples" ] ; then echo "file=$file" raw_out=$(cat "$samples/$file") else - raw_out=$(eval "sudo $smartctl_path -a $VDEV_UPATH") + raw_out=$(sudo smartctl -a "$VDEV_UPATH") fi # What kind of drive are we? Look for the right line in smartctl: diff --git a/config/Shellcheck.am b/config/Shellcheck.am index a22333ac45..6b805b797d 100644 --- a/config/Shellcheck.am +++ b/config/Shellcheck.am @@ -6,3 +6,17 @@ else @[ -z "$(SCRIPTS)$(SHELLCHECKSCRIPTS)" ] && exit; echo "skipping shellcheck of" $(SCRIPTS) $(SHELLCHECKSCRIPTS) "because shellcheck is not installed" endif @set -e; for dir in $(SHELLCHECKDIRS); do $(MAKE) -C $$dir shellcheck; done + + +# command -v *is* specified by POSIX and every shell in existence supports it +.PHONY: checkbashisms +checkbashisms: $(SCRIPTS) $(SHELLCHECKSCRIPTS) +if HAVE_CHECKBASHISMS + [ -z "$(SCRIPTS)$(SHELLCHECKSCRIPTS)" ] && exit; ! if [ -z "$(SHELLCHECK_SHELL)" ]; then \ + checkbashisms -npx $(SCRIPTS) $(SHELLCHECKSCRIPTS); else \ + for f in $(SCRIPTS) $(SHELLCHECKSCRIPTS); do echo $$f >&3; { echo '#!/bin/$(SHELLCHECK_SHELL)'; cat $$f; } | checkbashisms -npx; done; \ + fi 3>&2 2>&1 | grep -vFe "'command' with option other than -p" -e 'command -v' $(CHECKBASHISMS_IGNORE) >&2 +else + @[ -z "$(SCRIPTS)$(SHELLCHECKSCRIPTS)" ] && exit; echo "skipping checkbashisms of" $(SCRIPTS) $(SHELLCHECKSCRIPTS) "because checkbashisms is not installed" +endif + @set -e; for dir in $(SHELLCHECKDIRS); do $(MAKE) -C $$dir checkbashisms; done diff --git a/config/always-shellcheck.m4 b/config/always-shellcheck.m4 index 1e154e1c55..2a9a099746 100644 --- a/config/always-shellcheck.m4 +++ b/config/always-shellcheck.m4 @@ -1,7 +1,10 @@ dnl # -dnl # Check if shellcheck is available. +dnl # Check if shellcheck and/or checkbashisms are available. dnl # AC_DEFUN([ZFS_AC_CONFIG_ALWAYS_SHELLCHECK], [ AC_CHECK_PROG([SHELLCHECK], [shellcheck], [yes]) + AC_CHECK_PROG([CHECKBASHISMS], [checkbashisms], [yes]) + AM_CONDITIONAL([HAVE_SHELLCHECK], [test "x$SHELLCHECK" = "xyes"]) + AM_CONDITIONAL([HAVE_CHECKBASHISMS], [test "x$CHECKBASHISMS" = "xyes"]) ]) diff --git a/contrib/dracut/90zfs/Makefile.am b/contrib/dracut/90zfs/Makefile.am index 19b206ddd7..ff3a2b27f2 100644 --- a/contrib/dracut/90zfs/Makefile.am +++ b/contrib/dracut/90zfs/Makefile.am @@ -18,3 +18,6 @@ pkgdracut_DATA = \ zfs-rollback-bootfs.service SUBSTFILES += $(pkgdracut_SCRIPTS) $(pkgdracut_DATA) + +# Provided by /bin/sleep, and, again, every implementation of that supports this +CHECKBASHISMS_IGNORE = -e 'sleep only takes one integer' -e 'sleep 0.' diff --git a/scripts/make_gitrev.sh b/scripts/make_gitrev.sh index f5c743b137..da21455332 100755 --- a/scripts/make_gitrev.sh +++ b/scripts/make_gitrev.sh @@ -56,9 +56,9 @@ then # already exists (in the source) if [ -f "${top_srcdir}/${GITREV}" ] then - ZFS_GITREV="$(sed -n \ + ZFS_GITREV=$(sed -n \ '1s/^#define[[:blank:]]ZFS_META_GITREV "\([^"]*\)"$/\1/p' \ - "${top_srcdir}/${GITREV}")" + "${top_srcdir}/${GITREV}") fi elif [ ${dist} = yes ] then diff --git a/scripts/paxcheck.sh b/scripts/paxcheck.sh index 87e817500b..27acc95364 100755 --- a/scripts/paxcheck.sh +++ b/scripts/paxcheck.sh @@ -1,7 +1,6 @@ #!/bin/sh -# shellcheck disable=SC2039 -if ! type scanelf > /dev/null 2>&1; then +if ! command -v scanelf > /dev/null; then echo "scanelf (from pax-utils) is required for these checks." >&2 exit 3 fi