3 # garbage collect a package database
9 # default is failed packages, orphaned elf, vacuum
11 # only affect files with the status in the list
14 # -cC remove command history by date
15 remove_command_history=
18 # -eE remove orphaned elf
22 # -hH fix file content hash
24 # -hH remove install history by date
26 # -lL remove old logs, given by date
28 # -nN remove acked notes
30 # -oO remove outdated packages, unless preserved
32 # -pP remove history for missing packages
33 clean_missing_package_history=0
34 # -rR remove orphaned rows in files table packagefiles, notes, scripts, ...
35 remove_orphaned_files=1
36 # -hH remove all file content that is not scripts or configs
37 remove_packagefile_content=1
38 remove_script_content=1
39 remove_config_content=1
40 # -rR remove removed packages, unless preserved
41 remove_removed_packages=0
42 # -vV compactify the database file
45 db=${ZPMDB:-/var/lib/zpm/local.db}
47 while getopts 'f:'; do
54 echo must specify database file
61 # check for incorrect hash file content
63 # remove orphaned elf info
65 # remove failed packages
66 for pkg in $(zpm list -s failed); do
67 zpm rmpackage -m 'gc removed failed' "$pkg"
70 for pkg in $(zpm list -s dryrun); do
71 zpm rmpackage -m 'gc removed dryrun' "$pkg"
74 # remove incomplete packages
75 # TODO use temporary gc delete log trigger?
76 if [ $remove_incomplete_packages -ne 0 ]; then
77 for pkg in $(zpm list -F 'hash is null'); do
78 zpm rmpackage -m 'gc removed incomplete' $pkg
82 # remove old version packages, unless preserved
83 # TODO need 'rstatus' flag
84 if [ $remove_old_packages -ne 0 ]; then
85 # TODO check for preserved
86 # TODO add ability to keep a certain number of back packages
87 zpm shell $ZPMDB "delete from packages where status = 'updated'"
90 # remove removed packages, unless preserved
92 # remove orphaned rows in files table
93 # references in packagefiles, notes, scripts, possibly others
95 if [ $remove_orphaned_files -ne 0 ]; then
96 zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);'
99 # remove old logs, given by date
102 if [ $remove_acked_notes -ne 0 ]; then
103 zpm shell $ZPMDB 'delete from notes where ack != 0'
106 # remove install history by date
108 # remove history for missing packages
109 if [ $clean_missing_package_history -ne 0 ]; then
113 # remove command history by date
115 if [ $vacuum -ne 0 ]; then
116 # compactify the database file
117 osize=$(zpm stat -f '%s' $ZPMDB)
118 zpm log -v -i -a 'gc compacting' -t $ZPMDB
119 zpm shell $ZPMDB vacuum
120 nsize=$(zpm stat -f '%s' $ZPMDB)
122 printf 'original size %s\n' $osize
123 printf 'new size %s\n' $nsize