#!/usr/local/bin/perl # PERL program to remove archived Strike probabilities. @ARCHIVE = ("/data1/WWW/Tropical/StrikeProb/Archive","/data1/WWW/Tropical/StrikeProb/Archive/.Clip","/data1/WWW/Tropical/StrikeProb/Archive/.WML"); # Archive directories $MaxTries = 10; # Each try is .1 second. $UseLocking = 1; $LOCK_SH = 1; # shared lock $LOCK_EX = 2; # exclusive lock $LOCK_NB = 4; # don't block when locking $LOCK_UN = 8; # unlock foreach $ARCHIVE (@ARCHIVE) { opendir(ARCHDIR,"$ARCHIVE"); # Read Directory @archfiles = readdir(ARCHDIR); closedir(ARCHDIR); @archfiles = grep(/(\.html|\.wml)$/i,@archfiles); grep($_ = "$ARCHIVE/".$_,@archfiles); # Add root to file names for ($nrepeat=0; $nrepeat<=0; ++$nrepeat) { if (scalar(@archfiles) >= 1) { $narch = $#archfiles; for ($i=0; $i<=$narch; $i++) { $file = shift(@archfiles); if (open(AFILE,"+>>$file")) { $lockerror = &LockFile(AFILE,$LOCK_EX); if ($lockerror) { # can't get lock, probably writing which is OK close(AFILE); push(@archfiles,$file); # Save to try again later } else { close(AFILE); unlink("$file"); # go ahead and delete } } } } if (scalar(@archfiles) >= 1) {sleep(1);} # Wait } } exit(0); sub LockFile { local(*FILE,$locktype) = @_; local($TrysLeft) = $MaxTries; if ($UseLocking) { # Try to get a lock on the file while ($TrysLeft--) { # Try to use locking, if it doesn't use locking, the eval would # die. Catch that, and don't use locking. # Try to grab the lock with a non-blocking (4) exclusive (2) lock. # (4 | 2 = 6) $lockresult = eval("flock(FILE,$LOCK_NB|$locktype)"); if ($@) { #$UseLocking = 0; last; } if (!$lockresult) { select(undef,undef,undef,0.1); # Wait for 1/10 sec. } else { last; # We have gotten the lock. } } } else {return -1;} if ($TrysLeft >= 0) { # Success! return 0; } else { return -1; } } sub UnlockFile { local(*FILE) = @_; if ($UseLocking) { flock(FILE,$LOCK_UN); # Unlock the file. } }