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

114 lines
2.5 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Common::Conf::File;
use strict;
2010-03-01 21:32:28 +01:00
use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Conf::Serializer;
2012-07-11 15:34:58 +02:00
our $VERSION = '1.2.2';
sub prereq {
my $self = shift;
unless ( $self->{dirName} ) {
$Lemonldap::NG::Common::Conf::msg .=
'"dirName" is required in "File" configuration type ! \n';
return 0;
}
unless ( -d $self->{dirName} ) {
$Lemonldap::NG::Common::Conf::msg .=
"Directory \"$self->{dirName}\" does not exist ! \n";
return 0;
}
1;
}
sub available {
my $self = shift;
opendir D, $self->{dirName};
my @conf = readdir(D);
closedir D;
@conf = sort { $a <=> $b } map { /lmConf-(\d+)/ ? $1 : () } @conf;
return @conf;
}
sub lastCfg {
my $self = shift;
my @avail = $self->available;
return $avail[$#avail];
}
sub lock {
2007-03-09 20:46:09 +01:00
my $self = shift;
if ( $self->isLocked ) {
2007-03-09 20:46:09 +01:00
sleep 2;
return 0 if ( $self->isLocked );
2007-03-09 20:46:09 +01:00
}
unless ( open F, ">" . $self->{dirName} . "/lmConf.lock" ) {
$Lemonldap::NG::Common::Conf::msg .=
"Unable to lock (" . $self->{dirName} . "/lmConf.lock) \n";
2007-03-09 20:46:09 +01:00
return 0;
}
print F $$;
close F;
return 1;
}
sub isLocked {
2007-03-09 20:46:09 +01:00
my $self = shift;
-e $self->{dirName} . "/lmConf.lock";
}
sub unlock {
2007-03-09 20:46:09 +01:00
my $self = shift;
unlink $self->{dirName} . "/lmConf.lock";
1;
}
sub store {
my ( $self, $fields ) = @_;
$fields = $self->serialize($fields);
my $mask = umask;
umask( oct('0027') );
unless ( open FILE,
'>' . $self->{dirName} . "/lmConf-" . $fields->{cfgNum} )
{
$Lemonldap::NG::Common::Conf::msg .= "Open file failed: $! \n";
$self->unlock;
return UNKNOWN_ERROR;
}
2010-03-01 21:32:28 +01:00
foreach my $k ( sort keys %$fields ) {
print FILE "$k\n\t$fields->{$k}\n\n";
}
close FILE;
umask($mask);
$self->unlock;
return $fields->{cfgNum};
}
sub load {
my ( $self, $cfgNum, $fields ) = @_;
my $f;
local $/ = "";
open FILE, $self->{dirName} . "/lmConf-$cfgNum";
while (<FILE>) {
my ( $k, $v ) = split /\n\s+/;
chomp $k;
$v =~ s/\n*$//;
if ($fields) {
$f->{$k} = $v if ( grep { $_ eq $k } @$fields );
}
else {
$f->{$k} = $v;
}
}
close FILE;
return $self->unserialize($f);
}
sub delete {
my ( $self, $cfgNum ) = @_;
unlink( $self->{dirName} . "/lmConf-$cfgNum" );
}
1;
__END__