summaryrefslogtreecommitdiff
path: root/contrib/paccache.in
blob: b95b9646df17e56ee2315f0c432f571db24ee9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
#
# pacache - flexible pacman cache cleaning
#
# Copyright (C) 2011 Dave Reisner <dreisner@archlinux.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


shopt -s extglob

declare -r myname='paccache'

declare -a candidates=() cmdopts=() whitelist=() blacklist=()
declare -i delete=0 dryrun=0 filecount=0 move=0 needsroot=0 totalsaved=0 verbose=0
declare    cachedir=@localstatedir@/cache/pacman/pkg delim=$'\n' keep=3 movedir= scanarch=

msg() {
  local mesg=$1; shift
  printf "==> $mesg\n" "$@"
} >&2

error() {
  local mesg=$1; shift
  printf "==> ERROR: $mesg\n" "$@"
} >&2

die() {
  error "$@"
  exit 1
}

# reads a list of files on stdin and prints out deletion candidates
pkgfilter() {
  # there's whitelist and blacklist parameters passed to this
  # script after the block of awk.

  awk -v keep="$1" -v scanarch="$2" '
  function parse_filename(filename,     parts, count, i, pkgname, arch) {

    count = split(filename, parts, "-")

    i = 1
    pkgname = parts[i++]
    while (i <= count - 3) {
      pkgname = pkgname "-" parts[i++]
    }

    arch = substr(parts[count], 1, index(parts[count], ".") - 1)

    # filter on whitelist or blacklist
    if (wlen && !whitelist[pkgname]) return
    if (blen && blacklist[pkgname]) return

    if ("" == packages[pkgname,arch]) {
      packages[pkgname,arch] = filename
    } else {
      packages[pkgname,arch] = packages[pkgname,arch] SUBSEP filename
    }
  }

  BEGIN {
    # create whitelist
    wlen = ARGV[1]; delete ARGV[1]
    for (i = 2; i < 2 + wlen; i++) {
      whitelist[ARGV[i]] = 1
      delete ARGV[i]
    }

    # create blacklist
    blen = ARGV[i]; delete ARGV[i]
    while (i++ < ARGC) {
      blacklist[ARGV[i]] = 1
      delete ARGV[i]
    }

    # read package filenames
    while (getline < "/dev/stdin") {
      parse_filename($0)
    }

    for (pkglist in packages) {
      # idx[1,2] = idx[pkgname,arch]
      split(pkglist, idx, SUBSEP)

      # enforce architecture match if specified
      if (!scanarch || scanarch == idx[2]) {
        count = split(packages[idx[1], idx[2]], pkgs, SUBSEP)
        for(i = 1; i <= count - keep; i++) {
          print pkgs[i]
        }
      }
    }
  }' "${@:3}"
}

size_to_human() {
  awk -v size="$1" '
  BEGIN {
    suffix[1] = "B"
    suffix[2] = "KiB"
    suffix[3] = "MiB"
    suffix[4] = "GiB"
    suffix[5] = "TiB"
    count = 1

    while (size > 1024) {
      size /= 1024
      count++
    }

    sizestr = sprintf("%.2f", size)
    sub(/\.?0+$/, "", sizestr)
    printf("%s %s", sizestr, suffix[count])
  }'
}

runcmd() {
  if (( needsroot )); then
    msg "Privilege escalation required"
    if sudo -v &>/dev/null && sudo -l &>/dev/null; then
      sudo "$@"
    else
      printf '%s ' 'root'
      su -c "$(printf '%q ' "$@")"
    fi
  else
    "$@"
  fi
}

summarize() {
  local -i filecount=$1; shift
  local seenarch= seen= arch= name=
  local -r pkg_re='(.+)-[^-]+-[0-9]+-([^.]+)\.pkg.*'

  if (( delete )); then
    printf -v output 'finished: %d packages removed' "$filecount"
  elif (( move )); then
    printf -v output "finished: %d packages moved to \`%s'" "$filecount" "$movedir"
  elif (( dryrun )); then
    if (( verbose )); then
      msg "Candidate packages:"
      while read -r pkg; do
        if (( verbose >= 3 )); then
          [[ $pkg =~ $pkg_re ]] && name=${BASH_REMATCH[1]} arch=${BASH_REMATCH[2]}
          if [[ -z $seen || $seenarch != "$arch" || $seen != "$name" ]]; then
            seen=$name seenarch=$arch
            printf '%s (%s):\n' "$name" "$arch"
          fi
          printf '  %s\n' "$pkg"
        elif (( verbose >= 2 )); then
          printf "$PWD/%s$delim" "$pkg"
        else
          printf "%s$delim" "$pkg"
        fi
      done < <(printf '%s\n' "$@" | pacsort)
    fi
    printf -v output 'finished dry run: %d candidates' "$filecount"
  fi

  printf '\n' >&2
  msg "$output (diskspace saved: %s)" "$(size_to_human "$totalsaved")"
}

