File Coverage

blib/lib/CGI/Session/Driver/file.pm
Criterion Covered Total %
statement 53 75 70.6
branch 10 32 31.2
condition 2 9 22.2
subroutine 11 12 91.6
pod 5 5 100.0
total 81 133 60.9


line stmt bran cond sub pod time code
1             package CGI::Session::Driver::file;
2              
3             # $Id: file.pm 216 2005-09-01 10:52:26Z sherzodr $
4              
5 15     15   65 use strict;
  15         22  
  15         579  
6              
7 15     15   154 use Carp;
  15         25  
  15         977  
8 15     15   66 use File::Spec;
  15         22  
  15         385  
9 15     15   59 use Fcntl qw( :DEFAULT :flock :mode );
  15         28  
  15         10451  
10 15     15   6252 use CGI::Session::Driver;
  15         35  
  15         522  
11 15     15   90 use vars qw( $FileName);
  15         19  
  15         12033  
12              
13             @CGI::Session::Driver::file::ISA = ( "CGI::Session::Driver" );
14             $CGI::Session::Driver::file::VERSION = "3.4";
15             $FileName = "cgisess_%s";
16              
17             sub init {
18 24     24 1 33 my $self = shift;
19 24   66     224 $self->{Directory} ||= File::Spec->tmpdir();
20              
21 24 50       69 if (defined $CGI::Session::File::FileName) {
22 0         0 $FileName = $CGI::Session::File::FileName;
23             }
24              
25 24 50       541 unless ( -d $self->{Directory} ) {
26 0         0 require File::Path;
27 0 0       0 unless ( File::Path::mkpath($self->{Directory}) ) {
28 0         0 return $self->set_error( "init(): couldn't create directory path: $!" );
29             }
30             }
31             }
32              
33             sub retrieve {
34 17     17 1 32 my $self = shift;
35 17         27 my ($sid) = @_;
36              
37 17         34 my $directory = $self->{Directory};
38 17         67 my $file = sprintf( $FileName, $sid );
39 17         211 my $path = File::Spec->catfile($directory, $file);
40              
41 17 100       248 return 0 unless -e $path;
42              
43 12 50       280 unless ( sysopen(FH, $path, O_RDONLY) ) {
44 0         0 return $self->set_error( "retrieve(): couldn't open '$path': $!" );
45             }
46 12         30 my $rv = "";
47 12         154 while ( ) {
48 69         155 $rv .= $_;
49             }
50 12         91 close(FH);
51 12         46 return $rv;
52             }
53              
54              
55              
56             sub store {
57 13     13 1 25 my $self = shift;
58 13         32 my ($sid, $datastr) = @_;
59            
60 13         29 my $directory = $self->{Directory};
61 13         68 my $file = sprintf( $FileName, $sid );
62 13         220 my $path = File::Spec->catfile($directory, $file);
63 13 50       21912 sysopen(FH, $path, O_WRONLY|O_CREAT) or return $self->set_error( "store(): couldn't open '$path': $!" );
64 13 50       119 flock(FH, LOCK_EX) or return $self->set_error( "store(): couldn't lock '$path': $!" );
65 13 50       507 truncate(FH, 0) or return $self->set_error( "store(): couldn't truncate '$path': $!" );
66 13         87 print FH $datastr;
67 13 50       618 close(FH) or return $self->set_error( "store(): couldn't close '$path': $!" );
68 13         92 return 1;
69             }
70              
71              
72             sub remove {
73 7     7 1 13 my $self = shift;
74 7         12 my ($sid) = @_;
75              
76 7         17 my $directory = $self->{Directory};
77 7         32 my $file = sprintf( $FileName, $sid );
78 7         72 my $path = File::Spec->catfile($directory, $file);
79 7 50       596 unlink($path) or return $self->set_error( "remove(): couldn't unlink '$path': $!" );
80 7         45 return 1;
81             }
82              
83              
84             sub traverse {
85 0     0 1 0 my $self = shift;
86 0         0 my ($coderef) = @_;
87              
88 0 0 0     0 unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) {
      0        
89 0         0 croak "traverse(): usage error";
90             }
91              
92 0 0       0 opendir( DIRHANDLE, $self->{Directory} )
93             or return $self->set_error( "traverse(): couldn't open $self->{Directory}, " . $! );
94              
95 0         0 my $filename_pattern = $FileName;
96 0         0 $filename_pattern =~ s/\./\\./g;
97 0         0 $filename_pattern =~ s/\%s/(\.\+)/g;
98 0         0 while ( my $filename = readdir(DIRHANDLE) ) {
99 0 0       0 next if $filename =~ m/^\.\.?$/;
100 0         0 my $full_path = File::Spec->catfile($self->{Directory}, $filename);
101 0 0       0 my $mode = (stat($full_path))[2]
102             or return $self->set_error( "traverse(): stat failed for $full_path: " . $! );
103 0 0       0 next if S_ISDIR($mode);
104 0 0       0 if ( $filename =~ /^$filename_pattern$/ ) {
105 0         0 $coderef->($1);
106             }
107             }
108 0         0 closedir( DIRHANDLE );
109 0         0 return 1;
110             }
111              
112             sub DESTROY {
113 13     13   1958 my $self = shift;
114             }
115              
116             1;
117              
118             __END__;