1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-09 00:45:49 +02:00

Refactor config parse to remove none command, add version/help options.

The none command was a bit confusing since it was only valid when parsing failed but still needed to be added to various switches and logic. Replace with cfgInited() which should make it clearer what state configuration is in.

Make the default command help and convert --version and --help to real options.

Combine version and help output into a single function to simplify processing in main.

Additional reformatting and a bit of refactoring.
This commit is contained in:
David Steele
2024-07-23 16:39:02 +07:00
parent 6c757366c2
commit 8d6bceb541
29 changed files with 1124 additions and 978 deletions

View File

@ -33,5 +33,4 @@ data_start
environ@GLIBC_2.2.5 environ@GLIBC_2.2.5
main main
stderr@GLIBC_2.2.5 stderr@GLIBC_2.2.5
stdout@GLIBC_2.2.5
xmlFree@LIBXML2_2.4.30 xmlFree@LIBXML2_2.4.30

View File

@ -45,6 +45,14 @@ option:
- 512KiB - 512KiB
- 1MiB - 1MiB
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
neutral-umask: neutral-umask:
type: boolean type: boolean
internal: true internal: true
@ -56,6 +64,14 @@ option:
command: command:
build: {} build: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
# Logging options # Logging options
#--------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------------------------------
log-level: log-level:

View File

@ -98,6 +98,28 @@
<text> <text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>doc-pgbackrest help build</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>doc-pgbackrest help build repo-path</cmd>) then a full description of the option as it applies to the command will be displayed.</p> <p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>doc-pgbackrest help build</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>doc-pgbackrest help build repo-path</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text> </text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command> </command>
<!-- Noop command holds options that must be defined but we don't need --> <!-- Noop command holds options that must be defined but we don't need -->

View File

@ -48,10 +48,16 @@ cfgLoadUpdateOption(void)
THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd"); THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd");
// If repo-path is relative then make it absolute // If repo-path is relative then make it absolute
const String *const repoPath = cfgOptionStr(cfgOptRepoPath); if (cfgOptionValid(cfgOptRepoPath))
{
const String *const repoPath = cfgOptionStr(cfgOptRepoPath);
if (!strBeginsWithZ(repoPath, "/")) if (!strBeginsWithZ(repoPath, "/"))
cfgOptionSet(cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath)))); {
cfgOptionSet(
cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
}
}
FUNCTION_LOG_RETURN_VOID(); FUNCTION_LOG_RETURN_VOID();
} }
@ -89,27 +95,23 @@ cfgLoad(unsigned int argListSize, const char *argList[])
if (cfgCommand() == cfgCmdNoop) if (cfgCommand() == cfgCmdNoop)
THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'"); THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'");
// If a command is set // Load the log settings
if (cfgCommand() != cfgCmdNone && cfgCommand() != cfgCmdHelp && cfgCommand() != cfgCmdVersion) if (!cfgCommandHelp())
{ cfgLoadLogSetting();
// Load the log settings
if (!cfgCommandHelp())
cfgLoadLogSetting();
// Neutralize the umask to make the repository file/path modes more consistent // Neutralize the umask to make the repository file/path modes more consistent
if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask)) if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask))
umask(0000); umask(0000);
// Set IO buffer size // Set IO buffer size
if (cfgOptionValid(cfgOptBufferSize)) if (cfgOptionValid(cfgOptBufferSize))
ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize)); ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize));
// Update options that have complex rules // Update options that have complex rules
cfgLoadUpdateOption(); cfgLoadUpdateOption();
// Begin the command // Begin the command
cmdBegin(); cmdBegin();
}
} }
MEM_CONTEXT_TEMP_END(); MEM_CONTEXT_TEMP_END();

View File

@ -65,22 +65,15 @@ main(int argListSize, const char *argList[])
cmdBuild(cfgOptionStr(cfgOptRepoPath)); cmdBuild(cfgOptionStr(cfgOptRepoPath));
break; break;
// Help // Help/version
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp: case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdVersion: case cfgCmdVersion:
printf(PROJECT_NAME " Documentation " PROJECT_VERSION "\n"); cmdHelp(BUF(helpData, sizeof(helpData)));
fflush(stdout);
break; break;
// Error on commands that should have been handled // Error on commands that should have been handled
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
case cfgCmdNoop: case cfgCmdNoop:
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName()); THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
break; break;

View File

@ -300,6 +300,14 @@ option:
command-role: command-role:
main: {} main: {}
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
online: online:
type: boolean type: boolean
default: true default: true
@ -501,6 +509,14 @@ option:
command-role: command-role:
main: {} main: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
# Command-line only local/remote options # Command-line only local/remote options
#--------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------------------------------
exec-id: exec-id:

View File

@ -193,8 +193,6 @@ bldCfgRenderConfigAutoH(const Storage *const storageRepo, const BldCfg bldCfg)
strCatFmt(config, " %s,\n", strZ(bldEnumCmd(cmd->name))); strCatFmt(config, " %s,\n", strZ(bldEnumCmd(cmd->name)));
} }
strCatFmt(config, " %s,\n", strZ(bldEnumCmd(STRDEF("none"))));
strCatZ( strCatZ(
config, config,
"} ConfigCommand;\n"); "} ConfigCommand;\n");

View File

