From patchwork Fri Sep 14 22:58:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wisp3rwind X-Patchwork-Id: 776 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 761EC6E59948 for ; Fri, 14 Sep 2018 22:58:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on apollo X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00=-1,DKIM_SIGNED=0.1, MAILING_LIST_MULTI=-1,RCVD_IN_DNSWL_MED=-2.3,T_DKIM_INVALID=1 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Fri, 14 Sep 2018 22:58:55 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id EF0A4D6360A10; Fri, 14 Sep 2018 22:58:50 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [5.9.250.164]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by orion.archlinux.org (Postfix) with ESMTPS; Fri, 14 Sep 2018 22:58:50 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id D9C002CD5F; Fri, 14 Sep 2018 22:58:50 +0000 (UTC) Authentication-Results: luna.archlinux.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=posteo.eu header.i=@posteo.eu header.b=FIxS7cSF Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 7678E2CD5F for ; Fri, 14 Sep 2018 22:58:48 +0000 (UTC) Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by luna.archlinux.org (Postfix) with ESMTPS for ; Fri, 14 Sep 2018 22:58:48 +0000 (UTC) Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id D8D3120E6B for ; Sat, 15 Sep 2018 00:58:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.eu; s=2017; t=1536965927; bh=+jB8Qlet581GSRdp3fh1FcU/8Tqa0RG1WHEuYN5BLYI=; h=From:To:Cc:Subject:Date:From; b=FIxS7cSFlNP93MTDpK9Fn+o2j7ftTawWrqyTwbgqKwxFpm0V0iAltbcHZTC8Lkl/p 80xkIXwGUFVxMUCOpH+6Kd8Simhry2J0Tfi9NOI1GoB3xlhyRQaNtUmeoIffJX5tU4 r1PJHgCsfyrlnaCmUInC+q7yIADNo5g0jvyKpXc/pmvJErummeFhB6YlZYnm6JMHsX DLeMyYdrozvRxPmiAjeYYkcI+lcVtATew8VcfxsCE3ZWfHMdvFCx93lnla40Z/Q6CK eJeTHf/zH5NOBxZGhc2yppvbOVCgUNxQnmIbHXeZcQJ6CeO6+At1//p/ti78gYdybN a4vdum+Jbi3lw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 42BrY30Trfz9rxF; Sat, 15 Sep 2018 00:58:46 +0200 (CEST) From: wisp3rwind To: pacman-contrib@lists.archlinux.org Subject: [PATCH] paccache: add --age-atime and --age-mtime options Date: Sat, 15 Sep 2018 00:58:26 +0200 Message-Id: <20180914225826.4895-1-wisp3rwind@posteo.eu> X-Mailer: git-send-email 2.19.0 MIME-Version: 1.0 X-BeenThere: pacman-contrib@lists.archlinux.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for pacman-contrib development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: pacman-contrib-bounces@lists.archlinux.org Sender: "pacman-contrib" --- I would feel a lot more confident about using the paccache systemd service if it kept packages based on age instead of just the few most recent. This patch adds the functionality to skip candidates that are not older (in terms of atime or mtime) than some specified age. It seems to work, but I'm not exactly a bash expert, so please review with care. I'd appreciate if this could be merged! doc/paccache.8.txt | 5 ++++ src/paccache.sh.in | 58 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/doc/paccache.8.txt b/doc/paccache.8.txt index db81283..c9e3807 100644 --- a/doc/paccache.8.txt +++ b/doc/paccache.8.txt @@ -38,6 +38,11 @@ Options Scan for packages for a specific architecture. Default is to scan for all architectures. +*\--age-atime *:: +*\--age-mtime *:: + Only consider packages for removal with atime respectively mtime older than + specified. The age can be given as '10d', '1m', '1y', '1y1m' etc. + *-c, \--cachedir *:: Specify a different cache directory. This option can be used more than once. Default is to use the cache directory configured in 'pacman.conf'. diff --git a/src/paccache.sh.in b/src/paccache.sh.in index 012ba9f..8ee5792 100644 --- a/src/paccache.sh.in +++ b/src/paccache.sh.in @@ -27,6 +27,7 @@ declare -r myver='@PACKAGE_VERSION@' declare -a cachedirs=() candidates=() cmdopts=() whitelist=() blacklist=() declare -i delete=0 dryrun=0 filecount=0 move=0 needsroot=0 totalsaved=0 verbose=0 +declare -i age_atime=0 age_mtime=0 declare delim=$'\n' keep=3 movedir= scanarch= QUIET=0 @@ -40,6 +41,29 @@ die() { exit 1 } +# Parses the age --age-atime and --age-mtime arguments +parse_age() { + declare -i age=0 + if [[ $2 =~ ^[[:space:]]*([0-9]+[dmy][[:space:]]*)+$ ]]; then + # Add spaces to facilitate splitting + temp=${2//d/d } + temp=${temp//m/m } + temp=${temp//y/y } + read -a temp <<< "${temp[*]}" + for a in ${temp[@]}; do + num=${a:0: -1} + case ${a: -1} in + d) age=$(( age + num )) ;; + m) age=$(( age + num * 30 )) ;; + y) age=$(( age + num * 365 )) ;; + esac + done + else + die "argument '%s' to option '%s' must be of the form '([0-9]+[dmy])+'" "$2" "$1" + fi + echo $(( age * 24 * 60 * 60 )) +} + # reads a list of files on stdin and prints out deletion candidates pkgfilter() { # there's whitelist and blacklist parameters passed to this @@ -174,6 +198,10 @@ Usage: ${myname} [options] [targets...] -r, --remove remove candidate packages. Options: + --age-atime + --age-mtime keep packages with an atime/mtime that is not at least + ago, where is given as '10d', '1m', '1y', + '1y1m' etc. -a, --arch scan for "arch" (default: all architectures). -c, --cachedir scan "dir" for packages. can be used more than once. (default: read from @sysconfdir@/pacman.conf). @@ -200,7 +228,8 @@ version() { OPT_SHORT=':a:c:dfhi:k:m:qrsuVvz' OPT_LONG=('arch:' 'cachedir:' 'dryrun' 'force' 'help' 'ignore:' 'keep:' 'move' - 'nocolor' 'quiet' 'remove' 'uninstalled' 'version' 'verbose' 'null') + 'nocolor' 'quiet' 'remove' 'uninstalled' 'version' 'verbose' 'null' + 'age-atime:' 'age-mtime:') if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then exit 1 @@ -210,6 +239,18 @@ unset OPT_SHORT OPT_LONG OPTRET while :; do case $1 in + --age-atime) + age_atime=$(parse_age "$1" "$2") + if (( $? )); then + exit 1 + fi + shift ;; + --age-mtime) + age_mtime=$(parse_age "$1" "$2") + if (( $? )); then + exit 1 + fi + shift ;; -a|--arch) scanarch=$2 shift ;; @@ -319,6 +360,21 @@ for cachedir in "${cachedirs[@]}"; do popd &>/dev/null done +# remove any candidates that are not old enough yet +if (( $age_atime || $age_mtime )); then + currtime=$(date +%s) + for cand in "${candidates[@]}"; do + IFS=';' read -d '' -a temp <<< $(stat --format '%X;%Y' "$cand") + if (( ( $(( $currtime - ${temp[0]} )) > $age_atime ) && \ + ( $(( $currtime - ${temp[1]} )) > $age_mtime ) \ + )); then + candtemp+=("$cand") + fi + done + candidates=("${candtemp[@]}") + unset candtemp +fi + if (( ! ${#candidates[*]} )); then msg 'no candidate packages found for pruning' exit 0