6 expire-iso8601 [<options>] <number>x<interval> [<number>x<interval> ...]
8 -u<unitlen> <interval> is measured in units of <unitlen> seconds
9 (default is 86400, so <interval> is in days)
10 -s<slop> allow kept items to be <slop> seconds shorter apart than
11 specified; default is 10% of <unitlen>
12 -n do not really delete
13 -r recursive removal (rm -r)
15 /home/ian/junk/expire-iso8601 14x1 4x7
16 uses units of 86400s (1 day) with a slop of 8640
17 it keeps 14 daily items
18 (that is 14 items, dated no less than 86400-8640 apart)
20 (that is 4 items, dated no less than 7*86400-8640 apart)
21 the 14 daily and 7 weekly items may be the same, or not
22 There is no need to sort the list of <number>x<interval> pairs.
27 16 catastrophic failure
31 # Copyright 2006 Ian Jackson <ian@chiark.greenend.org.uk>
33 # This script and its documentation (if any) are free software; you
34 # can redistribute it and/or modify them under the terms of the GNU
35 # General Public License as published by the Free Software Foundation;
36 # either version 3, or (at your option) any later version.
38 # chiark-named-conf and its manpage are distributed in the hope that
39 # it will be useful, but WITHOUT ANY WARRANTY; without even the
40 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
41 # PURPOSE. See the GNU General Public License for more details.
43 # You should have received a copy of the GNU General Public License along
44 # with this program; if not, consult the Free Software Foundation's
45 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
49 badusage () { echo >&2 "bad usage: $*"; usage >&2; trap '' 0; exit 8; }
51 #-------------------- argument parsing --------------------
54 [ "x${2##*[^0-9]}" = "x$2" ] || \
55 badusage "bad $1 \`$2'; must be all digits"
56 [ "$2" ] || badusage "bad $2; must be nonempty"
65 while [ $# -ge 1 ]; do
69 --help) usage; exit 0 ;;
70 --*) badusage "unknown option $arg" ;;
76 -u*) alldigits unit "$val"; arg='' ;;
77 -s*) alldigits slop "$val"; arg='' ;;
78 *) badusage "unknown option ${1:0:2}" ;;
81 if test "x$arg" != x-; then set -- "$arg" "$@"; fi
83 *) set "$arg" "$@"; break ;;
87 [ $# -ge 1 ] || badusage 'too few arguments'
88 [ "$slop" ] || slop=$(( $unit / 10 ))
91 case "$ni" in *x*);; *) badusage "bad <number>x<interval> $ni";; esac
92 alldigits number "${ni%%x*}"
93 alldigits interval "${ni#*x}"
96 #-------------------- scanning the directory ----------
98 # We build in $l a list of the relevant filenames and the time_t's
101 # Each entry in $l is $time_t/$filename, and the list is
102 # newline-separated for the benefit of sort(1).
109 ????-??-??T[0-2][0-9]+[0-9][0-9][0-9][0-9]|\
110 ????-??-??T[0-2][0-9]:[0-6][0-9]+[0-9][0-9][0-9][0-9]|\
111 ????-??-??T[0-2][0-9]:[0-6][0-9]:[0-6][0-9]+[0-9][0-9][0-9][0-9])
112 conv="${cn%T*} ${cn#*T}";;
114 echo >&2 "ignoring $cn"
117 cs=$(date -d "$conv" +%s)
122 #-------------------- main computation --------------------
124 # We process each minimum/extent pair, to have it select a bunch of
125 # versions to keep. We annotate entries in $l: if we are keeping
126 # an entry we prepend a colon; temporarily, if we are keeping an entry
127 # because of this particular minimum/extent, we prepend a comma.
129 # For each minimum/extent pair we look at the list from most recent
131 # ie in order of increasing age
132 # ie in order of decreasing time_t
133 # and each time we're more than min older than the last item we kept,
134 # we mark the item to keep, until we have as many as we want.
136 # We build the new list (space-separated) in lnew.
149 min=$(( (${ni#*x} * $unit) / $div - $slop ))
155 cn=${ce#*/}; cl=${ce%%/*}
156 cs=${cl#,}; cs=${cs#:}
157 case $cl in ,*) ls=$cs; continue;; esac
158 if [ $wantcount != 0 ]; then
160 [ $(( $ls - $cs )) -ge $min ]; then
161 echo "keep (for $ni) $cn"
164 wantcount=$(( $wantcount - 1 ))
166 skipped=$(( $skipped+1 ))
173 if [ $wantcount = 0 ]; then break; fi
174 printf "%s" "insufficient (for $ni) by $wantcount"
175 if [ $skipped = 0 ]; then echo; break; fi
177 echo " shortening interval ${div}x"
183 case $ce in ,*) ce=:${ce#,};; esac
184 case $ce in ::*) ce=${ce#:};; esac
190 #-------------------- execution --------------------
195 nonbroken_echo () { (echo "$@"); }
196 # While we have subprocesses, we have to avoid bash calling write(1,...)
197 # because of a bug in bash (Debian #382798), so we arrange for a subshell
206 nonbroken_echo "expire $cn"
207 $rm $recurse -- $cn &
214 nonbroken_echo "all running"
218 wait $job || exitstatus=4
221 if [ $exitstatus = 0 ]; then
224 echo "complete, but problems deleting"