From df0d6b867b289ed98c4b9e7ced817dee155feb4f Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 13 Feb 2019 02:02:40 -0500 Subject: [PATCH] Revert "makechrootpkg: Avoid having code floating around outside of a function." This reverts commit 49088b0860276c664933c2b3e36a2fef714b7a07. The fundamental intention was flawed and broken, it caused annoying issues and regressions, and the self-avowed sole purpose of the change was so that a downstream project could *post-modify the script and source it as a library*. That is not okay. You don't wrap non-factorable code in a function called main() and call it a library. The only possible use for this is to treat makechrootpkg *internals* as a library, which is not supported. Downstream projects that wish to use the functionality of makechrootpkg should treat makepkg as a command with a public API in the form of command line options. That is kind of how commands of all kinds work, since forever. That is how all users of makechrootpkg *except for parabola* use it. Arguments that "it saves us the cost of fork+exec to bash" are simply invalid. Signed-off-by: Eli Schwartz Signed-off-by: Levente Polyak --- makechrootpkg.in | 238 +++++++++++++++++++++++------------------------ 1 file changed, 117 insertions(+), 121 deletions(-) diff --git a/makechrootpkg.in b/makechrootpkg.in index 4ea008b..6718d13 100644 --- a/makechrootpkg.in +++ b/makechrootpkg.in @@ -15,6 +15,27 @@ m4_include(lib/archroot.sh) shopt -s nullglob +default_makepkg_args=(--syncdeps --noconfirm --log --holdver --skipinteg) +makepkg_args=("${default_makepkg_args[@]}") +keepbuilddir=false +update_first=false +clean_first=false +run_namcap=false +temp_chroot=false +chrootdir= +passeddir= +makepkg_user= +declare -a install_pkgs +declare -i ret=0 + +bindmounts_ro=() +bindmounts_rw=() + +copy=$USER +[[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER +[[ -z "$copy" || $copy = root ]] && copy=copy +src_owner=${SUDO_USER:-$USER} + usage() { echo "Usage: ${0##*/} [options] -r [--] [makepkg args]" echo ' Run this script in a PKGBUILD dir to build a package inside a' @@ -293,140 +314,115 @@ move_products() { } # }}} -main() { - default_makepkg_args=(--syncdeps --noconfirm --log --holdver --skipinteg) - makepkg_args=("${default_makepkg_args[@]}") - keepbuilddir=false - update_first=false - clean_first=false - run_namcap=false - temp_chroot=false - chrootdir= - passeddir= - makepkg_user= - declare -a install_pkgs - declare -i ret=0 +while getopts 'hcur:I:l:nTD:d:U:' arg; do + case "$arg" in + c) clean_first=true ;; + D) bindmounts_ro+=("--bind-ro=$OPTARG") ;; + d) bindmounts_rw+=("--bind=$OPTARG") ;; + u) update_first=true ;; + r) passeddir="$OPTARG" ;; + I) install_pkgs+=("$OPTARG") ;; + l) copy="$OPTARG" ;; + n) run_namcap=true; makepkg_args+=(--install) ;; + T) temp_chroot=true; copy+="-$$" ;; + U) makepkg_user="$OPTARG" ;; + h|*) usage ;; + esac +done - bindmounts_ro=() - bindmounts_rw=() +[[ ! -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.' +makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}} - copy=$USER - [[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER - [[ -z "$copy" || $copy = root ]] && copy=copy - src_owner=${SUDO_USER:-$USER} +check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER - while getopts 'hcur:I:l:nTD:d:U:' arg; do - case "$arg" in - c) clean_first=true ;; - D) bindmounts_ro+=("--bind-ro=$OPTARG") ;; - d) bindmounts_rw+=("--bind=$OPTARG") ;; - u) update_first=true ;; - r) passeddir="$OPTARG" ;; - I) install_pkgs+=("$OPTARG") ;; - l) copy="$OPTARG" ;; - n) run_namcap=true; makepkg_args+=(--install) ;; - T) temp_chroot=true; copy+="-$$" ;; - U) makepkg_user="$OPTARG" ;; - h|*) usage ;; - esac - done +# Canonicalize chrootdir, getting rid of trailing / +chrootdir=$(readlink -e "$passeddir") +[[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir" +[[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir" - [[ ! -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.' - makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}} +if [[ ${copy:0:1} = / ]]; then + copydir=$copy +else + copydir="$chrootdir/$copy" +fi - check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER +# Pass all arguments after -- right to makepkg +makepkg_args+=("${@:$OPTIND}") - # Canonicalize chrootdir, getting rid of trailing / - chrootdir=$(readlink -e "$passeddir") - [[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir" - [[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir" +# See if -R or -e was passed to makepkg +for arg in "${makepkg_args[@]}"; do + case ${arg%%=*} in + --repackage|--noextract) keepbuilddir=true; break ;; + --repackage|--noextract) keepbuilddir=true; break ;; + --*) ;; + -*R*|-*e*) keepbuilddir=true; break ;; + esac +done - if [[ ${copy:0:1} = / ]]; then - copydir=$copy - else - copydir="$chrootdir/$copy" - fi +if [[ -n $SUDO_USER ]]; then + eval "USER_HOME=~$SUDO_USER" +else + USER_HOME=$HOME +fi - # Pass all arguments after -- right to makepkg - makepkg_args+=("${@:$OPTIND}") +umask 0022 - # See if -R or -e was passed to makepkg - for arg in "${makepkg_args[@]}"; do - case ${arg%%=*} in - --repackage|--noextract) keepbuilddir=true; break ;; - --repackage|--noextract) keepbuilddir=true; break ;; - --*) ;; - -*R*|-*e*) keepbuilddir=true; break ;; - esac - done +load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf" +load_vars /etc/makepkg.conf - if [[ -n $SUDO_USER ]]; then - eval "USER_HOME=~$SUDO_USER" - else - USER_HOME=$HOME - fi +# Use PKGBUILD directory if these don't exist +[[ -d $PKGDEST ]] || PKGDEST=$PWD +[[ -d $SRCDEST ]] || SRCDEST=$PWD +[[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD +[[ -d $LOGDEST ]] || LOGDEST=$PWD - umask 0022 +# Lock the chroot we want to use. We'll keep this lock until we exit. +lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy" - load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf" - load_vars /etc/makepkg.conf +if [[ ! -d $copydir ]] || $clean_first; then + sync_chroot "$chrootdir" "$copydir" "$copy" +fi - # Use PKGBUILD directory if these don't exist - [[ -d $PKGDEST ]] || PKGDEST=$PWD - [[ -d $SRCDEST ]] || SRCDEST=$PWD - [[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD - [[ -d $LOGDEST ]] || LOGDEST=$PWD - - # Lock the chroot we want to use. We'll keep this lock until we exit. - lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy" - - if [[ ! -d $copydir ]] || $clean_first; then - sync_chroot "$chrootdir" "$copydir" "$copy" - fi - - $update_first && arch-nspawn "$copydir" \ - "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - pacman -Syu --noconfirm - - if [[ -n ${install_pkgs[*]:-} ]]; then - install_packages "$copydir" "${install_pkgs[@]}" - ret=$? - # If there is no PKGBUILD we have done - [[ -f PKGBUILD ]] || return $ret - fi - - if [[ "$(id -u "$makepkg_user")" == 0 ]]; then - error "Running makepkg as root is not allowed." - exit 1 - fi - - download_sources "$copydir" "$makepkg_user" - - prepare_chroot "$copydir" "$USER_HOME" "$keepbuilddir" "$run_namcap" - - if arch-nspawn "$copydir" \ - --bind="$PWD:/startdir" \ - --bind="$SRCDEST:/srcdest" \ +$update_first && arch-nspawn "$copydir" \ "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ - /chrootbuild "${makepkg_args[@]}" - then - move_products "$copydir" "$src_owner" + pacman -Syu --noconfirm + +if [[ -n ${install_pkgs[*]:-} ]]; then + install_packages "$copydir" "${install_pkgs[@]}" + ret=$? + # If there is no PKGBUILD we have done + [[ -f PKGBUILD ]] || return $ret +fi + +if [[ "$(id -u "$makepkg_user")" == 0 ]]; then + error "Running makepkg as root is not allowed." + exit 1 +fi + +download_sources "$copydir" "$makepkg_user" + +prepare_chroot "$copydir" "$USER_HOME" "$keepbuilddir" "$run_namcap" + +if arch-nspawn "$copydir" \ + --bind="$PWD:/startdir" \ + --bind="$SRCDEST:/srcdest" \ + "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \ + /chrootbuild "${makepkg_args[@]}" +then + move_products "$copydir" "$src_owner" +else + (( ret += 1 )) +fi + +$temp_chroot && delete_chroot "$copydir" "$copy" + +if (( ret != 0 )); then + if $temp_chroot; then + die "Build failed" else - (( ret += 1 )) + die "Build failed, check %s/build" "$copydir" fi - - $temp_chroot && delete_chroot "$copydir" "$copy" - - if (( ret != 0 )); then - if $temp_chroot; then - die "Build failed" - else - die "Build failed, check %s/build" "$copydir" - fi - else - true - fi -} - -main "$@" +else + true +fi