chiark / gitweb /
expire-iso8601: Use date(1)
[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 our @files;
54 our $rm = 1;
55 our $recurse = 1;
56 our $unit = 86400;
57 our $slop;
58 our $debug = 0;
59 our @intervals;
60
61 sub badusage ($) {
62   print STDERR "bad usage: $_[0]\n$usage" or die $!;
63   exit 8;
64 }
65
66 sub scan () {
67 #  my $strp = DateTime::Format::Strptime->new();
68   foreach my $f (<[0-9]*>) {
69     if ($f  !~ m/^ \d\d\d\d - \d\d - \d\d 
70                  (?: T \d\d \: \d\d (?: \: \d\d )?
71                    (?: [-+] \d{1,2} \:? \d\d )? )? /x) {
72       print STDERR "ignoring $f\n";
73     }
74 #    my @t = Date::Parse::strptime($f);
75 #    @t = map { $_ // 0 } @t;
76 #    my $t = mktime @t;
77 #    m
78 #    my $t = $strp->parse_datetime($f);
79 #    $t = $t->epoch();
80 #    my @t = Date::Parse::strptime($f);
81 #print STDERR Dumper(\@t);
82 #    my $t = mktime(@t);
83     $!=0; $?=0; my $t = `date -d '$&' +%s`;
84     die "date(!) failed on $&: $? $!" if $! || $?;
85     chomp $t or confess;
86     push @files, { F => $f, T => $t, U => [] };
87   }
88 }
89
90 sub precomp () {
91   if (!@files) {
92     print STDERR "none at all yet!\n";
93     exit 0;
94   }
95
96   # newest first, which means biggest T
97   @files = sort { $b->{T} <=> $a->{T} || $b->{F} cmp $a->{F} } @files;
98   my $newest_t = $files[0]{T};
99   $_->{A} = ($newest_t - $_->{T}) / $unit foreach @files;
100   $slop /= $unit;
101
102   push @{$files[0]{U}}, "newest";
103
104   print DEBUG Dumper(scalar(@files), \@files, \@intervals) if $debug >= 2;
105 }
106
107 sub flag ($) {
108   my ($int) = @_;
109   my $n = $int->{N};
110   my $d = $int->{D};
111   my $dmax = $d + $slop;
112   my $spec = $int->{Spec};
113   my $start_age = ($n-1) * $d - $slop;
114   my $i = 0;
115
116   print DEBUG "FLAG $spec sa=$start_age dmax=$dmax\n";
117
118   # find $i, the youngest which is at least $start_age
119   for (;;) {
120     print DEBUG "i #$i $files[$i]{A}\n";
121     last if $files[$i]{A} >= $start_age;
122     if ($i == $#files) {
123       print STDERR "insufficiently old for $spec\n";
124       last;
125     }
126     $i++;
127   }
128
129   for (;;) {
130     push @{ $files[$i]{U} }, $spec;
131
132     # find $j, the closest to $i, preferably no more than $dmax younger
133     my $j = $i;
134     for (;;) {
135       $j--;
136       # at each point in this loop $j is the next candidate
137       last if $j < 0;
138       my $dt = $files[$i]{A} - $files[$j]{A};
139       print DEBUG "j #$j $files[$j]{A} dt=$dt\n";
140       last if $dt > $dmax;
141     }
142     last if $j < 0;
143     $j++;
144     if ($j == $i) {
145       $j--;
146       print STDERR "insufficiently dense for $spec after $files[$j]{F}\n";
147     }
148     print DEBUG "i #$j\n";
149
150     $i = $j;
151   }
152 }
153
154 sub implement () {
155   foreach (@files) {
156     next unless @{$_->{U}};
157     printf "keep %s for %s - age %.1f\n",
158       $_->{F}, "@{$_->{U}}", $_->{A};
159   }
160   foreach (@files) {
161     next if @{$_->{U}};
162     printf "remove %s - age %.1f\n",
163       $_->{F}, $_->{A};
164     if ($rm) {
165       my $r= system 'rm', ($recurse ? ('-r') : ()), "--", $_->{F};
166       die "run rm: $!\n" unless defined($r) && $r >= 0;
167       exit 12 if $r;
168     }
169   }
170 }
171
172 open DEBUG, ">/dev/null" or die $!;
173
174 while (@ARGV && $ARGV[0] =~ m/^-/) {
175   $_ = shift @ARGV;
176   last if $_ eq '-' || $_ eq '--';
177   if (m/^-[^-]/) {
178     while (m/^-./) {
179       if (s/^-n/-/) { $rm=0; }
180       elsif (s/-r/-/) { $recurse=1; }
181       elsif (s/-D/-/) { $debug++; }
182       elsif (s/-u(\d+)$//) { $unit=$1; }
183       elsif (s/-s(\d+)$//) { $slop=$1; }
184       else { badusage "unknown short option $_" }
185     }
186   } elsif (m/^--help$/) {
187     print $usage or die $!;
188     exit 0;
189   } else {
190     badusage "unknown long option $_"
191   }
192 }
193
194 badusage "too few arguments" unless @ARGV;
195
196 if ($debug) {
197   open DEBUG, ">&STDERR" or die $!;
198   DEBUG->autoflush(1);
199 }
200
201 $slop //= $unit * 0.1;
202
203 foreach (@ARGV) {
204   m/^(\d+)x(\d+)$/ or badusage "bad <number>x<interval> $_";
205   push @intervals, { Spec => $&, N => $1, D => $2 };
206 }
207
208 scan();
209 precomp();
210 foreach (@intervals) { flag $_ }
211 implement();