From e346e22d8fb15ffd099939c4f8052bcb02f8960d Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Sun, 6 Dec 2020 18:27:17 +0100 Subject: xi: refactor Suggestions by kerframil: - Fix the original su invocation bug. The original approach is veering very close to eval territory. Let's just not overload SUDO quite so hard and keep it coherent. - Use id -u (posix) instead of whoami (not posix). - Rather than try to make a scalar behave as if it were an array (it's not) then unsafely expand it, compose the positional parameter list as needed. Note that XBPS_BINPKGS and BRANCH are now expanded safely in the course of doing so. - Don't include improperly formed repository paths as parameters if git fails or prints nothing. - Have su pass sh as the zeroeth arg, not a dash so that, if an error in the shell context ever happens again, the resulting message will be less confusing. As an aside, if you want the environment to be as it normally would be for the root user, there should be a dash (or -l) before -c, not after --. Could help protect against issues of environmental pollution down the line. - Don't squash the exit status of the first call to xbps-install in the case that it's not zero and also not 16. - Make the fallback invocation be contingent upon -u xbps succeeding. Note that the exit status of either will be properly conveyed by the script i.e. we don't have to capture the exit value there. --- xi | 67 ++++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 25 deletions(-) mode change 100755 => 100644 xi diff --git a/xi b/xi old mode 100755 new mode 100644 index 0541b3c..df328f1 --- a/xi +++ b/xi @@ -1,38 +1,55 @@ #!/bin/sh # xi PKGS... - like xbps-install -S, but take cwd repo and sudo/su into account -BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null) if [ -n "$XBPS_HOSTDIR" ]; then XBPS_BINPKGS="$XBPS_HOSTDIR/binpkgs" else XBPS_DISTDIR="$(xdistdir 2>/dev/null)" || XBPS_DISTDIR=. XBPS_BINPKGS="$XBPS_DISTDIR/hostdir/binpkgs" fi -ADDREPO=" - --repository=$XBPS_BINPKGS/$BRANCH - --repository=$XBPS_BINPKGS/$BRANCH/nonfree - --repository=$XBPS_BINPKGS/$BRANCH/multilib - --repository=$XBPS_BINPKGS/$BRANCH/multilib/nonfree - --repository=$XBPS_BINPKGS/$BRANCH/debug - --repository=$XBPS_BINPKGS - --repository=$XBPS_BINPKGS/nonfree - --repository=$XBPS_BINPKGS/multilib - --repository=$XBPS_BINPKGS/multilib/nonfree - --repository=$XBPS_BINPKGS/debug -" -SUDO= -if command -v sudo >/dev/null && - sudo -l | grep -q -e ' ALL$' -e xbps-install; then - SUDO=sudo -elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then - SUDO=doas -elif [ "$(whoami)" != root ]; then - SUDO='su root -c '\''"$@"'\'' -- -' +set -- \ + --repository="$XBPS_BINPKGS" \ + --repository="$XBPS_BINPKGS/nonfree" \ + --repository="$XBPS_BINPKGS/multilib" \ + --repository="$XBPS_BINPKGS/multilib/nonfree" \ + --repository="$XBPS_BINPKGS/debug" \ + "$@" + +if BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null) && [ -n "$BRANCH" ]; then + set -- \ + --repository="$XBPS_BINPKGS/$BRANCH" \ + --repository="$XBPS_BINPKGS/$BRANCH/nonfree" \ + --repository="$XBPS_BINPKGS/$BRANCH/multilib" \ + --repository="$XBPS_BINPKGS/$BRANCH/multilib/nonfree" \ + --repository="$XBPS_BINPKGS/$BRANCH/debug" \ + "$@" fi -$SUDO xbps-install $ADDREPO -S "$@" -if [ $? -eq 16 ]; then - $SUDO xbps-install -u xbps - $SUDO xbps-install $ADDREPO -S "$@" +which_sudo() { + if command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then + echo sudo + elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then + echo doas + elif [ "$(id -u)" != 0 ]; then + echo su + fi +} + +do_install() { + if [ "$SUDO" = su ]; then + su root -c 'xbps-install "$@"' -- sh "$@" + else + $SUDO xbps-install "$@" + fi +} + +SUDO=$(which_sudo) +do_install -S "$@" +status=$? +if [ $status -eq 16 ]; then + do_install -u xbps && + do_install -S "$@" +else + exit $status fi -- cgit 1.4.1