chiark / gitweb /
7cb274e607991354cf88b890b108054c5c9ed9c7
[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.
110
111 # For each minimum/extent pair we look at the list from most recent
112 # to least 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.
117 #
118 # We build the new list (space-separated) in lnew.
119
120 l=$(sort -nr <<END
121 $l
122 END
123 )
124
125 for ni in "$@"; do
126         min=$(( ${ni#*x} * $unit - $slop ))
127         wantcount=${ni%x*}
128
129         ls=''
130         lnew=''
131         for ce in $l; do
132                 cn=${ce#*/}; cl=${ce%%/*}; cs=${cl#:}
133                 if [ $wantcount != 0 ]; then
134                         if ! [ "$ls" ] || \
135                            [ $(( $ls - $cs )) -ge $min ]; then
136                                 echo "keep (for $ni) $cn"
137                                 ls=$cs
138                                 ce=:$cs/$cn
139                                 wantcount=$(( $wantcount - 1 ))
140                         fi
141                 fi
142                 lnew="$lnew $ce"
143         done
144         if [ $wantcount != 0 ];then
145                 echo "insufficient (for $ni) by $wantcount"
146         fi
147         l=$lnew
148 done
149
150 #-------------------- execution --------------------
151
152 trap '' 0
153 exitstatus=0
154
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
158 # for each echo.
159
160 jobs=''
161 for ce in $l; do
162         case $ce in
163         :*);;
164         *)
165                 cn=${ce#*/}
166                 nonbroken_echo "expire $cn"
167                 $rm $recurse -- $cn &
168                 jobs="$jobs $!"
169                 ;;
170         esac
171 done
172
173 if [ "$jobs" ]; then
174         nonbroken_echo "all running"
175 fi
176
177 for job in $jobs; do
178         wait $job || exitstatus=4
179 done
180
181 if [ $exitstatus = 0 ]; then
182         echo "complete"
183 else
184         echo "complete, but problems deleting"
185 fi
186
187 exit $exitstatus