Reactor/Sessions/Filesystem.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 135 CC 14 SCAR 32.7
CC
14
Cov
0%
_key_to_fn
72.0
_storage_load
20.0
_storage_create
6.0
CRAP 26.3
1 ##############################################################################
2 ##
3 ## Web::Reactor application machinery
4 ## 2013 (c) Vladi Belperchinov-Shabanski "Cade"
5 ## <cade@bis.bg> <cade@biscom.net> <cade@cpan.org>
6 ##
7 ## LICENSE: GPLv2
8 ##
9 ##############################################################################
10 package Web::Reactor::Sessions::Filesystem;
11 use strict;
12 use Exception::Sink;
13 use Web::Reactor::Sessions;
14 use Web::Reactor::Utils;
15 use POSIX;
16 use Storable qw( freeze thaw lock_store lock_retrieve );
17 use Data::Dumper;
18 use Carp;
19  
20 our @ISA = qw( Web::Reactor::Sessions );
21  
22 ##############################################################################
23 ##
24 ## internal storage methods
25 ## they are all internal to this package
26 ##
27  
28 # create new session storage indexed by the given key components
29 # it is important that this function try to do atomic create in the storage.
30 # it must (and expected to) fail if session with the same key exists and never
31 # overwrite existing session storage!
32 # args:
33 # @key (i.e. @_) -- key components array, usually filled with 2 or 3 elements
34 # when 2: TYPE, SESS7c87y32d78asa4
35 # when 3: TYPE, SESS187yc5v87thccf, SESS2jdhfh74yc3847
36 # usually it is simple to $key = join '.' @_;
37 #
38 # returns:
39 # 1 if successful or 0 or undef if not possible or session id already exists
40 sub _storage_create
41 {
42 my $self = shift;
43
44 my $fn = $self->_key_to_fn( {}, @_ );
45 my $F;
46 if( sysopen $F, $fn, O_CREAT | O_EXCL, oct(600) )
47 {
48 close $F;
49 return 1;
50 }
51 else
52 {
53 return 0;
54 }
55 }
56  
57 # loads session data from the storage
58 # args:
59 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
60 # returns:
61 # hashref of session data or undef if error
62 sub _storage_load
63 {
64 my $self = shift;
65
66 my $fn = $self->_key_to_fn( { READONLY => 1 }, @_ );
67 if( ! -r $fn )
68 {
69 carp "error: session file not readable: $fn";
70 return undef;
71 }
72 my $in_data;
73 eval
74 {
75 $in_data = lock_retrieve( $fn );
76 die "error: cannot retrieve session data from [$fn]" unless $in_data;
77 };
78 if ($@)
79 {
80 carp "error: retrieving session failed $fn\n($@)"; # FIXME: must not be fatal
81 return undef;
82 }
83  
84 return $in_data;
85 }
86  
87 # saves session data to the storage
88 # args:
89 # data -- hashref of session data
90 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
91 # returns:
92 # 1 if successful, 0 or undef if failed
93 sub _storage_save
94 {
95 my $self = shift;
96 my $out_data = shift;
97
98 my $fn = $self->_key_to_fn( {}, @_ );
99  
100 return lock_store( $out_data, $fn );
101 }
102  
103 # checks if session exists in the storage
104 # args:
105 # @key (i.e. @_) -- key components array, example: $key = join '.' @_;
106 # returns:
107 # 1 if exists, 0 or undef if not
108 sub _storage_exists
109 {
110 my $self = shift;
111
112 my $fn = $self->_key_to_fn( { READONLY => 1 }, @_ );
113
114 return -e $fn ? 1 : 0;
115 }
116  
117 ##############################################################################
118 ##
119 ## helpers
120 ##
121  
122 # i.e.: _split_dir_components( '1234567890', 3, 3 ) returns '123/456/789/0'
123 sub _split_dir_components
124 {
125 my $self = shift;
126
127 my $s = shift;
128 my $c = shift; # parts count
129 my $l = shift || 2; # how long is each part
130
131 die "Web::Reactor::Sess::Filesystem:_split_dir_components: parts*length > length(s)-1" if $c * $l > length( $s ) - 1;
132  
133 my $r; # result
134
135 for my $p ( 0 .. $c-1 )
136 {
137 $r .= substr( $s, $p * $l, $l ) . '/';
138 }
139 # $r .= substr( $s, $c * $l );
140 $r .= $s;
141
142 return $r;
143 }
144  
145 sub _key_to_fn
146 {
147 my $self = shift;
148 my $opt = shift;
149 my @key = @_;
150  
151 my $r = shift @key; # this should be type
152 confess "invalid key component 0, needs ALPHANUMERIC type, got [$r]" unless $r =~ /^[A-Z]+$/;
153
154 my $vd = $self->{ 'ENV' }{ 'SESS_VAR_DIR' };
155 if( ! $vd )
156 {
157 my $app_root = $self->{ 'ENV' }{ 'APP_ROOT' };
158 boom "missing APP_ROOT" unless -d $app_root; # FIXME: function? get_app_root()
159 $vd = "$app_root/var";
160 }
161 dir_path_check( $vd ) unless -d $vd;
162 confess "missing SESS_VAR_DIR or APP_ROOT/var" unless -d $vd;
163  
164 while( @key > 0 )
165 {
166 my $c = shift @key;
167 confess "invalid key component needs ALPHANUMERIC, got [$c]" unless $c =~ /^[A-Za-z0-9]+$/;
168 $r .= '/' . $self->_split_dir_components( $c, 2, 2 );
169 }
170  
171 my $dir = $vd . '/' . $r;
172 my $chk = $dir;
173 $chk =~ s/\/[^\/]*$//;
174 dir_path_check( $chk ) unless $opt->{ 'READONLY' };
175  
176 return $dir . '.wrs';
177 }
178  
179 ##############################################################################
180 1;
181 ###EOF########################################################################