usage() {
  cat <<EOF
usage: $myname <operation> [options] [targets...]

$myname is a flexible pacman cache cleaning utility, which has numerous
options to help control how much, and what, is deleted from any directory
containing pacman package tarballs.

  Operations:
    -d               perform a dry run, only finding candidate packages.
    -m  <movedir>    move candidate packages to 'movedir'.
    -r               remove candidate packages.

  Options:
    -a  <arch>       scan for 'arch' (default: all architectures).
    -c  <cachedir>   scan 'cachedir' for packages (default: @localstatedir@/cache/pacman/pkg).
    -f               apply force to mv(1) and rm(1) operations.
    -h               display this help message.
    -i  <pkgs>       ignore 'pkgs', which is a comma separated. Alternatively,
                     specify '-' to read package names from stdin, newline delimited.
    -k  <num>        keep 'num' of each package in 'cachedir' (default: 3).
    -u               target uninstalled packages.
    -v               increase verbosity. specify up to 3 times.
    -z               use null delimiters for candidate names (only with -v and -vv)

EOF
}

if (( ! UID )); then
  error "Do not run this script as root. You will be prompted for privilege escalation."
  exit 42
fi

while getopts ':a:c:dfhi:k:m:rsuvz' opt; do
  case $opt in
    a) scanarch=$OPTARG ;;
    c) cachedir=$OPTARG ;;
    d) dryrun=1 ;;
    f) cmdopts=(-f) ;;
    h) usage
      exit 0 ;;
    i) if [[ $OPTARG = '-' ]]; then
         [[ ! -t 0 ]] && IFS=$'\n' read -r -d '' -a ign
       else
         IFS=',' read -r -a ign <<< "$OPTARG"
       fi
       blacklist+=("${ign[@]}")
      unset i ign ;;
    k) keep=$OPTARG
      if [[ -z $keep || -n ${keep//[0-9]/} ]]; then
        die 'argument to option -k must be a non-negative integer'
      else
        keep=$(( 10#$keep ))
      fi ;;
    m) move=1 movedir=$OPTARG ;;
    r) delete=1 ;;
    u) IFS=$'\n' read -r -d '' -a ign < <(pacman -Qq)
       blacklist+=("${ign[@]}")
       unset ign ;;
    v) (( ++verbose )) ;;
    z) delim='\0' ;;
    :) die "option '--%s' requires an argument" "$OPTARG" ;;
    ?) die "invalid option -- '%s'" "$OPTARG" ;;
  esac
done
shift $(( OPTIND - 1 ))

# remaining args are a whitelist
whitelist=("$@")

# sanity checks
case $(( dryrun+delete+move )) in
  0) die "no operation specified (use -h for help)" ;;
  [^1]) die "only one operation may be used at a time" ;;
esac

[[ -d $cachedir ]] ||
  die "cachedir \`%s' does not exist or is not a directory" "$cachedir"

[[ $movedir && ! -d $movedir ]] &&
  die "move-to directory \`%s' does not exist or is not a directory" "$movedir"

if (( move || delete )); then
  # make it an absolute path since we're about to chdir
  [[ ${movedir:0:1} != '/' ]] && movedir=$PWD/$movedir
  [[ ! -w $cachedir || ( $movedir && ! -w $movedir ) ]] && needsroot=1
fi

# unlikely that this will fail, but better make sure
cd "$cachedir" >/dev/null || die "failed to chdir to \`%s'" "$cachedir"

# note that these results are returned in an arbitrary order from awk, but
# they'll be resorted (in summarize) iff we have a verbosity level set.
IFS=$'\n' read -r -d '' -a candidates < \
  <(printf '%s\n' *.pkg.tar?(.+([^.])) | pacsort |
    pkgfilter "$keep" "$scanarch" \
      "${#whitelist[*]}" "${whitelist[@]}" \
      "${#blacklist[*]}" "${blacklist[@]}")

if (( ! ${#candidates[*]} )); then
  msg 'no candidate packages found for pruning'
  exit 1
fi

# grab this prior to signature scavenging
pkgcount=${#candidates[*]}

# copy the list, merging in any found sigs
for cand in "${candidates[@]}"; do
  candtemp+=("$cand")
  [[ -f $cand.sig ]] && candtemp+=("$cand.sig")
done
candidates=("${candtemp[@]}")
unset candtemp

# do this before we destroy anything
totalsaved=$(@SIZECMD@ "${candidates[@]}" | awk '{ sum += $1 } END { print sum }')

# crush. kill. destroy.
(( verbose )) && cmdopts+=(-v)
if (( delete )); then
  runcmd rm "${cmdopts[@]}" "${candidates[@]}"
elif (( move )); then
  runcmd mv "${cmdopts[@]}" "${candidates[@]}" "$movedir"
fi

summarize "$pkgcount" "${candidates[@]}"

# vim: set ts=2 sw=2 noet: