]> pd.if.org Git - zpackage/blob - zpm-gc
add scripts to package hashing
[zpackage] / zpm-gc
1 #!/bin/sh
2
3 set -e
4 # garbage collect a package database
5 # flags to skip phases
6
7 # -C clear all phases
8 # -n dry run
9 # -a all phases
10 # default is failed packages, orphaned elf, vacuum
11
12 # only affect files with the status in the list
13 status=
14 # phases
15 # -cC remove command history by date
16 remove_command_history=
17 # -dD remove dry run
18 remove_dry_run=1
19 # -eE remove orphaned elf
20 remove_orphaned_elf=1
21 # -fF remove failed
22 remove_failed=1
23 # -hH fix file content hash
24 remove_incomplete_packages=0
25 fix_hash=0
26 # -hH remove install history by date
27 remove_history=
28 # -lL remove old logs, given by date
29 remove_logs=
30 # -nN remove acked notes
31 remove_acked_notes=0
32 # -pP remove history for missing packages
33 clean_missing_package_history=0
34
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
41
42 # -vV compactify the database file
43 vacuum=0
44
45 # remove orphaned rows in files table packagefiles, notes, scripts, ...
46 unreferenced_file_content=1
47
48 # remove outdated packages, unless preserved
49 remove_old_packages=1
50
51 db=${ZPMDB:-/var/lib/zpm/local.db}
52
53 retain_old=${ZPM_KEEP_PACKAGES:-2}
54
55 while getopts 'f:PcCk:' opt; do
56         case $opt in
57                 f) db="$OPTARG" ;;
58                 P) remove_old_packages=0 ;;
59                 c) vacuum=1 ;;
60                 C) unreferenced_file_content=0 ;;
61                 k) retain_old="$OPTARG" ;;
62         esac
63 done
64
65 retain_old=$((retain_old + 0))
66 if [ $retain_old -le 0 ]; then
67         retain_old=0
68 fi
69
70 if [ -z "$db" ]; then
71         echo must specify database file
72         exit 1
73 fi
74
75 ZPMDB=$db
76 export ZPMDB
77
78 # check for incorrect hash file content
79
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
82
83 select
84 EL.file, PF.path,
85 PF.package || '-' || PF.version || '-' || PF.release as pkgid
86 from elfneeded EL
87 left join files F on F.hash = EL.file
88 left join packagefiles PF on PF.hash = EL.file
89 where F.hash is null
90 and EL.file is not null
91 and PF.path is not null
92 ;
93
94
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
99 done
100
101 for pkg in $(zpm list -s dryrun); do
102         zpm rmpackage -m 'gc removed dryrun' "$pkg"
103         echo removed dryrun $pkg
104 done
105
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
112         done
113 fi
114
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
121 done
122 fi
123
124 # remove orphaned rows in files table
125 # references in packagefiles, notes, scripts, possibly others
126
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"
131         fi
132 fi
133
134 # remove old logs, given by date
135
136 # remove acked notes
137 if [ $remove_acked_notes -ne 0 ]; then
138         zpm shell $ZPMDB 'delete from notes where ack != 0'
139 fi
140
141 # remove install history by date
142
143 # remove history for missing packages
144 if [ $clean_missing_package_history -ne 0 ]; then
145         # delete from zpmlog where ?
146         true
147 fi
148
149 # remove command history by date
150
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"
157
158         printf 'original size %s\n' $osize
159         printf 'new size %s\n' $nsize
160 fi