Reactor/Sessions/Filesystem.pm untested

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