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   99352 use strict;
  3         14  
  3         90  
13 3     3   15 use Symbol;
  3         6  
  3         157  
14 3     3   1469 use IO::File;
  3         3074  
  3         397  
15 3     3   27 use Fcntl;
  3         6  
  3         707  
16 3     3   23 use vars qw($VERSION);
  3         6  
  3         2602  
17            
18             $VERSION = '1.04';
19            
20             $Apache::Session::Store::File::Directory = '/tmp';
21            
22             sub new {
23 8     8 0 1732 my $class = shift;
24 8         13 my $self;
25            
26 8         27 $self->{fh} = Symbol::gensym();
27 8         121 $self->{opened} = 0;
28            
29 8         23 return bless $self, $class;
30             }
31            
32             sub insert {
33 4     4 0 11 my $self = shift;
34 4         10 my $session = shift;
35            
36 4   66     20 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
37            
38 4 50       92 if (-e $directory.'/'.$session->{data}->{_session_id}) {
39 0         0 die "Object already exists in the data store";
40             }
41            
42 4         18 my $file = $directory.'/'.$session->{data}->{_session_id};
43 4 50       231 sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
44             die "Could not open file $file: $!";
45            
46 4         20 $self->{opened} = 1;
47            
48 4         6 print {$self->{fh}} $session->{serialized};
  4         30  
49 4         154 $self->{fh}->flush();
50             }
51            
52             sub update {
53 2     2 0 312 my $self = shift;
54 2         4 my $session = shift;
55            
56 2   66     9 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
57            
58 2 50       6 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       90 truncate($self->{fh}, 0) || die "Could not truncate file: $!";
67 2         20 seek($self->{fh}, 0, 0);
68 2         4 print {$self->{fh}} $session->{serialized};
  2         7  
69 2         55 $self->{fh}->flush();
70             }
71            
72             sub materialize {
73 3     3 0 440 my $self = shift;
74 3         8 my $session = shift;
75            
76 3   66     17 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
77            
78 3         9 my $file = $directory.'/'.$session->{data}->{_session_id};
79 3 100       45 if (-e $file) {
80 2 50       8 if (!$self->{opened}) {
81 2 50       61 sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
82             die "Could not open file $file: $!";
83            
84 2         8 $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         4 my $fh = $self->{fh};
93 2         39 while (my $line = <$fh>) {
94 8         34 $session->{serialized} .= $line;
95             }
96             }
97             else {
98 1         14 die "Object does not exist in the data store";
99             }
100             }
101            
102             sub remove {
103 4     4 0 11 my $self = shift;
104 4         7 my $session = shift;
105            
106 4   66     18 my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
107            
108 4 100       13 if ($self->{opened}) {
109 3         36 CORE::close $self->{fh};
110 3         11 $self->{opened} = 0;
111             }
112            
113 4         14 my $file = $directory.'/'.$session->{data}->{_session_id};
114 4 50       97 if (-e $file) {
115 4 50       1009 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 4 my $self = shift;
125            
126 3 100       10 if ($self->{opened}) {
127 1         65 CORE::close $self->{fh};
128 1         7 $self->{opened} = 0;
129             }
130             }
131            
132             sub DESTROY {
133 8     8   704 my $self = shift;
134            
135 8 100       187 if ($self->{opened}) {
136 2         100 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 = 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 .
185            
186             =head1 SEE ALSO
187            
188             L