File Coverage

blib/lib/Apache/Session/Store/File.pm
Criterion Covered Total %
statement 59 64 92.2
branch 16 28 57.1
condition 8 12 66.7
subroutine 11 11 100.0
pod 0 6 0.0
total 94 121 77.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 Artistic License
7             #
8             ############################################################################
9              
10             package Apache::Session::Store::File;
11              
12 3     3   42 use strict;
  3         28  
  3         54  
13 3     3   48 use Symbol;
  3         28  
  3         107  
14 3     3   61 use Fcntl;
  3         29  
  3         60  
15 3     3   51 use vars qw($VERSION);
  3         57  
  3         52  
16              
17             $VERSION = '1.02';
18              
19             $Apache::Session::Store::File::Directory = '/tmp';
20              
21             sub new {
22 6     6 0 2591     my $class = shift;
23 6         52     my $self;
24                 
25 6         83     $self->{fh} = Symbol::gensym();
26 6         199     $self->{opened} = 0;
27                 
28 6         191     return bless $self, $class;
29             }
30              
31             sub insert {
32 2     2 0 26     my $self = shift;
33 2         21     my $session = shift;
34              
35 2   66     35     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
36              
37 2 50       76     if (-e $directory.'/'.$session->{data}->{_session_id}) {
38 0         0         die "Object already exists in the data store";
39                 }
40                 
41 2 50       353     sysopen ($self->{fh}, $directory.'/'.$session->{data}->{_session_id}, O_RDWR|O_CREAT) ||
42                     die "Could not open file ".$directory.'/'.$session->{data}->{_session_id}.": $!";
43              
44 2         22     $self->{opened} = 1;
45                 
46 2         20     print {$self->{fh}} $session->{serialized};
  2         36  
47             }
48              
49             sub update {
50 2     2 0 441     my $self = shift;
51 2         77     my $session = shift;
52                 
53 2   66     37     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
54              
55 2 50       24     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       376     truncate($self->{fh}, 0) || die "Could not truncate file: $!";
63 2         36     seek($self->{fh}, 0, 0);
64 2         18     print {$self->{fh}} $session->{serialized};
  2         29  
65             }
66              
67             sub materialize {
68 3     3 0 550     my $self = shift;
69 3         29     my $session = shift;
70                 
71 3   66     44     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
72                 
73 3 100       1207     if (-e $directory.'/'.$session->{data}->{_session_id}) {
74 2 50       50         if (!$self->{opened}) {
75 2 50       40             sysopen ($self->{fh}, $directory.'/'.$session->{data}->{_session_id}, O_RDWR|O_CREAT) ||
76                             die "Could not open file: $!";
77              
78 2         25             $self->{opened} = 1;
79                     }
80                     else {
81 0 0       0             seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
82                     }
83                     
84 2         20         my $fh = $self->{fh};
85 2         131         while (<$fh>) {
86 7         109             $session->{serialized} .= $_;
87                     }
88                 }
89                 else {
90 1         12         die "Object does not exist in the data store";
91                 }
92             }    
93              
94             sub remove {
95 2     2 0 25     my $self = shift;
96 2         20     my $session = shift;
97                     
98 2   66     35     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
99              
100 2 100       24     if ($self->{opened}) {
101 1         21         CORE::close $self->{fh};
102 1         10         $self->{opened} = 0;
103                 }
104              
105 2 50       55     if (-e $directory.'/'.$session->{data}->{_session_id}) {
106 2 50       311         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 28     my $self = shift;
116                 
117 3 100       37     if ($self->{opened}) {
118 1         71         CORE::close $self->{fh};
119 1         14         $self->{opened} = 0;
120                 }
121             }
122              
123             sub DESTROY {
124 6     6   88     my $self = shift;
125                 
126 6 100       70     if ($self->{opened}) {
127 2         199         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 <jwbaker@acm.org>.
176            
177             =head1 SEE ALSO
178            
179             L<Apache::Session>
180