File Coverage

blib/lib/Lemonldap/NG/Common/Conf/File.pm
Criterion Covered Total %
statement 13 72 18.0
branch 2 18 11.1
condition n/a
subroutine 4 12 33.3
pod 0 9 0.0
total 19 111 17.1


line stmt bran cond sub pod time code
1             package Lemonldap::NG::Common::Conf::File;
2              
3 1     1   3 use strict;
  1         2  
  1         33  
4 1     1   4 use Lemonldap::NG::Common::Conf::Constants; #inherits
  1         2  
  1         102  
5 1     1   372 use Lemonldap::NG::Common::Conf::Serializer;
  1         3  
  1         615  
6              
7             our $VERSION = '1.4.0';
8              
9             sub prereq {
10 1     1 0 3 my $self = shift;
11 1 50       5 unless ( $self->{dirName} ) {
12 0         0 $Lemonldap::NG::Common::Conf::msg .=
13             '"dirName" is required in "File" configuration type ! \n';
14 0         0 return 0;
15             }
16 1 50       22 unless ( -d $self->{dirName} ) {
17 0         0 $Lemonldap::NG::Common::Conf::msg .=
18             "Directory \"$self->{dirName}\" does not exist ! \n";
19 0         0 return 0;
20             }
21 1         7 1;
22             }
23              
24             sub available {
25 0     0 0   my $self = shift;
26 0           opendir D, $self->{dirName};
27 0           my @conf = readdir(D);
28 0           closedir D;
29 0 0         @conf = sort { $a <=> $b } map { /lmConf-(\d+)/ ? $1 : () } @conf;
  0            
  0            
30 0           return @conf;
31             }
32              
33             sub lastCfg {
34 0     0 0   my $self = shift;
35 0           my @avail = $self->available;
36 0           return $avail[$#avail];
37             }
38              
39             sub lock {
40 0     0 0   my $self = shift;
41 0 0         if ( $self->isLocked ) {
42 0           sleep 2;
43 0 0         return 0 if ( $self->isLocked );
44             }
45 0 0         unless ( open F, ">" . $self->{dirName} . "/lmConf.lock" ) {
46 0           $Lemonldap::NG::Common::Conf::msg .=
47             "Unable to lock (" . $self->{dirName} . "/lmConf.lock) \n";
48 0           return 0;
49             }
50 0           print F $$;
51 0           close F;
52 0           return 1;
53             }
54              
55             sub isLocked {
56 0     0 0   my $self = shift;
57 0           -e $self->{dirName} . "/lmConf.lock";
58             }
59              
60             sub unlock {
61 0     0 0   my $self = shift;
62 0           unlink $self->{dirName} . "/lmConf.lock";
63 0           1;
64             }
65              
66             sub store {
67 0     0 0   my ( $self, $fields ) = @_;
68 0           $fields = $self->serialize($fields);
69 0           my $mask = umask;
70 0           umask( oct('0027') );
71 0 0         unless ( open FILE,
72             '>' . $self->{dirName} . "/lmConf-" . $fields->{cfgNum} )
73             {
74 0           $Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n";
75 0           $self->unlock;
76 0           return UNKNOWN_ERROR;
77             }
78 0           foreach my $k ( sort keys %$fields ) {
79 0           print FILE "$k\n\t$fields->{$k}\n\n";
80             }
81 0           close FILE;
82 0           umask($mask);
83 0           return $fields->{cfgNum};
84             }
85              
86             sub load {
87 0     0 0   my ( $self, $cfgNum, $fields ) = @_;
88 0           my $f;
89 0           local $/ = "";
90 0           open FILE, $self->{dirName} . "/lmConf-$cfgNum";
91 0           while () {
92 0           my ( $k, $v ) = split /\n\s+/;
93 0           chomp $k;
94 0           $v =~ s/\n*$//;
95 0 0         if ($fields) {
96 0 0         $f->{$k} = $v if ( grep { $_ eq $k } @$fields );
  0            
97             }
98             else {
99 0           $f->{$k} = $v;
100             }
101             }
102 0           close FILE;
103 0           return $self->unserialize($f);
104             }
105              
106             sub delete {
107 0     0 0   my ( $self, $cfgNum ) = @_;
108 0           unlink( $self->{dirName} . "/lmConf-$cfgNum" );
109             }
110              
111             1;
112             __END__