blib/lib/Lemonldap/NG/Common/Conf/Backends/File.pm

filter: Statementstmt 41.2% 47 / 114 Branchbran 19.0% 8 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 9 totaltotal 35.0% 63 / 180 CC 22 SCAR 31.9
CC
22
Cov
35%
load
59.0
lock
20.0
delete
12.0
CRAP 24.2
« blib/lib/Lemonldap/NG/Common/Apache/Session/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Backends/Patroni.pm »
1 package Lemonldap::NG::Common::Conf::Backends::File;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Lemonldap::NG::Common::Conf::Constants; #inherits
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '2.20.2';
9 3 our $initDone;
10  
11 0 ▶sub Lemonldap::NG::Common::Conf::_lock {
Subroutine _lock: not called CC=1 SCAR=6.9
12 0 my ( $self, $cfgNum ) = @_;
13 0 return "$self->{dirName}/lmConf.lock";
14 }
15  
16 25 ▶sub Lemonldap::NG::Common::Conf::_file {
Subroutine _file: called CC=1 SCAR=0.0
17 25 my ( $self, $cfgNum ) = @_;
18 25 return "$self->{dirName}/lmConf-$cfgNum.json";
19 }
20  
21 4 ▶sub prereq {
Subroutine prereq: called CC=3 SCAR=14.2
Pod undocumented
22 4 my $self = shift;
23 4 ▶ unless ( $self->{dirName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dirName"}) 0 4 4
24 0 $Lemonldap::NG::Common::Conf::msg .=
25 "'dirName' is required in 'File' configuration type ! \n";
26 0 return 0;
27 }
28 4 ▶ unless ( -d $self->{dirName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"dirName"}) 0 4 4
29 0 $Lemonldap::NG::Common::Conf::msg .=
30 "Directory \"$self->{dirName}\" does not exist ! \n";
31 0 return 0;
32 }
33 4 1;
34 }
35  
36 0 ▶sub available {
Subroutine available: not called CC=2 SCAR=17.9
Pod undocumented
37 0 my $self = shift;
38 0 opendir D, $self->{dirName};
39 0 my @conf = readdir(D);
40 0 closedir D;
41 @conf =
42 0 sort { $a <=> $b }
43 0 ▶ map { /^lmConf-(\d+)(?:\.js(?:on))?$/ ? ( $1 + 0 ) : () } @conf;
Branch: 1 branch
BranchTrueFalseTotal
/^lmConf-(\d+)(?:\.js(?:on))?$/ ? : 0 0 0
44 0 return @conf;
45 }
46  
47 0 ▶sub lastCfg {
Subroutine lastCfg: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $self = shift;
49 0 my @avail = $self->available;
50 0 return $avail[$#avail];
51 }
52  
53 0 ▶sub lock {
Subroutine lock: not called CC=4 SCAR=30.0
Pod undocumented
54 0 my $self = shift;
55 0 ▶ if ( $self->isLocked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isLocked) 0 0 0
56 0 sleep 2;
57 0 ▶ return 0 if ( $self->isLocked );
Branch: 1 branch
BranchTrueFalseTotal
if $self->isLocked 0 0 0
58 }
59 0 ▶ unless ( open F, ">", $self->_lock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open F, ">", $self->_lock) 0 0 0
60 0 $Lemonldap::NG::Common::Conf::msg .=
61 "Unable to lock (" . $self->_lock . ") \n";
62 0 return 0;
63 }
64 0 print F $$;
65 0 close F;
66 0 return 1;
67 }
68  
69 0 ▶sub isLocked {
Subroutine isLocked: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my $self = shift;
71 0 -e $self->_lock;
72 }
73  
74 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my $self = shift;
76 0 unlink $self->_lock;
77 0 1;
78 }
79  
80 7 ▶sub store {
Subroutine store: called CC=3 SCAR=11.6
Pod undocumented
81 7 my ( $self, $fields ) = @_;
82 7 my $mask = umask;
83 7 umask( oct('0027') );
84 7 ▶ unless ( open FILE, '>', $self->_file( $fields->{cfgNum} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, ">", $self->_file($fields->{"cfgNum"})) 0 7 7
85 0 $Lemonldap::NG::Common::Conf::msg = "Open file failed: $! \n";
86 0 $self->unlock;
87 0 return UNKNOWN_ERROR;
88 }
89 7 binmode(FILE);
90 my $json_options = {
91 allow_nonref => 1,
92 (
93 $self->{prettyPrint}
94 ? (
95 7 ▶ pretty => 1,
Branch: 1 branch
BranchTrueFalseTotal
$self->{'prettyPrint'} ? : 0 7 7
96 canonical => 1
97 )
98 : ()
99 )
100 };
101 7 my $f = to_json( $fields, $json_options );
102 7 print FILE $f;
103 7 close FILE;
104 7 umask($mask);
105 7 return $fields->{cfgNum};
106 }
107  
108 9 ▶sub load {
Subroutine load: called CC=12 SCAR=40.8
Pod undocumented
109 9 my ( $self, $cfgNum, $fields ) = @_;
110 9 my ( $f, $filename );
111 9 ▶ if ( -e $self->_file($cfgNum) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $self->_file($cfgNum)) { } 9 0 9
elsif (-e "$self->{'dirName'}/lmConf-$cfgNum.js") { } 0 0 0
112 9 $filename = $self->_file($cfgNum);
113 }
114 elsif ( -e "$self->{dirName}/lmConf-$cfgNum.js" ) {
115 0 $filename = "$self->{dirName}/lmConf-$cfgNum.js";
116 }
117 9 ▶ if ($filename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename) { } 9 0 9
elsif (-e "$self->{'dirName'}/lmConf-$cfgNum") { } 0 0 0
118 9 local $/ = '';
119 9 my $ret;
120 9 ▶ unless ( open FILE, '<', $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, "<", $filename) 0 9 9
121 0 $Lemonldap::NG::Common::Conf::msg .= "Read error: $!$@";
122 0 return undef;
123 }
124 9 binmode FILE;
125 9 $f = join( '', <FILE> );
126 9 close FILE;
127 9 eval { $ret = from_json( $f, { allow_nonref => 1 } ) };
128 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
129 0 print STDERR "$@\n";
130 0 $Lemonldap::NG::Common::Conf::msg .=
131 "JSON fails to read file: $@ \n";
132 0 return undef;
133 }
134 9 return $ret;
135 }
136  
137 # Old format
138 elsif ( -e "$self->{dirName}/lmConf-$cfgNum" ) {
139 0 ▶ open FILE, '<', "$self->{dirName}/lmConf-$cfgNum" or die "$!$@";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", qq[$self->{"dirName"}/lmConf-$cfgNum] or die "$!$@" 0 0 0
140 0 local $/ = "";
141 0 ▶ unless ( open FILE, '<', $self->{dirName} . "/lmConf-$cfgNum" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, "<", $self->{"dirName"} . "/lmConf-$cfgNum") 0 0 0
142 0 $Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n";
143 0 return undef;
144 }
145 0 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
146 0 my ( $k, $v ) = split /\n\s+/;
147 0 chomp $k;
148 0 $v =~ s/\n*$//;
149 0 ▶ if ($fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields) { } 0 0 0
150 0 ▶ $f->{$k} = $v if ( grep { $_ eq $k } @$fields );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $k;} @$fields 0 0 0
151 }
152 else {
153 0 $f->{$k} = $v;
154 }
155 }
156 0 close FILE;
157 0 require Lemonldap::NG::Common::Conf::Serializer;
158 0 return $self->unserialize($f);
159 }
160 else {
161 0 $Lemonldap::NG::Common::Conf::msg .=
162 "Unable to find configuration file";
163 0 return undef;
164 }
165 }
166  
167 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod undocumented
168 0 my ( $self, $cfgNum ) = @_;
169 0 my $file = $self->_file($cfgNum);
170 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 0 0 0
171 0 my $res = unlink($file);
172 0 ▶ $Lemonldap::NG::Common::Conf::msg .= $! unless ($res);
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
173 0 return $res;
174 }
175 else {
176 0 $Lemonldap::NG::Common::Conf::msg .=
177 "Unable to delete conf $cfgNum, no such file";
178 0 return 0;
179 }
180 }
181  
182 3 1;
183 __END__
« blib/lib/Lemonldap/NG/Common/Apache/Session/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/Backends/Patroni.pm »