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 # removes old packages
81 zpm shell local.db.rehash "select package||'-'||version||'-'||release from package_age where age > 2 and status = 'updated'"|xargs -n1 zpm rmpackage -S installed -f local.db.rehash
85 PF.package || '-' || PF.version || '-' || PF.release as pkgid
87 left join files F on F.hash = EL.file
88 left join packagefiles PF on PF.hash = EL.file
90 and EL.file is not null
91 and PF.path is not null
95 # remove failed packages
96 for pkg in $(zpm list -s failed); do
97 zpm rmpackage -m 'gc removed failed' "$pkg"
98 echo removed failed $pkg
101 for pkg in $(zpm list -s dryrun); do
102 zpm rmpackage -m 'gc removed dryrun' "$pkg"
103 echo removed dryrun $pkg
106 # remove incomplete packages
107 # TODO use temporary gc delete log trigger?
108 if [ $remove_incomplete_packages -ne 0 ]; then
109 for pkg in $(zpm list -F 'hash is null'); do
110 zpm rmpackage -m 'gc removed incomplete' $pkg
111 echo removed incomplete $pkg
115 # remove old version packages, unless preserved
116 # remove removed packages, unless preserved
117 if [ $remove_old_packages -ne 0 ]; then
118 zpm shell $ZPMDB "select printf('%s-%s-%s',package,version,release) from package_age where age > $retain_old" | while read pkg; do
119 zpm rmpackage -m 'gc removed old package' $pkg
120 echo removed old package $pkg
124 # remove orphaned rows in files table
125 # references in packagefiles, notes, scripts, possibly others
127 if [ $remove_orphaned_files -ne 0 ]; then
128 count=$(zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);select changes()')
129 if [ $count -gt 0 ]; then
130 zpm log -v -i -a 'gc' -t $ZPMDB "removed $count unreferenced content"
134 # remove old logs, given by date
137 if [ $remove_acked_notes -ne 0 ]; then
138 zpm shell $ZPMDB 'delete from notes where ack != 0'
141 # remove install history by date
143 # remove history for missing packages
144 if [ $clean_missing_package_history -ne 0 ]; then
145 # delete from zpmlog where ?
149 # remove command history by date
151 # compactify the database file
152 if [ $vacuum -ne 0 ]; then
153 osize=$(zpm stat -f '%s' $ZPMDB)
154 zpm shell $ZPMDB vacuum
155 nsize=$(zpm stat -f '%s' $ZPMDB)
156 zpm log -v -i -a 'gc compacted' -t $ZPMDB "$osize -> $nsize"
158 printf 'original size %s\n' $osize
159 printf 'new size %s\n' $nsize