Last active
May 24, 2017 08:12
-
-
Save masato9000/8ae4517b1544ea543580d33f9fff0575 to your computer and use it in GitHub Desktop.
dirty and (probably) inelegant script to clean an xbps cache more selectively than xbps-remove -O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Use: xcleancache [-n] <function> | |
# -n: dry run (don't delete any files) | |
# Available functions: | |
# notinstalled: Remove all cached files for non-installed packages | |
# pruneme <x> : Remove all but the latest (x) obsolete versions from | |
# the cache for each package cached. | |
# By default, cache operated upon is at /var/cache/xbps, but this can be | |
# changed by passing the cachedir variable to this script. | |
notinstalled() { | |
_lastpkg= | |
_delpkg= | |
for _file in $cachedir/*.$arch.xbps; do | |
_pkg=${_file##*/} | |
_pkg=${_pkg%-*} | |
if [ ${_pkg}X = ${_lastpkg}X ]; then | |
if [ ${_pkg}X = ${_delpkg}X ]; then | |
echo rm -f $_file\[.sig\] | |
if [ -z "$dryrun" ]; then | |
rm -f $_file | |
rm -f $_file.sig | |
fi | |
fi | |
elif [ -z "$(xbps-query $_pkg -p state)" ]; then | |
_delpkg=$_pkg | |
echo rm -f $_file\[.sig\] | |
if [ -z "$dryrun" ]; then | |
rm -f $_file | |
rm -f $_file.sig | |
fi | |
fi | |
_lastpkg=$_pkg | |
done | |
[ -n "$dryrun" ] && echo Dry run: No files have been deleted | |
exit | |
} | |
pruneme() { | |
_keep=$1 | |
case $_keep in | |
''|0|*[!0-9]*) | |
echo "How many old versions do you want to keep? (should be at least 1)" >&2 | |
exit 1 | |
;; | |
esac | |
_lastpkg= | |
for _file in $cachedir/*.$arch.xbps; do | |
_file=${_file##*/} | |
_file=${_file%.*.*} | |
_name=${_file%-*} | |
_ver=${_file##*-} | |
if [ "$_name" != "$_lastpkg" ]; then | |
printf "\n%s %s" "$_name" "$_ver" | |
else | |
printf " %s" "$_ver" | |
fi | |
_lastpkg=$_name | |
done | while read _pkg _versions; do | |
_delver= | |
set -- $_versions | |
_vercount=$# | |
while [ $(($_vercount-1)) -gt $_keep ]; do | |
_vercount=$(($_vercount-1)) | |
_minver= | |
# versions do not always list in order, so we must test them... | |
for _ver in $_versions; do | |
if [ -z "$_minver" ]; then | |
_minver=$_ver | |
continue | |
fi | |
xbps-uhelper cmpver "$_minver" "$_ver" | |
[ $? -eq 1 ] && _minver=$_ver | |
done | |
#echo rm $cachedir/$_pkg-$_minver.$arch.xbps\[.sig\] | |
if [ -z "$dryrun" ]; then | |
rm -f "$cachedir/$_pkg-$_minver.$arch.xbps" | |
rm -f "$cachedir/$_pkg-$_minver.$arch.xbps.sig" | |
fi | |
# ...and extract them from the list | |
_versions=" $_versions " | |
_versions="${_versions% $_minver *} ${_versions#* $_minver }" | |
_delver="${_delver:+$_delver }$_minver" | |
done | |
if [ -n "$(echo $_delver)" ]; then | |
_keepver="$(echo $_versions)" | |
printf "%s\t\n" "$_pkg" | |
printf "\tkept: %s\n" "$_keepver" | |
printf "\tdeleted: %s\n" "$_delver" | |
fi | |
done | |
[ -n "$dryrun" ] && echo Dry run: No files have been deleted | |
exit | |
} | |
cachedir="${cachedir:-/var/cache/xbps}" | |
arch=$(xbps-uhelper arch) | |
dryrun= | |
if [ "$1"X = -nX ]; then | |
dryrun=y | |
shift | |
fi | |
"$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment