Add lookup to create modules list from alias

We return a kmod_list when searching for an alias. Right now, it only
search for aliases in config files.

To use it, we create a list:
	list = NULL;
	kmod_module_new_from_lookup(..., &list);

And iterate over it to get the modules and their details:

	kmod_list_foreach(l, list) {
		struct kmod_mod *mod = kmod_module_get_module(l);
		...
		... kmod_module_get_name(mod);
		... kmod_module_get_path(mod);
	}

Aliases might contain globs and are match by using fnmatch().
This commit is contained in:
Lucas De Marchi 2011-11-30 19:03:41 -02:00
parent 6e869df73d
commit 7f3eb0cced
5 changed files with 72 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#include "libkmod.h"
#include "libkmod-private.h"
//#include "libkmod-index.h"
/**
* kmod_module:
@ -152,6 +153,40 @@ KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
return mod;
}
KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
const char *alias,
struct kmod_list **list)
{
int err;
if (ctx == NULL || alias == NULL)
return -ENOENT;
if (list == NULL || *list != NULL) {
ERR(ctx, "An empty list is needed to create lookup\n");
return -ENOSYS;
}
err = kmod_lookup_alias_from_config(ctx, alias, list);
if (err < 0) {
kmod_module_unref_list(*list);
*list = NULL;
}
return err;
}
KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
{
for (; list != NULL; list = kmod_list_remove(list))
kmod_module_unref(list->data);
return 0;
}
KMOD_EXPORT struct kmod_module *kmod_module_get_module(struct kmod_list *l)
{
struct kmod_module *mod = l->data;

View File

@ -55,6 +55,7 @@ struct kmod_list *kmod_list_remove_data(struct kmod_list *list,
/* libkmod.c */
const char *kmod_get_dirname(struct kmod_ctx *ctx) __attribute__((nonnull(1)));
int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, struct kmod_list **list);
/* libkmod-config.c */
struct kmod_config {

View File

@ -23,6 +23,7 @@
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <fnmatch.h>
#include <string.h>
#include <ctype.h>
#include <sys/utsname.h>
@ -258,3 +259,30 @@ KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
{
ctx->log_priority = priority;
}
int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
struct kmod_list **list)
{
struct kmod_config *config = &ctx->config;
struct kmod_list *l;
int err;
kmod_list_foreach(l, config->aliases) {
const char *aliasname = kmod_alias_get_name(l);
const char *modname = kmod_alias_get_modname(l);
if (fnmatch(aliasname, name, 0) == 0) {
struct kmod_module *mod;
err = kmod_module_new_from_name(ctx, modname, &mod);
if (err < 0) {
ERR(ctx, "%s", strerror(-err));
return err;
}
*list = kmod_list_append(*list, mod);
}
}
return 0;
}

View File

@ -100,9 +100,13 @@ int kmod_module_new_from_name(struct kmod_ctx *ctx, const char *name,
struct kmod_module **mod);
int kmod_module_new_from_path(struct kmod_ctx *ctx, const char *path,
struct kmod_module **mod);
int kmod_module_new_from_lookup(struct kmod_ctx *ctx, const char *alias,
struct kmod_list **list);
struct kmod_module *kmod_module_ref(struct kmod_module *mod);
struct kmod_module *kmod_module_unref(struct kmod_module *mod);
int kmod_module_unref_list(struct kmod_list *list);
struct kmod_module *kmod_module_get_module(struct kmod_list *l);
int kmod_module_remove_module(struct kmod_module *mod, unsigned int flags);
int kmod_module_insert_module(struct kmod_module *mod, unsigned int flags);

View File

@ -19,11 +19,15 @@ global:
kmod_module_new_from_name;
kmod_module_new_from_path;
kmod_module_new_from_lookup;
kmod_module_ref;
kmod_module_unref;
kmod_module_unref_list;
kmod_module_remove_module;
kmod_module_insert_module;
kmod_module_get_module;
kmod_module_get_name;
kmod_module_get_path;
local: