24 lines
1.2 KiB
Bash
Executable File
24 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# delete-old-files DIR MINUTES PATTERN: deletes regular files under DIR on its
|
|
# filesystem whose name matches PATTERN and that were not modified for MINUTES,
|
|
# printing each one's size first. DIR must resolve, symlinks followed, into a
|
|
# directory listed in /etc/archie/delete-roots. Run by the host connector
|
|
# through sudo, so it trusts none of its arguments.
|
|
set -eu
|
|
[ "$#" -eq 3 ] || { echo "usage: delete-old-files DIR MINUTES PATTERN" >&2; exit 2; }
|
|
case "$1" in /*) ;; *) echo "dir must be absolute" >&2; exit 2 ;; esac
|
|
case "$2" in '' | *[!0-9]*) echo "minutes must be a number" >&2; exit 2 ;; esac
|
|
[ "$2" -ge 60 ] || { echo "minutes must be at least 60" >&2; exit 2; }
|
|
case "$3" in '' | */* | *\\*) echo "pattern must be a file name glob" >&2; exit 2 ;; esac
|
|
dir=$(realpath -e -- "$1") || exit 3
|
|
[ "$dir" != / ] || exit 3
|
|
ok=
|
|
while IFS= read -r root; do
|
|
case "$root" in /*) ;; *) continue ;; esac
|
|
root=$(realpath -e -- "$root") || continue
|
|
[ "$root" != / ] || continue
|
|
case "$dir" in "$root" | "$root"/*) ok=1 ;; esac
|
|
done < /etc/archie/delete-roots
|
|
[ -n "$ok" ] || { echo "dir is outside /etc/archie/delete-roots" >&2; exit 3; }
|
|
LC_ALL=C exec find -P "$dir" -xdev -type f -name "$3" -mmin +"$2" -printf '%s\n' -delete
|