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