All pastes #101494 Raw Edit

Miscellany

public text v1 · immutable
#101494 ·published 2006-07-26 22:56 UTC
rendered paste body
#!/usr/bin/perl
$queuedir = "/usr/dbbackup/daily"; # DEFAULT -q
$old_days = 7;    # DEFAULT -o
$debug = 0;    # DEFAULT -d
$verbose = 0;    # DEFAULT -v
 
while (@ARGV) {
    last if ($ARGV[0] !~ /-(\w+)$/);
    $flag = $1;
 
    CASE: {
 if ($flag =~ /^d/) {  # debug
     $debug = 1; last CASE;
 }
 if ($flag =~ /^o/) {  # "old" days (arg)
     shift;
     $old_days = $ARGV[0]; last CASE;
 }
 if ($flag =~ /^q/) {  # queue dir (arg)
     shift;
     $queuedir = $ARGV[0]; last CASE;
 }
 if ($flag =~ /^v/) {  # verbose
     $verbose = 1; last CASE;
 }
    }

    shift (@ARGV);
}
 
chdir $queuedir || die "Can't cd to $queuedir: $!\n";
opendir (DIR, $queuedir) || die "Can't read $queuedir: $!\n";
@files = readdir (DIR);
closedir (DIR);
 
# Reverse-sort:
#   examine, in order: xfiles tfiles qfiles nfiles lfiles dfiles
foreach (reverse (sort (@files))) {
    #print "$_\n" if $verbose; # not THAT verbose!
    next unless -f $_;  # skip it if it's already gone!
    
    if (/^x(f.*)/) {  # xfiles: transcript
 if (! -f ("d" . $1))  # No data file - delete this
     { &deal_with ($_, "no df"); } 
 if (! -f ("q" . $1))  # No queue file - delete this
     { &deal_with ($_, "no qf"); }
 next;
    }
    if (/^t(f.*)/) {  # tfiles: temporary qfile
 if (! -f ("d" . $1))  # No data file - delete this
     { &deal_with ($_, "no df"); }
 if (! -f ("q" . $1))  # No queue file - delete this
     { &deal_with ($_, "no qf"); }
 next;
    }
    if (/^q(f.*)/) {  # qfiles: queue control and header
 if (! -f ("d" . $1))  # No data file - delete this
     { &deal_with ($_, "no df"); }
 next;
    }
    if (/^n(f.*)/) {  # nfiles: ID Creation
 &deal_with ($_, "unused"); # Obsolete
 next;
    }
    if (/^l(f.*)/) {  # lfiles: lock files
 if (! -f ("q" . $1))  # No queue file - delete this
     { &deal_with ($_, "no qf"); }
 elsif (-M $_ > 2) {  # 2 days old: orphan (book, p.252)
     &deal_with ($_, "old lock");
     if ( -f ("x" . $1))  # No queue file - delete this
  { &deal_with ("x" . $1, "old lock"); }
 } next;
    }
    if (/^d(f.*)/) {  # dfiles: data (message body)
 if (! -f ("q" . $1))  # No queue file - delete this
     { &deal_with ($_, "no qf"); }
 next;
    }
    if (/^Q(f.*)/) {  # Qfiles: "bogus" files (see FAQ)
 print "Q-file: $_\n";  # Report only
 next;
    }

    if (-M $_ > $old_days) { # Old files
# print "OLD: $_\n";  # Report only
 unlink ($_);
 next;
    }

    if (-z $_) {  # Zero-length file - delete this
 &deal_with ($_, "zero-length");
 next;
    }
}

sub deal_with {
    local ($f, $s) = @_;

    if ($debug) {
 print "$f : $s\n"; # Merely report
    } else {
 print "REMOVE $f : $s\n" if $verbose;
 unlink ($f);  # No error-checking
    }
}