Debian Codegraph @

lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/File.pm

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