From 18448e1255f988f5b111a7f3deb84a350eb82a73 Mon Sep 17 00:00:00 2001 From: Robin Candau <53110319+Antiz96@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:32:10 +0100 Subject: [PATCH] Check if there are old or uninstalled cached packages in pacman's cache and offers to remove them if there are (#71) * Check if there are old or uninstalled cached packages in pacman's cache and offers to remove them if there are As suggested in https://github.com/Antiz96/arch-update/issues/70, this commit aims to add function to the script to check for old or uninstalled packages in pacman's cache and offers to remove them if there are. The check and removal are based on paccache (from the [pacman-contrib package](https://archlinux.org/packages/extra/x86_64/pacman-contrib/)). The default behavior is to keep the last 3 cached versions of installed packages and remove every cached versions of uninstalled packages. Closes https://github.com/Antiz96/arch-update/issues/70 * Fix var names * Syntax and style changes * Syntax and style changes * Syntax and style changes * Add documentation for the new 'cache checking' function --- README.md | 8 +++++++- doc/man/arch-update.1 | 3 ++- src/script/arch-update.sh | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5eb2dc1..e158774 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Features: - Automatic check and listing of every packages available for update (through [checkupdates](https://archlinux.org/packages/extra/x86_64/pacman-contrib/ "pacman-contrib package")). - Offers to print the latest Arch Linux news before applying updates (through [curl](https://archlinux.org/packages/core/x86_64/curl/ "curl package") and [htmlq](https://archlinux.org/packages/extra/x86_64/htmlq/ "htmlq package")). - Automatic check and listing of orphan packages and offering you to remove them. +- Automatic check for old and/or uninstalled cached packages in `pacman`'s cache and offering you to remove them (through [paccache](https://archlinux.org/packages/extra/x86_64/pacman-contrib/ "pacman-contrib package")). - Helps you processing pacnew/pacsave files (through [pacdiff](https://archlinux.org/packages/extra/x86_64/pacman-contrib/ "pacman-contrib package"), optionally requires [vim](https://archlinux.org/packages/extra/x86_64/vim/ "vim package") as the default [merge program](https://wiki.archlinux.org/title/Pacman/Pacnew_and_Pacsave#pacdiff "pacdiff merge program")). - Automatic check for pending kernel updates requiring a reboot to be applied and offers to do so if there's one. - Support for both [sudo](https://archlinux.org/packages/core/x86_64/sudo/ "sudo package") and [doas](https://archlinux.org/packages/extra/x86_64/opendoas/ "opendoas package"). @@ -120,10 +121,15 @@ When the update is over, the icon changes accordingly: ![top_bar_up_to_date](https://github.com/Antiz96/arch-update/assets/53110319/794696a0-3452-4afd-8d64-a41d64225082) -`arch-update` will also search for orphan packages/unused Flatpak packages and offers to remove them (if there are): +`arch-update` will then search for orphan packages/unused Flatpak packages and offers to remove them (if there are): ![remove_orphan](https://github.com/Antiz96/arch-update/assets/53110319/4abf2623-ba27-4c42-8289-884199bfb579) +`arch-update` will also search for old and/or uninstalled cached packages and offers to remove them (if there are): +*The default behavior is to keep the last 3 cached versions of installed packages and remove every cached versions of uninstalled packages* + +![remove_cached_packages](https://github.com/Antiz96/arch-update/assets/53110319/c920488c-d891-44c3-b1a7-3dcc93867b84) + Additionally `arch-update` will search for pacnew/pacsave files and offers to process them via `pacdiff` (if there are): ![process_pacnew](https://github.com/Antiz96/arch-update/assets/53110319/6f3430f7-fc28-48fa-b107-230f3f32ac5b) diff --git a/doc/man/arch-update.1 b/doc/man/arch-update.1 index 2263868..a613283 100755 --- a/doc/man/arch-update.1 +++ b/doc/man/arch-update.1 @@ -20,7 +20,7 @@ An update notifier/applier for Arch Linux that assists you with important pre/po .br .RB "Before performing the update, it offers to print the latest Arch Linux news to the user. Arch news that have been published within the last 15 days are tagged as '[NEW]'." .br -.RB "It also checks for orphan packages, unused Flatpak packages, pacnew/pacsave files and pending kernel update requiring a reboot to be applied and, if there are, offers to process them." +.RB "It also checks for orphan packages, unused Flatpak packages, old and/or uninstalled cached packages in pacman's cache, pacnew/pacsave files and pending kernel update requiring a reboot to be applied and, if there are, offers to process them." .br .RB "Those functions are launched when you click on the (.desktop) icon." .PP @@ -142,6 +142,7 @@ sudo sed -i "s/packages=$(checkupdates)/packages=$(checkupdates | awk '{print $1 .BR curl (1), .BR pacman (8), .BR pacdiff (8), +.BR paccache (8), .BR systemd.service (5), .BR systemd.timer (5), .BR yay (8), diff --git a/src/script/arch-update.sh b/src/script/arch-update.sh index f5bad3a..21200c9 100755 --- a/src/script/arch-update.sh +++ b/src/script/arch-update.sh @@ -301,6 +301,47 @@ orphan_packages() { fi } +# Definition of the packages_cache function: Search for old package archives in the pacman cache and offer to remove them if there are +packages_cache() { + pacman_cache_old=$(paccache -dk3 | sed -n 's/.*: \([0-9]*\) candidate.*/\1/p') + pacman_cache_uninstalled=$(paccache -duk0 | sed -n 's/.*: \([0-9]*\) candidate.*/\1/p') + + [ -z "${pacman_cache_old}" ] && pacman_cache_old="0" + [ -z "${pacman_cache_uninstalled}" ] && pacman_cache_uninstalled="0" + pacman_cache_total=$(("${pacman_cache_old}+${pacman_cache_uninstalled}")) + + if [ "${pacman_cache_total}" -gt 0 ]; then + echo "--Cached Packages--" + + if [ "${pacman_cache_total}" -eq 1 ]; then + echo -e "There's an old or uninstalled cached package\n" + read -rp $'Would you like to remove it from the cache now? [Y/n] ' answer + else + echo -e "There are old and/or uninstalled cached packages\n" + read -rp $'Would you like to remove them from the cache now? [Y/n] ' answer + fi + + case "${answer}" in + [Yy]|"") + echo -e "\n--Removing Cached Packages--" + + if [ "${pacman_cache_old}" -gt 0 ] && [ "${pacman_cache_uninstalled}" -eq 0 ]; then + echo -e "\nRemoving old cached packages..." && "${su_cmd}" paccache -r && echo -e "\nThe removal has been applied\n" || echo -e >&2 "\nAn error has occurred\nThe removal has been aborted\n" + elif [ "${pacman_cache_old}" -eq 0 ] && [ "${pacman_cache_uninstalled}" -gt 0 ]; then + echo -e "\nRemoving uninstalled cached packages..." && "${su_cmd}" paccache -ruk0 && echo -e "\nThe removal has been applied\n" || echo -e >&2 "\nAn error has occurred\nThe removal has been aborted\n" + elif [ "${pacman_cache_old}" -gt 0 ] && [ "${pacman_cache_uninstalled}" -gt 0 ]; then + echo -e "\nRemoving old cached packages..." && "${su_cmd}" paccache -r && echo -e "\nRemoving uninstalled cached packages..." && "${su_cmd}" paccache -ruk0 && echo -e "\nThe removal has been applied\n" || echo -e >&2 "\nAn error has occurred\nThe removal has been aborted\n" + fi + ;; + *) + echo -e "The removal hasn't been applied\n" + ;; + esac + else + echo -e "No old or uninstalled cached package found\n" + fi +} + # Definition of the pacnew_files function: Print pacnew files and offer to process them if there are pacnew_files() { pacnew_files=$(pacdiff -o) @@ -366,6 +407,7 @@ case "${option}" in update fi orphan_packages + packages_cache pacnew_files kernel_reboot read -n 1 -r -s -p $'Press \"enter\" to quit\n'