Session/Driver/db_file.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 231 CC 16 SCAR 24.5
CC
16
Cov
0%
init
30.0
_tie_db_file
20.0
retrieve
12.0
CRAP 11.6
1 package CGI::Session::Driver::db_file;
2  
3 # $Id$
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 use vars qw( @ISA $VERSION $FILE_NAME $UMask $NO_FOLLOW );
14  
15 @ISA = ( "CGI::Session::Driver" );
16 $VERSION = '4.43';
17 $FILE_NAME = "cgisess.db";
18 $UMask = 0660;
19 $NO_FOLLOW = eval { O_NOFOLLOW } || 0;
20  
21 sub init {
22 my $self = shift;
23  
24 $self->{FileName} ||= $CGI::Session::Driver::db_file::FILE_NAME;
25 unless ( $self->{Directory} ) {
26 $self->{Directory} = dirname( $self->{FileName} );
27 $self->{Directory} = File::Spec->tmpdir() if $self->{Directory} eq '.' && substr($self->{FileName},0,1) ne '.';
28 $self->{FileName} = basename( $self->{FileName} );
29 }
30 unless ( -d $self->{Directory} ) {
31 require File::Path;
32 File::Path::mkpath($self->{Directory}) or return $self->set_error("init(): couldn't mkpath: $!");
33 }
34
35 $self->{UMask} = $CGI::Session::Driver::db_file::UMask unless exists $self->{UMask};
36
37 return 1;
38 }
39  
40  
41 sub retrieve {
42 my $self = shift;
43 my ($sid) = @_;
44 croak "retrieve(): usage error" unless $sid;
45  
46 return 0 unless -f $self->_db_file;
47 my ($dbhash, $unlock) = $self->_tie_db_file(O_RDONLY) or return;
48 my $datastr = $dbhash->{$sid};
49 untie(%$dbhash);
50 $unlock->();
51 return $datastr || 0;
52 }
53  
54  
55 sub store {
56 my $self = shift;
57 my ($sid, $datastr) = @_;
58 croak "store(): usage error" unless $sid && $datastr;
59  
60 my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR, LOCK_EX) or return;
61 $dbhash->{$sid} = $datastr;
62 untie(%$dbhash);
63 $unlock->();
64 return 1;
65 }
66  
67  
68  
69 sub remove {
70 my $self = shift;
71 my ($sid) = @_;
72 croak "remove(): usage error" unless $sid;
73  
74
75 my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR, LOCK_EX) or return;
76 delete $dbhash->{$sid};
77 untie(%$dbhash);
78 $unlock->();
79 return 1;
80 }
81  
82  
83 sub DESTROY {}
84  
85  
86 sub _lock {
87 my $self = shift;
88 my ($db_file, $lock_type) = @_;
89  
90 croak "_lock(): usage error" unless $db_file;
91 $lock_type ||= LOCK_SH;
92  
93 my $lock_file = $db_file . '.lck';
94 if ( -l $lock_file ) {
95 unlink($lock_file) or
96 die $self->set_error("_lock(): '$lock_file' appears to be a symlink and I can't remove it: $!");
97 }
98 sysopen(LOCKFH, $lock_file, O_RDWR|O_CREAT|$NO_FOLLOW) or die "couldn't create lock file '$lock_file': $!";
99
100
101 flock(LOCKFH, $lock_type) or die "couldn't lock '$lock_file': $!";
102 return sub {
103 close(LOCKFH); # && unlink($lock_file); # keep the lock file around
104 1;
105 };
106 }
107  
108  
109  
110 sub _tie_db_file {
111 my $self = shift;
112 my ($o_mode, $lock_type) = @_;
113 $o_mode ||= O_RDWR|O_CREAT;
114
115 # DB_File will not touch a file unless it recognizes the format
116 # we can't detect the version of the underlying database without some very heavy checks so the easiest thing is
117 # to disable this for opening of the database
118
119 # # protect against symlinks
120 # $o_mode |= $NO_FOLLOW;
121  
122 my $db_file = $self->_db_file;
123 my $unlock = $self->_lock($db_file, $lock_type);
124 my %db;
125
126 my $create = ! -e $db_file;
127
128 if ( -l $db_file ) {
129 $create = 1;
130 unlink($db_file) or
131 return $self->set_error("_tie_db_file(): '$db_file' appears to be a symlink and I can't remove it: $!");
132 }
133
134 $o_mode = O_RDWR|O_CREAT|O_EXCL if $create;
135
136 unless( tie %db, "DB_File", $db_file, $o_mode, $self->{UMask} ){
137 $unlock->();
138 return $self->set_error("_tie_db_file(): couldn't tie '$db_file': $!");
139 }
140  
141 return (\%db, $unlock);
142 }
143  
144 sub _db_file {
145 my $self = shift;
146 return File::Spec->catfile( $self->{Directory}, $self->{FileName} );
147 }
148  
149 sub traverse {
150 my $self = shift;
151 my ($coderef) = @_;
152  
153 unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) {
154 croak "traverse(): usage error";
155 }
156  
157 my ($dbhash, $unlock) = $self->_tie_db_file(O_RDWR, LOCK_SH);
158 unless ( $dbhash ) {
159 return $self->set_error( "traverse(): couldn't get db handle, " . $self->errstr );
160 }
161 while ( my ($sid, undef) = each %$dbhash ) {
162 $coderef->( $sid );
163 }
164 untie(%$dbhash);
165 $unlock->();
166 return 1;
167 }
168  
169  
170 1;
171  
172 __END__;