@ -2572,6 +2572,28 @@
<text> <text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>pgbackrest help backup</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help backup type</cmd>) then a full description of the option as it applies to the command will be displayed.</p> <p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>pgbackrest help backup</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help backup type</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text> </text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command> </command>
<command id="server" name="Server"> <command id="server" name="Server">

View File

@ -163,7 +163,7 @@ cmdBegin(void)
{ {
FUNCTION_LOG_VOID(logLevelTrace); FUNCTION_LOG_VOID(logLevelTrace);
ASSERT(cfgCommand() != cfgCmdNone); ASSERT(cfgInited());
// This is fairly expensive log message to generate so skip it if it won't be output // This is fairly expensive log message to generate so skip it if it won't be output
if (logAny(cfgLogLevelDefault())) if (logAny(cfgLogLevelDefault()))
@ -198,7 +198,7 @@ cmdEnd(const int code, const String *const errorMessage)
FUNCTION_LOG_PARAM(STRING, errorMessage); FUNCTION_LOG_PARAM(STRING, errorMessage);
FUNCTION_LOG_END(); FUNCTION_LOG_END();
ASSERT(cfgCommand() != cfgCmdNone); ASSERT(cfgInited());
// Skip this log message if it won't be output. It's not too expensive but since we skipped cmdBegin(), may as well. // Skip this log message if it won't be output. It's not too expensive but since we skipped cmdBegin(), may as well.
if (logAny(cfgLogLevelDefault())) if (logAny(cfgLogLevelDefault()))

View File

@ -11,7 +11,7 @@ Exit Routines
#include "command/lock.h" #include "command/lock.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/log.h" #include "common/log.h"
#include "config/config.h" #include "config/config.intern.h"
#include "protocol/helper.h" #include "protocol/helper.h"
#include "version.h" #include "version.h"
@ -137,12 +137,11 @@ exitSafe(int result, const bool error, const SignalType signalType)
result = errorCode(); result = errorCode();
} }
// Log command end if a command is set // On error generate an error message for command end when config is initialized
if (cfgCommand() != cfgCmdNone) if (cfgInited())
{ {
String *errorMessage = NULL; String *errorMessage = NULL;
// On error generate an error message
if (result != 0) if (result != 0)
{ {
// On process terminate // On process terminate

View File

@ -254,6 +254,16 @@ helpRender(const Buffer *const helpData)
String *const result = strCatZ(strNew(), PROJECT_NAME " " PROJECT_VERSION); String *const result = strCatZ(strNew(), PROJECT_NAME " " PROJECT_VERSION);
// Display version only
if (!cfgCommandHelp() &&
((cfgCommand() == cfgCmdHelp && cfgOptionBool(cfgOptVersion) && !cfgOptionBool(cfgOptHelp)) ||
cfgCommand() == cfgCmdVersion))
{
strCatChr(result, '\n');
FUNCTION_LOG_RETURN(STRING, result);
}
MEM_CONTEXT_TEMP_BEGIN() MEM_CONTEXT_TEMP_BEGIN()
{ {
// Set a small buffer size to minimize memory usage // Set a small buffer size to minimize memory usage
@ -287,7 +297,7 @@ helpRender(const Buffer *const helpData)
const String *more = NULL; const String *more = NULL;
// Display general help // Display general help
if (cfgCommand() == cfgCmdNone) if (!cfgCommandHelp())
{ {
strCatZ( strCatZ(
result, result,

View File

@ -78,6 +78,7 @@ Option constants
#define CFGOPT_EXPIRE_AUTO "expire-auto" #define CFGOPT_EXPIRE_AUTO "expire-auto"
#define CFGOPT_FILTER "filter" #define CFGOPT_FILTER "filter"
#define CFGOPT_FORCE "force" #define CFGOPT_FORCE "force"
#define CFGOPT_HELP "help"
#define CFGOPT_IGNORE_MISSING "ignore-missing" #define CFGOPT_IGNORE_MISSING "ignore-missing"
#define CFGOPT_IO_TIMEOUT "io-timeout" #define CFGOPT_IO_TIMEOUT "io-timeout"
#define CFGOPT_JOB_RETRY "job-retry" #define CFGOPT_JOB_RETRY "job-retry"
@ -135,8 +136,9 @@ Option constants
#define CFGOPT_TLS_SERVER_PORT "tls-server-port" #define CFGOPT_TLS_SERVER_PORT "tls-server-port"
#define CFGOPT_TYPE "type" #define CFGOPT_TYPE "type"
#define CFGOPT_VERBOSE "verbose" #define CFGOPT_VERBOSE "verbose"
#define CFGOPT_VERSION "version"
#define CFG_OPTION_TOTAL 181 #define CFG_OPTION_TOTAL 183
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Option value constants Option value constants
@ -366,7 +368,6 @@ typedef enum
cfgCmdStop, cfgCmdStop,
cfgCmdVerify, cfgCmdVerify,
cfgCmdVersion, cfgCmdVersion,
cfgCmdNone,
} ConfigCommand; } ConfigCommand;
/*********************************************************************************************************************************** /***********************************************************************************************************************************
@ -418,6 +419,7 @@ typedef enum
cfgOptExpireAuto, cfgOptExpireAuto,
cfgOptFilter, cfgOptFilter,
cfgOptForce, cfgOptForce,
cfgOptHelp,
cfgOptIgnoreMissing, cfgOptIgnoreMissing,
cfgOptIoTimeout, cfgOptIoTimeout,
cfgOptJobRetry, cfgOptJobRetry,
@ -564,6 +566,7 @@ typedef enum
cfgOptTlsServerPort, cfgOptTlsServerPort,
cfgOptType, cfgOptType,
cfgOptVerbose, cfgOptVerbose,
cfgOptVersion,
} ConfigOption; } ConfigOption;
#endif #endif

View File

@ -37,19 +37,27 @@ cfgInit(Config *const config)
FUNCTION_TEST_RETURN_VOID(); FUNCTION_TEST_RETURN_VOID();
} }
FN_EXTERN bool
cfgInited(void)
{
FUNCTION_TEST_VOID();
FUNCTION_TEST_RETURN(ENUM, configLocal != NULL);
}
/**********************************************************************************************************************************/ /**********************************************************************************************************************************/
FN_EXTERN ConfigCommand FN_EXTERN ConfigCommand
cfgCommand(void) cfgCommand(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
FUNCTION_TEST_RETURN(ENUM, configLocal == NULL ? cfgCmdNone : configLocal->command); ASSERT(cfgInited());
FUNCTION_TEST_RETURN(ENUM, configLocal->command);
} }
FN_EXTERN ConfigCommandRole FN_EXTERN ConfigCommandRole
cfgCommandRole(void) cfgCommandRole(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
FUNCTION_TEST_RETURN(ENUM, configLocal->commandRole); FUNCTION_TEST_RETURN(ENUM, configLocal->commandRole);
} }
@ -61,8 +69,8 @@ cfgCommandSet(ConfigCommand commandId, ConfigCommandRole commandRoleId)
FUNCTION_TEST_PARAM(ENUM, commandRoleId); FUNCTION_TEST_PARAM(ENUM, commandRoleId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(commandId <= cfgCmdNone); ASSERT(commandId < CFG_COMMAND_TOTAL);
configLocal->command = commandId; configLocal->command = commandId;
configLocal->commandRole = commandRoleId; configLocal->commandRole = commandRoleId;
@ -85,7 +93,7 @@ cfgCommandJobRetry(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
// Return NULL if no retries // Return NULL if no retries
const unsigned int retryTotal = cfgOptionUInt(cfgOptJobRetry); const unsigned int retryTotal = cfgOptionUInt(cfgOptJobRetry);
@ -112,8 +120,8 @@ cfgCommandName(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command < cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN_CONST(STRINGZ, cfgParseCommandName(configLocal->command)); FUNCTION_TEST_RETURN_CONST(STRINGZ, cfgParseCommandName(configLocal->command));
} }
@ -131,7 +139,7 @@ cfgCommandParam(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
if (configLocal->paramList == NULL) if (configLocal->paramList == NULL)
{ {
@ -150,7 +158,7 @@ FN_EXTERN const String *
cfgExe(void) cfgExe(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
FUNCTION_TEST_RETURN(STRING, configLocal->exe); FUNCTION_TEST_RETURN(STRING, configLocal->exe);
} }
@ -160,8 +168,8 @@ cfgLockRequired(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command != cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
// Local roles never take a lock and the remote role has special logic for locking // Local roles never take a lock and the remote role has special logic for locking
FUNCTION_TEST_RETURN( FUNCTION_TEST_RETURN(
@ -178,8 +186,8 @@ cfgLockRemoteRequired(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command != cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(BOOL, configLocal->lockRemoteRequired); FUNCTION_TEST_RETURN(BOOL, configLocal->lockRemoteRequired);
} }
@ -190,8 +198,8 @@ cfgLockType(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command != cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(ENUM, configLocal->lockType); FUNCTION_TEST_RETURN(ENUM, configLocal->lockType);
} }
@ -202,8 +210,8 @@ cfgLogFile(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command != cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN( FUNCTION_TEST_RETURN(
BOOL, BOOL,
@ -221,8 +229,8 @@ cfgLogLevelDefault(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->command != cfgCmdNone); ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(ENUM, configLocal->logLevelDefault); FUNCTION_TEST_RETURN(ENUM, configLocal->logLevelDefault);
} }
@ -235,7 +243,7 @@ cfgOptionGroup(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId); FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].group); FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].group);
@ -250,7 +258,7 @@ cfgOptionGroupName(const ConfigOptionGroup groupId, const unsigned int groupIdx)
FUNCTION_TEST_PARAM(UINT, groupIdx); FUNCTION_TEST_PARAM(UINT, groupIdx);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL); ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal); ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal);
@ -280,7 +288,7 @@ cfgOptionGroupId(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId); FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal->option[optionId].group); ASSERT(configLocal->option[optionId].group);
@ -295,7 +303,7 @@ cfgOptionGroupIdxDefault(const ConfigOptionGroup groupId)
FUNCTION_TEST_PARAM(ENUM, groupId); FUNCTION_TEST_PARAM(ENUM, groupId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL); ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(configLocal->optionGroup[groupId].indexDefaultExists); ASSERT(configLocal->optionGroup[groupId].indexDefaultExists);
@ -311,7 +319,7 @@ cfgOptionGroupIdxToKey(const ConfigOptionGroup groupId, const unsigned int group
FUNCTION_TEST_PARAM(UINT, groupIdx); FUNCTION_TEST_PARAM(UINT, groupIdx);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL); ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal); ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal);
@ -327,7 +335,7 @@ cfgOptionKeyToIdx(const ConfigOption optionId, const unsigned int key)
FUNCTION_TEST_PARAM(UINT, key); FUNCTION_TEST_PARAM(UINT, key);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
unsigned int result = 0; unsigned int result = 0;
@ -360,7 +368,7 @@ cfgOptionGroupIdxTotal(const ConfigOptionGroup groupId)
FUNCTION_TEST_PARAM(ENUM, groupId); FUNCTION_TEST_PARAM(ENUM, groupId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL); ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
FUNCTION_TEST_RETURN(UINT, configLocal->optionGroup[groupId].indexTotal); FUNCTION_TEST_RETURN(UINT, configLocal->optionGroup[groupId].indexTotal);
@ -374,7 +382,7 @@ cfgOptionIdxDefault(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId); FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT( ASSERT(
!configLocal->option[optionId].group || configLocal->optionGroup[configLocal->option[optionId].groupId].indexDefaultExists); !configLocal->option[optionId].group || configLocal->optionGroup[configLocal->option[optionId].groupId].indexDefaultExists);
@ -392,7 +400,7 @@ cfgOptionIdxTotal(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId); FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
const ConfigOptionData *const option = &configLocal->option[optionId]; const ConfigOptionData *const option = &configLocal->option[optionId];
@ -408,7 +416,7 @@ cfgOptionDefault(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId); FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ConfigOptionData *const option = &configLocal->option[optionId]; ConfigOptionData *const option = &configLocal->option[optionId];
@ -428,7 +436,7 @@ cfgOptionDefaultSet(const ConfigOption optionId, const Variant *defaultValue)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT(configLocal->option[optionId].valid); ASSERT(configLocal->option[optionId].valid);
ASSERT(cfgParseOptionDataType(optionId) == cfgOptDataTypeString); ASSERT(cfgParseOptionDataType(optionId) == cfgOptDataTypeString);
@ -500,7 +508,7 @@ cfgOptionIdxDisplay(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -559,7 +567,7 @@ cfgOptionIdxName(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -604,7 +612,7 @@ cfgOptionIdxNegate(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -622,7 +630,7 @@ cfgOptionIdxReset(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -644,7 +652,7 @@ cfgOptionIdxInternal(
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -681,7 +689,7 @@ cfgOptionIdxVar(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_PARAM(UINT, optionIdx); FUNCTION_TEST_PARAM(UINT, optionIdx);
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -813,7 +821,7 @@ cfgOptionIdxLst(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_LOG_PARAM(UINT, optionIdx); FUNCTION_LOG_PARAM(UINT, optionIdx);
FUNCTION_LOG_END(); FUNCTION_LOG_END();
ASSERT(configLocal != NULL); ASSERT(cfgInited());
const VariantList *optionValue = cfgOptionIdxInternal(optionId, optionIdx, cfgOptDataTypeList, true)->value.list; const VariantList *optionValue = cfgOptionIdxInternal(optionId, optionIdx, cfgOptDataTypeList, true)->value.list;
@ -890,7 +898,7 @@ cfgOptionIdxSet(
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -985,7 +993,7 @@ cfgOptionIdxSource(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -1013,7 +1021,7 @@ cfgOptionIdxTest(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT(!cfgOptionValid(optionId) || ((!group && optionIdx == 0) || (group && optionIdx < indexTotal))); ASSERT(!cfgOptionValid(optionId) || ((!group && optionIdx == 0) || (group && optionIdx < indexTotal)));
@ -1030,7 +1038,7 @@ cfgOptionValid(const ConfigOption optionId)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].valid); FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].valid);
} }
@ -1043,7 +1051,7 @@ cfgOptionInvalidate(const ConfigOption optionId)
FUNCTION_TEST_END(); FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL); ASSERT(cfgInited());
configLocal->option[optionId].valid = false; configLocal->option[optionId].valid = false;

View File

@ -88,6 +88,9 @@ Init Function
// Init with new configuration // Init with new configuration
FN_EXTERN void cfgInit(Config *config); FN_EXTERN void cfgInit(Config *config);
// Has the configuration been initialized?
FN_EXTERN bool cfgInited(void);
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Command Functions Command Functions
***********************************************************************************************************************************/ ***********************************************************************************************************************************/

View File

@ -526,57 +526,53 @@ cfgLoad(const unsigned int argListSize, const char *argList[])
if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask)) if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask))
umask(0000); umask(0000);
// If a command is set // Initialize TCP settings
if (cfgCommand() != cfgCmdNone) if (cfgOptionValid(cfgOptSckKeepAlive))
{ {
// Initialize TCP settings sckInit(
if (cfgOptionValid(cfgOptSckKeepAlive)) cfgOptionBool(cfgOptSckBlock),
{ cfgOptionBool(cfgOptSckKeepAlive),
sckInit( cfgOptionTest(cfgOptTcpKeepAliveCount) ? cfgOptionInt(cfgOptTcpKeepAliveCount) : 0,
cfgOptionBool(cfgOptSckBlock), cfgOptionTest(cfgOptTcpKeepAliveIdle) ? cfgOptionInt(cfgOptTcpKeepAliveIdle) : 0,
cfgOptionBool(cfgOptSckKeepAlive), cfgOptionTest(cfgOptTcpKeepAliveInterval) ? cfgOptionInt(cfgOptTcpKeepAliveInterval) : 0);
cfgOptionTest(cfgOptTcpKeepAliveCount) ? cfgOptionInt(cfgOptTcpKeepAliveCount) : 0,
cfgOptionTest(cfgOptTcpKeepAliveIdle) ? cfgOptionInt(cfgOptTcpKeepAliveIdle) : 0,
cfgOptionTest(cfgOptTcpKeepAliveInterval) ? cfgOptionInt(cfgOptTcpKeepAliveInterval) : 0);
}
// Set IO buffer size
if (cfgOptionValid(cfgOptBufferSize))
ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize));
// Set IO timeout
if (cfgOptionValid(cfgOptIoTimeout))
ioTimeoutMsSet(cfgOptionUInt64(cfgOptIoTimeout));
// Open the log file if this command logs to a file
cfgLoadLogFile();
// Create the exec-id used to identify all locals and remotes spawned by this process. This allows lock contention to be
// easily resolved and makes it easier to associate processes from various logs.
if (cfgOptionValid(cfgOptExecId) && !cfgOptionTest(cfgOptExecId))
{
// Generate some random bytes
uint32_t execRandom;
cryptoRandomBytes((unsigned char *)&execRandom, sizeof(execRandom));
// Format a string with the pid and the random bytes to serve as the exec id
cfgOptionSet(cfgOptExecId, cfgSourceParam, VARSTR(strNewFmt("%d-%08x", getpid(), execRandom)));
}
// Begin the command
cmdBegin();
// Initialize the lock module
if (cfgOptionTest(cfgOptLockPath))
lockInit(cfgOptionStr(cfgOptLockPath), cfgOptionStr(cfgOptExecId));
// Acquire a lock if this command requires a lock
if (cfgLockType() != lockTypeNone && !cfgCommandHelp() && cfgLockRequired())
cmdLockAcquireP();
// Update options that have complex rules
cfgLoadUpdateOption();
} }
// Set IO buffer size (use the default for help to lower memory usage)
if (cfgOptionValid(cfgOptBufferSize) && !cfgCommandHelp())
ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize));
// Set IO timeout
if (cfgOptionValid(cfgOptIoTimeout))
ioTimeoutMsSet(cfgOptionUInt64(cfgOptIoTimeout));
// Open the log file if this command logs to a file
cfgLoadLogFile();
// Create the exec-id used to identify all locals and remotes spawned by this process. This allows lock contention to be
// easily resolved and makes it easier to associate processes from various logs.
if (cfgOptionValid(cfgOptExecId) && !cfgOptionTest(cfgOptExecId))
{
// Generate some random bytes
uint32_t execRandom;
cryptoRandomBytes((unsigned char *)&execRandom, sizeof(execRandom));
// Format a string with the pid and the random bytes to serve as the exec id
cfgOptionSet(cfgOptExecId, cfgSourceParam, VARSTR(strNewFmt("%d-%08x", getpid(), execRandom)));
}
// Begin the command
cmdBegin();
// Initialize the lock module
if (cfgOptionTest(cfgOptLockPath))
lockInit(cfgOptionStr(cfgOptLockPath), cfgOptionStr(cfgOptExecId));
// Acquire a lock if this command requires a lock
if (cfgLockType() != lockTypeNone && !cfgCommandHelp() && cfgLockRequired())
cmdLockAcquireP();
// Update options that have complex rules
cfgLoadUpdateOption();
} }
MEM_CONTEXT_TEMP_END(); MEM_CONTEXT_TEMP_END();

