chiark / gitweb /
new script summarise-mailbox-preserving-privacy
[chiark-utils.git] / scripts / expire-iso8601
1 #!/bin/bash
2 set -e
3                         usage () {
4                         cat <<END
5 usage:
6   expire-iso8601 [<options>] <number>x<interval> [<number>x<interval> ...]
7 options:
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)
14 example:
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)
19       and 7 weekly items
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.
23 exit status:
24    0                   ok
25    4                   rm failed
26    8                   bad usage
27    16                  catastrophic failure
28 END
29                         }
30
31 trap 'exit 16' 0
32 badusage () { echo >&2 "bad usage: $*"; usage >&2; trap '' 0; exit 8; }
33
34 #-------------------- argument parsing --------------------
35
36 alldigits () {
37         [ "x${2##*[^0-9]}" = "x$2" ] || \
38                 badusage "bad $1 \`$2'; must be all digits"
39         [ "$2" ] || badusage "bad $2; must be nonempty"
40         eval $1='$2'
41 }
42
43 rm=rm
44 recurse=''
45 unit=86400
46 slop=''
47
48 while [ $# -ge 1 ]; do
49         arg=$1; shift
50         case "$arg" in
51         --|-)   break ;;
52         --help) usage; exit 0 ;;
53         --*)    badusage "unknown option $arg" ;;
54         -*)
55                 val=${arg#-?}
56                 case "$arg" in
57                 -n*)    rm=: ;;
58                 -r*)    recurse=-r ;;
59                 -u*)    alldigits unit "$val"; arg='' ;;
60                 -s*)    alldigits slop "$val"; arg='' ;;
61                 *)      badusage "unknown option ${1:0:2}" ;;
62                 esac
63                 arg=-${arg#-?}
64                 if test "x$arg" != x-; then set -- "$arg" "$@"; fi
65                 ;;
66         *)      set "$arg" "$@"; break ;;
67         esac
68 done
69
70 [ $# -ge 1 ] || badusage 'too few arguments'
71 [ "$slop" ] || slop=$(( $unit / 10 ))
72
73 for ni in "$@"; do
74         case "$ni" in *x*);; *) badusage "bad <number>x<interval> $ni";; esac
75         alldigits number "${ni%%x*}"
76         alldigits interval "${ni#*x}"
77 done
78
79 #-------------------- scanning the directory ----------
80
81 # We build in $l a list of the relevant filenames and the time_t's
82 # they represent.
83 #
84 # Each entry in $l is $time_t/$filename, and the list is
85 # newline-separated for the benefit of sort(1).
86
87 ls=0
88 for cn in [0-9]*; do
89         case "$cn" in
90         ????-??-??)
91                 conv="$cn";;
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}";;
96         *)
97                 echo >&2 "ignoring $cn"
98                 continue;;
99         esac
100         cs=$(date -d "$conv" +%s)
101         l="$cs/$cn
102 $l"
103 done
104
105 #-------------------- main computation --------------------
106
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; temporarily, if we are keeping an entry
110 # because of this particular minimum/extent, we prepend a comma.
111
112 # For each minimum/extent pair we look at the list from most recent
113 # to least recent,
114 #   ie in order of increasing age
115 #   ie in order of decreasing time_t
116 # and each time we're more than min older than the last item we kept,
117 # we mark the item to keep, until we have as many as we want.
118 #
119 # We build the new list (space-separated) in lnew.
120
121 l=$(sort -nr <<END
122 $l
123 END
124 )
125
126 for ni in "$@"; do
127         wantcount=${ni%x*}
128
129         div=1
130
131         while true; do
132                 min=$(( (${ni#*x} * $unit) / $div - $slop ))
133
134                 ls=''
135                 lnew=''
136                 skipped=0
137                 for ce in $l; do
138                         cn=${ce#*/}; cl=${ce%%/*}
139                         cs=${cl#,}; cs=${cs#:}
140                         case $cl in ,*) ls=$cs; continue;; esac
141                         if [ $wantcount != 0 ]; then
142                                 if ! [ "$ls" ] || \
143                                    [ $(( $ls - $cs )) -ge $min ]; then
144                                         echo "keep (for $ni) $cn"
145                                         ce=,$ce
146                                         ls=$cs
147                                         wantcount=$(( $wantcount - 1 ))
148                                 else
149                                         skipped=$(( $skipped+1 ))
150                                 fi
151                         fi
152                         lnew="$lnew $ce"
153                 done
154                 l=$lnew
155
156                 if [ $wantcount = 0 ]; then break; fi
157                 printf "%s" "insufficient (for $ni) by $wantcount"
158                 if [ $skipped = 0 ]; then echo; break; fi
159                 div=$(( $div * 2 ))
160                 echo " shortening interval ${div}x"
161         done
162
163         # s/([,:]+).*/:\1/g
164         lnew=''
165         for ce in $l; do
166                 case $ce in ,*) ce=:${ce#,};; esac
167                 case $ce in ::*) ce=${ce#:};; esac
168                 lnew="$lnew $ce"
169         done
170         l=$lnew
171 done
172
173 #-------------------- execution --------------------
174
175 trap '' 0
176 exitstatus=0
177
178 nonbroken_echo () { (echo "$@"); }
179 # While we have subprocesses, we have to avoid bash calling write(1,...)
180 # because of a bug in bash (Debian #382798), so we arrange for a subshell
181 # for each echo.
182
183 jobs=''
184 for ce in $l; do
185         case $ce in
186         :*);;
187         *)
188                 cn=${ce#*/}
189                 nonbroken_echo "expire $cn"
190                 $rm $recurse -- $cn &
191                 jobs="$jobs $!"
192                 ;;
193         esac
194 done
195
196 if [ "$jobs" ]; then
197         nonbroken_echo "all running"
198 fi
199
200 for job in $jobs; do
201         wait $job || exitstatus=4
202 done
203
204 if [ $exitstatus = 0 ]; then
205         echo "complete"
206 else
207         echo "complete, but problems deleting"
208 fi
209
210 exit $exitstatus