4 # garbage collect a package database
10 # default is failed packages, orphaned elf, vacuum
12 # only affect files with the status in the list
15 # -cC remove command history by date
16 remove_command_history=
19 # -eE remove orphaned elf
23 # -hH fix file content hash
24 remove_incomplete_packages=0
26 # -hH remove install history by date
28 # -lL remove old logs, given by date
30 # -nN remove acked notes
32 # -pP remove history for missing packages
33 clean_missing_package_history=0
35 # -hH remove all file content that is not scripts or configs
36 remove_packagefile_content=1
37 remove_script_content=1
38 remove_config_content=1
39 # -rR remove removed packages, unless preserved
40 remove_removed_packages=0
42 # -vV compactify the database file
45 # remove orphaned rows in files table packagefiles, notes, scripts, ...
46 unreferenced_file_content=1
48 # remove outdated packages, unless preserved
51 db=${ZPMDB:-/var/lib/zpm/local.db}
53 retain_old=${ZPM_KEEP_PACKAGES:-2}
55 while getopts 'f:PcCk:' opt; do
58 P) remove_old_packages=0 ;;
60 C) unreferenced_file_content=0 ;;
61 k) retain_old="$OPTARG" ;;
65 retain_old=$((retain_old + 0))
66 if [ $retain_old -le 0 ]; then
71 echo must specify database file
78 # check for incorrect hash file content
80 # remove orphaned elf info
82 # remove failed packages
83 for pkg in $(zpm list -s failed); do
84 zpm rmpackage -m 'gc removed failed' "$pkg"
88 for pkg in $(zpm list -s dryrun); do
89 zpm rmpackage -m 'gc removed dryrun' "$pkg"
93 # remove incomplete packages
94 # TODO use temporary gc delete log trigger?
95 if [ $remove_incomplete_packages -ne 0 ]; then
96 for pkg in $(zpm list -F 'hash is null'); do
97 zpm rmpackage -m 'gc removed incomplete' $pkg
102 # remove old version packages, unless preserved
103 # remove removed packages, unless preserved
104 if [ $remove_old_packages -ne 0 ]; then
105 zpm shell $ZPMDB "select printf('%s-%s-%s',package,version,release) from package_age where age > $retain_old" | while read pkg; do
106 zpm rmpackage -m 'gc removed old package' $pkg
111 # remove orphaned rows in files table
112 # references in packagefiles, notes, scripts, possibly others
114 if [ $remove_orphaned_files -ne 0 ]; then
115 count=$(zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);select changes()')
116 if [ $count -gt 0 ]; then
117 zpm log -v -i -a 'gc' -t $ZPMDB "removed $count unreferenced content"
121 # remove old logs, given by date
124 if [ $remove_acked_notes -ne 0 ]; then
125 zpm shell $ZPMDB 'delete from notes where ack != 0'
128 # remove install history by date
130 # remove history for missing packages
131 if [ $clean_missing_package_history -ne 0 ]; then
132 # delete from zpmlog where ?
136 # remove command history by date
138 # compactify the database file
139 if [ $vacuum -ne 0 ]; then
140 osize=$(zpm stat -f '%s' $ZPMDB)
141 zpm shell $ZPMDB vacuum
142 nsize=$(zpm stat -f '%s' $ZPMDB)
143 zpm log -v -i -a 'gc compacted' -t $ZPMDB "$osize -> $nsize"
145 printf 'original size %s\n' $osize
146 printf 'new size %s\n' $nsize