chiark / gitweb /
8d4838f98ff063bd83c9a2e5e5ba9110e96da755
[chiark-utils.git] / scripts / expire-iso8601
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2006 Ian Jackson <ijackson@chiark.greenend.org.uk>
4 #
5 # This script and its documentation (if any) are free software; you
6 # can redistribute it and/or modify them under the terms of the GNU
7 # General Public License as published by the Free Software Foundation;
8 # either version 3, or (at your option) any later version.
9
10 # chiark-named-conf and its manpage are distributed in the hope that
11 # it will be useful, but WITHOUT ANY WARRANTY; without even the
12 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 # PURPOSE.  See the GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, consult the Free Software Foundation's
17 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
18
19 use strict;
20
21 our $usage = <<'END';
22 usage:
23   expire-iso8601 [<options>] <number>x<interval> [<number>x<interval> ...]
24 options:
25    -u<unitlen>  <interval> is measured in units of <unitlen> seconds
26                    (default is 86400, so <interval> is in days)
27    -s<slop>     allow kept items to be <slop> seconds shorter apart than
28                    specified; default is 10% of <unitlen>
29    -n           do not really delete
30    -r           recursive removal (rm -r)
31 example:
32    /home/ian/junk/expire-iso8601 14x1 4x7
33       uses units of 86400s (1 day) with a slop of 8640
34       it keeps 14 daily items
35        (that is 14 items, dated no less than 86400-8640 apart)
36       and 4 weekly items
37        (that is 4 items, dated no less than 7*86400-8640 apart)
38       the 14 daily and 7 weekly items may be the same, or not
39    There is no need to sort the list of <number>x<interval> pairs.
40 exit status:
41    0                   ok
42    4                   rm failed
43    8                   bad usage
44    16                  catastrophic failure
45 END
46
47 use POSIX;
48 use Carp;
49 use Data::Dumper;
50 use Date::Parse;
51 use DateTime::Format::Strptime;
52
53 $|=1;
54
55 our @files;
56 our $rm = 1;
57 our $recurse = 1;
58 our $unit = 86400;
59 our $slop;
60 our $debug = 0;
61 our @intervals;
62
63 sub badusage ($) {
64   print STDERR "bad usage: $_[0]\n$usage" or die $!;
65   exit 8;
66 }
67
68 sub scan () {
69 #  my $strp = DateTime::Format::Strptime->new();
70   foreach my $f (<[0-9]*>) {
71     if ($f  !~ m/^ \d\d\d\d - \d\d - \d\d 
72                  (?: T \d\d \: \d\d (?: \: \d\d )?
73                    (?: [-+] \d{1,2} \:? \d\d )? )? /x) {
74       print STDERR "ignoring $f\n";
75     }
76     my @t = Date::Parse::strptime($f);
77     @t = map { $_ // 0 } @t;
78     my $t = mktime @t;
79 #    m
80 #    my $t = $strp->parse_datetime($f);
81 #    $t = $t->epoch();
82 #    my @t = Date::Parse::strptime($f);
83 #print STDERR Dumper(\@t);
84 #    my $t = mktime(@t);
85 #    $!=0; $?=0; my $t = `date -d '$&' +%s`;
86 #    die "date(!) failed on $&: $? $!" if $! || $?;
87 #    chomp $t or confess;
88     push @files, { F => $f, T => $t, U => [] };
89   }
90 }
91
92 sub precomp () {
93   if (!@files) {
94     print STDERR "none at all yet!\n";
95     exit 0;
96   }
97
98   # newest first, which means biggest T
99   @files = sort { $b->{T} <=> $a->{T} || $b->{F} cmp $a->{F} } @files;
100   my $newest_t = $files[0]{T};
101   $_->{A} = ($newest_t - $_->{T}) / $unit foreach @files;
102   $slop /= $unit;
103
104   push @{$files[0]{U}}, "newest";
105
106   print DEBUG Dumper(scalar(@files), \@files, \@intervals) if $debug >= 2;
107 }
108
109 sub flag ($) {
110   my ($int) = @_;
111   my $n = $int->{N};
112   my $d = $int->{D};
113   my $dmin = $d - $slop;
114   my $dmax = $d + $slop;
115   my $spec = $int->{Spec};
116   my $start_age = ($n-1) * $d - $slop;
117   my $i = 0;
118   my $insufficiently_old = 0;
119
120   print DEBUG "FLAG $spec sa=$start_age dmin=$dmin dmax=$dmax\n";
121
122   # find $i, the youngest which is at least $start_age
123   for (;;) {
124     print DEBUG "i #$i $files[$i]{A}\n";
125     last if $files[$i]{A} >= $start_age;
126     if ($i == $#files) {
127       $insufficiently_old = 1;
128       print STDERR "insufficiently old for $spec\n";
129       last;
130     }
131     $i++;
132   }
133
134   my $oldest = $i;
135   my $count = 0;
136
137   my $use = sub {
138     my ($i, $spec) = @_;
139     push @{ $files[$i]{U} }, $spec;
140     $count++;
141   };
142
143   for (;;) {
144     $use->($i, $spec);
145
146     # find $j, the closest to $i, preferably no more than $dmax younger
147     my $j = $i;
148     for (;;) {
149       $j--;
150       # at each point in this loop $j is the next candidate
151       last if $j < 0;
152       my $dt = $files[$i]{A} - $files[$j]{A};
153       print DEBUG "j #$j $files[$j]{A} dt=$dt\n";
154       last if $dt > $dmax;
155     }
156     $j++;
157     if ($j == $i) {
158       $j--;
159       print STDERR "insufficiently dense for $spec after $files[$j]{F}\n";
160       last if $j < 0;
161     }
162     print DEBUG "i #$j\n";
163
164     $i = $j;
165   }
166
167   $i = $oldest;
168   while ($count < $n) {
169     for (;;) {
170       $i++;
171       if ($i > $#files) {
172         if (!$insufficiently_old) {
173           print STDERR
174             "insufficiently old for $spec (density compensation)\n";
175         }
176         return;
177       }
178       my $dt = $files[$i]{A} - $files[$oldest]{A};
179       print DEBUG "o #$i $files[$i]{A} dt=$dt\n";
180       last if $dt >= $dmin;
181     }
182     $use->($i, "$spec+");
183   }
184 }
185
186 sub implement () {
187   foreach (reverse @files) {
188     next unless @{$_->{U}};
189     printf "keep %s for %s - age %.1f\n",
190       $_->{F}, "@{$_->{U}}", $_->{A};
191   }
192   foreach (reverse @files) {
193     next if @{$_->{U}};
194     printf "remove %s - age %.1f\n",
195       $_->{F}, $_->{A};
196     if ($rm) {
197       my $r= system 'rm', ($recurse ? ('-r') : ()), "--", $_->{F};
198       die "run rm: $!\n" unless defined($r) && $r >= 0;
199       exit 12 if $r;
200     }
201   }
202 }
203
204 open DEBUG, ">/dev/null" or die $!;
205
206 while (@ARGV && $ARGV[0] =~ m/^-/) {
207   $_ = shift @ARGV;
208   last if $_ eq '-' || $_ eq '--';
209   if (m/^-[^-]/) {
210     while (m/^-./) {
211       if (s/^-n/-/) { $rm=0; }
212       elsif (s/-r/-/) { $recurse=1; }
213       elsif (s/-D/-/) { $debug++; }
214       elsif (s/-u(\d+)$//) { $unit=$1; }
215       elsif (s/-s(\d+)$//) { $slop=$1; }
216       else { badusage "unknown short option $_" }
217     }
218   } elsif (m/^--help$/) {
219     print $usage or die $!;
220     exit 0;
221   } else {
222     badusage "unknown long option $_"
223   }
224 }
225
226 badusage "too few arguments" unless @ARGV;
227
228 if ($debug) {
229   open DEBUG, ">&STDERR" or die $!;
230   DEBUG->autoflush(1);
231 }
232
233 $slop //= $unit * 0.1;
234
235 foreach (@ARGV) {
236   m/^(\d+)x(\d+)$/ or badusage "bad <number>x<interval> $_";
237   push @intervals, { Spec => $&, N => $1, D => $2 };
238 }
239
240 scan();
241 precomp();
242 foreach (@intervals) { flag $_ }
243 implement();