1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

lvmcmdline: runtime function resolving

Instead of resolving and storing 'command_fn'
withing 'struct command' use just funtion enum
and resolve function pointer just in place,
where it is really needed - first try to resolve
'new style' and fallback to 'old style' named.
This commit is contained in:
Zdenek Kabelac 2024-05-12 16:46:02 +02:00
parent bebbb1e66a
commit 1a219c69ee
2 changed files with 11 additions and 31 deletions

View File

@ -22,12 +22,9 @@ struct logical_volume;
/* old per-command-name function */
typedef int (*command_fn) (struct cmd_context *cmd, int argc, char **argv);
/* new per-command-line-id functions */
typedef int (*command_id_fn) (struct cmd_context *cmd, int argc, char **argv);
struct command_function {
int command_enum;
command_id_fn fn;
command_fn fn; /* new style */
};
struct command_name {
@ -188,11 +185,8 @@ struct command {
uint16_t command_enum; /* <command_id>_CMD */
uint16_t command_index; /* position in commands[] */
const struct command_function *functions; /* new style */
command_fn fn; /* old style */
unsigned int cmd_flags; /* CMD_FLAG_ */
uint16_t lvm_command_enum; /* position in commands[] */
uint16_t lvm_command_enum; /* position in command_names[] */
uint16_t cmd_flags; /* CMD_FLAG_ */
/* definitions of opt/pos args */

View File

@ -1301,7 +1301,7 @@ static void _set_valid_args_for_command_name(int ci)
command_names_args[ci].num_args = num_args;
}
static const struct command_function *_find_command_id_function(int command_enum)
static command_fn _find_command_id_function(int command_enum)
{
int i;
@ -1310,7 +1310,7 @@ static const struct command_function *_find_command_id_function(int command_enum
for (i = 0; i < CMD_COUNT; i++) {
if (_command_functions[i].command_enum == command_enum)
return &_command_functions[i];
return _command_functions[i].fn;
}
return NULL;
}
@ -1335,8 +1335,6 @@ static int _command_name_compare(const void *on1, const void *on2)
int lvm_register_commands(struct cmd_context *cmd, const char *run_name)
{
int i;
const char *last_name = NULL;
const struct command_name *cname = NULL;
/* already initialized */
if (_cmdline.commands)
@ -1357,19 +1355,6 @@ int lvm_register_commands(struct cmd_context *cmd, const char *run_name)
for (i = 0; i < COMMAND_COUNT; i++) {
commands_idx[i] = &commands[i];
commands[i].command_index = i;
/* new style */
commands[i].functions = _find_command_id_function(commands[i].command_enum);
/* old style */
if (!commands[i].functions) {
if (!last_name || strcmp(last_name, commands[i].name)) {
last_name = commands[i].name;
cname = find_command_name(last_name);
}
if (cname)
commands[i].fn = cname->fn;
}
}
/* Sort all commands by its name for quick binary search */
@ -3053,6 +3038,7 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
int skip_hyphens;
int refresh_done = 0;
int io;
command_fn fn;
/* Avoid excessive access to /etc/localtime and set TZ variable for glibc
* so it does not need to check /etc/localtime everytime that needs that info */
@ -3281,12 +3267,12 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
goto_out;
}
if (cmd->command->functions)
/* A command-line-specific function is used */
ret = cmd->command->functions->fn(cmd, argc, argv);
else
/* A command-line-specific function is used */
if (!(fn = _find_command_id_function(cmd->command->command_enum)))
/* The old style command-name function is used */
ret = cmd->command->fn(cmd, argc, argv);
fn = command_names[cmd->command->lvm_command_enum].fn;
ret = fn(cmd, argc, argv);
lvmlockd_disconnect();
fin_locking(cmd);