]> sourceware.org Git - lvm2.git/commitdiff
lvmcmdline: runtime function resolving
authorZdenek Kabelac <zkabelac@redhat.com>
Sun, 12 May 2024 14:46:02 +0000 (16:46 +0200)
committerZdenek Kabelac <zkabelac@redhat.com>
Mon, 13 May 2024 00:15:55 +0000 (02:15 +0200)
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.

tools/command.h
tools/lvmcmdline.c

index 3e6e0b47bfba914a6a56c65d9ac8c17ee2e5bc2d..738c65e69540a92b76e687111fc0b8571a9c5a50 100644 (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 */
 
index dfcef5912e26cd032e445fe9108fccd4221bf5d9..1ade744b2ef0c1f409074c91911c2a6981cdf390 100644 (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);
This page took 0.046163 seconds and 5 git commands to generate.