3 # Copyright 2006 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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.
23 expire-iso8601 [<options>] <number>x<interval> [<number>x<interval> ...]
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 or
28 longer apart than specified; default is 0.1 unit
29 -n do not really delete
30 -r recursive removal (rm -r)
33 /home/ian/junk/expire-iso8601 14x1 4x7
34 uses units of 86400s (1 day) with a slop of 8640
35 it keeps 14 daily items
36 (that is 14 items, dated no less than 86400-8640 apart)
38 (that is 4 items, dated no less than 7*86400-8640 apart)
39 the 14 daily and 7 weekly items may be the same, or not
40 There is no need to sort the list of <number>x<interval> pairs.
45 -1 catastrophic failure
52 use DateTime::Format::Strptime;
66 print STDERR "bad usage: $_[0]\n$usage" or die $!;
71 # my $strp = DateTime::Format::Strptime->new();
72 foreach my $f (<[0-9]*>) {
73 if ($f !~ m/^ \d\d\d\d - \d\d - \d\d
74 (?: T \d\d \: \d\d (?: \: \d\d )?
75 (?: [-+] \d{1,2} \:? \d\d )? )?
77 print STDERR "ignoring $f\n";
85 my @t = Date::Parse::strptime($f);
86 @t = map { $_ // 0 } @t;
89 # my $t = $strp->parse_datetime($f);
91 # my @t = Date::Parse::strptime($f);
92 #print STDERR Dumper(\@t);
94 # $!=0; $?=0; my $t = `date -d '$&' +%s`;
95 # die "date(!) failed on $&: $? $!" if $! || $?;
96 # chomp $t or confess;
97 push @files, { F => $f, T => $t, U => [] };
103 print STDERR "none at all yet!\n";
107 # newest first, which means biggest T
108 @files = sort { $b->{T} <=> $a->{T} || $b->{F} cmp $a->{F} } @files;
109 my $newest_t = $files[0]{T};
110 $_->{A} = ($newest_t - $_->{T}) / $unit foreach @files;
113 push @{$files[0]{U}}, "newest";
115 print DEBUG Dumper(scalar(@files), \@files, \@intervals) if $debug >= 2;
122 my $dmin = $d - $slop;
123 my $dmax = $d + $slop;
124 my $spec = $int->{Spec};
125 my $start_age = ($n-1) * $d - $slop;
127 my $insufficiently_old = 0;
129 print DEBUG "FLAG $spec sa=$start_age dmin=$dmin dmax=$dmax\n";
131 # find $i, the youngest which is at least $start_age
133 print DEBUG "i #$i $files[$i]{A}\n";
134 last if $files[$i]{A} >= $start_age;
136 $insufficiently_old = 1;
137 print STDERR "insufficiently old for $spec\n";
148 push @{ $files[$i]{U} }, $spec;
155 # find $j, the closest to $i, preferably no more than $dmax younger
159 # at each point in this loop $j is the next candidate
161 my $dt = $files[$i]{A} - $files[$j]{A};
162 print DEBUG "j #$j $files[$j]{A} dt=$dt\n";
168 print STDERR "insufficiently dense for $spec after $files[$j]{F}\n";
171 print DEBUG "i #$j\n";
177 while ($count < $n) {
181 if (!$insufficiently_old) {
183 "insufficiently old for $spec (density compensation)\n";
187 my $dt = $files[$i]{A} - $files[$oldest]{A};
188 print DEBUG "o #$i $files[$i]{A} dt=$dt\n";
189 last if $dt >= $dmin;
191 $use->($i, "$spec+");
198 my $r= system 'rm', ($recurse ? ('-r') : ()), "--", $fn;
199 die "run rm: $!\n" unless defined($r) && $r >= 0;
205 foreach (reverse sort @oldfiles) {
206 printf "remove %s - old\n", $_;
209 foreach (reverse @files) {
210 next unless @{$_->{U}};
211 printf "keep %s for %s - age %.1f\n",
212 $_->{F}, "@{$_->{U}}", $_->{A};
214 foreach (reverse @files) {
216 printf "remove %s - age %.1f\n",
219 my $tmp = "$_->{F}.rm";
220 rename $_->{F}, $tmp or die "rename $_->{F} to $tmp: $!\n";
226 open DEBUG, ">/dev/null" or die $!;
228 while (@ARGV && $ARGV[0] =~ m/^-/) {
230 last if $_ eq '-' || $_ eq '--';
233 if (s/^-n/-/) { $rm=0; }
234 elsif (s/-r/-/) { $recurse=1; }
235 elsif (s/-D/-/) { $debug++; }
236 elsif (s/-u(\d+)$//) { $unit=$1; }
237 elsif (s/-s(\d+)$//) { $slop=$1; }
238 else { badusage "unknown short option $_" }
240 } elsif (m/^--help$/) {
241 print $usage or die $!;
244 badusage "unknown long option $_"
248 badusage "too few arguments" unless @ARGV;
251 open DEBUG, ">&STDERR" or die $!;
255 $slop //= $unit * 0.1;
258 m/^(\d+)x(\d+)$/ or badusage "bad <number>x<interval> $_";
259 push @intervals, { Spec => $&, N => $1, D => $2 };
264 foreach (@intervals) { flag $_ }