On 9/14/18 6:58 PM, wisp3rwind wrote:
--- 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>*:: +*\--age-mtime <age>*:: + 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 <dir>*:: 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 )) +}
This seems extremely complex, and I can think of two alternatives that would be a lot simpler. First, find -mtime 2, with a suitable glob pattern, by replacing printf '%s\n' "$PWD"/*.pkg.tar!(*.sig) when calculating the "candidates" array. Second: compare_file="$(mktemp -t paccache_timestamp.XXXXXX)" touch -d '2 days ago' "$compare_file" and then instead of this:
+# 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
for cand in "${candidates[@]}"; do if [[ $cand -nt $compare_file ]]; then candtemp+=("$cand") fi done The find command accepts a number of days, the touch command accepts a fairly decent "natural language" description. -- Eli Schwartz Bug Wrangler and Trusted User