File Coverage

blib/lib/Apache/Session/Store/File.pm
Criterion Covered Total %
statement 68 74 91.8
branch 16 28 57.1
condition 8 12 66.6
subroutine 12 12 100.0
pod 0 6 0.0
total 104 132 78.7


line stmt bran cond sub pod time code
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     3   75069 use strict;
  3         7  
  3         137  
13 3     3   18 use Symbol;
  3         6  
  3         207  
14 3     3   3443 use IO::File;
  3         4450  
  3         722  
15 3     3   24 use Fcntl;
  3         7  
  3         1184  
16 3     3   22 use vars qw($VERSION);
  3         6  
  3         3008  
17              
18             $VERSION = '1.04';
19              
20             $Apache::Session::Store::File::Directory = '/tmp';
21              
22             sub new {
23 8     8 0 1769 my $class = shift;
24 8         13 my $self;
25            
26 8         40 $self->{fh} = Symbol::gensym();
27 8         136 $self->{opened} = 0;
28            
29 8         43 return bless $self, $class;
30             }
31              
32             sub insert {
33 4     4 0 12 my $self = shift;
34 4         15 my $session = shift;
35            
36 4   66     22 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
37              
38 4 50       85 if (-e $directory.'/'.$session->{data}->{_session_id}) {
39 0         0 die "Object already exists in the data store";
40             }
41            
42 4         16 my $file = $directory.'/'.$session->{data}->{_session_id};
43 4 50       351 sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
44             die "Could not open file $file: $!";
45              
46 4         14 $self->{opened} = 1;
47            
48 4         6 print {$self->{fh}} $session->{serialized};
  4         32  
49 4         224 $self->{fh}->flush();
50             }
51              
52             sub update {
53 2     2 0 272 my $self = shift;
54 2         2 my $session = shift;
55            
56 2   66     14 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
57              
58 2 50       8 if (!$self->{opened}) {
59 0         0 my $file = $directory.'/'.$session->{data}->{_session_id};
60 0 0       0 sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
61             die "Could not open file $file: $!";
62              
63 0         0 $self->{opened} = 1;
64             }
65            
66 2 50       321 truncate($self->{fh}, 0) || die "Could not truncate file: $!";
67 2         17 seek($self->{fh}, 0, 0);
68 2         4 print {$self->{fh}} $session->{serialized};
  2         40  
69 2         66 $self->{fh}->flush();
70             }
71              
72             sub materialize {
73 3     3 0 292 my $self = shift;
74 3         5 my $session = shift;
75            
76 3   66     13 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
77            
78 3         10 my $file = $directory.'/'.$session->{data}->{_session_id};
79 3 100       39 if (-e $file) {
80 2 50       9 if (!$self->{opened}) {
81 2 50       66 sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
82             die "Could not open file $file: $!";
83              
84 2         6 $self->{opened} = 1;
85             }
86             else {
87 0 0       0 seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
88             }
89              
90 2         4 $session->{serialized} = '';
91            
92 2         5 my $fh = $self->{fh};
93 2         198 while (my $line = <$fh>) {
94 8         38 $session->{serialized} .= $line;
95             }
96             }
97             else {
98 1         13 die "Object does not exist in the data store";
99             }
100             }
101              
102             sub remove {
103 4     4 0 10 my $self = shift;
104 4         26 my $session = shift;
105            
106 4   66     20 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
107              
108 4 100       17 if ($self->{opened}) {
109 3         47 CORE::close $self->{fh};
110 3         6 $self->{opened} = 0;
111             }
112              
113 4         21 my $file = $directory.'/'.$session->{data}->{_session_id};
114 4 50       76 if (-e $file) {
115 4 50       429 unlink ($file) ||
116             die "Could not remove file $file: $!";
117             }
118             else {
119 0         0 die "Object does not exist in the data store";
120             }
121             }
122              
123             sub close {
124 3     3 0 5 my $self = shift;
125            
126 3 100       12 if ($self->{opened}) {
127 1         15 CORE::close $self->{fh};
128 1         4 $self->{opened} = 0;
129             }
130             }
131              
132             sub DESTROY {
133 8     8   570 my $self = shift;
134            
135 8 100       194 if ($self->{opened}) {
136 2         85 CORE::close $self->{fh};
137             }
138             }
139              
140             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 = new Apache::Session::Store::File;
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>