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 7 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
32 badusage () { echo >&2 "bad usage: $*"; usage >&2; trap '' 0; exit 8; }
34 #-------------------- argument parsing --------------------
37 [ "x${2##*[^0-9]}" = "x$2" ] || \
38 badusage "bad $1 \`$2'; must be all digits"
39 [ "$2" ] || badusage "bad $2; must be nonempty"
48 while [ $# -ge 1 ]; do
52 --help) usage; exit 0 ;;
53 --*) badusage "unknown option $arg" ;;
59 -u*) alldigits unit "$val"; arg='' ;;
60 -s*) alldigits slop "$val"; arg='' ;;
61 *) badusage "unknown option ${1:0:2}" ;;
64 if test "x$arg" != x-; then set -- "$arg" "$@"; fi
66 *) set "$arg" "$@"; break ;;
70 [ $# -ge 1 ] || badusage 'too few arguments'
71 [ "$slop" ] || slop=$(( $unit / 10 ))
74 case "$ni" in *x*);; *) badusage "bad <number>x<interval> $ni";; esac
75 alldigits number "${ni%%x*}"
76 alldigits interval "${ni#*x}"
79 #-------------------- scanning the directory ----------
81 # We build in $l a list of the relevant filenames and the time_t's
84 # Each entry in $l is $time_t/$filename, and the list is
85 # newline-separated for the benefit of sort(1).
92 ????-??-??T[0-2][0-9]+[0-9][0-9][0-9][0-9]|\
93 ????-??-??T[0-2][0-9]:[0-6][0-9]+[0-9][0-9][0-9][0-9]|\
94 ????-??-??T[0-2][0-9]:[0-6][0-9]:[0-6][0-9]+[0-9][0-9][0-9][0-9])
95 conv="${cn%T*} ${cn#*T}";;
97 echo >&2 "ignoring $cn"
100 cs=$(date -d "$conv" +%s)
105 #-------------------- main computation --------------------
107 # We process each minimum/extent pair, to have it select a bunch of
108 # versions to keep. We annotate entries in $l: if we are keeping
109 # an entry we prepend a colon.
111 # For each minimum/extent pair we look at the list from most recent
113 # ie in order of increasing age
114 # ie in order of decreasing time_t
115 # and each time we're more than min older than the last item we kept,
116 # we mark the item to keep, until we have as many as we want.
118 # We build the new list (space-separated) in lnew.
126 min=$(( ${ni#*x} * $unit - $slop ))
132 cn=${ce#*/}; cl=${ce%%/*}; cs=${cl#:}
133 if [ $wantcount != 0 ]; then
135 [ $(( $ls - $cs )) -ge $min ]; then
136 echo "keep (for $ni) $cn"
139 wantcount=$(( $wantcount - 1 ))
144 if [ $wantcount != 0 ];then
145 echo "insufficient (for $ni) by $wantcount"
150 #-------------------- execution --------------------
155 nonbroken_echo () { (echo "$@"); }
156 # While we have subprocesses, we have to avoid bash calling write(1,...)
157 # because of a bug in bash (Debian #382798), so we arrange for a subshell
166 nonbroken_echo "expire $cn"
167 $rm $recurse -- $cn &
174 nonbroken_echo "all running"
178 wait $job || exitstatus=4
181 if [ $exitstatus = 0 ]; then
184 echo "complete, but problems deleting"