Skip to content

Commit

Permalink
Check if there are old or uninstalled cached packages in pacman's cac…
Browse files Browse the repository at this point in the history
…he 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 #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 #70

* Fix var names

* Syntax and style changes

* Syntax and style changes

* Syntax and style changes

* Add documentation for the new 'cache checking' function
  • Loading branch information
Antiz96 authored Dec 3, 2023
1 parent bf89894 commit 18448e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion doc/man/arch-update.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
42 changes: 42 additions & 0 deletions src/script/arch-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 18448e1

Please sign in to comment.