blib/lib/Config/Model/BackendMgr.pm

filter: Statementstmt 93.6% 222 / 237 Branchbran 69.8% 74 / 106 Conditioncond 56.6% 30 / 53 MC/DCmcdc 26.6% 8 / 30 Subroutinesub 100% 29 / 29 Podpod 8.3% 1 / 12 totaltotal 77.9% 364 / 467 CC 79 SCAR 23.6
CC
79
Cov
82%
try_read_backend
31.8
close_file_to_write
17.0
__ANON__
10.0
CRAP 10.6
« blib/lib/Config/Model/DumpAsData.pm ↑ Summary blib/lib/Config/Model/Iterator.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::BackendMgr 2.169;
11  
12 59 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 59 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 59 ▶use Config::Model::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 59 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 59 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 59 ▶use Scalar::Util qw/weaken reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 59 ▶use Path::Tiny 0.070;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 59 my $logger = get_logger('BackendMgr');
30 59 my $user_logger = get_logger('User');
31  
32 # one BackendMgr per file
33  
34 59 has 'node' => (
35 is => 'ro',
36 isa => 'Config::Model::Node',
37 weak_ref => 1,
38 required => 1
39 );
40 98 ▶has 'file_backup' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 59 has 'rw_config' => (
43 is => 'ro',
44 isa => 'HashRef',
45 required => 1
46 );
47  
48 59 has 'backend_obj' => (
49 is => 'rw',
50 isa => 'Config::Model::Backend::Any',
51 lazy => 1 ,
52 builder => '_build_backend_obj',
53 );
54  
55 98 ▶sub _build_backend_obj {
Subroutine _build_backend_obj: called CC=3 SCAR=11.1
56 98 my $self = shift;
57  
58 98 my $backend = $self->rw_config->{backend};
59 $logger->warn("function parameter for a backend is deprecated. Please implement 'read' method in backend $backend")
60 98 ▶ if $self->rw_config->{function};
Branch: 1 branch
BranchTrueFalseTotal
if $self->rw_config->{"function"} 0 98 98
61 # try to load a specific Backend class
62 98 ▶ my $f = $self->rw_config->{function} || 'read';
Condition50%1/2
$self->rw_config->{'function'} || 'read'
l!l
098
MC/DC0%0/1
$self->rw_config->{'function'} || 'read'
$self->rw_config->{'function'}
Truth table50%1/2
A
A= $self->rw_config->{'function'}
Aresult
11
00
63 98 my $c = $self->load_backend_class( $backend, $f );
64  
65 59 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
66 return $c->new(
67 node => $self->node,
68 name => $backend,
69 auto_create => $self->rw_config->{auto_create},
70 auto_delete => $self->rw_config->{auto_delete},
71 98 );
72 }
73  
74 59 has support_annotation => (
75 is => 'ro',
76 isa => 'Bool',
77 default => 0,
78 );
79  
80 59 with "Config::Model::Role::ComputeFunction";
81 59 with "Config::Model::Role::FileHandler";
82  
83 # check if dir is present. May create it in auto_create write mode
84 130 ▶sub get_cfg_dir_path ($self, %args) {
Subroutine get_cfg_dir_path: called CC=8 SCAR=20.9
Pod undocumented
85 130 ▶ my $w = $args{write} || 0;
Condition100%2/2
$args{'write'} || 0
l!l
3694
MC/DC100%1/1
$args{'write'} || 0
$args{'write'}
Truth table100%2/2
A
A= $args{'write'}
Aresult
11
00
86 130 my $dir = $self->get_tuned_config_dir(%args);
87  
88 130 ▶ if ( not $dir->is_dir and $w and $args{auto_create} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dir->is_dir and $w and $args{"auto_create"}) 1 129 130
Condition83%5/6
not $dir->is_dir and $w and $args{"auto_create"}
!ll&&!rl&&r
12901
not $dir->is_dir and $w
!ll&&!rl&&r
12541
MC/DC66%2/3
not $dir->is_dir and $w and $args{"auto_create"}
not $dir->is_dir $w $args{"auto_create"}
Truth table75%3/4
not(A) and B and C
A= not $dir->is_dirB= $wC= $args{"auto_create"}
ABCresult
0XX0
10X0
1100
1111
89 1 $logger->info("creating directory $dir");
90 1 $dir->mkpath;
91 }
92  
93 130 ▶ unless ( $dir->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir->is_dir) 4 126 130
94 4 ▶ my $mode = $w ? 'write' : 'read';
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 0 4 4
95 4 $logger->info( "$args{backend}: missing directory $dir ($mode mode)" );
96 4 return ( 0, $dir );
97 }
98  
99 126 ▶ $logger->trace( "dir: " . $dir // '<undef>' );
Condition50%1/2
'dir: ' . $dir // "<undef>"
l!l
1260
MC/DC0%0/1
'dir: ' . $dir // "<undef>"
'dir: ' . $dir
Truth table50%1/2
A
A= 'dir: ' . $dir
Aresult
11
00
100  
101 126 return ( 1, $dir );
102 }
103  
104 # return (1, config file path) constructed from arguments or return
105 # (0). May create directory in auto_create write mode.
106 140 ▶sub get_cfg_file_path ($self, %args) {
Subroutine get_cfg_file_path: called CC=8 SCAR=20.8
Pod undocumented
107 140 ▶ my $w = $args{write} || 0;
Condition100%2/2
$args{'write'} || 0
l!l
40100
MC/DC100%1/1
$args{'write'} || 0
$args{'write'}
Truth table100%2/2
A
A= $args{'write'}
Aresult
11
00
108  
109 # config file override
110 140 my $cfo = $args{config_file};
111  
112 140 ▶ if ( defined $cfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfo) 10 130 140
113 my $override
114 10 ▶ = $args{root} ? $args{root}->child($cfo)
Branch: 2 branches
BranchTrueFalseTotal
$args{'root'} ? : 6 4 10
$cfo =~ m[^/]u ? : 2 2 4
115 : $cfo =~ m!^/! ? path($cfo)
116 : path('.')->child($cfo);
117  
118 10 ▶ my $mode = $w ? 'write' : 'read';
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 4 6 10
119 10 $logger->trace("$args{backend} override target file is $override ($mode mode)");
120 10 return ( 1, $override );
121 }
122  
123 130 my ( $dir_ok, $dir ) = $self->get_cfg_dir_path(%args);
124  
125 130 ▶ if ( defined $args{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"file"}) 111 19 130
126 111 ▶ my $file = $args{skip_compute} ? $args{file} : $self->node->compute_string($args{file});
Branch: 1 branch
BranchTrueFalseTotal
$args{'skip_compute'} ? : 25 86 111
127 111 my $res = $dir->child($file);
128 111 $logger->trace("get_cfg_file_path: returns $res");
129 111 return ( $dir_ok, $res );
130 }
131  
132 19 return 0;
133 }
134  
135 58 ▶sub open_read_file ($self, $file_path) {
Subroutine open_read_file: called CC=2 SCAR=6.9
Pod undocumented
136 58 ▶ if ( $file_path->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_path->is_file) { } 56 2 58
137 56 $logger->debug("open_read_file: open $file_path for read");
138 # store a backup in memory in case there's a problem
139 56 $self->file_backup( [ $file_path->lines_utf8 ] );
140 56 return $file_path->filehandle("<", ":utf8");
141 }
142 else {
143 2 return;
144 }
145 }
146  
147 # called at configuration node creation
148 196 ▶sub load_backend_class ($self, $backend, $function) {
Subroutine load_backend_class: called CC=8 SCAR=20.9
Pod undocumented
149 196 $logger->trace("load_backend_class: called with backend $backend, function $function");
150 196 my %c;
151  
152 196 my $k = "Config::Model::Backend::" . ucfirst($backend);
153 196 my $f = $k . '.pm';
154 196 $f =~ s!::!/!g;
155 196 $c{$k} = $f;
156  
157 # try another class
158 196 $k =~ s/_(\w)/uc($1)/ge;
159 196 $f =~ s/_(\w)/uc($1)/ge;
160 196 $c{$k} = $f;
161  
162 196 foreach my $c ( sort keys %c ) {
163 202 ▶ if ( $c->can($function) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can($function)) 179 23 202
164  
165 # no need to load class
166 179 $logger->debug("load_backend_class: $c is already loaded (can $function)");
167 179 return $c;
168 }
169 }
170  
171 # look for file to load
172 17 my $class_to_load;
173 17 foreach my $c ( sort keys %c ) {
174 23 $logger->trace("load_backend_class: looking to load class $c");
175 23 foreach my $prefix (@INC) {
176 197 my $realfilename = "$prefix/$c{$c}";
177 197 ▶ $class_to_load = $c if -f $realfilename;
Branch: 1 branch
BranchTrueFalseTotal
if -f $realfilename 35 162 197
178 }
179 }
180  
181 17 ▶ if (not defined $class_to_load) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class_to_load) 0 17 17
182 0 Config::Model::Exception::Model->throw(
183 object => $self->node,
184 error => "backend error: cannot find Perl class for backend: '$backend'",
185 );
186 };
187 17 my $file_to_load = $c{$class_to_load};
188  
189 17 $logger->trace("load_backend_class: loading class $class_to_load, $file_to_load");
190 17 eval { require $file_to_load; };
191  
192 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
193 0 die "Error with backend $backend: could not parse $file_to_load: $@\n";
194 }
195 17 return $class_to_load;
196 }
197  
198 98 ▶sub read_config_data ($self, %args) {
Subroutine read_config_data: called CC=7 SCAR=20.1
Pod undocumented
199 98 $logger->trace( "called for node ", $self->node->location );
200  
201 98 my $check = delete $args{check};
202 98 my $config_file_override = delete $args{config_file};
203 98 my $auto_create_override = delete $args{auto_create};
204  
205 98 ▶ croak "unexpected args " . join( ' ', keys %args ) . "\n" if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 98 98
206  
207 98 my $rw_config = dclone $self->rw_config ;
208  
209 98 my $instance = $self->node->instance();
210  
211 # root override is passed by the instance
212 98 my $root_dir = $instance->root_dir ;
213  
214 98 my $auto_create = $rw_config->{auto_create};
215 98 my $backend = $rw_config->{backend};
216  
217 98 ▶ if ( $rw_config->{default_layer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw_config->{"default_layer"}) 2 96 98
218 2 $self->read_config_sub_layer( $rw_config, $root_dir, $config_file_override, $check,
219 $backend );
220 }
221  
222 98 my ( $res, $file ) =
223 $self->try_read_backend( $rw_config, $root_dir, $config_file_override, $check, $backend );
224  
225 98 ▶ Config::Model::Exception::ConfigFile::Missing->throw (
Branch: 1 branch
BranchTrueFalseTotal
unless $res or $auto_create_override or $auto_create 0 98 98
Condition50%4/8
$res or $auto_create_override or $auto_create
l!l&&r!l&&!r
9260
$res or $auto_create_override
l!l&&r!l&&!r
9206
$file || '<unknown>'
l!l
00
MC/DC0%0/3
$res or $auto_create_override or $auto_create
$res $auto_create_override $auto_create
MC/DC0%0/1
$file || '<unknown>'
$file
Truth table50%2/4
A or B or C
A= $resB= $auto_create_overrideC= $auto_create
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $file
Aresult
11
00
226 file => $file || "<unknown>",
227 object => $self->node,
228 ) unless $res or $auto_create_override or $auto_create;
229  
230 98 return;
231 }
232  
233 2 ▶sub read_config_sub_layer ($self, $rw_config, $root_dir, $config_file_override, $check, $backend) {
Subroutine read_config_sub_layer: called CC=6 SCAR=18.0
Pod undocumented
234  
235 2 my $layered_config = delete $rw_config->{default_layer};
236 2 my $layered_read = dclone $rw_config ;
237  
238 2 foreach my $item ( qw/file config_dir os_config_dir/ ) {
239 6 my $lc = delete $layered_config->{$item};
240 6 ▶ $layered_read->{$item} = $lc if $lc;
Branch: 1 branch
BranchTrueFalseTotal
if $lc 4 2 6
241 }
242  
243 Config::Model::Exception::Model->throw(
244 2 ▶ error => "backend error: unexpected default_layer parameters: "
Branch: 1 branch
BranchTrueFalseTotal
if %$layered_config 0 2 2
245 . join( ' ', sort keys %$layered_config ),
246 object => $self->node,
247 ) if %$layered_config;
248  
249 2 my $i = $self->node->instance;
250 2 my $already_in_layered = $i->layered;
251  
252 # layered stuff here
253 2 ▶ if ( not $already_in_layered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($already_in_layered) 2 0 2
254 2 $i->layered_clear;
255 2 $i->layered_start;
256 }
257  
258 2 $self->try_read_backend( $layered_read, $root_dir, $config_file_override, $check, $backend );
259  
260 2 ▶ if ( not $already_in_layered ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($already_in_layered) 2 0 2
261 2 $i->layered_stop;
262 }
263 2 return;
264 }
265  
266 # called at configuration node creation, NOT when writing
267 100 ▶sub try_read_backend ($self, $rw_config, $root_dir, $config_file_override, $check, $backend) {
Subroutine try_read_backend: called CC=15 SCAR=34.6
Pod undocumented
268 100 my $read_dir = $self->get_tuned_config_dir(%$rw_config);
269  
270 100 my @read_args = (
271 %$rw_config,
272 root => $root_dir,
273 config_dir => $read_dir,
274 backend => $backend,
275 check => $check,
276 config_file => $config_file_override
277 );
278  
279 100 my $backend_obj = $self->backend_obj();
280  
281 100 ▶ if ($backend_obj->can('suffix')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend_obj->can("suffix")) 0 100 100
282 0 $logger->warn("suffix method is deprecated. you can remove it from backend $backend");
283 }
284  
285 100 my ( $file_ok, $file_path ) = $self->get_cfg_file_path(
286 @read_args,
287 skip_compute => $backend_obj->skip_open,
288 );
289  
290 100 my $fh;
291 100 ▶ if (not $backend_obj->skip_open and $file_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $backend_obj->skip_open and $file_ok) 58 42 100
Condition100%3/3
not $backend_obj->skip_open and $file_ok
!ll&&!rl&&r
291358
MC/DC100%2/2
not $backend_obj->skip_open and $file_ok
not $backend_obj->skip_open $file_ok
Truth table100%3/3
not(A) and B
A= not $backend_obj->skip_openB= $file_ok
ABresult
0X0
100
111
292 58 $fh = $self->open_read_file($file_path) ;
293 }
294  
295 100 ▶ my $f = $self->rw_config->{function} || 'read';
Condition50%1/2
$self->rw_config->{'function'} || 'read'
l!l
0100
MC/DC0%0/1
$self->rw_config->{'function'} || 'read'
$self->rw_config->{'function'}
Truth table50%1/2
A
A= $self->rw_config->{'function'}
Aresult
11
00
296 100 ▶ if ($logger->is_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_info) 0 100 100
297 0 ▶ my $fp = defined $file_path ? " on $file_path":'' ;
Branch: 1 branch
BranchTrueFalseTotal
defined $file_path ? : 0 0 0
298 0 $logger->info( "Read with $backend " . reftype($backend_obj) . "::$f".$fp);
299 }
300  
301 100 my $res;
302  
303 100 eval {
304 100 $res = $backend_obj->$f(
305 @read_args,
306 file_path => $file_path,
307 object => $self->node,
308 );
309 };
310 100 my $error = $@;
311  
312 # catch eval error
313 100 ▶ if ( ref($error) and $error->isa('Config::Model::Exception::Syntax') ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $error and $error->isa('Config::Model::Exception::Syntax')) { } 0 100 100
elsif (ref $error and $error->isa('Config::Model::Exception')) { } 0 100 100
elsif (ref $error) { } 0 100 100
elsif ($error) { } 0 100 100
Condition33%1/3
ref $error and $error->isa("Config::Model::Exception::Syntax")
!ll&&!rl&&r
10000
MC/DC0%0/2
ref $error and $error->isa("Config::Model::Exception::Syntax")
ref $error $error->isa("Config::Model::Exception::Syntax")
Truth table33%1/3
A and B
A= ref $errorB= $error->isa("Config::Model::Exception::Syntax")
ABresult
0X0
100
111
314  
315 0 ▶ $error->parsed_file( $file_path) unless $error->parsed_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $error->parsed_file 0 0 0
316 0 $error->rethrow;
317 }
318 ▶ elsif ( ref $error and $error->isa('Config::Model::Exception') ) {
Condition33%1/3
ref $error and $error->isa("Config::Model::Exception")
!ll&&!rl&&r
10000
MC/DC0%0/2
ref $error and $error->isa("Config::Model::Exception")
ref $error $error->isa("Config::Model::Exception")
Truth table33%1/3
A and B
A= ref $errorB= $error->isa("Config::Model::Exception")
ABresult
0X0
100
111
319 0 $error->rethrow ;
320 }
321 elsif ( ref $error ) {
322 0 die $error ;
323 }
324 elsif ( $error ) {
325 0 die "Backend $backend failed to read $file_path: $error";
326 }
327  
328 # only backend based on C::M::Backend::Any can support annotations
329 100 ▶ if ($backend_obj->can('annotation')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend_obj->can("annotation")) 100 0 100
330 100 $self->{support_annotation} = $backend_obj->annotation ;
331 }
332  
333 100 return ( $res, $file_path );
334 }
335  
336 98 ▶sub auto_write_init ($self) {
Subroutine auto_write_init: called CC=2 SCAR=6.9
Pod undocumented
337 98 my $rw_config = dclone $self->rw_config ;
338  
339 98 my $instance = $self->node->instance();
340  
341 # root override is passed by the instance
342 98 my $root_dir = $instance->root_dir;
343  
344 98 my $backend = $rw_config->{backend};
345  
346 98 my $write_dir = $self->get_tuned_config_dir(%$rw_config);
347  
348 98 $logger->trace( "auto_write_init creating write cb ($backend) for ", $self->node->name );
349  
350 98 my @wr_args = (
351 %$rw_config, # model data
352 config_dir => $write_dir, # override from instance
353 write => 1, # for get_cfg_file_path
354 root => $root_dir, # override from instance
355 );
356  
357 # used bby C::M::Dumper and C::M::DumpAsData
358 98 $self->{auto_write}{$backend} = 1;
359  
360 98 my $wb;
361 98 ▶ my $f = $rw_config->{function} || 'write';
Condition50%1/2
$rw_config->{'function'} || 'write'
l!l
098
MC/DC0%0/1
$rw_config->{'function'} || 'write'
$rw_config->{'function'}
Truth table50%1/2
A
A= $rw_config->{'function'}
Aresult
11
00
362 98 my $backend_class = $self->load_backend_class( $backend, $f );
363 98 my $location = $self->node->name;
364 98 my $node = $self->node; # closure
365  
366 # provide a proper write back function
367 56 ▶ $wb = sub (%cb_args) {
Subroutine __ANON__: called CC=10 SCAR=23.0
368 56 my $force_delete = delete $cb_args{force_delete} ;
369 56 ▶ $logger->debug( "write cb ($backend) called for $location ", $force_delete ? '' : ' (deleted)' );
Branch: 1 branch
BranchTrueFalseTotal
$force_delete ? : 3 53 56
370 56 my $backend_obj = $self->backend_obj();
371  
372 56 my ($fh, $file_ok, $file_path );
373  
374 56 ▶ if (not $backend_class->skip_open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($backend_class->skip_open) 40 16 56
375 40 ( $file_ok, $file_path ) = $self->get_cfg_file_path( @wr_args, %cb_args);
376 }
377  
378 56 ▶ if ($file_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ok) 35 21 56
379 35 $fh = $self->open_file_to_write( $backend, $file_path, delete $cb_args{backup} );
380 }
381  
382 # override needed for "save as" button
383 56 my %backend_args = (
384 @wr_args,
385 file_path => $file_path,
386 object => $node,
387 %cb_args # override from user
388 );
389  
390 56 my $res;
391 56 ▶ if ($force_delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_delete) { } 3 53 56
392 3 $backend_obj->delete(%backend_args);
393 }
394 else {
395 53 $res = eval { $backend_obj->$f( %backend_args ); };
396 53 my $error = $@;
397 53 ▶ $logger->error( "write backend $backend $backend_class" . '::' . "$f failed: $error" )
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 53 53
398 if $error;
399 53 $self->close_file_to_write( $error, $file_path, $rw_config->{file_mode} );
400  
401 $self->auto_delete($file_path, \%backend_args)
402 53 ▶ if $rw_config->{auto_delete} and not $backend_class->skip_open ;
Branch: 1 branch
BranchTrueFalseTotal
if $rw_config->{"auto_delete"} and not $backend_class->skip_open 8 45 53
Condition100%3/3
$rw_config->{"auto_delete"} and not $backend_class->skip_open
!ll&&!rl&&r
34118
MC/DC100%2/2
$rw_config->{"auto_delete"} and not $backend_class->skip_open
$rw_config->{"auto_delete"} not $backend_class->skip_open
Truth table100%3/3
A and not(B)
A= $rw_config->{"auto_delete"}B= not $backend_class->skip_open
ABresult
0X0
100
111
403 }
404  
405 56 ▶ return defined $res ? $res : $@ ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
defined $res ? : 34 22 56
$@ ? : 0 22 22
406 98 };
407  
408 98 $logger->trace( "registering write $backend in node " . $self->node->name );
409  
410 98 $instance->register_write_back( $self->node->location, $backend, $wb );
411 98 return;
412 }
413  
414 8 ▶sub auto_delete ($self, $file_path, $args) {
Subroutine auto_delete: called CC=7 SCAR=19.7
Pod documented
415  
416 8 ▶ return unless $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_path 5 3 8
417  
418 3 my $perl_data;
419 3 ▶ $perl_data = $self->node->dump_as_data( full_dump => $args->{full_dump} // 0)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->node 3 0 3
Condition50%1/2
$args->{"full_dump"} // 0
l!l
03
MC/DC0%0/1
$args->{"full_dump"} // 0
$args->{"full_dump"}
Truth table50%1/2
A
A= $args->{"full_dump"}
Aresult
11
00
420 if defined $self->node;
421  
422 3 ▶ my $size = ref($perl_data) eq 'HASH' ? scalar keys %$perl_data
Branch: 2 branches
BranchTrueFalseTotal
ref $perl_data eq 'HASH' ? : 1 2 3
ref $perl_data eq 'ARRAY' ? : 0 2 2
423 : ref($perl_data) eq 'ARRAY' ? scalar @$perl_data
424 : $perl_data ;
425 3 ▶ if (not $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size) 2 1 3
426 2 $logger->info( "Removing $file_path (no data to store)" );
427 2 unlink($file_path);
428 }
429 3 return;
430 }
431  
432  
433 35 ▶sub open_file_to_write ($self, $backend, $file_path, $backup) {
Subroutine open_file_to_write: called CC=6 SCAR=21.6
Pod undocumented
434 35 my $do_backup = defined $backup;
435 35 ▶ $backup ||= 'old'; # use old only if defined
Condition50%1/2
$backup ||= "old"
l!l
035
MC/DC0%0/1
$backup ||= "old"
$backup
Truth table50%1/2
A
A= $backup
Aresult
11
00
436 35 ▶ $backup = '.' . $backup unless $backup =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $backup =~ /^\./u 35 0 35
437  
438 # make sure that parent dir exists before creating file
439 35 $file_path->parent->mkpath;
440  
441 35 ▶ if ( $do_backup and $file_path->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_backup and $file_path->is_file) 0 35 35
Condition33%1/3
$do_backup and $file_path->is_file
!ll&&!rl&&r
3500
MC/DC0%0/2
$do_backup and $file_path->is_file
$do_backup $file_path->is_file
Truth table33%1/3
A and B
A= $do_backupB= $file_path->is_file
ABresult
0X0
100
111
442 0 ▶ $file_path->copy( $file_path.$backup ) or die "Backup copy failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
$file_path->copy($file_path . $backup) or die "Backup copy failed: $!" 0 0 0
443 }
444  
445 35 $logger->debug("$backend backend opened file $file_path to write");
446 35 return $file_path->filehandle(">",":utf8");
447 }
448  
449 53 ▶sub close_file_to_write ($self, $error, $file_path, $file_mode) {
Subroutine close_file_to_write: called CC=8 SCAR=28.3
Pod undocumented
450  
451 53 ▶ return unless defined $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_path 18 35 53
452  
453 35 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 35 35
454 # restore backup and display error
455 0 $logger->warn("Error during write, restoring backup data in $file_path" );
456 0 $file_path->append_utf8({ truncate => 1 }, $self->file_backup );
457 0 ▶ $error->rethrow if ref($error) and $error->can('rethrow');
Branch: 1 branch
BranchTrueFalseTotal
if ref $error and $error->can("rethrow") 0 0 0
Condition0%0/3
ref $error and $error->can("rethrow")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $error and $error->can("rethrow")
ref $error $error->can("rethrow")
Truth table0%0/3
A and B
A= ref $errorB= $error->can("rethrow")
ABresult
0X0
100
111
458 0 die $error;
459 }
460  
461 # TODO: move chmod in a backend role
462 35 ▶ $file_path->chmod($file_mode) if $file_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $file_mode 1 34 35
463  
464 # TODO: move in a backend role
465 # check file size and remove empty files
466 35 ▶ $file_path->remove if -z $file_path and not -l $file_path;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file_path and not -l $file_path 0 35 35
Condition33%1/3
-z $file_path and not -l $file_path
!ll&&!rl&&r
3500
MC/DC0%0/2
-z $file_path and not -l $file_path
-z $file_path not -l $file_path
Truth table33%1/3
A and not(B)
A= -z $file_pathB= not -l $file_path
ABresult
0X0
100
111
467  
468 35 return;
469 }
470  
471 1 ▶sub is_auto_write_for_type ($self, $type) {
Subroutine is_auto_write_for_type: called CC=2 SCAR=7.6
Pod undocumented
472 1 ▶ return $self->{auto_write}{$type} || 0;
Condition50%1/2
$self->{'auto_write'}{$type} || 0
l!l
01
MC/DC0%0/1
$self->{'auto_write'}{$type} || 0
$self->{'auto_write'}{$type}
Truth table50%1/2
A
A= $self->{'auto_write'}{$type}
Aresult
11
00
473 }
474  
475 59 __PACKAGE__->meta->make_immutable;
476  
477 59 1;
478  
479 # ABSTRACT: Load configuration node on demand
480  
481 __END__
« blib/lib/Config/Model/DumpAsData.pm ↑ Summary blib/lib/Config/Model/Iterator.pm »