Reactor/Sessions/Filesystem.pm untested

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