3 # pacache - flexible pacman cache cleaning
5 # Copyright (C) 2011 Dave Reisner <dreisner@archlinux.org>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 declare -r myname
='paccache'
24 declare -r myver
='@PACKAGE_VERSION@'
26 declare -a candidates
=() cmdopts
=() whitelist
=() blacklist
=()
27 declare -i delete
=0 dryrun
=0 filecount
=0 move
=0 needsroot
=0 totalsaved
=0 verbose
=0
28 declare cachedir
=@localstatedir@
/cache
/pacman
/pkg delim
=$
'\n' keep
=3 movedir
= scanarch
=
32 printf "==> $mesg\n" "$@"
37 printf "==> ERROR: $mesg\n" "$@"
45 # reads a list of files on stdin and prints out deletion candidates
47 # there's whitelist and blacklist parameters passed to this
48 # script after the block of awk.
50 awk -v keep
="$1" -v scanarch
="$2" '
51 function parse_filename(filename, parts, count, i, pkgname, arch) {
53 count = split(filename, parts, "-")
57 while (i <= count - 3) {
58 pkgname = pkgname "-" parts[i++]
61 arch = substr(parts[count], 1, index(parts[count], ".") - 1)
63 # filter on whitelist or blacklist
64 if (wlen && !whitelist[pkgname]) return
65 if (blen && blacklist[pkgname]) return
67 if ("" == packages[pkgname,arch]) {
68 packages[pkgname,arch] = filename
70 packages[pkgname,arch] = packages[pkgname,arch] SUBSEP filename
76 wlen = ARGV[1]; delete ARGV[1]
77 for (i = 2; i < 2 + wlen; i++) {
78 whitelist[ARGV[i]] = 1
83 blen = ARGV[i]; delete ARGV[i]
85 blacklist[ARGV[i]] = 1
89 # read package filenames
90 while (getline < "/dev/stdin") {
94 for (pkglist in packages) {
95 # idx[1,2] = idx[pkgname,arch]
96 split(pkglist, idx, SUBSEP)
98 # enforce architecture match if specified
99 if (!scanarch || scanarch == idx[2]) {
100 count = split(packages[idx[1], idx[2]], pkgs, SUBSEP)
101 for(i = 1; i <= count - keep; i++) {
119 while (size > 1024) {
124 sizestr = sprintf("%.2f", size)
125 sub(/\.?0+$/, "", sizestr)
126 printf("%s %s", sizestr, suffix[count])
131 if (( needsroot
)); then
132 msg
"Privilege escalation required"
133 if sudo
-v &>/dev
/null
&& sudo
-l &>/dev
/null
; then
137 su
-c "$(printf '%q ' "$@
")"
145 local -i filecount
=$1; shift
146 local seenarch
= seen
= arch
= name
=
147 local -r pkg_re
='(.+)-[^-]+-[0-9]+-([^.]+)\.pkg.*'
149 if (( delete
)); then
150 printf -v output
'finished: %d packages removed' "$filecount"
151 elif (( move
)); then
152 printf -v output
"finished: %d packages moved to \`%s'" "$filecount" "$movedir"
153 elif (( dryrun
)); then
154 if (( verbose
)); then
155 msg
"Candidate packages:"
156 while read -r pkg
; do
157 if (( verbose
>= 3 )); then
158 [[ $pkg =~
$pkg_re ]] && name
=${BASH_REMATCH[1]} arch
=${BASH_REMATCH[2]}
159 if [[ -z $seen ||
$seenarch != "$arch" ||
$seen != "$name" ]]; then
160 seen
=$name seenarch
=$arch
161 printf '%s (%s):\n' "$name" "$arch"
163 printf ' %s\n' "$pkg"
164 elif (( verbose
>= 2 )); then
165 printf "$PWD/%s$delim" "$pkg"
167 printf "%s$delim" "$pkg"
169 done < <(printf '%s\n' "$@" | pacsort
)
171 printf -v output
'finished dry run: %d candidates' "$filecount"
175 msg
"$output (diskspace saved: %s)" "$(size_to_human "$totalsaved")"
180 usage: $myname <operation> [options] [targets...]
182 $myname is a flexible pacman cache cleaning utility, which has numerous
183 options to help control how much, and what, is deleted from any directory
184 containing pacman package tarballs.
187 -d perform a dry run, only finding candidate packages.
188 -m <movedir> move candidate packages to 'movedir'.
189 -r remove candidate packages.
192 -a <arch> scan for 'arch' (default: all architectures).
193 -c <cachedir> scan 'cachedir' for packages (default: @localstatedir@/cache/pacman/pkg).
194 -f apply force to mv(1) and rm(1) operations.
195 -h display this help message.
196 -i <pkgs> ignore 'pkgs', which is a comma separated. Alternatively,
197 specify '-' to read package names from stdin, newline delimited.
198 -k <num> keep 'num' of each package in 'cachedir' (default: 3).
199 -u target uninstalled packages.
200 -v increase verbosity. specify up to 3 times.
201 -z use null delimiters for candidate names (only with -v and -vv)
207 printf "%s %s\n" "$myname" "$myver"
208 echo 'Copyright (C) 2011 Dave Reisner <dreisner@archlinux.org>'
212 error
"Do not run this script as root. You will be prompted for privilege escalation."
216 # TODO: remove this workaround and use a sane command line parser (like the
217 # parse_options library from scripts/) here
218 if [[ $1 = -@
(h|
-help) ]]; then
221 elif [[ $1 = -@
(V|
-version) ]]; then
226 while getopts ':a:c:dfi:k:m:rsuvz' opt
; do
228 a
) scanarch
=$OPTARG ;;
229 c
) cachedir
=$OPTARG ;;
232 i
) if [[ $OPTARG = '-' ]]; then
233 [[ ! -t 0 ]] && IFS
=$
'\n' read -r -d '' -a ign
235 IFS
=',' read -r -a ign
<<< "$OPTARG"
237 blacklist
+=("${ign[@]}")
240 if [[ -z $keep ||
-n ${keep//[0-9]/} ]]; then
241 die
'argument to option -k must be a non-negative integer'
245 m
) move
=1 movedir
=$OPTARG ;;
247 u
) IFS
=$
'\n' read -r -d '' -a ign
< <(pacman
-Qq)
248 blacklist
+=("${ign[@]}")
250 v
) (( ++verbose
)) ;;
252 :) die
"option '--%s' requires an argument" "$OPTARG" ;;
253 ?
) die
"invalid option -- '%s'" "$OPTARG" ;;
256 shift $
(( OPTIND
- 1 ))
258 # remaining args are a whitelist
262 case $
(( dryrun
+delete
+move
)) in
263 0) die
"no operation specified (use -h for help)" ;;
264 [^
1]) die
"only one operation may be used at a time" ;;
267 [[ -d $cachedir ]] ||
268 die
"cachedir \`%s' does not exist or is not a directory" "$cachedir"
270 [[ $movedir && ! -d $movedir ]] &&
271 die
"move-to directory \`%s' does not exist or is not a directory" "$movedir"
273 if (( move || delete
)); then
274 # make it an absolute path since we're about to chdir
275 [[ ${movedir:0:1} != '/' ]] && movedir
=$PWD/$movedir
276 [[ ! -w $cachedir ||
( $movedir && ! -w $movedir ) ]] && needsroot
=1
279 # unlikely that this will fail, but better make sure
280 cd "$cachedir" >/dev
/null || die
"failed to chdir to \`%s'" "$cachedir"
282 # note that these results are returned in an arbitrary order from awk, but
283 # they'll be resorted (in summarize) iff we have a verbosity level set.
284 IFS
=$
'\n' read -r -d '' -a candidates
< \
285 <(printf '%s\n' *.pkg.
tar?
(.
+([^.
])) | pacsort |
286 pkgfilter
"$keep" "$scanarch" \
287 "${#whitelist[*]}" "${whitelist[@]}" \
288 "${#blacklist[*]}" "${blacklist[@]}")
290 if (( ! ${#candidates[*]} )); then
291 msg
'no candidate packages found for pruning'
295 # grab this prior to signature scavenging
296 pkgcount
=${#candidates[*]}
298 # copy the list, merging in any found sigs
299 for cand
in "${candidates[@]}"; do
301 [[ -f $cand.sig
]] && candtemp
+=("$cand.sig")
303 candidates
=("${candtemp[@]}")
306 # do this before we destroy anything
307 totalsaved
=$
(@SIZECMD@
"${candidates[@]}" |
awk '{ sum += $1 } END { print sum }')
309 # crush. kill. destroy.
310 (( verbose
)) && cmdopts
+=(-v)
311 if (( delete
)); then
312 runcmd
rm "${cmdopts[@]}" "${candidates[@]}"
313 elif (( move
)); then
314 runcmd
mv "${cmdopts[@]}" "${candidates[@]}" "$movedir"
317 summarize
"$pkgcount" "${candidates[@]}"
319 # vim: set ts=2 sw=2 noet: