Bug 60375

Summary: [FILE] Cleans sendpr-code db by removing expired codes.
Product: Documentation Reporter: Eric Anderson <anderson>
Component: Books & ArticlesAssignee: Ceri Davies <ceri>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   

Description Eric Anderson 2003-12-18 20:50:13 UTC
      Perl program that is cronable - removes stale codes from sendpr db based on an expired time. 

Ceri requested this.

Fix: 

#!/usr/bin/perl -T
#
# 
#
# Copyright (c) 2003 Eric Anderson

use DB_File;
use Fcntl qw(:DEFAULT :flock);
use strict;

$ENV{"PATH"} = "/bin:/usr/bin";
$ENV{"TMPDIR"} = "/tmp";

my($fd, $db_obj, %db_hash, $currenttime, $randomcode, $expiretime, $dbpath);

############################################
$dbpath = "/tmp/sendpr-code.db";
$expiretime = 900;              # seconds until code expires
############################################

$currenttime = time();

# DB stuff here
$db_obj = tie(%db_hash, 'DB_File', $dbpath, O_CREAT|O_RDWR, 0644)
                    or die "dbcreate $dbpath $!";
$fd = $db_obj->fd;
open(DB_FH, "+<&=$fd") or die "fdopen $!";

unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
    unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" }
}

foreach $randomcode (keys %db_hash) {
        if ( ($currenttime - $expiretime) <= $db_hash{$randomcode}) {
                delete $db_hash{"$randomcode"};
        }
}

$db_obj->sync();                   # to flush
flock(DB_FH, LOCK_UN);
undef $db_obj;

untie %db_hash;
How-To-Repeat: No problems reported yet.. :)  

File for Ceri..
Comment 1 Ceri Davies freebsd_committer freebsd_triage 2003-12-19 18:53:46 UTC
Responsible Changed
From-To: freebsd-www->ceri

For me.
Comment 2 Ceri Davies freebsd_committer freebsd_triage 2004-01-07 23:26:06 UTC
State Changed
From-To: open->closed

This has been committed; I made a tiny change to warn() if the untie 
failed, just in case.