From 45f87aa9d5b44e4ff2f73be6597d4024bcded8e3 Mon Sep 17 00:00:00 2001
From: "Jan Alexander Steffens (heftig)" <jan.steffens@gmail.com>
Date: Tue, 7 Mar 2017 19:27:36 +0100
Subject: [PATCH] lib/archroot.sh: Simplify check_root

Move the function and save the orig_argv right along it.
---
 arch-nspawn.in   |  4 +---
 archbuild.in     |  5 ++---
 lib/archroot.sh  | 13 +++++++++++++
 lib/common.sh    | 12 ------------
 makechrootpkg.in |  5 ++---
 mkarchroot.in    |  4 +---
 6 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/arch-nspawn.in b/arch-nspawn.in
index 5c0f46f..5d5620d 100644
--- a/arch-nspawn.in
+++ b/arch-nspawn.in
@@ -25,8 +25,6 @@ usage() {
 	exit 1
 }
 
-orig_argv=("$@")
-
 while getopts 'hC:M:c:' arg; do
 	case "$arg" in
 		C) pac_conf="$OPTARG" ;;
@@ -39,7 +37,7 @@ done
 shift $(($OPTIND - 1))
 
 (( $# < 1 )) && die 'You must specify a directory.'
-check_root "$0" "${orig_argv[@]}"
+check_root
 
 working_dir=$(readlink -f "$1")
 shift 1
diff --git a/archbuild.in b/archbuild.in
index 9c5d706..4fb33c3 100644
--- a/archbuild.in
+++ b/archbuild.in
@@ -1,6 +1,7 @@
 #!/bin/bash
 
 m4_include(lib/common.sh)
+m4_include(lib/archroot.sh)
 
 base_packages=(base-devel)
 makechrootpkg_args=(-c -n)
@@ -29,8 +30,6 @@ usage() {
 	exit 1
 }
 
-orig_argv=("$@")
-
 while getopts 'hcr:' arg; do
 	case "${arg}" in
 		c) clean_first=true ;;
@@ -39,7 +38,7 @@ while getopts 'hcr:' arg; do
 	esac
 done
 
-check_root "$0" "${orig_argv[@]}"
+check_root
 
 # Pass all arguments after -- right to makepkg
 makechrootpkg_args+=("${@:$OPTIND}")
diff --git a/lib/archroot.sh b/lib/archroot.sh
index 25e94fb..7d7ab67 100644
--- a/lib/archroot.sh
+++ b/lib/archroot.sh
@@ -1 +1,14 @@
 CHROOT_VERSION='v4'
+
+##
+#  usage : check_root
+##
+orig_argv=("$0" "$@")
+check_root() {
+	(( EUID == 0 )) && return
+	if type -P sudo >/dev/null; then
+		exec sudo -- "${orig_argv[@]}"
+	else
+		exec su root -c "$(printf ' %q' "${orig_argv[@]}")"
+	fi
+}
diff --git a/lib/common.sh b/lib/common.sh
index 387fb8c..599be54 100644
--- a/lib/common.sh
+++ b/lib/common.sh
@@ -227,15 +227,3 @@ find_cached_package() {
 			return 1
 	esac
 }
-
-##
-#  usage : check_root ("$0" "$@")
-##
-check_root() {
-	(( EUID == 0 )) && return
-	if type -P sudo >/dev/null; then
-		exec sudo -- "$@"
-	else
-		exec su root -c "$(printf ' %q' "$@")"
-	fi
-}
diff --git a/makechrootpkg.in b/makechrootpkg.in
index 79b387d..695b898 100644
--- a/makechrootpkg.in
+++ b/makechrootpkg.in
@@ -9,6 +9,7 @@
 # GNU General Public License for more details.
 
 m4_include(lib/common.sh)
+m4_include(lib/archroot.sh)
 
 shopt -s nullglob
 
@@ -258,8 +259,6 @@ move_products() {
 }
 # }}}
 
-orig_argv=("$@")
-
 while getopts 'hcur:I:l:nTD:d:U:' arg; do
 	case "$arg" in
 		c) clean_first=true ;;
@@ -279,7 +278,7 @@ done
 [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.'
 [[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.'
 
-check_root "$0" "${orig_argv[@]}"
+check_root
 
 # Canonicalize chrootdir, getting rid of trailing /
 chrootdir=$(readlink -e "$passeddir")
diff --git a/mkarchroot.in b/mkarchroot.in
index e03b4f9..010de3c 100644
--- a/mkarchroot.in
+++ b/mkarchroot.in
@@ -23,8 +23,6 @@ usage() {
 	exit 1
 }
 
-orig_argv=("$@")
-
 while getopts 'hC:M:c:' arg; do
 	case "$arg" in
 		C) pac_conf="$OPTARG" ;;
@@ -38,7 +36,7 @@ shift $(($OPTIND - 1))
 
 (( $# < 2 )) && die 'You must specify a directory and one or more packages.'
 
-check_root "$0" "${orig_argv[@]}"
+check_root
 
 working_dir="$(readlink -f $1)"
 shift 1
-- 
GitLab