purge option
authorheiko
Wed, 03 Jan 2007 11:56:56 +0000
changeset 27 7062e28b526b
parent 26 57fb725ce549
child 28 a6f60adbae92
purge option
debian/cron.daily
exigrey.pl
--- a/debian/cron.daily	Wed Jan 03 10:18:24 2007 +0000
+++ b/debian/cron.daily	Wed Jan 03 11:56:56 2007 +0000
@@ -1,2 +1,3 @@
 #! /bin/bash
 exigrey --clean 3 '*'
+exigrey --purge 5 '*'
--- a/exigrey.pl	Wed Jan 03 10:18:24 2007 +0000
+++ b/exigrey.pl	Wed Jan 03 11:56:56 2007 +0000
@@ -10,6 +10,7 @@
        !ME! --list [db]			# list all items
        !ME! --stat [db ...]		# print short statistic
        !ME! --clean [days [db ...]]	# remove items not used since <days> days
+       !ME! --purge [days [db ...]]	# remove items older than <days> days
        !ME! --dbs [glob]		# list dbm files in default directory
 
        Defaults: delay: !$DEFAULT{delay}!
@@ -37,6 +38,7 @@
 my $opt_insert;
 my $opt_help;
 my $opt_clean;
+my $opt_purge;
 my $opt_dbs;
 
 sub getDBs($);
@@ -49,6 +51,7 @@
 	"insert!" => \$opt_insert,
 	"stats!" => \$opt_stats,
 	"clean!" => \$opt_clean,
+	"purge!" => \$opt_purge,
 	"dbs!" => \$opt_dbs,
 	"help!" => \$opt_help,
     ) or die ME.": Bad usage, try ".ME." --help.\n";
@@ -79,13 +82,14 @@
 	    my %h;
 	    my $db = connectDB(\%h, $_);
 
-	    my ($seen, $returned, $oldest);
-	    $oldest = time();
+	    my ($seen, $returned, $oldest_c, $oldest_u);
+	    $oldest_c = $oldest_u = time();
 	    iterate(%h, sub {
 		my ($item, $v0, $v1, $dv) = @_;
 		++$seen;
 		++$returned if $dv;
-		$oldest = $v0 if $v0 < $oldest;
+		$oldest_c = $v0 if $v0 < $oldest_c;
+		$oldest_u = $v1 if $v1 < $oldest_u;
 	    });
 
 	    $_ = <<__;
@@ -93,7 +97,8 @@
 	      db: $db (ls: %.1f MB / du: %.1f MB)
 	   total: $seen
     not returned: %d (%d%%)
-	  oldest: %.1f days (%s)
+oldest (created): %.1f days (%s)
+   oldest (used): %.1f days (%s)
 __
 	    printf $_, 
 		    scalar(localtime), 
@@ -101,31 +106,39 @@
 		    ((stat $db)[12]*512)/(1024*1024),
 		    $seen - $returned, 
 		    int(100 * ($seen-$returned)/$seen), 
-		    ((time - $oldest) / 86400), scalar(localtime $oldest);
+		    ((time - $oldest_c) / 86400), scalar(localtime $oldest_c),
+		    ((time - $oldest_u) / 86400), scalar(localtime $oldest_u);
 	    print "\n" if @ARGV;
 
 	}
 	exit 0;
     }
 
-    if ($opt_clean) {
+    if ($opt_clean or $opt_purge) {
+
 	my $cut = time() - (86400 * (@ARGV ? shift : 7));
+
 	@ARGV = ($DEFAULT{db}) unless @ARGV;
 	@ARGV = getDBs($ARGV[0]) if $ARGV[0] =~ /[\*\?]/;
+
 	foreach (@ARGV ? @ARGV : $DEFAULT{db}) {
 	    my %h;
 	    my $tmp = tmpfile();
 	    my $db = connectDB(\%h, $_);
 	    iterate(%h, sub {
 		my ($item, $v0, $v1, $dv) = @_;
-		print $tmp $item if $v1 <= $cut;
+		my $rv = defined $opt_purge ? \$v0 : \$v1;
+		print $tmp "$item\0" if $$rv <= $cut;
 	    });
 
 	    seek($tmp, 0, 0) or die "Can't seek tmpfile";
+
+	    $/ = "\0";
 	    delete $h{$_} while <$tmp>;
+	    print "$. items deleted from $db\n";
+
 	    close($tmp);
 
-	    print "$. items deleted from $db\n";
 	}
 	exit 0;
     }