chiark / gitweb /
expire-iso8601: tidy docs and exit status
[chiark-utils.git] / scripts / expire-iso8601
index a36729860079d715388c309487f28984a455070b..8ec0aef11544a1c8c8c86ac52414a085eb07c1c1 100755 (executable)
@@ -24,10 +24,11 @@ usage:
 options:
    -u<unitlen>  <interval> is measured in units of <unitlen> seconds
                    (default is 86400, so <interval> is in days)
-   -s<slop>     allow kept items to be <slop> seconds shorter apart than
-                   specified; default is 10% of <unitlen>
-   -n           do not really delete
-   -r           recursive removal (rm -r)
+   -s<slop>       allow kept items to be <slop> seconds shorter or
+                   longer apart than specified; default is 0.1 unit
+   -n             do not really delete
+   -r             recursive removal (rm -r)
+   --help
 example:
    /home/ian/junk/expire-iso8601 14x1 4x7
       uses units of 86400s (1 day) with a slop of 8640
@@ -41,7 +42,7 @@ exit status:
    0                   ok
    4                   rm failed
    8                   bad usage
-   16                  catastrophic failure
+  -1                   catastrophic failure
 END
 
 use POSIX;
@@ -52,6 +53,7 @@ use DateTime::Format::Strptime;
 
 $|=1;
 
+our @oldfiles;
 our @files;
 our $rm = 1;
 our $recurse = 1;
@@ -70,9 +72,16 @@ sub scan () {
   foreach my $f (<[0-9]*>) {
     if ($f  !~ m/^ \d\d\d\d - \d\d - \d\d 
                 (?: T \d\d \: \d\d (?: \: \d\d )?
-                  (?: [-+] \d{1,2} \:? \d\d )? )? $/x) {
+                  (?: [-+] \d{1,2} \:? \d\d )? )? 
+                ( \.rm )? $/x) {
       print STDERR "ignoring $f\n";
     }
+
+    if ($1) {
+      push @oldfiles, $f;
+      next;
+    }
+
     my @t = Date::Parse::strptime($f);
     @t = map { $_ // 0 } @t;
     my $t = mktime @t;
@@ -188,11 +197,15 @@ sub do_rm ($) {
   if ($rm) {
     my $r= system 'rm', ($recurse ? ('-r') : ()), "--", $fn;
     die "run rm: $!\n" unless defined($r) && $r >= 0;
-    exit 12 if $r;
+    exit 4 if $r;
   }
 }
 
 sub implement () {
+  foreach (reverse sort @oldfiles) {
+    printf "remove %s - old\n", $_;
+    do_rm($_);
+  }
   foreach (reverse @files) {
     next unless @{$_->{U}};
     printf "keep %s for %s - age %.1f\n",