2014-12-16 00:20:42 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONFIG MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Config::Config;
|
2014-12-16 00:20:42 +02:00
|
|
|
|
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-12-16 00:20:42 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
use Cwd qw(abs_path);
|
|
|
|
use Exporter qw(import);
|
2015-08-29 20:20:46 +02:00
|
|
|
our @EXPORT = qw();
|
2015-07-02 16:05:13 +02:00
|
|
|
use File::Basename qw(dirname basename);
|
2015-06-14 00:25:49 +02:00
|
|
|
use Getopt::Long qw(GetOptions);
|
2016-07-29 20:02:11 +02:00
|
|
|
use Storable qw(dclone);
|
2014-12-16 00:20:42 +02:00
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Ini;
|
2017-06-09 23:51:41 +02:00
|
|
|
use pgBackRest::Common::Io::Base;
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Log;
|
2017-08-25 22:47:47 +02:00
|
|
|
use pgBackRest::Common::String;
|
2016-06-12 15:13:46 +02:00
|
|
|
use pgBackRest::Common::Wait;
|
2017-11-27 00:45:00 +02:00
|
|
|
use pgBackRest::LibC qw(:config :configDefine);
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Version;
|
2015-07-02 16:05:13 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# Export config constants and functions
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-11-27 00:45:00 +02:00
|
|
|
push(@EXPORT, @{$pgBackRest::LibC::EXPORT_TAGS{config}});
|
|
|
|
push(@EXPORT, @{$pgBackRest::LibC::EXPORT_TAGS{configDefine}});
|
2014-12-16 00:20:42 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# SOURCE Constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant CFGDEF_SOURCE_CONFIG => 'config';
|
|
|
|
push @EXPORT, qw(CFGDEF_SOURCE_CONFIG);
|
|
|
|
use constant CFGDEF_SOURCE_PARAM => 'param';
|
|
|
|
push @EXPORT, qw(CFGDEF_SOURCE_PARAM);
|
|
|
|
use constant CFGDEF_SOURCE_DEFAULT => 'default';
|
|
|
|
push @EXPORT, qw(CFGDEF_SOURCE_DEFAULT);
|
2014-12-16 00:20:42 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# Configuration section constants
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
use constant CFGDEF_SECTION_GLOBAL => 'global';
|
|
|
|
push @EXPORT, qw(CFGDEF_SECTION_GLOBAL);
|
|
|
|
use constant CFGDEF_SECTION_STANZA => 'stanza';
|
|
|
|
push @EXPORT, qw(CFGDEF_SECTION_STANZA);
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2016-08-24 18:39:27 +02:00
|
|
|
# Module variables
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2015-07-02 16:05:13 +02:00
|
|
|
my %oOption; # Option hash
|
|
|
|
my $strCommand; # Command (backup, archive-get, ...)
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strCommandHelp; # The command that help is being generate for
|
2016-12-04 01:01:17 +02:00
|
|
|
my $bInitLog = false; # Has logging been initialized yet?
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# configLogging - configure logging based on options
|
2016-12-04 01:01:17 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub configLogging
|
|
|
|
{
|
|
|
|
my $bLogInitForce = shift;
|
|
|
|
|
2016-12-05 00:51:00 +02:00
|
|
|
if ($bInitLog || (defined($bLogInitForce) && $bLogInitForce))
|
2016-12-04 01:01:17 +02:00
|
|
|
{
|
|
|
|
logLevelSet(
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionValid(CFGOPT_LOG_LEVEL_FILE) ? cfgOption(CFGOPT_LOG_LEVEL_FILE) : OFF,
|
|
|
|
cfgOptionValid(CFGOPT_LOG_LEVEL_CONSOLE) ? cfgOption(CFGOPT_LOG_LEVEL_CONSOLE) : OFF,
|
|
|
|
cfgOptionValid(CFGOPT_LOG_LEVEL_STDERR) ? cfgOption(CFGOPT_LOG_LEVEL_STDERR) : OFF,
|
|
|
|
cfgOptionValid(CFGOPT_LOG_TIMESTAMP) ? cfgOption(CFGOPT_LOG_TIMESTAMP) : undef);
|
2016-12-04 01:01:17 +02:00
|
|
|
|
|
|
|
$bInitLog = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(configLogging);
|
2014-12-16 00:20:42 +02:00
|
|
|
|
2015-01-31 22:37:59 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# configLoad - load configuration
|
2015-03-12 18:15:19 +02:00
|
|
|
#
|
2017-11-02 14:14:13 +02:00
|
|
|
# Additional conditions that cannot be codified by the option definitions are also tested here.
|
2015-01-31 22:37:59 +02:00
|
|
|
####################################################################################################################################
|
2015-03-12 18:15:19 +02:00
|
|
|
sub configLoad
|
2015-01-31 22:37:59 +02:00
|
|
|
{
|
2016-09-07 14:07:37 +02:00
|
|
|
my $bInitLogging = shift;
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# Clear option in case it was loaded before
|
|
|
|
%oOption = ();
|
|
|
|
|
|
|
|
# Build hash with all valid command-line options
|
2017-08-25 22:47:47 +02:00
|
|
|
my @stryOptionAllow;
|
2015-01-31 22:37:59 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
for (my $iOptionId = 0; $iOptionId < cfgOptionTotal(); $iOptionId++)
|
2015-01-31 22:37:59 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strKey = cfgOptionName($iOptionId);
|
|
|
|
|
|
|
|
foreach my $bAltName (false, true)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2016-08-24 18:39:27 +02:00
|
|
|
my $strOptionName = $strKey;
|
|
|
|
|
|
|
|
if ($bAltName)
|
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
if (!defined(cfgDefOptionNameAlt($iOptionId)))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
$strOptionName = cfgDefOptionNameAlt($iOptionId);
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $strOption = $strOptionName;
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_HASH || cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_LIST)
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
$strOption .= '=s@';
|
|
|
|
}
|
2017-11-02 14:14:13 +02:00
|
|
|
elsif (cfgDefOptionType($iOptionId) ne CFGDEF_TYPE_BOOLEAN)
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
$strOption .= '=s';
|
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push(@stryOptionAllow, $strOption);
|
2016-08-24 18:39:27 +02:00
|
|
|
|
|
|
|
# Check if the option can be negated
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionNegate($iOptionId))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
push(@stryOptionAllow, 'no-' . $strOptionName);
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-01-31 22:37:59 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# Get command-line options
|
|
|
|
my %oOptionTest;
|
2015-01-31 22:37:59 +02:00
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
# If nothing was passed on the command line then display help
|
|
|
|
if (@ARGV == 0)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgCommandSet(CFGCMD_HELP);
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
# Else process command line options
|
|
|
|
else
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2015-09-08 13:31:24 +02:00
|
|
|
# Parse command line options
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!GetOptions(\%oOptionTest, @stryOptionAllow))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strCommand = cfgCommandName(CFGCMD_HELP);
|
2015-09-08 13:31:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
# Validate and store options
|
2016-08-24 18:39:27 +02:00
|
|
|
my $bHelp = false;
|
2015-09-08 13:31:24 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($ARGV[0]) && $ARGV[0] eq cfgCommandName(CFGCMD_HELP) && defined($ARGV[1]))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
$bHelp = true;
|
2017-08-25 22:47:47 +02:00
|
|
|
$strCommandHelp = $ARGV[1];
|
2016-08-24 18:39:27 +02:00
|
|
|
$ARGV[0] = $ARGV[1];
|
|
|
|
}
|
2015-09-08 13:31:24 +02:00
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
optionValidate(\%oOptionTest, $bHelp);
|
2015-09-08 13:31:24 +02:00
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
if ($bHelp)
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgCommandSet(CFGCMD_HELP);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# If this is not the remote and logging is allowed (to not overwrite log levels for tests) then set the log level so that
|
2016-12-04 01:01:17 +02:00
|
|
|
# INFO/WARN messages can be displayed (the user may still disable them). This should be run before any WARN logging is
|
|
|
|
# generated.
|
|
|
|
if (!defined($bInitLogging) || $bInitLogging)
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
2016-12-04 01:01:17 +02:00
|
|
|
configLogging(true);
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:44:53 +02:00
|
|
|
# Log the command begin
|
|
|
|
commandBegin();
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Neutralize the umask to make the repository file/path modes more consistent
|
|
|
|
if (cfgOptionValid(CFGOPT_NEUTRAL_UMASK) && cfgOption(CFGOPT_NEUTRAL_UMASK))
|
2016-12-04 21:44:53 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
umask(0000);
|
2016-12-04 21:44:53 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Set db-cmd and backup-cmd to defaults if they are not set. The command depends on the currently running exe so can't be
|
|
|
|
# calculated correctly in the C Library -- perhaps in the future this value will be passed in or set some other way
|
|
|
|
if (cfgOptionValid(CFGOPT_BACKUP_CMD) && cfgOptionTest(CFGOPT_BACKUP_HOST) && !cfgOptionTest(CFGOPT_BACKUP_CMD))
|
2015-07-23 19:11:38 +02:00
|
|
|
{
|
2017-11-27 01:43:51 +02:00
|
|
|
cfgOptionSet(CFGOPT_BACKUP_CMD, backrestBin());
|
2017-08-25 22:47:47 +02:00
|
|
|
$oOption{cfgOptionName(CFGOPT_BACKUP_CMD)}{source} = CFGDEF_SOURCE_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfgOptionValid(CFGOPT_DB_CMD))
|
|
|
|
{
|
|
|
|
for (my $iOptionIdx = 1; $iOptionIdx <= cfgOptionIndexTotal(CFGOPT_DB_HOST); $iOptionIdx++)
|
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgOptionTest(cfgOptionIdFromIndex(CFGOPT_DB_HOST, $iOptionIdx)) &&
|
|
|
|
!cfgOptionTest(cfgOptionIdFromIndex(CFGOPT_DB_CMD, $iOptionIdx)))
|
2017-08-25 22:47:47 +02:00
|
|
|
{
|
2017-11-27 01:43:51 +02:00
|
|
|
cfgOptionSet(cfgOptionIdFromIndex(CFGOPT_DB_CMD, $iOptionIdx), backrestBin());
|
2017-11-02 14:14:13 +02:00
|
|
|
$oOption{cfgOptionIdFromIndex(CFGOPT_DB_CMD, $iOptionIdx)}{source} = CFGDEF_SOURCE_DEFAULT;
|
2017-08-25 22:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-23 19:11:38 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:01:18 +02:00
|
|
|
# Protocol timeout should be greater than db timeout
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgOptionTest(CFGOPT_DB_TIMEOUT) && cfgOptionTest(CFGOPT_PROTOCOL_TIMEOUT) &&
|
|
|
|
cfgOption(CFGOPT_PROTOCOL_TIMEOUT) <= cfgOption(CFGOPT_DB_TIMEOUT))
|
2016-06-23 00:01:18 +02:00
|
|
|
{
|
2016-08-25 17:27:00 +02:00
|
|
|
# If protocol-timeout is default then increase it to be greater than db-timeout
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgOptionSource(CFGOPT_PROTOCOL_TIMEOUT) eq CFGDEF_SOURCE_DEFAULT)
|
2016-08-25 17:27:00 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionSet(CFGOPT_PROTOCOL_TIMEOUT, cfgOption(CFGOPT_DB_TIMEOUT) + 30);
|
2016-08-25 17:27:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR,
|
2017-08-25 22:47:47 +02:00
|
|
|
"'" . cfgOption(CFGOPT_PROTOCOL_TIMEOUT) . "' is not valid for '" .
|
|
|
|
cfgOptionName(CFGOPT_PROTOCOL_TIMEOUT) . "' option\n" .
|
2017-12-05 21:59:09 +02:00
|
|
|
"HINT: 'protocol-timeout' option (" . cfgOption(CFGOPT_PROTOCOL_TIMEOUT) .
|
|
|
|
") should be greater than 'db-timeout' option (" . cfgOption(CFGOPT_DB_TIMEOUT) . ").",
|
2016-08-25 17:27:00 +02:00
|
|
|
ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
2016-06-23 00:01:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
# Make sure that backup and db are not both remote
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgOptionTest(CFGOPT_DB_HOST) && cfgOptionTest(CFGOPT_BACKUP_HOST))
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
2016-08-24 18:39:27 +02:00
|
|
|
confess &log(ERROR, 'db and backup cannot both be configured as remote', ERROR_CONFIG);
|
2015-04-01 21:58:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 14:07:37 +02:00
|
|
|
# Warn when retention-full is not set
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgOptionValid(CFGOPT_RETENTION_FULL) && !cfgOptionTest(CFGOPT_RETENTION_FULL))
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
|
|
|
&log(WARN,
|
|
|
|
"option retention-full is not set, the repository may run out of space\n" .
|
|
|
|
"HINT: to retain full backups indefinitely (without warning), set option 'retention-full' to the maximum.");
|
|
|
|
}
|
|
|
|
|
|
|
|
# If archive retention is valid for the command, then set archive settings
|
2017-08-25 22:47:47 +02:00
|
|
|
if (cfgOptionValid(CFGOPT_RETENTION_ARCHIVE))
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strArchiveRetentionType = cfgOption(CFGOPT_RETENTION_ARCHIVE_TYPE, false);
|
|
|
|
my $iArchiveRetention = cfgOption(CFGOPT_RETENTION_ARCHIVE, false);
|
|
|
|
my $iFullRetention = cfgOption(CFGOPT_RETENTION_FULL, false);
|
|
|
|
my $iDifferentialRetention = cfgOption(CFGOPT_RETENTION_DIFF, false);
|
2016-09-07 14:07:37 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strMsgArchiveOff = "WAL segments will not be expired: option '" . cfgOptionName(CFGOPT_RETENTION_ARCHIVE_TYPE) .
|
2016-09-07 14:07:37 +02:00
|
|
|
"=${strArchiveRetentionType}' but ";
|
|
|
|
|
|
|
|
# If the archive retention is not explicitly set then determine what it should be set to so the user does not have to.
|
|
|
|
if (!defined($iArchiveRetention))
|
|
|
|
{
|
|
|
|
# If retention-archive-type is default, then if retention-full is set, set the retention-archive to this value,
|
|
|
|
# else ignore archiving
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_FULL)
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
|
|
|
if (defined($iFullRetention))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionSet(CFGOPT_RETENTION_ARCHIVE, $iFullRetention);
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_DIFF)
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
|
|
|
# if retention-diff is set then user must have set it
|
|
|
|
if (defined($iDifferentialRetention))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionSet(CFGOPT_RETENTION_ARCHIVE, $iDifferentialRetention);
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&log(WARN,
|
2017-08-25 22:47:47 +02:00
|
|
|
$strMsgArchiveOff . "neither option '" . cfgOptionName(CFGOPT_RETENTION_ARCHIVE) .
|
|
|
|
"' nor option '" . cfgOptionName(CFGOPT_RETENTION_DIFF) . "' is set");
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_INCR)
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
&log(WARN, $strMsgArchiveOff . "option '" . cfgOptionName(CFGOPT_RETENTION_ARCHIVE) . "' is not set");
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# If retention-archive is set then check retention-archive-type and issue a warning if the corresponding setting is
|
|
|
|
# UNDEF since UNDEF means backups will not be expired but they should be in the practice of setting this
|
|
|
|
# value even though expiring the archive itself is OK and will be performed.
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($strArchiveRetentionType eq CFGOPTVAL_BACKUP_TYPE_DIFF && !defined($iDifferentialRetention))
|
2016-09-07 14:07:37 +02:00
|
|
|
{
|
|
|
|
&log(WARN,
|
2017-08-25 22:47:47 +02:00
|
|
|
"option '" . cfgOptionName(CFGOPT_RETENTION_DIFF) . "' is not set for '" .
|
|
|
|
cfgOptionName(CFGOPT_RETENTION_ARCHIVE_TYPE) . "=" . &CFGOPTVAL_BACKUP_TYPE_DIFF . "' \n" .
|
2016-09-07 14:07:37 +02:00
|
|
|
"HINT: to retain differential backups indefinitely (without warning), set option '" .
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionName(CFGOPT_RETENTION_DIFF) . "' to the maximum.");
|
2016-09-07 14:07:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-08 13:31:24 +02:00
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
push @EXPORT, qw(configLoad);
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# optionValueGet
|
|
|
|
#
|
|
|
|
# Find the value of an option using both the regular and alt values. Error if both are defined.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub optionValueGet
|
|
|
|
{
|
|
|
|
my $strOption = shift;
|
|
|
|
my $hOption = shift;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strValue = $hOption->{$strOption};
|
2016-08-24 18:39:27 +02:00
|
|
|
|
|
|
|
# Some options have an alternate name so check for that as well
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = cfgOptionId($strOption);
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (defined(cfgDefOptionNameAlt($iOptionId)))
|
2016-07-30 14:49:26 +02:00
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
my $strOptionAlt = cfgDefOptionNameAlt($iOptionId);
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strValueAlt = $hOption->{$strOptionAlt};
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($strValueAlt))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
|
|
|
if (!defined($strValue))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strValue = $strValueAlt;
|
2016-12-04 16:25:40 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
delete($hOption->{$strOptionAlt});
|
2016-12-04 16:25:40 +02:00
|
|
|
$hOption->{$strOption} = $strValue;
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
confess &log(ERROR, "'${strOption}' and '${strOptionAlt}' cannot both be defined", ERROR_OPTION_INVALID_VALUE);
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-30 14:49:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
return $strValue;
|
2015-01-31 22:37:59 +02:00
|
|
|
}
|
2015-01-31 20:48:09 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2016-06-18 15:55:00 +02:00
|
|
|
# optionValidate
|
2015-01-31 20:48:09 +02:00
|
|
|
#
|
2015-06-18 22:55:09 +02:00
|
|
|
# Make sure the command-line options are valid based on the command.
|
2015-01-31 20:48:09 +02:00
|
|
|
####################################################################################################################################
|
2016-06-18 15:55:00 +02:00
|
|
|
sub optionValidate
|
2015-01-31 20:48:09 +02:00
|
|
|
{
|
2015-03-12 18:15:19 +02:00
|
|
|
my $oOptionTest = shift;
|
2015-09-08 13:31:24 +02:00
|
|
|
my $bHelp = shift;
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
# Check that the command is present and valid
|
|
|
|
$strCommand = $ARGV[0];
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
if (!defined($strCommand))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2015-06-18 22:55:09 +02:00
|
|
|
confess &log(ERROR, "command must be specified", ERROR_COMMAND_REQUIRED);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iCommandId = cfgCommandId($strCommand);
|
|
|
|
|
|
|
|
if ($iCommandId eq "-1")
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2015-06-18 22:55:09 +02:00
|
|
|
confess &log(ERROR, "invalid command ${strCommand}", ERROR_COMMAND_INVALID);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-01-31 20:48:09 +02:00
|
|
|
|
2016-02-06 23:49:12 +02:00
|
|
|
# Hash to store contents of the config file. The file will be loaded once the config dependency is resolved unless all options
|
2015-03-12 18:15:19 +02:00
|
|
|
# are set on the command line or --no-config is specified.
|
|
|
|
my $oConfig;
|
|
|
|
my $bConfigExists = true;
|
|
|
|
|
|
|
|
# Keep track of unresolved dependencies
|
|
|
|
my $bDependUnresolved = true;
|
|
|
|
my %oOptionResolved;
|
|
|
|
|
|
|
|
# Loop through all possible options
|
|
|
|
while ($bDependUnresolved)
|
|
|
|
{
|
|
|
|
# Assume that all dependencies will be resolved in this loop
|
|
|
|
$bDependUnresolved = false;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
for (my $iOptionId = 0; $iOptionId < cfgOptionTotal(); $iOptionId++)
|
2015-01-31 20:48:09 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strOption = cfgOptionName($iOptionId);
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# Skip the option if it has been resolved in a prior loop
|
|
|
|
if (defined($oOptionResolved{$strOption}))
|
2015-01-31 20:48:09 +02:00
|
|
|
{
|
2015-03-12 18:15:19 +02:00
|
|
|
next;
|
2015-01-31 20:48:09 +02:00
|
|
|
}
|
2015-01-31 22:37:59 +02:00
|
|
|
|
2016-06-18 15:55:00 +02:00
|
|
|
# Determine if an option is valid for a command
|
2017-11-02 14:14:13 +02:00
|
|
|
$oOption{$strOption}{valid} = cfgDefOptionValid($iCommandId, $iOptionId);
|
2017-08-25 22:47:47 +02:00
|
|
|
|
|
|
|
if (!$oOption{$strOption}{valid})
|
2016-06-18 15:55:00 +02:00
|
|
|
{
|
|
|
|
$oOptionResolved{$strOption} = true;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
# Store the option value
|
|
|
|
my $strValue = optionValueGet($strOption, $oOptionTest);
|
2015-03-03 03:36:12 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# Check to see if an option can be negated. Make sure that it is not set and negated at the same time.
|
|
|
|
my $bNegate = false;
|
2015-03-03 03:36:12 +02:00
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionNegate($iOptionId))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
$bNegate = defined($$oOptionTest{'no-' . $strOption});
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
if ($bNegate && defined($strValue))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "option '${strOption}' cannot be both set and negated", ERROR_OPTION_NEGATE);
|
|
|
|
}
|
2015-04-08 00:36:59 +02:00
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if ($bNegate && cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_BOOLEAN)
|
2015-04-08 00:36:59 +02:00
|
|
|
{
|
|
|
|
$strValue = false;
|
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
# Check dependency for the command then for the option
|
2015-04-01 21:58:33 +02:00
|
|
|
my $bDependResolved = true;
|
|
|
|
my $strDependOption;
|
|
|
|
my $strDependValue;
|
|
|
|
my $strDependType;
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionDepend($iCommandId, $iOptionId))
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
|
|
|
# Check if the depend option has a value
|
2017-11-02 14:14:13 +02:00
|
|
|
my $iDependOptionId = cfgDefOptionDependOption($iCommandId, $iOptionId);
|
2017-08-25 22:47:47 +02:00
|
|
|
$strDependOption = cfgOptionName($iDependOptionId);
|
2015-04-19 23:29:52 +02:00
|
|
|
$strDependValue = $oOption{$strDependOption}{value};
|
2015-04-01 21:58:33 +02:00
|
|
|
|
|
|
|
# Make sure the depend option has been resolved, otherwise skip this option for now
|
|
|
|
if (!defined($oOptionResolved{$strDependOption}))
|
|
|
|
{
|
|
|
|
$bDependUnresolved = true;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strDependValue))
|
|
|
|
{
|
|
|
|
$bDependResolved = false;
|
|
|
|
$strDependType = 'source';
|
|
|
|
}
|
|
|
|
|
|
|
|
# If a depend value exists, make sure the option value matches
|
2017-11-02 14:14:13 +02:00
|
|
|
if ($bDependResolved && cfgDefOptionDependValueTotal($iCommandId, $iOptionId) == 1 &&
|
|
|
|
cfgDefOptionDependValue($iCommandId, $iOptionId, 0) ne $strDependValue)
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
|
|
|
$bDependResolved = false;
|
|
|
|
$strDependType = 'value';
|
|
|
|
}
|
|
|
|
|
|
|
|
# If a depend list exists, make sure the value is in the list
|
2017-11-02 14:14:13 +02:00
|
|
|
if ($bDependResolved && cfgDefOptionDependValueTotal($iCommandId, $iOptionId) > 1 &&
|
|
|
|
!cfgDefOptionDependValueValid($iCommandId, $iOptionId, $strDependValue))
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
|
|
|
$bDependResolved = false;
|
|
|
|
$strDependType = 'list';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 16:05:13 +02:00
|
|
|
# If the option value is undefined and not negated, see if it can be loaded from the config file
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!defined($strValue) && !$bNegate && $strOption ne cfgOptionName(CFGOPT_CONFIG) &&
|
2017-11-02 14:14:13 +02:00
|
|
|
defined(cfgDefOptionSection($iOptionId)) && $bDependResolved)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
# If the config option has not been resolved yet then continue processing
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!defined($oOptionResolved{cfgOptionName(CFGOPT_CONFIG)}) ||
|
|
|
|
!defined($oOptionResolved{cfgOptionName(CFGOPT_STANZA)}))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
$bDependUnresolved = true;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the config option is defined try to get the option from the config file
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($bConfigExists && defined($oOption{cfgOptionName(CFGOPT_CONFIG)}{value}))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
# Attempt to load the config file if it has not been loaded
|
|
|
|
if (!defined($oConfig))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strConfigFile = $oOption{cfgOptionName(CFGOPT_CONFIG)}{value};
|
2015-03-12 18:15:19 +02:00
|
|
|
$bConfigExists = -e $strConfigFile;
|
|
|
|
|
|
|
|
if ($bConfigExists)
|
|
|
|
{
|
|
|
|
if (!-f $strConfigFile)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strConfigFile}' is not a file", ERROR_FILE_INVALID);
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Load Storage::Helper module
|
|
|
|
require pgBackRest::Storage::Helper;
|
|
|
|
pgBackRest::Storage::Helper->import();
|
|
|
|
|
|
|
|
$oConfig = iniParse(${storageLocal->('/')->get($strConfigFile)}, {bRelaxed => true});
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get the section that the value should be in
|
2017-11-02 14:14:13 +02:00
|
|
|
my $strSection = cfgDefOptionSection($iOptionId);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Always check for the option in the stanza section first
|
|
|
|
if (cfgOptionTest(CFGOPT_STANZA))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strValue = optionValueGet($strOption, $$oConfig{cfgOption(CFGOPT_STANZA)});
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Only continue searching when strSection != CFGDEF_SECTION_STANZA. Some options (e.g. db-path) can only be
|
2016-04-14 15:30:54 +02:00
|
|
|
# configured in the stanza section.
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!defined($strValue) && $strSection ne CFGDEF_SECTION_STANZA)
|
2016-04-14 15:30:54 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
# Check the stanza command section
|
|
|
|
if (cfgOptionTest(CFGOPT_STANZA))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strValue = optionValueGet($strOption, $$oConfig{cfgOption(CFGOPT_STANZA) . ":${strCommand}"});
|
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Check the global command section
|
|
|
|
if (!defined($strValue))
|
|
|
|
{
|
|
|
|
$strValue = optionValueGet($strOption, $$oConfig{&CFGDEF_SECTION_GLOBAL . ":${strCommand}"});
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2016-04-14 15:30:54 +02:00
|
|
|
|
|
|
|
# Finally check the global section
|
|
|
|
if (!defined($strValue))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strValue = optionValueGet($strOption, $$oConfig{&CFGDEF_SECTION_GLOBAL});
|
2016-04-14 15:30:54 +02:00
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Fix up data types
|
|
|
|
if (defined($strValue))
|
|
|
|
{
|
2016-04-14 15:30:54 +02:00
|
|
|
# The empty string is undefined
|
2015-03-12 18:15:19 +02:00
|
|
|
if ($strValue eq '')
|
|
|
|
{
|
|
|
|
$strValue = undef;
|
|
|
|
}
|
2016-04-14 15:30:54 +02:00
|
|
|
# Convert Y or N to boolean
|
2017-11-02 14:14:13 +02:00
|
|
|
elsif (cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_BOOLEAN)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
if ($strValue eq 'y')
|
|
|
|
{
|
|
|
|
$strValue = true;
|
|
|
|
}
|
|
|
|
elsif ($strValue eq 'n')
|
|
|
|
{
|
|
|
|
$strValue = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strValue}' is not valid for '${strOption}' option",
|
|
|
|
ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 15:30:54 +02:00
|
|
|
# Convert a list of key/value pairs to a hash
|
2017-11-02 14:14:13 +02:00
|
|
|
elsif (cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_HASH ||
|
|
|
|
cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_LIST)
|
2016-04-14 15:30:54 +02:00
|
|
|
{
|
|
|
|
my @oValue = ();
|
|
|
|
|
|
|
|
# If there is only one key/value
|
|
|
|
if (ref(\$strValue) eq 'SCALAR')
|
|
|
|
{
|
|
|
|
push(@oValue, $strValue);
|
|
|
|
}
|
|
|
|
# Else if there is an array of values
|
|
|
|
else
|
|
|
|
{
|
|
|
|
@oValue = @{$strValue};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Reset the value hash
|
|
|
|
$strValue = {};
|
|
|
|
|
|
|
|
# Iterate and parse each key/value pair
|
|
|
|
foreach my $strHash (@oValue)
|
|
|
|
{
|
|
|
|
my $iEqualIdx = index($strHash, '=');
|
|
|
|
|
|
|
|
if ($iEqualIdx < 1 || $iEqualIdx == length($strHash) - 1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strHash}' is not valid for '${strOption}' option",
|
|
|
|
ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strHashKey = substr($strHash, 0, $iEqualIdx);
|
|
|
|
my $strHashValue = substr($strHash, length($strHashKey) + 1);
|
|
|
|
|
|
|
|
$$strValue{$strHashKey} = $strHashValue;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 02:15:17 +02:00
|
|
|
# In all other cases the value should be scalar
|
|
|
|
elsif (ref(\$strValue) ne 'SCALAR')
|
|
|
|
{
|
|
|
|
confess &log(
|
|
|
|
ERROR, "option '${strOption}' cannot be specified multiple times", ERROR_OPTION_MULTIPLE_VALUE);
|
|
|
|
}
|
2015-04-19 23:29:52 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$oOption{$strOption}{source} = CFGDEF_SOURCE_CONFIG;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionDepend($iCommandId, $iOptionId) && !$bDependResolved && defined($strValue))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2016-02-20 02:32:02 +02:00
|
|
|
my $strError = "option '${strOption}' not valid without option ";
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iDependOptionId = cfgOptionId($strDependOption);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-04-01 21:58:33 +02:00
|
|
|
if ($strDependType eq 'source')
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2016-02-20 02:32:02 +02:00
|
|
|
confess &log(ERROR, "${strError}'${strDependOption}'", ERROR_OPTION_INVALID);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# If a depend value exists, make sure the option value matches
|
2015-04-01 21:58:33 +02:00
|
|
|
if ($strDependType eq 'value')
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionType($iDependOptionId) eq CFGDEF_TYPE_BOOLEAN)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strError .=
|
2017-11-02 14:14:13 +02:00
|
|
|
"'" . (cfgDefOptionDependValue($iCommandId, $iOptionId, 0) ? '' : 'no-') . "${strDependOption}'";
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-04-01 21:58:33 +02:00
|
|
|
else
|
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
$strError .= "'${strDependOption}' = '" . cfgDefOptionDependValue($iCommandId, $iOptionId, 0) . "'";
|
2015-04-01 21:58:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
confess &log(ERROR, $strError, ERROR_OPTION_INVALID);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 02:32:02 +02:00
|
|
|
$strError .= "'${strDependOption}'";
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# If a depend list exists, make sure the value is in the list
|
2015-04-01 21:58:33 +02:00
|
|
|
if ($strDependType eq 'list')
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2015-04-01 21:58:33 +02:00
|
|
|
my @oyValue;
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
for (my $iValueId = 0; $iValueId < cfgDefOptionDependValueTotal($iCommandId, $iOptionId); $iValueId++)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
push(@oyValue, "'" . cfgDefOptionDependValue($iCommandId, $iOptionId, $iValueId) . "'");
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-04-01 21:58:33 +02:00
|
|
|
|
|
|
|
$strError .= @oyValue == 1 ? " = $oyValue[0]" : " in (" . join(", ", @oyValue) . ")";
|
|
|
|
confess &log(ERROR, $strError, ERROR_OPTION_INVALID);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
# Is the option defined?
|
|
|
|
if (defined($strValue))
|
|
|
|
{
|
|
|
|
# Check that floats and integers are valid
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_INTEGER ||
|
|
|
|
cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_FLOAT)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
# Test that the string is a valid float or integer by adding 1 to it. It's pretty hokey but it works and it
|
2015-03-12 18:15:19 +02:00
|
|
|
# beats requiring Scalar::Util::Numeric to do it properly.
|
2016-09-06 15:44:50 +02:00
|
|
|
my $bError = false;
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
eval
|
|
|
|
{
|
|
|
|
my $strTest = $strValue + 1;
|
2016-09-06 15:44:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
$bError = true;
|
2015-03-12 18:15:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# Check that integers are really integers
|
2017-11-02 14:14:13 +02:00
|
|
|
if (!$bError && cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_INTEGER &&
|
2015-03-12 18:15:19 +02:00
|
|
|
(int($strValue) . 'S') ne ($strValue . 'S'))
|
|
|
|
{
|
|
|
|
$bError = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Error if the value did not pass tests
|
|
|
|
!$bError
|
|
|
|
or confess &log(ERROR, "'${strValue}' is not valid for '${strOption}' option", ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
# Process an allow list for the command then for the option
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionAllowList($iCommandId, $iOptionId) &&
|
|
|
|
!cfgDefOptionAllowListValueValid($iCommandId, $iOptionId, $strValue))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strValue}' is not valid for '${strOption}' option", ERROR_OPTION_INVALID_VALUE);
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
# Process an allow range for the command then for the option
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionAllowRange($iCommandId, $iOptionId) &&
|
|
|
|
($strValue < cfgDefOptionAllowRangeMin($iCommandId, $iOptionId) ||
|
|
|
|
$strValue > cfgDefOptionAllowRangeMax($iCommandId, $iOptionId)))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strValue}' is not valid for '${strOption}' option", ERROR_OPTION_INVALID_RANGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set option value
|
2017-10-30 16:50:35 +02:00
|
|
|
if (ref($strValue) eq 'ARRAY' &&
|
2017-11-02 14:14:13 +02:00
|
|
|
(cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_HASH || cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_LIST))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
foreach my $strItem (@{$strValue})
|
|
|
|
{
|
2016-05-11 15:21:39 +02:00
|
|
|
my $strKey;
|
|
|
|
my $strValue;
|
|
|
|
|
|
|
|
# If the keys are expected to have values
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionType($iOptionId) eq CFGDEF_TYPE_HASH)
|
2016-05-11 15:21:39 +02:00
|
|
|
{
|
|
|
|
# Check for = and make sure there is a least one character on each side
|
|
|
|
my $iEqualPos = index($strItem, '=');
|
|
|
|
|
|
|
|
if ($iEqualPos < 1 || length($strItem) <= $iEqualPos + 1)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${strItem}' not valid key/value for '${strOption}' option",
|
|
|
|
ERROR_OPTION_INVALID_PAIR);
|
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
$strKey = substr($strItem, 0, $iEqualPos);
|
|
|
|
$strValue = substr($strItem, $iEqualPos + 1);
|
|
|
|
}
|
|
|
|
# Else no values are expected so set value to true
|
|
|
|
else
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2016-05-11 15:21:39 +02:00
|
|
|
$strKey = $strItem;
|
|
|
|
$strValue = true;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check that the key has not already been set
|
2015-04-19 23:29:52 +02:00
|
|
|
if (defined($oOption{$strOption}{$strKey}{value}))
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
|
|
|
confess &log(ERROR, "'${$strItem}' already defined for '${strOption}' option",
|
|
|
|
ERROR_OPTION_DUPLICATE_KEY);
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:21:39 +02:00
|
|
|
# Set key/value
|
|
|
|
$oOption{$strOption}{value}{$strKey} = $strValue;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-19 23:29:52 +02:00
|
|
|
$oOption{$strOption}{value} = $strValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If not config sourced then it must be a param
|
|
|
|
if (!defined($oOption{$strOption}{source}))
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$oOption{$strOption}{source} = CFGDEF_SOURCE_PARAM;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 20:01:01 +02:00
|
|
|
# Else try to set a default
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif ($bDependResolved)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2015-04-19 23:29:52 +02:00
|
|
|
# Source is default for this option
|
2017-08-25 22:47:47 +02:00
|
|
|
$oOption{$strOption}{source} = CFGDEF_SOURCE_DEFAULT;
|
2015-04-19 23:29:52 +02:00
|
|
|
|
2015-06-18 22:55:09 +02:00
|
|
|
# Check for default in command then option
|
2017-11-02 14:14:13 +02:00
|
|
|
my $strDefault = cfgDefOptionDefault($iCommandId, $iOptionId);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
|
|
|
# If default is defined
|
|
|
|
if (defined($strDefault))
|
|
|
|
{
|
|
|
|
# Only set default if dependency is resolved
|
2015-04-19 23:29:52 +02:00
|
|
|
$oOption{$strOption}{value} = $strDefault if !$bNegate;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
2015-03-16 20:01:01 +02:00
|
|
|
# Else check required
|
2017-11-02 14:14:13 +02:00
|
|
|
elsif (cfgDefOptionRequired($iCommandId, $iOptionId) && !$bHelp)
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
confess &log(ERROR,
|
|
|
|
"${strCommand} command requires option: ${strOption}" .
|
2017-11-02 14:14:13 +02:00
|
|
|
(defined(cfgDefOptionSection($iOptionId)) &&
|
|
|
|
cfgDefOptionSection($iOptionId) eq CFGDEF_SECTION_STANZA ? "\nHINT: does this stanza exist?" : ''),
|
2017-08-25 22:47:47 +02:00
|
|
|
ERROR_OPTION_REQUIRED);
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
$oOptionResolved{$strOption} = true;
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 16:25:40 +02:00
|
|
|
|
|
|
|
# Make sure all options specified on the command line are valid
|
|
|
|
foreach my $strOption (sort(keys(%{$oOptionTest})))
|
|
|
|
{
|
|
|
|
# Strip "no-" off the option
|
|
|
|
$strOption = $strOption =~ /^no\-/ ? substr($strOption, 3) : $strOption;
|
|
|
|
|
|
|
|
if (!$oOption{$strOption}{valid})
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "option '${strOption}' not valid for command '${strCommand}'", ERROR_OPTION_COMMAND);
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 01:03:28 +02:00
|
|
|
|
|
|
|
# If a config file was loaded then determine if all options are valid in the config file
|
|
|
|
if (defined($oConfig))
|
|
|
|
{
|
|
|
|
configFileValidate($oConfig);
|
|
|
|
}
|
2015-03-03 05:58:32 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 01:03:28 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# configFileValidate
|
|
|
|
#
|
2017-05-16 17:28:51 +02:00
|
|
|
# Determine if the configuration file contains any invalid options or placements. Not valid on remote.
|
2017-03-23 01:03:28 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub configFileValidate
|
|
|
|
{
|
|
|
|
my $oConfig = shift;
|
|
|
|
|
|
|
|
my $bFileValid = true;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!cfgCommandTest(CFGCMD_REMOTE) && !cfgCommandTest(CFGCMD_LOCAL))
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-05-16 17:28:51 +02:00
|
|
|
foreach my $strSectionKey (keys(%$oConfig))
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-05-16 17:28:51 +02:00
|
|
|
my ($strSection, $strCommand) = ($strSectionKey =~ m/([^:]*):*(\w*-*\w*)/);
|
2017-03-23 01:03:28 +02:00
|
|
|
|
2017-05-16 17:28:51 +02:00
|
|
|
foreach my $strOption (keys(%{$$oConfig{$strSectionKey}}))
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strOptionDisplay = $strOption;
|
2017-05-16 17:28:51 +02:00
|
|
|
my $strValue = $$oConfig{$strSectionKey}{$strOption};
|
2017-03-23 01:03:28 +02:00
|
|
|
|
2017-05-16 17:28:51 +02:00
|
|
|
# Is the option listed as an alternate name for another option? If so, replace it with the recognized option.
|
|
|
|
my $strOptionAltName = optionAltName($strOption);
|
|
|
|
|
|
|
|
if (defined($strOptionAltName))
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-05-16 17:28:51 +02:00
|
|
|
$strOption = $strOptionAltName;
|
2017-03-23 01:03:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:28:51 +02:00
|
|
|
# Is the option a valid pgbackrest option?
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!(cfgOptionId($strOption) ne '-1' || defined($strOptionAltName)))
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
&log(WARN, cfgOption(CFGOPT_CONFIG) . " file contains invalid option '${strOptionDisplay}'");
|
2017-03-23 01:03:28 +02:00
|
|
|
$bFileValid = false;
|
|
|
|
}
|
2017-05-16 17:28:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
# Is the option valid for the command section in which it is located?
|
|
|
|
if (defined($strCommand) && $strCommand ne '')
|
|
|
|
{
|
2017-11-02 14:14:13 +02:00
|
|
|
if (!cfgDefOptionValid(cfgCommandId($strCommand), cfgOptionId($strOption)))
|
2017-05-16 17:28:51 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
&log(WARN, cfgOption(CFGOPT_CONFIG) . " valid option '${strOptionDisplay}' is not valid for command " .
|
2017-05-16 17:28:51 +02:00
|
|
|
"'${strCommand}'");
|
|
|
|
$bFileValid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Is the valid option a stanza-only option and not located in a global section?
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionSection(cfgOptionId($strOption)) eq CFGDEF_SECTION_STANZA &&
|
2017-08-25 22:47:47 +02:00
|
|
|
$strSection eq CFGDEF_SECTION_GLOBAL)
|
2017-05-16 17:28:51 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
&log(WARN,
|
|
|
|
cfgOption(CFGOPT_CONFIG) . " valid option '${strOptionDisplay}' is a stanza section option and is" .
|
|
|
|
" not valid in section ${strSection}\n" .
|
2017-05-16 17:28:51 +02:00
|
|
|
"HINT: global options can be specified in global or stanza sections but not visa-versa");
|
|
|
|
$bFileValid = false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 01:03:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bFileValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# optionAltName
|
|
|
|
#
|
|
|
|
# Returns the ALT_NAME for the option if one exists.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub optionAltName
|
|
|
|
{
|
|
|
|
my $strOption = shift;
|
|
|
|
|
|
|
|
my $strOptionAltName = undef;
|
|
|
|
|
|
|
|
# Check if the options exists as an alternate name (e.g. db-host has altname db1-host)
|
2017-08-25 22:47:47 +02:00
|
|
|
for (my $iOptionId = 0; $iOptionId < cfgOptionTotal(); $iOptionId++)
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strKey = cfgOptionName($iOptionId);
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (defined(cfgDefOptionNameAlt($iOptionId)) && cfgDefOptionNameAlt($iOptionId) eq $strOption)
|
2017-03-23 01:03:28 +02:00
|
|
|
{
|
|
|
|
$strOptionAltName = $strKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strOptionAltName;
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
2017-11-02 14:14:13 +02:00
|
|
|
# cfgOptionIdFromIndex - return name for options that can be indexed (e.g. db1-host, db2-host).
|
2016-08-24 18:39:27 +02:00
|
|
|
####################################################################################################################################
|
2017-11-02 14:14:13 +02:00
|
|
|
sub cfgOptionIdFromIndex
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2016-08-24 18:39:27 +02:00
|
|
|
my $iIndex = shift;
|
|
|
|
my $bForce = shift;
|
|
|
|
|
|
|
|
# If the option doesn't have a prefix it can't be indexed
|
|
|
|
$iIndex = defined($iIndex) ? $iIndex : 1;
|
2017-11-02 14:14:13 +02:00
|
|
|
my $strPrefix = cfgDefOptionPrefix($iOptionId);
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!defined($strPrefix))
|
2016-08-24 18:39:27 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
if ($iIndex > 1)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "'" . cfgOptionName($iOptionId) . "' option does not allow indexing");
|
|
|
|
}
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return $iOptionId;
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return cfgOptionId("${strPrefix}${iIndex}" . substr(cfgOptionName($iOptionId), index(cfgOptionName($iOptionId), '-')));
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
push @EXPORT, qw(cfgOptionIdFromIndex);
|
2016-08-24 18:39:27 +02:00
|
|
|
|
2015-03-16 20:01:01 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgOptionSource - how was the option set?
|
2015-03-16 20:01:01 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgOptionSource
|
2015-03-16 20:01:01 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2015-03-16 20:01:01 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionValid($iOptionId, true);
|
2015-03-16 20:01:01 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return $oOption{cfgOptionName($iOptionId)}{source};
|
2015-03-16 20:01:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgOptionSource);
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgOptionValid - is the option valid for the current command?
|
2015-10-28 11:10:36 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgOptionValid
|
2015-10-28 11:10:36 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
|
|
|
my $bError = shift;
|
2015-10-28 11:10:36 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# If defined then this is the command help is being generated for so all valid checks should be against that command
|
|
|
|
my $iCommandId;
|
2015-10-28 11:10:36 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($strCommandHelp))
|
2015-10-28 11:10:36 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$iCommandId = cfgCommandId($strCommandHelp);
|
|
|
|
}
|
|
|
|
# Else try to use the normal command
|
|
|
|
elsif (defined($strCommand))
|
|
|
|
{
|
|
|
|
$iCommandId = cfgCommandId($strCommand);
|
2015-10-28 11:10:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
if (defined($iCommandId) && cfgDefOptionValid($iCommandId, $iOptionId))
|
2016-06-18 15:55:00 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($bError) && $bError)
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strOption = cfgOptionName($iOptionId);
|
|
|
|
|
2016-06-18 15:55:00 +02:00
|
|
|
if (!defined($oOption{$strOption}))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "option '${strOption}' does not exist");
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
confess &log(ASSERT, "option '${strOption}' not valid for command '" . cfgCommandName(cfgCommandGet()) . "'");
|
2016-06-18 15:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgOptionValid);
|
2016-06-18 15:55:00 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgOption - get option value
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgOption
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2015-03-12 18:15:19 +02:00
|
|
|
my $bRequired = shift;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
cfgOptionValid($iOptionId, true);
|
|
|
|
|
|
|
|
my $strOption = cfgOptionName($iOptionId);
|
2016-06-18 15:55:00 +02:00
|
|
|
|
2015-04-19 23:29:52 +02:00
|
|
|
if (!defined($oOption{$strOption}{value}) && (!defined($bRequired) || $bRequired))
|
2015-03-03 03:36:12 +02:00
|
|
|
{
|
2015-03-12 18:15:19 +02:00
|
|
|
confess &log(ASSERT, "option ${strOption} is required");
|
2015-03-03 03:36:12 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:29:52 +02:00
|
|
|
return $oOption{$strOption}{value};
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgOption);
|
2015-03-03 05:58:32 +02:00
|
|
|
|
2016-02-06 06:03:29 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgOptionDefault - get option default value
|
2016-02-06 06:03:29 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgOptionDefault
|
2016-02-06 06:03:29 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
|
|
|
|
|
|
|
cfgOptionValid($iOptionId, true);
|
|
|
|
|
2017-11-02 14:14:13 +02:00
|
|
|
return cfgDefOptionDefault(cfgCommandId($strCommand), $iOptionId);
|
2017-08-25 22:47:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(cfgOptionDefault);
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# cfgOptionSet - set option value and source
|
|
|
|
####################################################################################################################################
|
|
|
|
sub cfgOptionSet
|
|
|
|
{
|
|
|
|
my $iOptionId = shift;
|
2016-02-06 06:03:29 +02:00
|
|
|
my $oValue = shift;
|
2016-12-05 00:51:00 +02:00
|
|
|
my $bForce = shift;
|
2016-02-06 06:03:29 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strOption = cfgOptionName($iOptionId);
|
|
|
|
|
|
|
|
if (!cfgOptionValid($iOptionId, !defined($bForce) || !$bForce))
|
2016-12-05 00:51:00 +02:00
|
|
|
{
|
|
|
|
$oOption{$strOption}{valid} = true;
|
|
|
|
}
|
2016-06-18 15:55:00 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$oOption{$strOption}{source} = CFGDEF_SOURCE_PARAM;
|
2016-02-06 06:03:29 +02:00
|
|
|
$oOption{$strOption}{value} = $oValue;
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgOptionSet);
|
2016-02-06 06:03:29 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgOptionTest - test if an option exists or has a specific value
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgOptionTest
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2015-03-12 18:15:19 +02:00
|
|
|
my $strValue = shift;
|
2015-01-31 22:37:59 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
if (!cfgOptionValid($iOptionId))
|
2016-06-18 15:55:00 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
if (defined($strValue))
|
2015-01-31 22:37:59 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
return cfgOption($iOptionId) eq $strValue ? true : false;
|
2015-01-31 22:37:59 +02:00
|
|
|
}
|
2015-03-12 18:15:19 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return defined($oOption{cfgOptionName($iOptionId)}{value}) ? true : false;
|
2015-03-12 18:15:19 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgOptionTest);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgCommandGet - get the current command
|
2015-04-01 21:58:33 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgCommandGet
|
2015-04-01 21:58:33 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
return cfgCommandId($strCommand);
|
2015-04-01 21:58:33 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgCommandGet);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgCommandTest - test that the current command is equal to the provided value
|
2015-03-12 18:15:19 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgCommandTest
|
2015-03-12 18:15:19 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iCommandIdTest = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return cfgCommandName($iCommandIdTest) eq $strCommand;
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgCommandTest);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2016-12-04 21:44:53 +02:00
|
|
|
# commandBegin
|
2015-08-29 20:20:46 +02:00
|
|
|
#
|
2016-12-04 21:44:53 +02:00
|
|
|
# Log information about the command when it begins.
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2016-12-04 21:44:53 +02:00
|
|
|
sub commandBegin
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-09-17 17:53:28 +02:00
|
|
|
&log(
|
2017-08-25 22:47:47 +02:00
|
|
|
$strCommand eq cfgCommandName(CFGCMD_INFO) ? DEBUG : INFO,
|
2017-10-24 18:35:36 +02:00
|
|
|
"${strCommand} command begin " . BACKREST_VERSION . ':' .
|
|
|
|
cfgCommandWrite(cfgCommandId($strCommand), true, '', false, undef, true));
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2016-12-04 21:44:53 +02:00
|
|
|
# commandEnd
|
2015-08-29 20:20:46 +02:00
|
|
|
#
|
2016-12-04 21:44:53 +02:00
|
|
|
# Log information about the command that ended.
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2016-12-04 21:44:53 +02:00
|
|
|
sub commandEnd
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2016-12-04 21:44:53 +02:00
|
|
|
my $iExitCode = shift;
|
|
|
|
my $strSignal = shift;
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
if (defined($strCommand))
|
|
|
|
{
|
2016-12-04 21:44:53 +02:00
|
|
|
&log(
|
2017-08-25 22:47:47 +02:00
|
|
|
$strCommand eq cfgCommandName(CFGCMD_INFO) ? DEBUG : INFO,
|
2016-12-04 21:44:53 +02:00
|
|
|
"${strCommand} command end: " . (defined($iExitCode) && $iExitCode != 0 ?
|
|
|
|
($iExitCode == ERROR_TERM ? "terminated on signal " .
|
|
|
|
(defined($strSignal) ? "[SIG${strSignal}]" : 'from child process') :
|
2017-04-12 18:41:34 +02:00
|
|
|
sprintf('aborted with exception [%03d]', $iExitCode)) :
|
2016-12-04 21:44:53 +02:00
|
|
|
'completed successfully'));
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:44:53 +02:00
|
|
|
push @EXPORT, qw(commandEnd);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgCommandSet - set current command (usually for triggering follow-on commands)
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgCommandSet
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iCommandId = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-12-04 21:44:53 +02:00
|
|
|
commandEnd();
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$strCommand = cfgCommandName($iCommandId);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2016-12-04 21:44:53 +02:00
|
|
|
commandBegin();
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgCommandSet);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
# cfgCommandWrite - using the options for the current command, write the command string for another command
|
2015-08-29 20:20:46 +02:00
|
|
|
#
|
2017-08-25 22:47:47 +02:00
|
|
|
# For example, this can be used to write the archive-get command for recovery.conf during a restore.
|
2015-08-29 20:20:46 +02:00
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub cfgCommandWrite
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iNewCommandId = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
my $bIncludeConfig = shift;
|
|
|
|
my $strExeString = shift;
|
|
|
|
my $bIncludeCommand = shift;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $oOptionOverride = shift;
|
2017-10-24 18:35:36 +02:00
|
|
|
my $bDisplayOnly = shift;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# Set defaults
|
2017-11-27 01:43:51 +02:00
|
|
|
$strExeString = defined($strExeString) ? $strExeString : backrestBin();
|
2015-08-29 20:20:46 +02:00
|
|
|
$bIncludeConfig = defined($bIncludeConfig) ? $bIncludeConfig : false;
|
|
|
|
$bIncludeCommand = defined($bIncludeCommand) ? $bIncludeCommand : true;
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Iterate the options to figure out which ones are not default and need to be written out to the new command string
|
2017-08-25 22:47:47 +02:00
|
|
|
for (my $iOptionId = 0; $iOptionId < cfgOptionTotal(); $iOptionId++)
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strOption = cfgOptionName($iOptionId);
|
2017-11-02 14:14:13 +02:00
|
|
|
my $bSecure = cfgDefOptionSecure($iOptionId);
|
2017-08-25 22:47:47 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Skip option if it is secure and should not be output in logs or the command line
|
2017-10-24 18:35:36 +02:00
|
|
|
next if ($bSecure && !$bDisplayOnly);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Process any option id overrides first
|
|
|
|
if (defined($oOptionOverride->{$iOptionId}))
|
|
|
|
{
|
|
|
|
if (defined($oOptionOverride->{$iOptionId}{value}))
|
|
|
|
{
|
2017-10-24 18:35:36 +02:00
|
|
|
$strExeString .= cfgCommandWriteOptionFormat(
|
|
|
|
$strOption, false, $bSecure, {value => $oOptionOverride->{$iOptionId}{value}});
|
2017-08-25 22:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 18:35:36 +02:00
|
|
|
# And process overrides passed by string - this is used by Perl compatibility functions
|
2017-08-25 22:47:47 +02:00
|
|
|
elsif (defined($oOptionOverride->{$strOption}))
|
2015-10-08 17:43:56 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($oOptionOverride->{$strOption}{value}))
|
2015-10-08 17:43:56 +02:00
|
|
|
{
|
2017-10-24 18:35:36 +02:00
|
|
|
$strExeString .= cfgCommandWriteOptionFormat(
|
|
|
|
$strOption, false, $bSecure, {value => $oOptionOverride->{$strOption}{value}});
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# else look for non-default options in the current configuration
|
2017-11-02 14:14:13 +02:00
|
|
|
elsif (cfgDefOptionValid($iNewCommandId, $iOptionId) &&
|
2015-10-08 17:43:56 +02:00
|
|
|
defined($oOption{$strOption}{value}) &&
|
2017-08-25 22:47:47 +02:00
|
|
|
($bIncludeConfig ?
|
|
|
|
$oOption{$strOption}{source} ne CFGDEF_SOURCE_DEFAULT : $oOption{$strOption}{source} eq CFGDEF_SOURCE_PARAM))
|
2015-08-29 20:20:46 +02:00
|
|
|
{
|
|
|
|
my $oValue;
|
2015-10-08 17:43:56 +02:00
|
|
|
my $bMulti = false;
|
2015-08-29 20:20:46 +02:00
|
|
|
|
|
|
|
# If this is a hash then it will break up into multple command-line options
|
|
|
|
if (ref($oOption{$strOption}{value}) eq 'HASH')
|
|
|
|
{
|
|
|
|
$oValue = $oOption{$strOption}{value};
|
2015-10-08 17:43:56 +02:00
|
|
|
$bMulti = true;
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
# Else a single value but store it in a hash anyway to make processing below simpler
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$oValue = {value => $oOption{$strOption}{value}};
|
|
|
|
}
|
|
|
|
|
2017-10-24 18:35:36 +02:00
|
|
|
$strExeString .= cfgCommandWriteOptionFormat($strOption, $bMulti, $bSecure, $oValue);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($bIncludeCommand)
|
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
$strExeString .= ' ' . cfgCommandName($iNewCommandId);
|
2015-08-29 20:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $strExeString;
|
2015-01-31 20:48:09 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
push @EXPORT, qw(cfgCommandWrite);
|
2015-08-29 20:20:46 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
# Helper function for cfgCommandWrite() to correctly format options for command-line usage
|
|
|
|
sub cfgCommandWriteOptionFormat
|
2016-02-23 16:25:22 +02:00
|
|
|
{
|
|
|
|
my $strOption = shift;
|
|
|
|
my $bMulti = shift;
|
2017-10-24 18:35:36 +02:00
|
|
|
my $bSecure = shift;
|
2016-02-23 16:25:22 +02:00
|
|
|
my $oValue = shift;
|
|
|
|
|
|
|
|
# Loops though all keys in the hash
|
|
|
|
my $strOptionFormat = '';
|
|
|
|
my $strParam;
|
|
|
|
|
|
|
|
foreach my $strKey (sort(keys(%$oValue)))
|
|
|
|
{
|
|
|
|
# Get the value - if the original value was a hash then the key must be prefixed
|
2017-10-24 18:35:36 +02:00
|
|
|
my $strValue = $bSecure ? '<redacted>' : ($bMulti ? "${strKey}=" : '') . $$oValue{$strKey};
|
2016-02-23 16:25:22 +02:00
|
|
|
|
|
|
|
# Handle the no- prefix for boolean values
|
2017-11-02 14:14:13 +02:00
|
|
|
if (cfgDefOptionType(cfgOptionId($strOption)) eq CFGDEF_TYPE_BOOLEAN)
|
2016-02-23 16:25:22 +02:00
|
|
|
{
|
|
|
|
$strParam = '--' . ($strValue ? '' : 'no-') . $strOption;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strParam = "--${strOption}=${strValue}";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add quotes if the value has spaces in it
|
|
|
|
$strOptionFormat .= ' ' . (index($strValue, " ") != -1 ? "\"${strParam}\"" : $strParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strOptionFormat;
|
|
|
|
}
|
|
|
|
|
2014-12-16 00:20:42 +02:00
|
|
|
1;
|