X-Git-Url: https://pd.if.org/git/?p=zpackage;a=blobdiff_plain;f=zpm-gc;h=d2d49fc861c0dd95472395f13be317312f2b1410;hp=08ba1e19c85f73446da5472d9c4d20defca279c2;hb=09c80a1f8918d888063f4d1a75921a99fe5f0b5c;hpb=7c9e7e8d37a7d513a6815ddb2192c1dbdfb6570e diff --git a/zpm-gc b/zpm-gc index 08ba1e1..d2d49fc 100755 --- a/zpm-gc +++ b/zpm-gc @@ -1,5 +1,6 @@ #!/bin/sh +set -e # garbage collect a package database # flags to skip phases @@ -20,6 +21,7 @@ remove_orphaned_elf=1 # -fF remove failed remove_failed=1 # -hH fix file content hash +remove_incomplete_packages=0 fix_hash=0 # -hH remove install history by date remove_history= @@ -27,29 +29,44 @@ remove_history= remove_logs= # -nN remove acked notes remove_acked_notes=0 -# -oO remove outdated packages, unless preserved -remove_old_packages=0 # -pP remove history for missing packages clean_missing_package_history=0 -# -rR remove orphaned rows in files table packagefiles, notes, scripts, ... -remove_orphaned_files=1 + # -hH remove all file content that is not scripts or configs remove_packagefile_content=1 remove_script_content=1 remove_config_content=1 # -rR remove removed packages, unless preserved remove_removed_packages=0 + # -vV compactify the database file -vacuum=1 +vacuum=0 + +# remove orphaned rows in files table packagefiles, notes, scripts, ... +unreferenced_file_content=1 + +# remove outdated packages, unless preserved +remove_old_packages=1 db=${ZPMDB:-/var/lib/zpm/local.db} -while getopts 'f:'; do +retain_old=${ZPM_KEEP_PACKAGES:-2} + +while getopts 'f:PcCk:' opt; do case $opt in f) db="$OPTARG" ;; + P) remove_old_packages=0 ;; + c) vacuum=1 ;; + C) unreferenced_file_content=0 ;; + k) retain_old="$OPTARG" ;; esac done +retain_old=$((retain_old + 0)) +if [ $retain_old -le 0 ]; then + retain_old=0 +fi + if [ -z "$db" ]; then echo must specify database file exit 1 @@ -65,10 +82,12 @@ export ZPMDB # remove failed packages for pkg in $(zpm list -s failed); do zpm rmpackage -m 'gc removed failed' "$pkg" + echo removed $pkg done for pkg in $(zpm list -s dryrun); do zpm rmpackage -m 'gc removed dryrun' "$pkg" + echo removed $pkg done # remove incomplete packages @@ -76,24 +95,27 @@ done if [ $remove_incomplete_packages -ne 0 ]; then for pkg in $(zpm list -F 'hash is null'); do zpm rmpackage -m 'gc removed incomplete' $pkg + echo removed $pkg done fi # remove old version packages, unless preserved -# TODO need 'rstatus' flag +# remove removed packages, unless preserved if [ $remove_old_packages -ne 0 ]; then - # TODO check for preserved - # TODO add ability to keep a certain number of back packages - zpm shell $ZPMDB "delete from packages where status = 'updated'" + zpm shell $ZPMDB "select printf('%s-%s-%s',package,version,release) from package_age where age > $retain_old" | while read pkg; do + zpm rmpackage -m 'gc removed old package' $pkg + echo removed $pkg +done fi -# remove removed packages, unless preserved - # remove orphaned rows in files table # references in packagefiles, notes, scripts, possibly others if [ $remove_orphaned_files -ne 0 ]; then - zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);' + count=$(zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);select changes()') + if [ $count -gt 0 ]; then + zpm log -v -i -a 'gc' -t $ZPMDB "removed $count unreferenced content" + fi fi # remove old logs, given by date @@ -107,17 +129,18 @@ fi # remove history for missing packages if [ $clean_missing_package_history -ne 0 ]; then - + # delete from zpmlog where ? + true fi # remove command history by date +# compactify the database file if [ $vacuum -ne 0 ]; then - # compactify the database file osize=$(zpm stat -f '%s' $ZPMDB) - zpm log -v -i -a 'gc compacting' -t $ZPMDB zpm shell $ZPMDB vacuum nsize=$(zpm stat -f '%s' $ZPMDB) + zpm log -v -i -a 'gc compacted' -t $ZPMDB "$osize -> $nsize" printf 'original size %s\n' $osize printf 'new size %s\n' $nsize