From b2da60f68a153b1876f8416de3033623518ae000 Mon Sep 17 00:00:00 2001 From: classabbyamp Date: Tue, 13 Feb 2024 23:56:22 -0500 Subject: [PATCH] xbps-src: add purge-binpkgs action --- common/xbps-src/shutils/purge_binpkgs.sh | 80 ++++++++++++++++++++++++ xbps-src | 6 ++ 2 files changed, 86 insertions(+) create mode 100644 common/xbps-src/shutils/purge_binpkgs.sh diff --git a/common/xbps-src/shutils/purge_binpkgs.sh b/common/xbps-src/shutils/purge_binpkgs.sh new file mode 100644 index 0000000000000..259e7ca9ec843 --- /dev/null +++ b/common/xbps-src/shutils/purge_binpkgs.sh @@ -0,0 +1,80 @@ +# scans binpkgs for packages that don't match the current version and removes them. + +purge_binpkgs() { + local max cur percent pnew dir rdata arch template pkg ver rev + local binpkg pkgfile pkgver pkgname version repos + local -a binpkg_dirs templates binpkgs + local -A versions + + if [ -z "$XBPS_REPOSITORY" ]; then + msg_error "The variable \$XBPS_REPOSITORY is not set." + exit 1 + fi + + # Scan all templates for their current versions + mapfile -t templates < <(find srcpkgs -mindepth 1 -maxdepth 1 -type d -printf "srcpkgs/%f/template\n") + max="${#templates[@]}" + cur=0 + if [ -z "$max" ]; then + msg_error "No srcpkgs/*/template files found. Wrong working directory?" + exit 1 + fi + percent=-1 + for template in "${templates[@]}"; do + pkg="${template#*/}" + pkg="${pkg%/*}" + if [ ! -L "srcpkgs/$pkg" ]; then + ver="$(grep -Eh "^version=.*$" "${template}")" + rev="$(grep -Eh "^revision=.*$" "${template}")" + versions["$pkg"]="${ver#*=}_${rev#*=}" + fi + cur=$((cur + 1)) + pnew=$((100 * cur / max)) + if [ $pnew -ne $percent ]; then + percent="$pnew" + printf "\rScanning templates: %3d%% (%d/%d)" "$percent" "$cur" "$max" + fi + done + echo + + binpkgs=("$XBPS_REPOSITORY"/**/*.xbps) + max=${#binpkgs[@]} + if [ -z "$max" ]; then + msg_error "No binpkgs found in '$XBPS_REPOSITORY'" + exit 1 + fi + cur=0 + for binpkg in "${binpkgs[@]}"; do + pkgfile="${binpkg##*/}" + pkgver="${pkgfile%.*.*}" + pkgname="$(find -P srcpkgs -maxdepth 2 -samefile "srcpkgs/${pkgver%-*}/template" 2>/dev/null | cut -d/ -f2)" + [ -z "$pkgname" ] && pkgname="${pkgver%-*}" + version="${pkgver##*-}" + # existing package + if [ -n "${versions[$pkgname]}" ]; then + # remove if not matching the template version + if ! xbps-uhelper cmpver "${versions[$pkgname]}" "$version"; then + rm -vf "$binpkg" + cur=$((cur + 1)) + fi + else + # nonexistant package, remove + rm -vf "$binpkg" + cur=$((cur + 1)) + fi + done + + mapfile -t binpkg_dirs < <(find "$XBPS_REPOSITORY" -type d) + repos=0 + for dir in "${binpkg_dirs[@]}"; do + for rdata in "$dir"/*-repodata; do + arch="${rdata##*/}" + arch="${arch%-repodata}" + XBPS_TARGET_ARCH="${arch}" xbps-rindex -c "$dir" + repos=$((repos + 1)) + done + done + echo "Cleaned $cur/$max binpkgs in $XBPS_REPOSITORY" + echo "Cleaned $repos repositories in $XBPS_REPOSITORY" + echo "Done." +} diff --git a/xbps-src b/xbps-src index 77695b9f510d3..b6542d4c4f255 100755 --- a/xbps-src +++ b/xbps-src @@ -81,6 +81,9 @@ remove remove-autodeps Removes all package dependencies that were installed automatically. +purge-binpkgs + Removes all obsolete packages in /binpkgs. + purge-distfiles Removes all obsolete distfiles in /sources. @@ -860,6 +863,9 @@ case "$XBPS_TARGET" in list) $XBPS_QUERY_CMD -l ;; + purge-binpkgs) + purge_binpkgs + ;; purge-distfiles) purge_distfiles ;;