MXS-304: Minor refactoring of config.c
The loading of a single file refactored out into a separate function. To be used later when multiple files are loaded and processed.
This commit is contained in:
@ -268,7 +268,7 @@ char* config_clean_string_list(const char* str)
|
|||||||
* @return zero on error
|
* @return zero on error
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
handler(void *userdata, const char *section, const char *name, const char *value)
|
ini_handler(void *userdata, const char *section, const char *name, const char *value)
|
||||||
{
|
{
|
||||||
CONFIG_CONTEXT *cntxt = (CONFIG_CONTEXT *)userdata;
|
CONFIG_CONTEXT *cntxt = (CONFIG_CONTEXT *)userdata;
|
||||||
CONFIG_CONTEXT *ptr = cntxt;
|
CONFIG_CONTEXT *ptr = cntxt;
|
||||||
@ -352,6 +352,47 @@ handler(void *userdata, const char *section, const char *name, const char *value
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load single configuration file.
|
||||||
|
*
|
||||||
|
* @param file The file to load.
|
||||||
|
* @param context The context used when parsing.
|
||||||
|
*
|
||||||
|
* @return True if the file could be parsed, false otherwise.
|
||||||
|
*/
|
||||||
|
static bool config_load_single_file(const char* file, CONFIG_CONTEXT* context)
|
||||||
|
{
|
||||||
|
int rval = -1;
|
||||||
|
|
||||||
|
if (!config_has_duplicate_sections(file))
|
||||||
|
{
|
||||||
|
if ((rval = ini_parse(file, ini_handler, context)) != 0)
|
||||||
|
{
|
||||||
|
char errorbuffer[1024 + 1];
|
||||||
|
|
||||||
|
if (rval > 0)
|
||||||
|
{
|
||||||
|
snprintf(errorbuffer, sizeof(errorbuffer),
|
||||||
|
"Failed to parse configuration file. Error on line %d.", rval);
|
||||||
|
}
|
||||||
|
else if (rval == -1)
|
||||||
|
{
|
||||||
|
snprintf(errorbuffer, sizeof(errorbuffer),
|
||||||
|
"Failed to parse configuration file. Failed to open file.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(errorbuffer, sizeof(errorbuffer),
|
||||||
|
"Failed to parse configuration file. Memory allocation failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
MXS_ERROR("%s", errorbuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rval == 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Load the configuration file for the MaxScale
|
* @brief Load the configuration file for the MaxScale
|
||||||
*
|
*
|
||||||
@ -364,15 +405,8 @@ handler(void *userdata, const char *section, const char *name, const char *value
|
|||||||
bool
|
bool
|
||||||
config_load(const char *file)
|
config_load(const char *file)
|
||||||
{
|
{
|
||||||
CONFIG_CONTEXT config = {.object = ""};
|
|
||||||
int ini_rval;
|
|
||||||
bool rval = false;
|
bool rval = false;
|
||||||
|
|
||||||
if (config_has_duplicate_sections(file))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Temporary - should use configuration values and test return value (bool) */
|
/* Temporary - should use configuration values and test return value (bool) */
|
||||||
dcb_pre_alloc(1000);
|
dcb_pre_alloc(1000);
|
||||||
session_pre_alloc(250);
|
session_pre_alloc(250);
|
||||||
@ -380,35 +414,16 @@ config_load(const char *file)
|
|||||||
global_defaults();
|
global_defaults();
|
||||||
feedback_defaults();
|
feedback_defaults();
|
||||||
|
|
||||||
if ((ini_rval = ini_parse(file, handler, &config)) != 0)
|
CONFIG_CONTEXT config = {.object = ""};
|
||||||
|
|
||||||
|
if (config_load_single_file(file, &config))
|
||||||
{
|
{
|
||||||
char errorbuffer[1024 + 1];
|
config_file = file;
|
||||||
|
|
||||||
if (ini_rval > 0)
|
if (check_config_objects(config.next) && process_config_context(config.next))
|
||||||
{
|
{
|
||||||
snprintf(errorbuffer, sizeof(errorbuffer),
|
rval = true;
|
||||||
"Error: Failed to parse configuration file. Error on line %d.", ini_rval);
|
|
||||||
}
|
}
|
||||||
else if (ini_rval == -1)
|
|
||||||
{
|
|
||||||
snprintf(errorbuffer, sizeof(errorbuffer),
|
|
||||||
"Error: Failed to parse configuration file. Failed to open file.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
snprintf(errorbuffer, sizeof(errorbuffer),
|
|
||||||
"Error: Failed to parse configuration file. Memory allocation failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
MXS_ERROR("%s", errorbuffer);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
config_file = file;
|
|
||||||
|
|
||||||
if (check_config_objects(config.next) && process_config_context(config.next))
|
|
||||||
{
|
|
||||||
rval = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free_config_context(config.next);
|
free_config_context(config.next);
|
||||||
@ -446,7 +461,7 @@ config_reload()
|
|||||||
config.object = "";
|
config.object = "";
|
||||||
config.next = NULL;
|
config.next = NULL;
|
||||||
|
|
||||||
if (ini_parse(config_file, handler, &config) < 0)
|
if (ini_parse(config_file, ini_handler, &config) < 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user