From ce84f1762c65e39b59b0677e7f50f1104afe78eb Mon Sep 17 00:00:00 2001 From: osm0sis Date: Sun, 29 Nov 2020 20:35:34 -0400 Subject: [PATCH] scripts: fix sloppy/unpredictable `cmd && this || that` statements - be a bit more POSIX to avoid any potential issues when full shell stdout/err are redirected - actual logic chains remain unchanged --- scripts/addon.d.sh | 21 +++++++++++++++++---- scripts/boot_patch.sh | 9 ++++++++- scripts/emulator.sh | 3 ++- scripts/update_binary.sh | 3 ++- scripts/util_functions.sh | 32 +++++++++++++++++++++++--------- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/scripts/addon.d.sh b/scripts/addon.d.sh index 0b0c35ea3..3a95d400a 100644 --- a/scripts/addon.d.sh +++ b/scripts/addon.d.sh @@ -13,7 +13,8 @@ trampoline() { exec sh $MAGISKBIN/addon.d.sh "$@" exit $? elif [ "$1" = post-restore ]; then - ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false + BOOTMODE=false + ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true $BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true if ! $BOOTMODE; then @@ -24,7 +25,13 @@ trampoline() { [ -z $OUTFD ] && OUTFD=$(ps | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2) [ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2) fi - ui_print() { $BOOTMODE && log -t Magisk -- "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD; } + ui_print() { + if $BOOTMODE; then + log -t Magisk -- "$1" + else + echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD + fi + } ui_print "***********************" ui_print " Magisk addon.d failed" @@ -88,7 +95,12 @@ main() { if $backuptool_ab; then # Swap the slot for addon.d-v2 - if [ ! -z $SLOT ]; then [ $SLOT = _a ] && SLOT=_b || SLOT=_a; fi + if [ ! -z $SLOT ]; then + case $SLOT in + _a) SLOT=_b;; + _b) SLOT=_a;; + esac + fi fi find_boot_image @@ -128,7 +140,8 @@ case "$1" in post-restore) initialize if $backuptool_ab; then - $BOOTMODE && su=su || su=sh + su=sh + $BOOTMODE && su=su exec $su -c "sh $0 addond-v2" else # Run in background, hack for addon.d-v1 diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 3bdba05bb..2b25942c4 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -32,7 +32,14 @@ # Pure bash dirname implementation getdir() { case "$1" in - */*) dir=${1%/*}; [ -z $dir ] && echo "/" || echo $dir ;; + */*) + dir=${1%/*} + if [ -z $dir ]; then + echo "/" + else + echo $dir + fi + ;; *) echo "." ;; esac } diff --git a/scripts/emulator.sh b/scripts/emulator.sh index 9f1230d54..80968ccbd 100755 --- a/scripts/emulator.sh +++ b/scripts/emulator.sh @@ -63,7 +63,8 @@ pgrep magiskd >/dev/null && pkill -9 magiskd [ -f /system/bin/magisk ] && umount -l /system/bin # SELinux stuffs -[ -e /sys/fs/selinux ] && SELINUX=true || SELINUX=false +SELINUX=false +[ -e /sys/fs/selinux ] && SELINUX=true if $SELINUX; then ln -sf ./magiskinit magiskpolicy ./magiskpolicy --live --magisk diff --git a/scripts/update_binary.sh b/scripts/update_binary.sh index 8d2505bfb..504f7e694 100644 --- a/scripts/update_binary.sh +++ b/scripts/update_binary.sh @@ -14,7 +14,8 @@ setup_bb() { export BBBIN case "$1" in "extract"|"-x") - [ -z "$2" ] && BBBIN=./busybox || BBBIN="$2" + BBBIN=./busybox + [ -z "$2" ] || BBBIN="$2" extract_bb ;; "sh") diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index d11586b67..b790b8220 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -12,7 +12,11 @@ ################### ui_print() { - $BOOTMODE && echo "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD + if $BOOTMODE; then + echo "$1" + else + echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD + fi } toupper() { @@ -64,7 +68,8 @@ print_title() { local len line1len line2len pounds line1len=$(echo -n $1 | wc -c) line2len=$(echo -n $2 | wc -c) - [ $line1len -gt $line2len ] && len=$line1len || len=$line2len + len=$line2len + [ $line1len -gt $line2len ] && len=$line1len len=$((len + 2)) pounds=$(printf "%${len}s" | tr ' ' '*') ui_print "$pounds" @@ -258,8 +263,8 @@ mount_partitions() { fi mount -o bind /system_root/system /system else - grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts \ - && SYSTEM_ROOT=true || SYSTEM_ROOT=false + SYSTEM_ROOT=false + grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts && SYSTEM_ROOT=true fi # /vendor is used only on some older devices for recovery AVBv1 signing so is not critical if fails [ -L /system/vendor ] && mount_name vendor$SLOT /vendor '-o ro' @@ -426,7 +431,11 @@ install_magisk() { $BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0" fi - $IS64BIT && mv -f magiskinit64 magiskinit 2>/dev/null || rm -f magiskinit64 + if $IS64BIT; then + mv -f magiskinit64 magiskinit 2>/dev/null + else + rm -f magiskinit64 + fi # Source the boot patcher SOURCEDMODE=true @@ -511,7 +520,8 @@ check_data() { # Test if DE storage is writable $DATA && [ -d /data/adb ] && touch /data/adb/.rw && rm /data/adb/.rw && DATA_DE=true fi - $DATA && NVBASE=/data || NVBASE=/cache/data_adb + NVBASE=/data + $DATA || NVBASE=/cache/data_adb $DATA_DE && NVBASE=/data/adb resolve_vars } @@ -652,14 +662,18 @@ install_module() { api_level_arch_detect # Setup busybox and binaries - $BOOTMODE && boot_actions || recovery_actions + if $BOOTMODE; then + boot_actions + else + recovery_actions + fi # Extract prop file unzip -o "$ZIPFILE" module.prop -d $TMPDIR >&2 [ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!" - local MODDIRNAME - $BOOTMODE && MODDIRNAME=modules_update || MODDIRNAME=modules + local MODDIRNAME=modules + $BOOTMODE && MODDIRNAME=modules_update local MODULEROOT=$NVBASE/$MODDIRNAME MODID=`grep_prop id $TMPDIR/module.prop` MODNAME=`grep_prop name $TMPDIR/module.prop`