From ffccc1fbb9113c6f109e6d880107c1a85886389b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 6 Aug 2018 15:35:18 -0400 Subject: chcleanup: Tidy up - Move a few lines around - Add comments - Don't bother checking [[ -n $TEXTDOMAIN ]]/TEXTDOMAINDIR, they're never set. - Put the pacman db at "$TEMPDIR/db", not "$TEMPDIR". - Create a pacman=() variable for storing all of our scratch flags - Drop the pkglist='' variable; expand it out where used Altogether, there should be no user-visible changes here --- src/chroot-tools/chcleanup.in | 53 ++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/chroot-tools/chcleanup.in b/src/chroot-tools/chcleanup.in index 0326716..1d674aa 100644 --- a/src/chroot-tools/chcleanup.in +++ b/src/chroot-tools/chcleanup.in @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -eE # Copyright (C) 2011-2012 Nicolás Reynolds # Copyright (C) 2012-2013, 2015, 2017-2018 Luke Shumaker # @@ -23,34 +24,33 @@ # along with this program. If not, see . # Performs chroot cleanup smartly, it only removes the unneeded packages or -# leaves you with a cleansystem -# -# See: HOOKPREBUILD +# leaves you with a clean system -set -eE - -DRYRUN=${DRYRUN:-false} +# Library routines ############################################################# # Statically include various library routines to avoid having # dependencies on outside files. -[[ -n ${TEXTDOMAIN:-} ]] || export TEXTDOMAIN='libretools' -[[ -n ${TEXTDOMAINDIR:-} ]] || export TEXTDOMAINDIR='/usr/share/locale' +export TEXTDOMAIN='libretools' +export TEXTDOMAINDIR='/usr/share/locale' if type gettext &>/dev/null; then _() { gettext "$@"; } else _() { echo "$@"; } fi + # Begin chcleanup.lib ########################################################## m4_include(chcleanup.lib) # End chcleanup.lib ############################################################ +# User interface ############################################################### +DRYRUN=${DRYRUN:-false} if [[ ! -f /.arch-chroot ]] && ! ${DRYRUN}; then error "(chcleanup): Must be run inside of a chroot" exit 1 fi -# Figure out which packages we want +# Load configuration ########################################################### CHROOTPKG=(base-devel) # Note: the in-chroot pkgconfdir is non-configurable, this is # intentionally hard-coded. @@ -66,38 +66,35 @@ else DEPENDS=() fi +# Main ######################################################################### + msg "Cleaning chroot..." -# Sync the local repo with pacman +# Sync the local repo with pacman (a limited form of `pacman -Sy`) cp /repo/repo.db /var/lib/pacman/sync/repo.db # Setup the temporary directory TEMPDIR="$(mktemp --tmpdir -d "${0##*/}.XXXXXXXXXX")" trap "rm -rf -- ${TEMPDIR@Q}" EXIT -cp -a /var/lib/pacman/sync "${TEMPDIR}/" -pkglist="${TEMPDIR}"/pkglist.txt - -# Get the full list of packages needed by dependencies, including the base system -msg2 "Creating a full list of packages..." -mkdir "$TEMPDIR/hooks" +# Set up a scratch pacman DB +mkdir -- "$TEMPDIR/db" "$TEMPDIR/db/local" "$TEMPDIR/hooks" +cp -a -t "${TEMPDIR}/db" -- /var/lib/pacman/sync { echo /usr/share/libalpm/hooks; pacman-conf HookDir; } | while read -r dir; do for hook in "$dir"/*.hook; do ln -sfT -- /dev/null "$TEMPDIR/hooks/${hook##*/}" done done +pacman=(pacman --dbpath="$TEMPDIR/db" --hookdir="$TEMPDIR/hooks") +# Get the full list of packages needed by dependencies, including the base system +msg2 "Creating a full list of packages..." for var in CHROOTPKG CHROOTEXTRAPKG DEPENDS; do - declare -n fullpkgs="$var" - mapfile -t missingpkgs < <(pacman --dbpath="$TEMPDIR" -T -- "${fullpkgs[@]}") - if (( ${#missingpkgs[@]} == 0 )); then + declare -n pkgsref="$var" + mapfile -t pkgs < <("${pacman[@]}" -T -- "${pkgsref[@]}") + if (( ${#pkgs[@]} == 0 )); then continue fi - # Even though `pacman -T` filtered installed packages out, - # if still if use --needed because of groups. - pacman \ - --dbpath="$TEMPDIR" --hookdir="$TEMPDIR/hooks" \ - -S --dbonly --noscriptlet --needed --noconfirm -- "${missingpkgs[@]}" \ - <&- &>"$TEMPDIR/pacman.txt" || ret=$? + "${pacman[@]}" -S --dbonly --noscriptlet --needed --noconfirm -- "${pkgs[@]}" <&- >& "$TEMPDIR/pacman.txt" || ret=$? if (( ret != 0 )); then error "Could not create a full list of packages, exiting." plain "This is likely caused by a dependency that could not be found." @@ -105,11 +102,11 @@ for var in CHROOTPKG CHROOTEXTRAPKG DEPENDS; do exit $ret fi done -pacman --dbpath="$TEMPDIR" -Qq >"$pkglist" +"${pacman[@]}" -Qq >"$TEMPDIR/pkglist.txt" # Diff installed packages against a clean chroot then remove leftovers packages=($(comm -23 <(pacman -Qq | sort -u) \ - <(sort -u "${pkglist}"))) + <(sort -u "$TEMPDIR/pkglist.txt"))) if [[ ${#packages[@]} = 0 ]]; then msg2 "No packages to remove" else @@ -124,7 +121,7 @@ else fi packages=($(comm -13 <(pacman -Qq | sort -u) \ - <(sort -u "${pkglist}"))) + <(sort -u "$TEMPDIR/pkglist.txt"))) if [[ ${#packages[@]} = 0 ]]; then msg2 "No packages to add" else -- cgit v1.2.2