Session/Store/File.pm

filter: Statementstmt 92.2% 71 / 77 Branchbran 60.0% 18 / 30 Conditioncond 66.6% 8 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 75.1% 109 / 145 CC 20 SCAR 15.2
CC
20
Cov
82%
materialize
7.5
update
6.4
remove
5.2
CRAP 4.6
« Session/Generate/ModUsertrack.pm ↑ Summary Session.pm »
1 #############################################################################
2 #
3 # Apache::Session::Store::File
4 # Implements session object storage via flat files
5 # Copyright(c) 1998, 1999, 2000, 2004 Jeffrey William Baker (jwbaker@acm.org)
6 # Distribute under the Perl License
7 #
8 ############################################################################
9
10 package Apache::Session::Store::File;
11
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 3 $VERSION = '1.04';
19
20 3 $Apache::Session::Store::File::Directory = '/tmp';
21
22 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 8 my $class = shift;
24 8 my $self;
25
26 8 $self->{fh} = Symbol::gensym();
27 8 $self->{opened} = 0;
28
29 8 return bless $self, $class;
30 }
31
32 4 ▶sub insert {
Subroutine insert: called CC=4 SCAR=14.3
Pod undocumented
33 4 my $self = shift;
34 4 my $session = shift;
35
36 4 ▶ my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
Condition66%2/3
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
l!l&&r!l&&!r
310
MC/DC0%0/2
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
$session->{'args'}{'Directory'} $Apache::Session::Store::File::Directory
Truth table66%2/3
A || B
A= $session->{'args'}{'Directory'}B= $Apache::Session::Store::File::Directory
ABresult
1X1
011
000
37
38 4 ▶ if (-e $directory.'/'.$session->{data}->{_session_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $directory . '/' . $session->{"data"}{"_session_id"}) 0 4 4
39 0 die "Object already exists in the data store";
40 }
41
42 4 my $file = $directory.'/'.$session->{data}->{_session_id};
43 4 ▶ sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
Branch: 1 branch
BranchTrueFalseTotal
sysopen $self->{"fh"}, $file, 66 or die "Could not open file ${file}: $!" 4 0 4
44 die "Could not open file $file: $!";
45
46 4 $self->{opened} = 1;
47
48 4 print {$self->{fh}} $session->{serialized};
49 4 $self->{fh}->flush();
50 }
51
52 2 ▶sub update {
Subroutine update: called CC=5 SCAR=18.5
Pod undocumented
53 2 my $self = shift;
54 2 my $session = shift;
55
56 2 ▶ my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
Condition66%2/3
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
l!l&&r!l&&!r
110
MC/DC0%0/2
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
$session->{'args'}{'Directory'} $Apache::Session::Store::File::Directory
Truth table66%2/3
A || B
A= $session->{'args'}{'Directory'}B= $Apache::Session::Store::File::Directory
ABresult
1X1
011
000
57
58 2 ▶ if (!$self->{opened}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opened"}) 0 2 2
59 0 my $file = $directory.'/'.$session->{data}->{_session_id};
60 0 ▶ sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
Branch: 1 branch
BranchTrueFalseTotal
sysopen $self->{"fh"}, $file, 66 or die "Could not open file ${file}: $!" 0 0 0
61 die "Could not open file $file: $!";
62
63 0 $self->{opened} = 1;
64 }
65
66 2 ▶ truncate($self->{fh}, 0) || die "Could not truncate file: $!";
Branch: 1 branch
BranchTrueFalseTotal
truncate $self->{"fh"}, 0 or die "Could not truncate file: $!" 2 0 2
67 2 seek($self->{fh}, 0, 0);
68 2 print {$self->{fh}} $session->{serialized};
69 2 $self->{fh}->flush();
70 }
71
72 3 ▶sub materialize {
Subroutine materialize: called CC=7 SCAR=20.2
Pod undocumented
73 3 my $self = shift;
74 3 my $session = shift;
75
76 3 ▶ my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
Condition66%2/3
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
l!l&&r!l&&!r
120
MC/DC0%0/2
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
$session->{'args'}{'Directory'} $Apache::Session::Store::File::Directory
Truth table66%2/3
A || B
A= $session->{'args'}{'Directory'}B= $Apache::Session::Store::File::Directory
ABresult
1X1
011
000
77
78 3 my $file = $directory.'/'.$session->{data}->{_session_id};
79 3 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 2 1 3
80 2 ▶ if (!$self->{opened}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'opened'}) { } 2 0 2
81 2 ▶ sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
Branch: 1 branch
BranchTrueFalseTotal
sysopen $self->{"fh"}, $file, 66 or die "Could not open file ${file}: $!" 2 0 2
82 die "Could not open file $file: $!";
83
84 2 $self->{opened} = 1;
85 }
86 else {
87 0 ▶ seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, 0, 0 or die "Could not seek file: $!" 0 0 0
88 }
89
90 2 $session->{serialized} = '';
91
92 2 my $fh = $self->{fh};
93 2 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 7 2 9
94 7 $session->{serialized} .= $line;
95 }
96 }
97 else {
98 1 die "Object does not exist in the data store";
99 }
100 }
101
102 4 ▶sub remove {
Subroutine remove: called CC=5 SCAR=16.6
Pod undocumented
103 4 my $self = shift;
104 4 my $session = shift;
105
106 4 ▶ my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
Condition66%2/3
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
l!l&&r!l&&!r
310
MC/DC0%0/2
$session->{'args'}{'Directory'} || $Apache::Session::Store::File::Directory
$session->{'args'}{'Directory'} $Apache::Session::Store::File::Directory
Truth table66%2/3
A || B
A= $session->{'args'}{'Directory'}B= $Apache::Session::Store::File::Directory
ABresult
1X1
011
000
107
108 4 ▶ if ($self->{opened}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opened"}) 3 1 4
109 3 CORE::close $self->{fh};
110 3 $self->{opened} = 0;
111 }
112
113 4 my $file = $directory.'/'.$session->{data}->{_session_id};
114 4 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 4 0 4
115 4 ▶ unlink ($file) ||
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Could not remove file ${file}: $!" 4 0 4
116 die "Could not remove file $file: $!";
117 }
118 else {
119 0 die "Object does not exist in the data store";
120 }
121 }
122
123 3 ▶sub close {
Subroutine close: called CC=2 SCAR=6.9
Pod undocumented
124 3 my $self = shift;
125
126 3 ▶ if ($self->{opened}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opened"}) 1 2 3
127 1 CORE::close $self->{fh};
128 1 $self->{opened} = 0;
129 }
130 }
131
132 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
133 8 my $self = shift;
134
135 8 ▶ if ($self->{opened}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opened"}) 2 6 8
136 2 CORE::close $self->{fh};
137 }
138 }
139
140 3 1;
141
142 =pod
143
144 =head1 NAME
145
146 Apache::Session::Store::File - Store persistent data on the filesystem
147
148 =head1 SYNOPSIS
149
150
151 use Apache::Session::Store::File;
152
153 my $store = Apache::Session::Store::File->new;
154
155 $store->insert($ref);
156 $store->update($ref);
157 $store->materialize($ref);
158 $store->remove($ref);
159
160 =head1 DESCRIPTION
161
162 This module fulfills the storage interface of Apache::Session. The serialized
163 objects are stored in files on your filesystem.
164
165 =head1 OPTIONS
166
167 This module requires one argument in the usual Apache::Session style. The
168 name of the option is Directory, and the value is the full path of the
169 directory where you wish to place the files. Example
170
171 tie %s, 'Apache::Session::File', undef,
172 {Directory => '/tmp/sessions'};
173
174 =head1 NOTES
175
176 All session objects are stored in the same directory. Some filesystems, such
177 as Linux's ext2fs, have O(n) performance where n is the number of files in a
178 directory. Other filesystems, like Sun's UFS, and Linux's reiserfs, do not
179 have this problem. You should consider your filesystem's performance before
180 using this module to store many objects.
181
182 =head1 AUTHOR
183
184 This module was written by Jeffrey William Baker <jwbaker@acm.org>.
185
186 =head1 SEE ALSO
187
188 L<Apache::Session>
« Session/Generate/ModUsertrack.pm ↑ Summary Session.pm »