2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
# COMMON INI MODULE
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Common::Ini;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2017-04-10 19:24:45 +02:00
|
|
|
use English '-no_match_vars';
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
use Exporter qw(import);
|
2015-08-29 20:20:46 +02:00
|
|
|
our @EXPORT = qw();
|
2018-06-11 20:52:26 +02:00
|
|
|
use File::Basename qw(dirname);
|
2015-06-14 00:25:49 +02:00
|
|
|
use JSON::PP;
|
|
|
|
use Storable qw(dclone);
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
2018-06-11 20:52:26 +02:00
|
|
|
use pgBackRest::LibC qw(:crypto);
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Version;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Boolean constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant INI_TRUE => JSON::PP::true;
|
2016-04-15 04:50:02 +02:00
|
|
|
push @EXPORT, qw(INI_TRUE);
|
2015-06-14 00:25:49 +02:00
|
|
|
use constant INI_FALSE => JSON::PP::false;
|
2016-04-15 04:50:02 +02:00
|
|
|
push @EXPORT, qw(INI_FALSE);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Ini control constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant INI_SECTION_BACKREST => 'backrest';
|
2015-06-22 19:11:07 +02:00
|
|
|
push @EXPORT, qw(INI_SECTION_BACKREST);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
use constant INI_KEY_CHECKSUM => 'backrest-checksum';
|
|
|
|
push @EXPORT, qw(INI_KEY_CHECKSUM);
|
|
|
|
use constant INI_KEY_FORMAT => 'backrest-format';
|
|
|
|
push @EXPORT, qw(INI_KEY_FORMAT);
|
|
|
|
use constant INI_KEY_VERSION => 'backrest-version';
|
|
|
|
push @EXPORT, qw(INI_KEY_VERSION);
|
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
use constant INI_SECTION_CIPHER => 'cipher';
|
|
|
|
push @EXPORT, qw(INI_SECTION_CIPHER);
|
|
|
|
|
|
|
|
use constant INI_KEY_CIPHER_PASS => 'cipher-pass';
|
|
|
|
push @EXPORT, qw(INI_KEY_CIPHER_PASS);
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Ini file copy extension
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant INI_COPY_EXT => '.copy';
|
|
|
|
push @EXPORT, qw(INI_COPY_EXT);
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Ini sort orders
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant INI_SORT_FORWARD => 'forward';
|
|
|
|
push @EXPORT, qw(INI_SORT_FORWARD);
|
|
|
|
use constant INI_SORT_REVERSE => 'reverse';
|
|
|
|
push @EXPORT, qw(INI_SORT_REVERSE);
|
|
|
|
use constant INI_SORT_NONE => 'none';
|
|
|
|
push @EXPORT, qw(INI_SORT_NONE);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# new()
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Load Storage::Helper module
|
|
|
|
require pgBackRest::Storage::Helper;
|
|
|
|
pgBackRest::Storage::Helper->import();
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
|
|
|
my $strOperation,
|
|
|
|
$self->{strFileName},
|
|
|
|
my $bLoad,
|
|
|
|
my $strContent,
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->{oStorage},
|
2017-04-10 19:24:45 +02:00
|
|
|
$self->{iInitFormat},
|
|
|
|
$self->{strInitVersion},
|
2017-06-09 23:51:41 +02:00
|
|
|
my $bIgnoreMissing,
|
2017-11-06 19:51:12 +02:00
|
|
|
$self->{strCipherPass}, # Passphrase to read/write the file
|
|
|
|
my $strCipherPassSub, # Passphrase to read/write subsequent files
|
2017-04-10 19:24:45 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->new', \@_,
|
|
|
|
{name => 'strFileName', trace => true},
|
|
|
|
{name => 'bLoad', optional => true, default => true, trace => true},
|
|
|
|
{name => 'strContent', optional => true, trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'oStorage', optional => true, default => storageLocal(), trace => true},
|
2017-04-10 19:24:45 +02:00
|
|
|
{name => 'iInitFormat', optional => true, default => BACKREST_FORMAT, trace => true},
|
|
|
|
{name => 'strInitVersion', optional => true, default => BACKREST_VERSION, trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'bIgnoreMissing', optional => true, default => false, trace => true},
|
2017-11-06 19:51:12 +02:00
|
|
|
{name => 'strCipherPass', optional => true, trace => true},
|
|
|
|
{name => 'strCipherPassSub', optional => true, trace => true},
|
2017-04-10 19:24:45 +02:00
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
if (defined($self->{oStorage}->cipherPassUser()) && !defined($self->{strCipherPass}))
|
|
|
|
{
|
2018-11-07 02:38:38 +02:00
|
|
|
confess &log(ERROR, 'passphrase is required when storage is encrypted', ERROR_CRYPTO);
|
2017-11-06 19:51:12 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Set changed to false
|
|
|
|
$self->{bModified} = false;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Set exists to false
|
|
|
|
$self->{bExists} = false;
|
|
|
|
|
|
|
|
# Load the file if requested
|
|
|
|
if ($bLoad)
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->load($bIgnoreMissing);
|
2017-04-10 19:24:45 +02:00
|
|
|
}
|
|
|
|
# Load from a string if provided
|
|
|
|
elsif (defined($strContent))
|
|
|
|
{
|
|
|
|
$self->{oContent} = iniParse($strContent);
|
|
|
|
$self->headerCheck();
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
# Initialize if not loading the file and not loading from string or if a load was attempted and the file does not exist
|
2017-06-09 23:51:41 +02:00
|
|
|
if (!$self->{bExists} && !defined($strContent))
|
2017-04-10 19:24:45 +02:00
|
|
|
{
|
|
|
|
$self->numericSet(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, $self->{iInitFormat});
|
|
|
|
$self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion});
|
2017-11-06 19:51:12 +02:00
|
|
|
|
|
|
|
# Determine if the passphrase section should be set
|
|
|
|
if (defined($self->{strCipherPass}) && defined($strCipherPassSub))
|
|
|
|
{
|
|
|
|
$self->set(INI_SECTION_CIPHER, INI_KEY_CIPHER_PASS, undef, $strCipherPassSub);
|
|
|
|
}
|
|
|
|
elsif ((defined($self->{strCipherPass}) && !defined($strCipherPassSub)) ||
|
|
|
|
(!defined($self->{strCipherPass}) && defined($strCipherPassSub)))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, 'a user passphrase and sub passphrase are both required when encrypting');
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-06-09 23:51:41 +02:00
|
|
|
# loadVersion() - load a version (main or copy) of the ini file
|
|
|
|
####################################################################################################################################
|
|
|
|
sub loadVersion
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $bCopy = shift;
|
|
|
|
my $bIgnoreError = shift;
|
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
# Make sure the file encryption setting is valid for the repo
|
|
|
|
if ($self->{oStorage}->encryptionValid($self->{oStorage}->encrypted($self->{strFileName} . ($bCopy ? INI_COPY_EXT : ''),
|
|
|
|
{bIgnoreMissing => $bIgnoreError})))
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2017-11-06 19:51:12 +02:00
|
|
|
# Load main
|
|
|
|
my $rstrContent = $self->{oStorage}->get(
|
|
|
|
$self->{oStorage}->openRead($self->{strFileName} . ($bCopy ? INI_COPY_EXT : ''),
|
|
|
|
{bIgnoreMissing => $bIgnoreError, strCipherPass => $self->{strCipherPass}}));
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
# If the file exists then attempt to parse it
|
|
|
|
if (defined($rstrContent))
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2017-11-06 19:51:12 +02:00
|
|
|
my $rhContent = iniParse($$rstrContent, {bIgnoreInvalid => $bIgnoreError});
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
# If the content is valid then check the header
|
|
|
|
if (defined($rhContent))
|
2017-06-09 23:51:41 +02:00
|
|
|
{
|
2017-11-06 19:51:12 +02:00
|
|
|
$self->{oContent} = $rhContent;
|
|
|
|
|
|
|
|
# If the header is invalid then undef content
|
|
|
|
if (!$self->headerCheck({bIgnoreInvalid => $bIgnoreError}))
|
|
|
|
{
|
|
|
|
delete($self->{oContent});
|
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 19:51:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to parse '$self->{strFileName}" . ($bCopy ? INI_COPY_EXT : '') . "'" .
|
2018-11-07 02:38:38 +02:00
|
|
|
"\nHINT: Is or was the repo encrypted?", ERROR_CRYPTO);
|
2017-11-06 19:51:12 +02:00
|
|
|
}
|
2017-06-09 23:51:41 +02:00
|
|
|
|
|
|
|
return defined($self->{oContent});
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# load() - load the ini
|
2017-04-10 19:24:45 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub load
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-06-09 23:51:41 +02:00
|
|
|
my $bIgnoreMissing = shift;
|
|
|
|
|
|
|
|
# If main was not loaded then try the copy
|
|
|
|
if (!$self->loadVersion(false, true))
|
|
|
|
{
|
|
|
|
if (!$self->loadVersion(true, true))
|
|
|
|
{
|
|
|
|
return if $bIgnoreMissing;
|
|
|
|
|
|
|
|
confess &log(ERROR, "unable to open $self->{strFileName} or $self->{strFileName}" . INI_COPY_EXT, ERROR_FILE_MISSING);
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
$self->{bExists} = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# headerCheck() - check that version and checksum in header are as expected
|
|
|
|
####################################################################################################################################
|
|
|
|
sub headerCheck
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$bIgnoreInvalid,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '->headerCheck', \@_,
|
|
|
|
{name => 'bIgnoreInvalid', optional => true, default => false, trace => true},
|
|
|
|
);
|
|
|
|
|
|
|
|
# Eval so exceptions can be ignored on bIgnoreInvalid
|
|
|
|
my $bValid = true;
|
|
|
|
|
|
|
|
eval
|
|
|
|
{
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
# Make sure the ini is valid by testing checksum
|
2017-04-10 19:24:45 +02:00
|
|
|
my $strChecksum = $self->get(INI_SECTION_BACKREST, INI_KEY_CHECKSUM, undef, false);
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strTestChecksum = $self->hash();
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if (!defined($strChecksum) || $strChecksum ne $strTestChecksum)
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
confess &log(ERROR,
|
|
|
|
"invalid checksum in '$self->{strFileName}', expected '${strTestChecksum}' but found " .
|
|
|
|
(defined($strChecksum) ? "'${strChecksum}'" : '[undef]'),
|
|
|
|
ERROR_CHECKSUM);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure that the format is current, otherwise error
|
|
|
|
my $iFormat = $self->get(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, false, 0);
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if ($iFormat != $self->{iInitFormat})
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
confess &log(ERROR,
|
|
|
|
"invalid format in '$self->{strFileName}', expected $self->{iInitFormat} but found ${iFormat}", ERROR_FORMAT);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2015-09-08 22:58:00 +02:00
|
|
|
|
|
|
|
# Check if the version has changed
|
2017-04-10 19:24:45 +02:00
|
|
|
if (!$self->test(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion}))
|
2015-09-08 22:58:00 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion});
|
2015-09-08 22:58:00 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
return true;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
or do
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Confess the error if it should not be ignored
|
|
|
|
if (!$bIgnoreInvalid)
|
|
|
|
{
|
|
|
|
confess $EVAL_ERROR;
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Return false when errors are ignored
|
|
|
|
$bValid = false;
|
|
|
|
};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bValid', value => $bValid, trace => true}
|
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# iniParse() - parse from standard INI format to a hash.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
push @EXPORT, qw(iniParse);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
sub iniParse
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strContent,
|
|
|
|
$bRelaxed,
|
|
|
|
$bIgnoreInvalid,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::iniParse', \@_,
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'strContent', required => false, trace => true},
|
2017-04-10 19:24:45 +02:00
|
|
|
{name => 'bRelaxed', optional => true, default => false, trace => true},
|
|
|
|
{name => 'bIgnoreInvalid', optional => true, default => false, trace => true},
|
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Ini content
|
|
|
|
my $oContent = undef;
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strSection;
|
|
|
|
|
|
|
|
# Create the JSON object
|
|
|
|
my $oJSON = JSON::PP->new()->allow_nonref();
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Eval so exceptions can be ignored on bIgnoreInvalid
|
|
|
|
eval
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Read the INI file
|
2017-06-09 23:51:41 +02:00
|
|
|
foreach my $strLine (split("\n", defined($strContent) ? $strContent : ''))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strLine = trim($strLine);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Skip lines that are blank or comments
|
|
|
|
if ($strLine ne '' && $strLine !~ '^[ ]*#.*')
|
|
|
|
{
|
|
|
|
# Get the section
|
|
|
|
if (index($strLine, '[') == 0)
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strSection = substr($strLine, 1, length($strLine) - 2);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!defined($strSection))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "key/value pair '${strLine}' found outside of a section", ERROR_CONFIG);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Get key and value
|
|
|
|
my $iIndex = index($strLine, '=');
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if ($iIndex == -1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to find '=' in '${strLine}'", ERROR_CONFIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strKey = substr($strLine, 0, $iIndex);
|
|
|
|
my $strValue = substr($strLine, $iIndex + 1);
|
|
|
|
|
|
|
|
# If relaxed then read the value directly
|
|
|
|
if ($bRelaxed)
|
2016-04-14 15:30:54 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
if (defined($oContent->{$strSection}{$strKey}))
|
2016-04-14 15:30:54 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
if (ref($oContent->{$strSection}{$strKey}) ne 'ARRAY')
|
|
|
|
{
|
|
|
|
$oContent->{$strSection}{$strKey} = [$oContent->{$strSection}{$strKey}];
|
|
|
|
}
|
2016-04-14 15:30:54 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
push(@{$oContent->{$strSection}{$strKey}}, $strValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oContent->{$strSection}{$strKey} = $strValue;
|
|
|
|
}
|
2016-04-14 15:30:54 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
# Else read the value as stricter JSON
|
2016-04-14 15:30:54 +02:00
|
|
|
else
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
${$oContent}{$strSection}{$strKey} = $oJSON->decode($strValue);
|
2016-04-14 15:30:54 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
# Error if the file is empty
|
|
|
|
if (!($bRelaxed || defined($oContent)))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'no key/value pairs found', ERROR_CONFIG);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
or do
|
|
|
|
{
|
|
|
|
# Confess the error if it should not be ignored
|
|
|
|
if (!$bIgnoreInvalid)
|
|
|
|
{
|
|
|
|
confess $EVAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Undef content when errors are ignored
|
|
|
|
undef($oContent);
|
|
|
|
};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'oContent', value => $oContent, trace => true}
|
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# save() - save the file.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub save
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Save only if modified
|
2017-04-10 19:24:45 +02:00
|
|
|
if ($self->{bModified})
|
|
|
|
{
|
|
|
|
# Calculate the hash
|
|
|
|
$self->hash();
|
|
|
|
|
|
|
|
# Save the file
|
2017-11-06 19:51:12 +02:00
|
|
|
$self->{oStorage}->put($self->{strFileName}, iniRender($self->{oContent}), {strCipherPass => $self->{strCipherPass}});
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->{oStorage}->pathSync(dirname($self->{strFileName}));
|
2017-11-06 19:51:12 +02:00
|
|
|
$self->{oStorage}->put($self->{strFileName} . INI_COPY_EXT, iniRender($self->{oContent}),
|
|
|
|
{strCipherPass => $self->{strCipherPass}});
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->{oStorage}->pathSync(dirname($self->{strFileName}));
|
2017-04-10 19:24:45 +02:00
|
|
|
$self->{bModified} = false;
|
2016-10-14 13:21:47 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Indicate the file now exists
|
|
|
|
$self->{bExists} = true;
|
|
|
|
|
|
|
|
# File was saved
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# File was not saved
|
|
|
|
return false;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# saveCopy - save only a copy of the file.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub saveCopy
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if ($self->{oStorage}->exists($self->{strFileName}))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "cannot save copy only when '$self->{strFileName}' exists");
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->hash();
|
2017-11-06 19:51:12 +02:00
|
|
|
$self->{oStorage}->put($self->{strFileName} . INI_COPY_EXT, iniRender($self->{oContent}),
|
|
|
|
{strCipherPass => $self->{strCipherPass}});
|
2017-06-09 23:51:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# iniRender() - render hash to standard INI format.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
push @EXPORT, qw(iniRender);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
sub iniRender
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2015-09-02 01:05:10 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$oContent,
|
2016-02-12 04:42:27 +02:00
|
|
|
$bRelaxed,
|
2015-09-02 01:05:10 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2017-04-10 19:24:45 +02:00
|
|
|
__PACKAGE__ . '::iniRender', \@_,
|
2015-09-02 01:05:10 +02:00
|
|
|
{name => 'oContent', trace => true},
|
2016-04-14 15:30:54 +02:00
|
|
|
{name => 'bRelaxed', default => false, trace => true},
|
2015-09-02 01:05:10 +02:00
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
# Open the ini file for writing
|
2017-04-10 19:24:45 +02:00
|
|
|
my $strContent = '';
|
2015-06-14 00:25:49 +02:00
|
|
|
my $bFirst = true;
|
|
|
|
|
|
|
|
# Create the JSON object canonical so that fields are alpha ordered to pass unit tests
|
|
|
|
my $oJSON = JSON::PP->new()->canonical()->allow_nonref();
|
|
|
|
|
|
|
|
# Write the INI file
|
2015-06-21 18:06:13 +02:00
|
|
|
foreach my $strSection (sort(keys(%$oContent)))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
|
|
|
# Add a linefeed between sections
|
|
|
|
if (!$bFirst)
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strContent .= "\n";
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Write the section
|
2017-04-10 19:24:45 +02:00
|
|
|
$strContent .= "[${strSection}]\n";
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
# Iterate through all keys in the section
|
2017-04-10 19:24:45 +02:00
|
|
|
foreach my $strKey (sort(keys(%{$oContent->{$strSection}})))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
|
|
|
# If the value is a hash then convert it to JSON, otherwise store as is
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strValue = ${$oContent}{$strSection}{$strKey};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
# If relaxed then store as old-style config
|
|
|
|
if ($bRelaxed)
|
|
|
|
{
|
2016-04-14 15:30:54 +02:00
|
|
|
# If the value is an array then save each element to a separate key/value pair
|
|
|
|
if (ref($strValue) eq 'ARRAY')
|
|
|
|
{
|
|
|
|
foreach my $strArrayValue (@{$strValue})
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strContent .= "${strKey}=${strArrayValue}\n";
|
2016-04-14 15:30:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else write a standard key/value pair
|
|
|
|
else
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strContent .= "${strKey}=${strValue}\n";
|
2016-04-14 15:30:54 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
# Else write as stricter JSON
|
|
|
|
else
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$strContent .= "${strKey}=" . $oJSON->encode($strValue) . "\n";
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bFirst = false;
|
|
|
|
}
|
|
|
|
|
2015-09-02 01:05:10 +02:00
|
|
|
# Return from function and log return values if any
|
2017-04-10 19:24:45 +02:00
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strContent', value => $strContent, trace => true}
|
|
|
|
);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# hash() - generate hash for the manifest.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub hash
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Remove the old checksum
|
2017-04-10 19:24:45 +02:00
|
|
|
delete($self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM});
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
# Set the new checksum
|
2018-06-11 20:52:26 +02:00
|
|
|
$self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} =
|
|
|
|
cryptoHashOne('sha1', JSON::PP->new()->canonical()->allow_nonref()->encode($self->{oContent}));
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
return $self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM};
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# get() - get a value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub get
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
2017-04-10 19:24:45 +02:00
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
2015-06-14 00:25:49 +02:00
|
|
|
my $bRequired = shift;
|
|
|
|
my $oDefault = shift;
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Parameter constraints
|
2015-06-14 00:25:49 +02:00
|
|
|
if (!defined($strSection))
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
confess &log(ASSERT, 'strSection is required');
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if (defined($strSubKey) && !defined($strKey))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "strKey is required when strSubKey '${strSubKey}' is requested");
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Get the result
|
|
|
|
my $oResult = $self->{oContent}->{$strSection};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if (defined($strKey) && defined($oResult))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$oResult = $oResult->{$strKey};
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
if (defined($strSubKey) && defined($oResult))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$oResult = $oResult->{$strSubKey};
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
# When result is not defined
|
|
|
|
if (!defined($oResult))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Error if a result is required
|
|
|
|
if (!defined($bRequired) || $bRequired)
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
confess &log(ASSERT, "strSection '$strSection'" . (defined($strKey) ? ", strKey '$strKey'" : '') .
|
|
|
|
(defined($strSubKey) ? ", strSubKey '$strSubKey'" : '') . ' is required but not defined');
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Return default if specified
|
|
|
|
if (defined($oDefault))
|
|
|
|
{
|
|
|
|
return $oDefault;
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $oResult
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# boolGet() - get a boolean value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub boolGet
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
2017-04-10 19:24:45 +02:00
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
my $bRequired = shift;
|
|
|
|
my $bDefault = shift;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
return $self->get(
|
|
|
|
$strSection, $strKey, $strSubKey, $bRequired,
|
|
|
|
defined($bDefault) ? ($bDefault ? INI_TRUE : INI_FALSE) : undef) ? true : false;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# numericGet() - get a numeric value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub numericGet
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
2017-04-10 19:24:45 +02:00
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
my $bRequired = shift;
|
|
|
|
my $nDefault = shift;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
return $self->get($strSection, $strKey, $strSubKey, $bRequired, defined($nDefault) ? $nDefault + 0 : undef) + 0;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# set - set a value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub set
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
2017-04-10 19:24:45 +02:00
|
|
|
my $oValue = shift;
|
|
|
|
|
|
|
|
# Parameter constraints
|
|
|
|
if (!(defined($strSection) && defined($strKey)))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, 'strSection and strKey are required');
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
my $oCurrentValue;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
if (defined($strSubKey))
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$oCurrentValue = \$self->{oContent}{$strSection}{$strKey}{$strSubKey};
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
$oCurrentValue = \$self->{oContent}{$strSection}{$strKey};
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
if (!defined($$oCurrentValue) ||
|
|
|
|
defined($oCurrentValue) != defined($oValue) ||
|
|
|
|
${dclone($oCurrentValue)} ne ${dclone(\$oValue)})
|
|
|
|
{
|
|
|
|
$$oCurrentValue = $oValue;
|
|
|
|
|
|
|
|
if (!$self->{bModified})
|
|
|
|
{
|
|
|
|
$self->{bModified} = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# boolSet - set a boolean value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
2015-08-29 20:20:46 +02:00
|
|
|
sub boolSet
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
|
|
|
my $bValue = shift;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
$self->set($strSection, $strKey, $strSubKey, $bValue ? INI_TRUE : INI_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# numericSet - set a numeric value.
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub numericSet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
|
|
|
my $nValue = shift;
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
$self->set($strSection, $strKey, $strSubKey, defined($nValue) ? $nValue + 0 : undef);
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# remove - remove a value.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub remove
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
|
|
|
my $strKey = shift;
|
|
|
|
my $strSubKey = shift;
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Test if the value exists
|
|
|
|
if ($self->test($strSection, $strKey, $strSubKey))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Remove a subkey
|
|
|
|
if (defined($strSubKey))
|
|
|
|
{
|
|
|
|
delete($self->{oContent}{$strSection}{$strKey}{$strSubKey});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove a key
|
|
|
|
if (defined($strKey))
|
|
|
|
{
|
|
|
|
if (!defined($strSubKey))
|
|
|
|
{
|
|
|
|
delete($self->{oContent}{$strSection}{$strKey});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove the section if it is now empty
|
|
|
|
if (keys(%{$self->{oContent}{$strSection}}) == 0)
|
|
|
|
{
|
|
|
|
delete($self->{oContent}{$strSection});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove a section
|
|
|
|
if (!defined($strKey))
|
|
|
|
{
|
|
|
|
delete($self->{oContent}{$strSection});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Record changes
|
|
|
|
if (!$self->{bModified})
|
|
|
|
{
|
|
|
|
$self->{bModified} = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
|
|
|
|
return false;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# keys - get the list of keys in a section.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub keys
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
2016-09-06 15:35:02 +02:00
|
|
|
my $strSortOrder = shift;
|
|
|
|
|
|
|
|
if ($self->test($strSection))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
if (!defined($strSortOrder) || $strSortOrder eq INI_SORT_FORWARD)
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2016-09-06 15:35:02 +02:00
|
|
|
return (sort(keys(%{$self->get($strSection)})));
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
elsif ($strSortOrder eq INI_SORT_REVERSE)
|
2016-09-06 15:35:02 +02:00
|
|
|
{
|
|
|
|
return (sort {$b cmp $a} (keys(%{$self->get($strSection)})));
|
|
|
|
}
|
2017-04-10 19:24:45 +02:00
|
|
|
elsif ($strSortOrder eq INI_SORT_NONE)
|
2016-09-06 15:35:02 +02:00
|
|
|
{
|
|
|
|
return (keys(%{$self->get($strSection)}));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "invalid strSortOrder '${strSortOrder}'");
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:35:02 +02:00
|
|
|
my @stryEmptyArray;
|
|
|
|
return @stryEmptyArray;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# test - test a value.
|
2015-06-14 00:25:49 +02:00
|
|
|
#
|
2017-04-10 19:24:45 +02:00
|
|
|
# Test a value to see if it equals the supplied test value. If no test value is given, tests that the section, key, or subkey
|
|
|
|
# is defined.
|
2015-06-14 00:25:49 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub test
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
|
|
|
my $strValue = shift;
|
|
|
|
my $strSubValue = shift;
|
|
|
|
my $strTest = shift;
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Get the value
|
2015-06-14 00:25:49 +02:00
|
|
|
my $strResult = $self->get($strSection, $strValue, $strSubValue, false);
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
# Is there a result
|
2015-06-14 00:25:49 +02:00
|
|
|
if (defined($strResult))
|
|
|
|
{
|
2017-04-10 19:24:45 +02:00
|
|
|
# Is there a value to test against?
|
2015-06-14 00:25:49 +02:00
|
|
|
if (defined($strTest))
|
|
|
|
{
|
2018-06-06 21:52:28 +02:00
|
|
|
# Make sure these are explicit strings or Devel::Cover thinks they are equal if one side is a boolean
|
|
|
|
return ($strResult . '') eq ($strTest . '') ? true : false;
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2017-04-10 19:24:45 +02:00
|
|
|
# boolTest - test a boolean value, see test().
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub boolTest
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strSection = shift;
|
|
|
|
my $strValue = shift;
|
|
|
|
my $strSubValue = shift;
|
|
|
|
my $bTest = shift;
|
|
|
|
|
|
|
|
return $self->test($strSection, $strValue, $strSubValue, defined($bTest) ? ($bTest ? INI_TRUE : INI_FALSE) : undef);
|
|
|
|
}
|
|
|
|
|
2017-11-06 19:51:12 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# cipherPassSub - gets the passphrase (if it exists) used to read/write subsequent files
|
|
|
|
####################################################################################################################################
|
|
|
|
sub cipherPassSub
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->get(INI_SECTION_CIPHER, INI_KEY_CIPHER_PASS, undef, false);
|
|
|
|
}
|
|
|
|
|
2017-04-10 19:24:45 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Properties.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub modified {shift->{bModified}} # Has the data been modified since last load/save?
|
|
|
|
sub exists {shift->{bExists}} # Is the data persisted to file?
|
2017-11-06 19:51:12 +02:00
|
|
|
sub cipherPass {shift->{strCipherPass}} # Return passphrase (will be undef if repo not encrypted)
|
2017-04-10 19:24:45 +02:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
1;
|