]> pd.if.org Git - zpackage/blob - zpm-gc
use getopts in gc
[zpackage] / zpm-gc
1 #!/bin/sh
2
3 # garbage collect a package database
4 # flags to skip phases
5
6 # -C clear all phases
7 # -n dry run
8 # -a all phases
9 # default is failed packages, orphaned elf, vacuum
10
11 # only affect files with the status in the list
12 status=
13 # phases
14 # -cC remove command history by date
15 remove_command_history=
16 # -dD remove dry run
17 remove_dry_run=1
18 # -eE remove orphaned elf
19 remove_orphaned_elf=1
20 # -fF remove failed
21 remove_failed=1
22 # -hH fix file content hash
23 fix_hash=0
24 # -hH remove install history by date
25 remove_history=
26 # -lL remove old logs, given by date
27 remove_logs=
28 # -nN remove acked notes
29 remove_acked_notes=0
30 # -oO remove outdated packages, unless preserved
31 remove_old_packages=0
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
43 vacuum=1
44
45 db=${ZPMDB:-/var/lib/zpm/local.db}
46
47 while getopts 'f:'; do
48         case $opt in
49                 f) db="$OPTARG" ;;
50         esac
51 done
52
53 if [ -z "$db" ]; then
54         echo must specify database file
55         exit 1
56 fi
57
58 ZPMDB=$db
59 export ZPMDB
60
61 # check for incorrect hash file content
62
63 # remove orphaned elf info
64
65 # remove failed packages
66 for pkg in $(zpm list -s failed); do
67         zpm rmpackage -m 'gc removed failed' "$pkg"
68 done
69
70 for pkg in $(zpm list -s dryrun); do
71         zpm rmpackage -m 'gc removed dryrun' "$pkg"
72 done
73
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
79         done
80 fi
81
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'"
88 fi
89
90 # remove removed packages, unless preserved
91
92 # remove orphaned rows in files table
93 # references in packagefiles, notes, scripts, possibly others
94
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);'
97 fi
98
99 # remove old logs, given by date
100
101 # remove acked notes
102 if [ $remove_acked_notes -ne 0 ]; then
103         zpm shell $ZPMDB 'delete from notes where ack != 0'
104 fi
105
106 # remove install history by date
107
108 # remove history for missing packages
109 if [ $clean_missing_package_history -ne 0 ]; then
110
111 fi
112
113 # remove command history by date
114
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)
121
122         printf 'original size %s\n' $osize
123         printf 'new size %s\n' $nsize
124 fi