Session/Driver/file.pm

filter: Statementstmt 91.9% 91 / 99 Branchbran 61.1% 33 / 54 Conditioncond 41.1% 7 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 75.5% 151 / 200 CC 33 SCAR 20.8
CC
33
Cov
77%
store
11.7
traverse
11.0
retrieve
9.7
CRAP 8.0
« Session/ID/incr.pm ↑ Summary Session/Driver.pm »
1 package CGI::Session::Driver::file;
2  
3 # $Id$
4  
5 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Fcntl qw( :DEFAULT :flock :mode );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use CGI::Session::Driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use vars qw( $FileName $NoFlock $UMask $NO_FOLLOW );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # keep historical behavior
15  
16 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 24 *FileName = \$CGI::Session::File::FileName;
19 }
20  
21 24 @CGI::Session::Driver::file::ISA = ( "CGI::Session::Driver" );
22 24 $CGI::Session::Driver::file::VERSION = '4.43';
23 24 $FileName = "cgisess_%s";
24 24 $NoFlock = 0;
25 24 $UMask = 0660;
26 24 ▶$NO_FOLLOW = eval { O_NOFOLLOW } || 0;
Condition50%1/2
eval { do { 131072 } } || 0
l!l
240
MC/DC0%0/1
eval { do { 131072 } } || 0
eval { do { 131072 } }
Truth table50%1/2
A
A= eval { do { 131072 } }
Aresult
11
00
27  
28 48 ▶sub init {
Subroutine init: called CC=6 SCAR=22.3
Pod documented
29 48 my $self = shift;
30 48 ▶ $self->{Directory} ||= File::Spec->tmpdir();
Condition66%2/3
$self->{"Directory"} ||= "File::Spec"->tmpdir
l!l&&r!l&&!r
38100
MC/DC0%0/2
$self->{"Directory"} ||= "File::Spec"->tmpdir
$self->{"Directory"} "File::Spec"->tmpdir
Truth table66%2/3
A ||= B
A= $self->{"Directory"}B= "File::Spec"->tmpdir
ABresult
1X1
011
000
31  
32 48 ▶ unless ( -d $self->{Directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"Directory"}) 0 48 48
33 0 require File::Path;
34 0 ▶ unless ( File::Path::mkpath($self->{Directory}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Path::mkpath($self->{"Directory"})) 0 0 0
35 0 return $self->set_error( "init(): couldn't create directory path: $!" );
36 }
37 }
38
39 48 ▶ $self->{NoFlock} = $NoFlock unless exists $self->{NoFlock};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"NoFlock"} 48 0 48
40 48 ▶ $self->{UMask} = $UMask unless exists $self->{UMask};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"UMask"} 48 0 48
41
42 48 return 1;
43 }
44  
45 87 ▶sub _file {
Subroutine _file: called CC=2 SCAR=7.2
46 87 my ($self,$sid) = @_;
47 87 my $id = $sid;
48 87 $id =~ s|\\|/|g;
49  
50 87 ▶ if ($id =~ m|/|)
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ m[/]) 0 87 87
51 {
52 0 return $self->set_error( "_file(): Session ids cannot contain \\ or / chars: $sid" );
53 }
54  
55 87 return File::Spec->catfile($self->{Directory}, sprintf( $FileName, $sid ));
56 }
57  
58 34 ▶sub retrieve {
Subroutine retrieve: called CC=8 SCAR=22.8
Pod documented
59 34 my $self = shift;
60 34 my ($sid) = @_;
61  
62 34 my $path = $self->_file($sid);
63
64 34 ▶ return 0 unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 14 20 34
65  
66 # make certain our filehandle goes away when we fall out of scope
67 20 local *FH;
68  
69 20 ▶ if (-l $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 0 20 20
70 0 ▶ unlink($path) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or return $self->set_error("retrieve(): '${path}' appears to be a symlink and I couldn't remove it: $!") 0 0 0
71 return $self->set_error("retrieve(): '$path' appears to be a symlink and I couldn't remove it: $!");
72 0 return 0; # we deleted this so we have no hope of getting back anything
73 }
74 20 ▶ sysopen(FH, $path, O_RDONLY | $NO_FOLLOW ) || return $self->set_error( "retrieve(): couldn't open '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, $path, 0 | $CGI::Session::Driver::file::NO_FOLLOW or return $self->set_error("retrieve(): couldn't open '${path}': $!") 20 0 20
75
76 20 ▶ $self->{NoFlock} || flock(FH, LOCK_SH) or return $self->set_error( "retrieve(): couldn't lock '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"NoFlock"} or flock FH, 1 or return $self->set_error("retrieve(): couldn't lock '${path}': $!") 20 0 20
Condition33%1/3
$self->{"NoFlock"} or flock FH, 1
l!l&&r!l&&!r
0200
MC/DC0%0/2
$self->{"NoFlock"} or flock FH, 1
$self->{"NoFlock"} flock FH, 1
Truth table33%1/3
A or B
A= $self->{"NoFlock"}B= flock FH, 1
ABresult
1X1
011
000
77  
78 20 my $rv = "";
79 20 ▶ while ( <FH> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FH)) 69 20 89
80 69 $rv .= $_;
81 }
82 20 close(FH);
83 20 return $rv;
84 }
85  
86  
87  
88 35 ▶sub store {
Subroutine store: called CC=10 SCAR=24.6
Pod documented
89 35 my $self = shift;
90 35 my ($sid, $datastr) = @_;
91
92 35 my $path = $self->_file($sid);
93
94 # make certain our filehandle goes away when we fall out of scope
95 35 local *FH;
96
97 35 my $mode = O_WRONLY|$NO_FOLLOW;
98
99 # kill symlinks when we spot them
100 35 ▶ if (-l $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 1 34 35
101 1 ▶ unlink($path) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or return $self->set_error("store(): '${path}' appears to be a symlink and I couldn't remove it: $!") 1 0 1
102 return $self->set_error("store(): '$path' appears to be a symlink and I couldn't remove it: $!");
103 }
104
105 35 ▶ $mode = O_RDWR|O_CREAT|O_EXCL unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 27 8 35
106
107 35 ▶ sysopen(FH, $path, $mode, $self->{UMask}) or return $self->set_error( "store(): couldn't open '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
sysopen FH, $path, $mode, $self->{"UMask"} or return $self->set_error("store(): couldn't open '${path}': $!") 35 0 35
108
109 # sanity check to make certain we're still ok
110 35 ▶ if (-l $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $path) 0 35 35
111 0 return $self->set_error("store(): '$path' is a symlink, check for malicious processes");
112 }
113
114 # prevent race condition (RT#17949)
115 35 ▶ $self->{NoFlock} || flock(FH, LOCK_EX) or return $self->set_error( "store(): couldn't lock '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"NoFlock"} or flock FH, 2 or return $self->set_error("store(): couldn't lock '${path}': $!") 35 0 35
Condition33%1/3
$self->{"NoFlock"} or flock FH, 2
l!l&&r!l&&!r
0350
MC/DC0%0/2
$self->{"NoFlock"} or flock FH, 2
$self->{"NoFlock"} flock FH, 2
Truth table33%1/3
A or B
A= $self->{"NoFlock"}B= flock FH, 2
ABresult
1X1
011
000
116 35 ▶ truncate(FH, 0) or return $self->set_error( "store(): couldn't truncate '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
truncate FH, 0 or return $self->set_error("store(): couldn't truncate '${path}': $!") 35 0 35
117
118 35 print FH $datastr;
119 35 ▶ close(FH) or return $self->set_error( "store(): couldn't close '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
close FH or return $self->set_error("store(): couldn't close '${path}': $!") 35 0 35
120 35 return 1;
121 }
122  
123  
124 16 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
125 16 my $self = shift;
126 16 my ($sid) = @_;
127 16 my $path = $self -> _file($sid);
128 16 ▶ unlink($path) or return $self->set_error( "remove(): couldn't unlink '$path': $!" );
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or return $self->set_error("remove(): couldn't unlink '${path}': $!") 16 0 16
129 16 return 1;
130 }
131  
132  
133 1 ▶sub traverse {
Subroutine traverse: called CC=10 SCAR=24.0
Pod documented
134 1 my $self = shift;
135 1 my ($coderef) = @_;
136  
137 1 ▶ unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($coderef and ref $coderef and ref $coderef eq "CODE") 0 1 1
Condition33%2/6
$coderef and ref $coderef and ref $coderef eq "CODE"
!ll&&!rl&&r
001
$coderef and ref $coderef
!ll&&!rl&&r
001
MC/DC0%0/3
$coderef and ref $coderef and ref $coderef eq "CODE"
$coderef ref $coderef ref $coderef eq "CODE"
Truth table25%1/4
A and B and C
A= $coderefB= ref $coderefC= ref $coderef eq "CODE"
ABCresult
0XX0
10X0
1100
1111
138 0 croak "traverse(): usage error";
139 }
140  
141 opendir( DIRHANDLE, $self->{Directory} )
142 1 ▶ or return $self->set_error( "traverse(): couldn't open $self->{Directory}, " . $! );
Branch: 1 branch
BranchTrueFalseTotal
opendir DIRHANDLE, $self->{"Directory"} or return $self->set_error(qq[traverse(): couldn't open $self->{"Directory"}, ] . $!) 1 0 1
143  
144 1 my $filename_pattern = $FileName;
145 1 $filename_pattern =~ s/\./\\./g;
146 1 $filename_pattern =~ s/\%s/(\.\+)/g;
147 1 ▶ while ( my $filename = readdir(DIRHANDLE) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = readdir DIRHANDLE)) 53 1 54
148 53 ▶ next if $filename =~ m/^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /^\.\.?$/ 2 51 53
149 51 my $full_path = File::Spec->catfile($self->{Directory}, $filename);
150 51 ▶ my $mode = (stat($full_path))[2]
Branch: 1 branch
BranchTrueFalseTotal
my $mode = (stat $full_path)[2] or return $self->set_error("traverse(): stat failed for ${full_path}: " . $!) 51 0 51
151 or return $self->set_error( "traverse(): stat failed for $full_path: " . $! );
152 51 ▶ next if S_ISDIR($mode);
Branch: 1 branch
BranchTrueFalseTotal
if S_ISDIR($mode) 1 50 51
153 50 ▶ if ( $filename =~ /^$filename_pattern$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^$filename_pattern$/) 1 49 50
154 1 $coderef->($1);
155 }
156 }
157 1 closedir( DIRHANDLE );
158 1 return 1;
159 }
160  
161  
162 48 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
163 48 my $self = shift;
164 }
165  
166 24 1;
167  
168 __END__;
« Session/ID/incr.pm ↑ Summary Session/Driver.pm »