]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools/modprobe: factor out module_new_from_any()
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 19 May 2025 19:54:33 +0000 (20:54 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 13 Jun 2025 18:52:41 +0000 (13:52 -0500)
Split the functionality into a helper function, which will be used with
later commits.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/353
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/modprobe.c

index fe5db66d348191de09ea4efe87e110248dcb5dc6..54a71d6a524247096903e9ea4380c441a836825c 100644 (file)
@@ -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;