View File

@ -2432,6 +2432,30 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/force ), // opt/force
), // opt/force ), // opt/force
// ----------------------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/help
( // opt/help
PARSE_RULE_OPTION_NAME("help"), // opt/help
PARSE_RULE_OPTION_TYPE(Boolean), // opt/help
PARSE_RULE_OPTION_REQUIRED(true), // opt/help
PARSE_RULE_OPTION_SECTION(CommandLine), // opt/help
// opt/help
PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/help
( // opt/help
PARSE_RULE_OPTION_COMMAND(Help) // opt/help
), // opt/help
// opt/help
PARSE_RULE_OPTIONAL // opt/help
( // opt/help
PARSE_RULE_OPTIONAL_GROUP // opt/help
( // opt/help
PARSE_RULE_OPTIONAL_DEFAULT // opt/help
( // opt/help
PARSE_RULE_VAL_BOOL_FALSE, // opt/help
), // opt/help
), // opt/help
), // opt/help
), // opt/help
// -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/ignore-missing PARSE_RULE_OPTION // opt/ignore-missing
( // opt/ignore-missing ( // opt/ignore-missing
PARSE_RULE_OPTION_NAME("ignore-missing"), // opt/ignore-missing PARSE_RULE_OPTION_NAME("ignore-missing"), // opt/ignore-missing
@ -10650,6 +10674,30 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/verbose ), // opt/verbose
), // opt/verbose ), // opt/verbose
), // opt/verbose ), // opt/verbose
// -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/version
( // opt/version
PARSE_RULE_OPTION_NAME("version"), // opt/version
PARSE_RULE_OPTION_TYPE(Boolean), // opt/version
PARSE_RULE_OPTION_REQUIRED(true), // opt/version
PARSE_RULE_OPTION_SECTION(CommandLine), // opt/version
// opt/version
PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/version
( // opt/version
PARSE_RULE_OPTION_COMMAND(Help) // opt/version
), // opt/version
// opt/version
PARSE_RULE_OPTIONAL // opt/version
( // opt/version
PARSE_RULE_OPTIONAL_GROUP // opt/version
( // opt/version
PARSE_RULE_OPTIONAL_DEFAULT // opt/version
( // opt/version
PARSE_RULE_VAL_BOOL_FALSE, // opt/version
), // opt/version
), // opt/version
), // opt/version
), // opt/version
}; };
/*********************************************************************************************************************************** /***********************************************************************************************************************************
@ -10943,6 +10991,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptExecId, // opt-resolve-order cfgOptExecId, // opt-resolve-order
cfgOptExpireAuto, // opt-resolve-order cfgOptExpireAuto, // opt-resolve-order
cfgOptFilter, // opt-resolve-order cfgOptFilter, // opt-resolve-order
cfgOptHelp, // opt-resolve-order
cfgOptIgnoreMissing, // opt-resolve-order cfgOptIgnoreMissing, // opt-resolve-order
cfgOptIoTimeout, // opt-resolve-order cfgOptIoTimeout, // opt-resolve-order
cfgOptJobRetry, // opt-resolve-order cfgOptJobRetry, // opt-resolve-order
@ -11013,6 +11062,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptTlsServerPort, // opt-resolve-order cfgOptTlsServerPort, // opt-resolve-order
cfgOptType, // opt-resolve-order cfgOptType, // opt-resolve-order
cfgOptVerbose, // opt-resolve-order cfgOptVerbose, // opt-resolve-order
cfgOptVersion, // opt-resolve-order
cfgOptArchiveCheck, // opt-resolve-order cfgOptArchiveCheck, // opt-resolve-order
cfgOptArchiveCopy, // opt-resolve-order cfgOptArchiveCopy, // opt-resolve-order
cfgOptArchiveModeCheck, // opt-resolve-order cfgOptArchiveModeCheck, // opt-resolve-order

File diff suppressed because it is too large Load Diff

View File

@ -175,12 +175,6 @@ main(int argListSize, const char *argList[])
cmdExpire(); cmdExpire();
break; break;
// Help command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Info command // Info command
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdInfo: case cfgCmdInfo:
@ -271,17 +265,12 @@ main(int argListSize, const char *argList[])
cmdVerify(); cmdVerify();
break; break;
// Display version // Help/version commands
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
case cfgCmdVersion: case cfgCmdVersion:
printf(PROJECT_NAME " " PROJECT_VERSION "\n"); cmdHelp(BUF(helpData, sizeof(helpData)));
fflush(stdout);
break; break;
// Error on commands that should have been handled
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
THROW(AssertError, "'none' command should have been handled");
} }
} }
// Local/remote commands // Local/remote commands

View File

@ -65,6 +65,14 @@ option:
command: command:
test: {} test: {}
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
neutral-umask: neutral-umask:
type: boolean type: boolean
internal: true internal: true
@ -123,6 +131,14 @@ option:
command: command:
test: {} test: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
vm: vm:
type: string type: string
internal: true internal: true

View File

@ -230,6 +230,28 @@
<text> <text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>test-pgbackrest help test</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help test vm</cmd>) then a full description of the option as it applies to the command will be displayed.</p> <p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>test-pgbackrest help test</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help test vm</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text> </text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command> </command>
<!-- Noop command holds options that must be defined but we don't need --> <!-- Noop command holds options that must be defined but we don't need -->

View File

@ -65,7 +65,7 @@ hrnCfgLoad(ConfigCommand commandId, const StringList *argListParam, const HrnCfg
} }
// Insert the command so it does not interfere with parameters // Insert the command so it does not interfere with parameters
if (commandId != cfgCmdNone) if (commandId < CFG_COMMAND_TOTAL)
strLstInsert(argList, 0, cfgParseCommandRoleName(commandId, param.role)); strLstInsert(argList, 0, cfgParseCommandRoleName(commandId, param.role));
// Insert the project exe // Insert the project exe

View File

@ -126,7 +126,7 @@ protocolLocalExec(
// Load configuration // Load configuration
StringList *const paramList = protocolLocalParam(protocolStorageType, hostIdx, processId); StringList *const paramList = protocolLocalParam(protocolStorageType, hostIdx, processId);
hrnCfgLoadP(cfgCmdNone, paramList, .noStd = true); hrnCfgLoadP(CFG_COMMAND_TOTAL, paramList, .noStd = true);
// Change log process id to aid in debugging // Change log process id to aid in debugging
hrnLogProcessIdSet(processId); hrnLogProcessIdSet(processId);
@ -224,7 +224,7 @@ protocolRemoteExec(
// Load configuration // Load configuration
StringList *const paramList = protocolRemoteParam(protocolStorageType, hostIdx); StringList *const paramList = protocolRemoteParam(protocolStorageType, hostIdx);
hrnCfgLoadP(cfgCmdNone, paramList, .noStd = true); hrnCfgLoadP(CFG_COMMAND_TOTAL, paramList, .noStd = true);
// Change log process id to aid in debugging // Change log process id to aid in debugging
hrnLogProcessIdSet(processId); hrnLogProcessIdSet(processId);

View File

@ -48,16 +48,28 @@ cfgLoadUpdateOption(void)
THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd"); THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd");
// If repo-path is relative then make it absolute // If repo-path is relative then make it absolute
const String *const repoPath = cfgOptionStr(cfgOptRepoPath); if (cfgOptionValid(cfgOptRepoPath))
{
const String *const repoPath = cfgOptionStr(cfgOptRepoPath);
if (!strBeginsWithZ(repoPath, "/")) if (!strBeginsWithZ(repoPath, "/"))
cfgOptionSet(cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath)))); {
cfgOptionSet(
cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
}
}
// If test-path is relative then make it absolute // If test-path is relative then make it absolute
const String *const testPath = cfgOptionStr(cfgOptTestPath); if (cfgOptionValid(cfgOptTestPath))
{
const String *const testPath = cfgOptionStr(cfgOptTestPath);
if (!strBeginsWithZ(testPath, "/")) if (!strBeginsWithZ(testPath, "/"))
cfgOptionSet(cfgOptTestPath, cfgOptionSource(cfgOptTestPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(testPath)))); {
cfgOptionSet(
cfgOptTestPath, cfgOptionSource(cfgOptTestPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(testPath))));
}
}
FUNCTION_LOG_RETURN_VOID(); FUNCTION_LOG_RETURN_VOID();
} }
@ -95,27 +107,22 @@ cfgLoad(unsigned int argListSize, const char *argList[])
if (cfgCommand() == cfgCmdNoop) if (cfgCommand() == cfgCmdNoop)
THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'"); THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'");
// If a command is set // Load the log settings
if (cfgCommand() != cfgCmdNone && cfgCommand() != cfgCmdHelp && cfgCommand() != cfgCmdVersion) cfgLoadLogSetting();
{
// Load the log settings
if (!cfgCommandHelp())
cfgLoadLogSetting();
// Neutralize the umask to make the repository file/path modes more consistent // Neutralize the umask to make the repository file/path modes more consistent
if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask)) if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask))
umask(0000); umask(0000);
// Set IO buffer size // Set IO buffer size
if (cfgOptionValid(cfgOptBufferSize)) if (cfgOptionValid(cfgOptBufferSize))
ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize)); ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize));
// Update options that have complex rules // Update options that have complex rules
cfgLoadUpdateOption(); cfgLoadUpdateOption();
// Begin the command // Begin the command
cmdBegin(); cmdBegin();
}
} }
MEM_CONTEXT_TEMP_END(); MEM_CONTEXT_TEMP_END();

View File

@ -86,22 +86,15 @@ main(int argListSize, const char *argList[])
break; break;
} }
// Help // Help/version
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp: case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdVersion: case cfgCmdVersion:
printf(PROJECT_NAME " Test " PROJECT_VERSION "\n"); cmdHelp(BUF(helpData, sizeof(helpData)));
fflush(stdout);
break; break;
// Error on commands that should have been handled // Error on commands that should have been handled
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
case cfgCmdNoop: case cfgCmdNoop:
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName()); THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
break; break;

View File

@ -525,7 +525,6 @@ testRun(void)
" cfgCmdBackup,\n" " cfgCmdBackup,\n"
" cfgCmdHelp,\n" " cfgCmdHelp,\n"
" cfgCmdVersion,\n" " cfgCmdVersion,\n"
" cfgCmdNone,\n"
"} ConfigCommand;\n" "} ConfigCommand;\n"
"\n" "\n"
COMMENT_BLOCK_BEGIN "\n" COMMENT_BLOCK_BEGIN "\n"

View File

@ -29,6 +29,7 @@ testRun(void)
// ***************************************************************************************************************************** // *****************************************************************************************************************************
if (testBegin("exitInit() and exitOnSignal()")) if (testBegin("exitInit() and exitOnSignal()"))
{ {
TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no command");
HRN_CFG_LOAD(cfgCmdHelp, strLstNew()); HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
HRN_FORK_BEGIN() HRN_FORK_BEGIN()
@ -46,11 +47,6 @@ testRun(void)
// ***************************************************************************************************************************** // *****************************************************************************************************************************
if (testBegin("exitSafe()")) if (testBegin("exitSafe()"))
{ {
HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
cfgCommandSet(cfgCmdNone, cfgCmdRoleMain);
TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no command");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
StringList *argList = strLstNew(); StringList *argList = strLstNew();
hrnCfgArgRawZ(argList, cfgOptStanza, "test"); hrnCfgArgRawZ(argList, cfgOptStanza, "test");

View File

@ -136,6 +136,23 @@ testRun(void)
{ {
StringList *argList = NULL; StringList *argList = NULL;
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version");
const char *versionOnly = zNewFmt("%s\n", helpVersion);
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "version");
TEST_RESULT_VOID(testCfgLoad(argList), "version from version command");
TEST_RESULT_STR_Z(helpRender(helpData), versionOnly, "check text");
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "--version");
TEST_RESULT_VOID(testCfgLoad(argList), "version from version option");
TEST_RESULT_STR_Z(helpRender(helpData), versionOnly, "check text");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("general invocation"); TEST_TITLE("general invocation");
@ -150,6 +167,13 @@ testRun(void)
TEST_RESULT_VOID(testCfgLoad(argList), "help from help command"); TEST_RESULT_VOID(testCfgLoad(argList), "help from help command");
TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text"); TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text");
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "--version");
strLstAddZ(argList, "--help");
TEST_RESULT_VOID(testCfgLoad(argList), "help from help option");
TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version command"); TEST_TITLE("version command");

View File

@ -883,8 +883,10 @@ testRun(void)
hrnCfgArgRawZ(argList, cfgOptLogLevelFile, "off"); hrnCfgArgRawZ(argList, cfgOptLogLevelFile, "off");
hrnCfgArgRawZ(argList, cfgOptRepoRetentionFull, "2"); hrnCfgArgRawZ(argList, cfgOptRepoRetentionFull, "2");
ioBufferSizeSet(333);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "help command for backup"); TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "help command for backup");
TEST_RESULT_UINT(ioBufferSize(), 1048576, "buffer size set to option default"); TEST_RESULT_UINT(ioBufferSize(), 333, "buffer size not updated by help command");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help command for help"); TEST_TITLE("help command for help");
@ -894,8 +896,10 @@ testRun(void)
strLstAddZ(argList, "help"); strLstAddZ(argList, "help");
strLstAddZ(argList, "help"); strLstAddZ(argList, "help");
ioBufferSizeSet(333);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "load config"); TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "load config");
TEST_RESULT_UINT(ioBufferSize(), 1048576, "buffer size set to option default"); TEST_RESULT_UINT(ioBufferSize(), 333, "buffer size not updated by help command");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("command takes lock and opens log file and uses custom tcp settings"); TEST_TITLE("command takes lock and opens log file and uses custom tcp settings");

View File

@ -82,12 +82,12 @@ testRun(void)
// Config functions that are not tested with parse // Config functions that are not tested with parse
// ***************************************************************************************************************************** // *****************************************************************************************************************************
if (testBegin("cfg*()")) if (testBegin("cfgInited()"))
{ {
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("config command defaults to none before cfgInit()"); TEST_TITLE("config command defaults to none before cfgInit()");
TEST_RESULT_UINT(cfgCommand(), cfgCmdNone, "command is none"); TEST_RESULT_BOOL(cfgInited(), false, "config is not inited");
} }
// config and config-include-path options // config and config-include-path options
@ -1547,8 +1547,8 @@ testRun(void)
hrnLogLevelStdOutSet(logLevelOff); hrnLogLevelStdOutSet(logLevelOff);
hrnLogLevelStdErrSet(logLevelOff); hrnLogLevelStdErrSet(logLevelOff);
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList)), "no command"); TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList)), "no command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set"); TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none"); TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelWarn, "console logging is warn"); TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelWarn, "console logging is warn");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelOff, "stderr logging is off"); TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelOff, "stderr logging is off");
harnessLogLevelReset(); harnessLogLevelReset();
@ -1561,8 +1561,8 @@ testRun(void)
strLstAddZ(argList, "help"); strLstAddZ(argList, "help");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help command"); TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set"); TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is help"); TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
argList = strLstNew(); argList = strLstNew();
strLstAddZ(argList, TEST_BACKREST_EXE); strLstAddZ(argList, TEST_BACKREST_EXE);
@ -1571,7 +1571,7 @@ testRun(void)
TEST_RESULT_VOID( TEST_RESULT_VOID(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help for version command"); cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help for version command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set"); TEST_RESULT_BOOL(cfgCommandHelp(), true, "command help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdVersion, "command is version"); TEST_RESULT_INT(cfgCommand(), cfgCmdVersion, "command is version");
TEST_RESULT_Z(cfgCommandName(), "version", "command name is version"); TEST_RESULT_Z(cfgCommandName(), "version", "command name is version");
@ -1582,7 +1582,7 @@ testRun(void)
TEST_RESULT_VOID( TEST_RESULT_VOID(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config"); cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set"); TEST_RESULT_BOOL(cfgCommandHelp(), true, "command help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help"); TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
@ -1593,8 +1593,9 @@ testRun(void)
strLstAddZ(argList, "--help"); strLstAddZ(argList, "--help");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config"); TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set"); TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none"); TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptHelp), true, "help option is set");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version option"); TEST_TITLE("version option");
@ -1604,8 +1605,9 @@ testRun(void)
strLstAddZ(argList, "--version"); strLstAddZ(argList, "--version");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config"); TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set"); TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdVersion, "command is version"); TEST_RESULT_UINT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptVersion), true, "version option is set");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help and version options"); TEST_TITLE("help and version options");
@ -1616,8 +1618,10 @@ testRun(void)
strLstAddZ(argList, "--version"); strLstAddZ(argList, "--version");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config"); TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is not set"); TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none"); TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptHelp), true, "version option is set");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptVersion), true, "version option is set");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("error on command with --help option"); TEST_TITLE("error on command with --help option");
@ -1629,7 +1633,7 @@ testRun(void)
TEST_ERROR( TEST_ERROR(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError, cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError,
"invalid option '--help'"); "option 'help' not valid for command 'backup'");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("error on command with --version option"); TEST_TITLE("error on command with --version option");
@ -1641,7 +1645,7 @@ testRun(void)
TEST_ERROR( TEST_ERROR(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError, cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError,
"invalid option '--version'"); "option 'version' not valid for command 'backup'");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help command - should not fail on missing options"); TEST_TITLE("help command - should not fail on missing options");