| 1 | package CGI::Session::Driver::db_file; | ||
| 2 | |||
| 3 | # $Id: db_file.pm 216 2005-09-01 10:52:26Z sherzodr $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use DB_File; | ||
| 9 | use File::Spec; | ||
| 10 | use File::Basename; | ||
| 11 | use CGI::Session::Driver; | ||
| 12 | use Fcntl qw( :DEFAULT :flock ); | ||
| 13 | |||
| 14 | @CGI::Session::Driver::db_file::ISA = ( "CGI::Session::Driver" ); | ||
| 15 | $CGI::Session::Driver::db_file::VERSION = "1.2"; | ||
| 16 | $CGI::Session::Driver::db_file::FILE_NAME = "cgisess.db"; | ||
| 17 | |||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | $self->{FileName} ||= $CGI::Session::Driver::db_file::FILE_NAME; | ||
| 23 | unless ( $self->{Directory} ) { | ||
| 24 | $self->{Directory} = dirname( $self->{FileName} ); | ||
| 25 | $self->{FileName} = basename( $self->{FileName} ); | ||
| 26 | } | ||
| 27 | unless ( -d $self->{Directory} ) { | ||
| 28 | require File::Path; | ||
| 29 | File::Path::mkpath($self->{Directory}) or return $self->set_error("init(): couldn't mkpath: $!"); | ||
| 30 | } | ||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub retrieve { | ||
| 36 | my $self = shift; | ||
| 37 | my ($sid) = @_; | ||
| 38 | croak "retrieve(): usage error" unless $sid; | ||
| 39 | |||
| 40 | my ($dbhash, $unlock) = $self->_tie_db_file(O_RDONLY) or return; | ||
| 41 | my $datastr = $dbhash->{$sid}; | ||
| 42 | untie(%$dbhash); | ||
| 43 | $unlock->(); | ||
| 44 | return $datastr || 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub store { | ||
| 49 | my $self = shift; | ||
| 50 | my ($sid, $datastr) = @_; | ||
| 51 | croak "store(): usage error" unless $sid && $datastr; | ||
| 52 | |||
| 53 | my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR|O_CREAT, LOCK_EX) or return; | ||
| 54 | $dbhash->{$sid} = $datastr; | ||
| 55 | untie(%$dbhash); | ||
| 56 | $unlock->(); | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | sub remove { | ||
| 63 | my $self = shift; | ||
| 64 | my ($sid) = @_; | ||
| 65 | croak "remove(): usage error" unless $sid; | ||
| 66 | |||
| 67 | my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR, LOCK_EX) or return; | ||
| 68 | delete $dbhash->{$sid}; | ||
| 69 | untie(%$dbhash); | ||
| 70 | $unlock->(); | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub DESTROY {} | ||
| 76 | |||
| 77 | |||
| 78 | sub _lock { | ||
| 79 | my $self = shift; | ||
| 80 | my ($db_file, $lock_type) = @_; | ||
| 81 | |||
| 82 | croak "_lock(): usage error" unless $db_file; | ||
| 83 | $lock_type ||= LOCK_SH; | ||
| 84 | |||
| 85 | my $lock_file = $db_file . '.lck'; | ||
| 86 | sysopen(LOCKFH, $lock_file, O_RDWR|O_CREAT) or die "couldn't create lock file '$lock_file': $!"; | ||
| 87 | flock(LOCKFH, $lock_type) or die "couldn't lock '$lock_file': $!"; | ||
| 88 | return sub { | ||
| 89 | close(LOCKFH) && unlink($lock_file); | ||
| 90 | 1; | ||
| 91 | }; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | sub _tie_db_file { | ||
| 97 | my $self = shift; | ||
| 98 | my ($o_mode, $lock_type) = @_; | ||
| 99 | $o_mode ||= O_RDWR|O_CREAT; | ||
| 100 | |||
| 101 | my $db_file = File::Spec->catfile( $self->{Directory}, $self->{FileName} ); | ||
| 102 | my $unlock = $self->_lock($db_file, $lock_type); | ||
| 103 | my %db; | ||
| 104 | unless( tie %db, "DB_File", $db_file, $o_mode, 0666 ){ | ||
| 105 | $unlock->(); | ||
| 106 | return $self->set_error("_tie_db_file(): couldn't tie '$db_file': $!"); | ||
| 107 | } | ||
| 108 | return (\%db, $unlock); | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | sub traverse { | ||
| 114 | my $self = shift; | ||
| 115 | my ($coderef) = @_; | ||
| 116 | |||
| 117 | unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) { | ||
| 118 | croak "traverse(): usage error"; | ||
| 119 | } | ||
| 120 | |||
| 121 | my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR, LOCK_SH); | ||
| 122 | unless ( $dbhash ) { | ||
| 123 | return $self->set_error( "traverse(): couldn't get db handle, " . $self->errstr ); | ||
| 124 | } | ||
| 125 | while ( my ($sid, undef) = each %$dbhash ) { | ||
| 126 | $coderef->( $sid ); | ||
| 127 | } | ||
| 128 | untie(%$dbhash); | ||
| 129 | $unlock->(); | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | __END__; |