File Coverage

blib/lib/Apache/Session/Store/File.pm
Criterion Covered Total %
statement 59 64 92.1
branch 16 28 57.1
condition 8 12 66.6
subroutine 11 11 100.0
pod 0 6 0.0
total 94 121 77.6


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 Artistic License
7             #
8             ############################################################################
9              
10             package Apache::Session::Store::File;
11              
12 3     3   72515 use strict;
  3         5  
  3         116  
13 3     3   17 use Symbol;
  3         5  
  3         228  
14 3     3   18 use Fcntl;
  3         7  
  3         1036  
15 3     3   20 use vars qw($VERSION);
  3         5  
  3         3019  
16              
17             $VERSION = '1.02';
18              
19             $Apache::Session::Store::File::Directory = '/tmp';
20              
21             sub new {
22 6     6 0 1736 my $class = shift;
23 6         8 my $self;
24            
25 6         23 $self->{fh} = Symbol::gensym();
26 6         91 $self->{opened} = 0;
27            
28 6         22 return bless $self, $class;
29             }
30              
31             sub insert {
32 2     2 0 6 my $self = shift;
33 2         3 my $session = shift;
34            
35 2   66     12 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
36              
37 2 50       48 if (-e $directory.'/'.$session->{data}->{_session_id}) {
38 0         0 die "Object already exists in the data store";
39             }
40            
41             sysopen ($self->{fh}, $directory.'/'.$session->{data}->{_session_id}, O_RDWR|O_CREAT) ||
42 2 50       150 die "Could not open file ".$directory.'/'.$session->{data}->{_session_id}.": $!";
43              
44 2         6 $self->{opened} = 1;
45            
46 2         4 print {$self->{fh}} $session->{serialized};
  2         21  
47             }
48              
49             sub update {
50 2     2 0 601 my $self = shift;
51 2         5 my $session = shift;
52            
53 2   66     13 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
54              
55 2 50       11 if (!$self->{opened}) {
56 0 0       0 sysopen ($self->{fh}, $directory.'/'.$session->{data}->{_session_id}, O_RDWR|O_CREAT) ||
57             die "Could not open file: $!";
58            
59 0         0 $self->{opened} = 1;
60             }
61            
62 2 50       177 truncate($self->{fh}, 0) || die "Could not truncate file: $!";
63 2         11 seek($self->{fh}, 0, 0);
64 2         4 print {$self->{fh}} $session->{serialized};
  2         10  
65             }
66              
67             sub materialize {
68 3     3 0 509 my $self = shift;
69 3         4 my $session = shift;
70            
71 3   66     14 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
72            
73 3 100       42 if (-e $directory.'/'.$session->{data}->{_session_id}) {
74 2 50       8 if (!$self->{opened}) {
75 2 50       56 sysopen ($self->{fh}, $directory.'/'.$session->{data}->{_session_id}, O_RDWR|O_CREAT) ||
76             die "Could not open file: $!";
77              
78 2         6 $self->{opened} = 1;
79             }
80             else {
81 0 0       0 seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
82             }
83            
84 2         5 my $fh = $self->{fh};
85 2         27 while (<$fh>) {
86 8         31 $session->{serialized} .= $_;
87             }
88             }
89             else {
90 1         15 die "Object does not exist in the data store";
91             }
92             }
93              
94             sub remove {
95 2     2 0 7 my $self = shift;
96 2         4 my $session = shift;
97            
98 2   66     13 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
99              
100 2 100       9 if ($self->{opened}) {
101 1         13 CORE::close $self->{fh};
102 1         3 $self->{opened} = 0;
103             }
104              
105 2 50       35 if (-e $directory.'/'.$session->{data}->{_session_id}) {
106 2 50       191 unlink ($directory.'/'.$session->{data}->{_session_id}) ||
107             die "Could not remove file: $!";
108             }
109             else {
110 0         0 die "Object does not exist in the data store";
111             }
112             }
113              
114             sub close {
115 3     3 0 4 my $self = shift;
116            
117 3 100       11 if ($self->{opened}) {
118 1         61 CORE::close $self->{fh};
119 1         5 $self->{opened} = 0;
120             }
121             }
122              
123             sub DESTROY {
124 6     6   1116 my $self = shift;
125            
126 6 100       75 if ($self->{opened}) {
127 2         88 CORE::close $self->{fh};
128             }
129             }
130              
131             1;
132              
133             =pod
134              
135             =head1 NAME
136              
137             Apache::Session::Store::File - Store persistent data on the filesystem
138              
139             =head1 SYNOPSIS
140              
141              
142             use Apache::Session::Store::File;
143            
144             my $store = new Apache::Session::Store::File;
145            
146             $store->insert($ref);
147             $store->update($ref);
148             $store->materialize($ref);
149             $store->remove($ref);
150              
151             =head1 DESCRIPTION
152              
153             This module fulfills the storage interface of Apache::Session. The serialized
154             objects are stored in files on your filesystem.
155              
156             =head1 OPTIONS
157              
158             This module requires one argument in the usual Apache::Session style. The
159             name of the option is Directory, and the value is the full path of the
160             directory where you wish to place the files. Example
161              
162             tie %s, 'Apache::Session::File', undef,
163             {Directory => '/tmp/sessions'};
164              
165             =head1 NOTES
166              
167             All session objects are stored in the same directory. Some filesystems, such
168             as Linux's ext2fs, have O(n) performance where n is the number of files in a
169             directory. Other filesystems, like Sun's UFS, and Linux's reiserfs, do not
170             have this problem. You should consider your filesystem's performance before
171             using this module to store many objects.
172              
173             =head1 AUTHOR
174              
175             This module was written by Jeffrey William Baker .
176              
177             =head1 SEE ALSO
178              
179             L