]> pd.if.org Git - zpackage/blob - zpm-gc
ignore non-empty directories at unlink
[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=${1:-${ZPMDB:-/var/lib/zpm/local.db}}
46
47 if [ -z "$db" ]; then
48         echo must specify database file
49         exit 1
50 fi
51
52 ZPMDB=$db
53 export ZPMDB
54
55 # check for incorrect hash file content
56
57 # remove orphaned elf info
58
59 # remove failed packages
60 for pkg in $(zpm list -s failed); do
61         zpm log -i -a 'gc removing' -t "$pkg"
62         zpm rmpackage "$pkg"
63 done
64
65 for pkg in $(zpm list -s dryrun); do
66         zpm log -i -a 'gc removing' -t "$pkg"
67         zpm rmpackage "$pkg"
68 done
69
70 # remove incomplete packages
71 # TODO use temporary gc delete log trigger?
72 if [ $remove_incomplete_packages -ne 0 ]; then
73         zpm shell $ZPMDB "delete from packages where hash is null"
74 fi
75
76 # remove old version packages, unless preserved
77 # TODO need 'rstatus' flag
78 if [ $remove_old_packages -ne 0 ]; then
79         # TODO check for preserved
80         # TODO add ability to keep a certain number of back packages
81         zpm shell $ZPMDB "delete from packages where status = 'updated'"
82 fi
83
84 # remove removed packages, unless preserved
85
86 # remove orphaned rows in files table
87 # references in packagefiles, notes, scripts, possibly others
88
89 if [ $remove_orphaned_files -ne 0 ]; then
90         zpm shell $ZPMDB 'delete from files where hash in (select hash from filerefs where refcount = 0);'
91 fi
92
93 # remove old logs, given by date
94
95 # remove acked notes
96 if [ $remove_acked_notes -ne 0 ]; then
97         zpm shell $ZPMDB 'delete from notes where ack != 0'
98 fi
99
100 # remove install history by date
101
102 # remove history for missing packages
103 if [ $clean_missing_package_history -ne 0 ]; then
104
105 fi
106
107 # remove command history by date
108
109 if [ $vacuum -ne 0 ]; then
110         # compactify the database file
111         osize=$(zpm stat -f '%s' $ZPMDB)
112         zpm log -v -i -a 'gc compacting' -t $ZPMDB
113         zpm shell $ZPMDB vacuum
114         nsize=$(zpm stat -f '%s' $ZPMDB)
115
116         printf 'original size %s\n' $osize
117         printf 'new size %s\n' $nsize
118 fi