From: Emil Velikov Date: Mon, 19 May 2025 19:54:33 +0000 (+0100) Subject: tools/modprobe: factor out module_new_from_any() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5753d25231ad061e6719b7eea3fc96e994c56566;p=thirdparty%2Fkmod.git tools/modprobe: factor out module_new_from_any() Split the functionality into a helper function, which will be used with later commits. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/353 Signed-off-by: Lucas De Marchi --- diff --git a/tools/modprobe.c b/tools/modprobe.c index fe5db66d..54a71d6a 100644 --- a/tools/modprobe.c +++ b/tools/modprobe.c @@ -200,6 +200,27 @@ static int show_config(struct kmod_ctx *ctx) return 0; } +static int module_new_from_any(struct kmod_ctx *ctx, const char *module, + struct kmod_module **mod, struct kmod_list **list) +{ + if (strstartswith(module, "/") || strstartswith(module, "./")) { + int err = kmod_module_new_from_path(ctx, module, mod); + if (err < 0) { + LOG("Failed to get module from path %s: %s\n", module, + strerror(-err)); + return -ENOENT; + } + } else { + int err = kmod_module_new_from_lookup(ctx, module, list); + if (*list == NULL || err < 0) { + LOG("Module %s not found in directory %s\n", module, + ctx ? kmod_get_dirname(ctx) : "(missing)"); + return -ENOENT; + } + } + return 0; +} + static int show_modversions(struct kmod_ctx *ctx, const char *filename) { struct kmod_list *l, *list = NULL; @@ -592,21 +613,9 @@ static int insmod(struct kmod_ctx *ctx, const char *alias, const char *extra_opt struct kmod_module *mod = NULL; int err, flags = 0; - if (strstartswith(alias, "/") || strstartswith(alias, "./")) { - err = kmod_module_new_from_path(ctx, alias, &mod); - if (err < 0) { - LOG("Failed to get module from path %s: %s\n", alias, - strerror(-err)); - return -ENOENT; - } - } else { - err = kmod_module_new_from_lookup(ctx, alias, &list); - if (list == NULL || err < 0) { - LOG("Module %s not found in directory %s\n", alias, - ctx ? kmod_get_dirname(ctx) : "(missing)"); - return -ENOENT; - } - } + err = module_new_from_any(ctx, alias, &mod, &list); + if (err < 0) + return err; if (strip_modversion || force) flags |= KMOD_PROBE_FORCE_MODVERSION;