Server list handling functions consistency
The functions handling servers lists now act with a bit more consistency, always returning the array size instead of NULL- terminating arrays.
This commit is contained in:
@ -1057,22 +1057,45 @@ SERVER* config_get_server(const MXS_CONFIG_PARAMETER *params, const char *key)
|
||||
return server_find_by_unique_name(value);
|
||||
}
|
||||
|
||||
SERVER** config_get_serverlist(const MXS_CONFIG_PARAMETER *params, const char *key)
|
||||
int config_get_server_list(const MXS_CONFIG_PARAMETER *params, const char *key,
|
||||
SERVER*** output)
|
||||
{
|
||||
const char *value = config_get_value_string(params, key);
|
||||
char **server_names = NULL;
|
||||
SERVER **servers = NULL;
|
||||
int n_names = config_parse_server_list(value, &server_names);
|
||||
int found = 0;
|
||||
const int n_names = config_parse_server_list(value, &server_names);
|
||||
if (n_names > 0)
|
||||
{
|
||||
servers = server_find_by_unique_names(server_names, n_names);
|
||||
SERVER** servers;
|
||||
found = server_find_by_unique_names(server_names, n_names, &servers);
|
||||
for (int i = 0; i < n_names; i++)
|
||||
{
|
||||
MXS_FREE(server_names[i]);
|
||||
}
|
||||
MXS_FREE(server_names);
|
||||
|
||||
if (found)
|
||||
{
|
||||
/* Fill in the result array */
|
||||
SERVER** result = MXS_CALLOC(found, sizeof(SERVER*));
|
||||
if (result)
|
||||
{
|
||||
int res_ind = 0;
|
||||
for (int i = 0; i < n_names; i++)
|
||||
{
|
||||
if (servers[i])
|
||||
{
|
||||
result[res_ind] = servers[i];
|
||||
res_ind++;
|
||||
}
|
||||
}
|
||||
*output = result;
|
||||
ss_dassert(found == res_ind);
|
||||
}
|
||||
MXS_FREE(servers);
|
||||
}
|
||||
}
|
||||
return servers;
|
||||
return found;
|
||||
}
|
||||
|
||||
char* config_copy_string(const MXS_CONFIG_PARAMETER *params, const char *key)
|
||||
@ -3470,7 +3493,7 @@ bool config_param_is_valid(const MXS_MODULE_PARAM *params, const char *key,
|
||||
return valid;
|
||||
}
|
||||
|
||||
static int config_parse_server_list(const char *servers, char ***output_array)
|
||||
int config_parse_server_list(const char *servers, char ***output_array)
|
||||
{
|
||||
ss_dassert(servers);
|
||||
|
||||
@ -3486,7 +3509,7 @@ static int config_parse_server_list(const char *servers, char ***output_array)
|
||||
char **results = MXS_CALLOC(out_arr_size, sizeof(char*));
|
||||
if (!results)
|
||||
{
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parse the server names from the list. They are separated by ',' and will
|
||||
@ -3531,8 +3554,10 @@ static int config_parse_server_list(const char *servers, char ***output_array)
|
||||
if (output_ind == 0)
|
||||
{
|
||||
MXS_FREE(results);
|
||||
results = NULL;
|
||||
}
|
||||
*output_array = results;
|
||||
else
|
||||
{
|
||||
*output_array = results;
|
||||
}
|
||||
return output_ind;
|
||||
}
|
||||
|
@ -111,15 +111,4 @@ SSL_LISTENER *make_ssl_structure(CONFIG_CONTEXT *obj, bool require_cert, int *er
|
||||
*/
|
||||
bool config_have_required_ssl_params(CONFIG_CONTEXT *obj);
|
||||
|
||||
/**
|
||||
* Parses a list of server names and writes the results in an array of strings
|
||||
* with one server in each. The output array and its elements should be deallocated
|
||||
* by the caller. The server names are not checked to be valid servers.
|
||||
*
|
||||
* @param servers A list of server names.
|
||||
* @param output_array Save location for the array of server names. Set to null if none found.
|
||||
* @return How many servers were found and set into the array.
|
||||
*/
|
||||
static int config_parse_server_list(const char *servers, char ***output_array);
|
||||
|
||||
MXS_END_DECLS
|
||||
|
@ -296,39 +296,45 @@ SERVER * server_find_by_unique_name(const char *name)
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Find several servers with the names specified in an array with a given size.
|
||||
* The returned array (but not the elements) should be freed by the caller, and
|
||||
* is null-terminated.
|
||||
* The returned array (but not the elements) should be freed by the caller.
|
||||
* If no valid server names were found or in case of error, nothing is written
|
||||
* to the output parameter.
|
||||
*
|
||||
* @param servers An array of server names
|
||||
* @param size number of elements in the server names array
|
||||
* @return A null-terminated array of SERVERs. May contain less elements than
|
||||
* requested if some server names are not found.
|
||||
* @param size Number of elements in the input server names array, equal to output
|
||||
* size if any servers are found.
|
||||
* @param output Where to save the output. Contains null elements for invalid server
|
||||
* names. If all were invalid, the output is left untouched.
|
||||
* @return Number of valid server names found
|
||||
*/
|
||||
SERVER** server_find_by_unique_names(char **server_names, int size)
|
||||
int server_find_by_unique_names(char **server_names, int size, SERVER*** output)
|
||||
{
|
||||
ss_dassert(server_names);
|
||||
ss_dassert(server_names && (size > 0));
|
||||
|
||||
SERVER **results = MXS_CALLOC(size + 1, sizeof(SERVER*)); // +1 for null
|
||||
SERVER **results = MXS_CALLOC(size, sizeof(SERVER*));
|
||||
if (!results)
|
||||
{
|
||||
return NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int res_ind = 0;
|
||||
for (int i = 0; server_names[i] != NULL; i++)
|
||||
int found = 0;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
SERVER *serv = server_find_by_unique_name(server_names[i]);
|
||||
if (serv)
|
||||
{
|
||||
results[res_ind] = serv;
|
||||
res_ind++;
|
||||
}
|
||||
results[i] = server_find_by_unique_name(server_names[i]);
|
||||
found += (results[i]) ? 1 : 0;
|
||||
}
|
||||
return results;
|
||||
|
||||
if (found)
|
||||
{
|
||||
*output = results;
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_FREE(results);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user