Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Directly register the hooks from inside the plugin, no need thrid party interface
Baptiste Daroussin committed 13 years ago
commit bdb66d53f5c11542947e7aad69dbc471cc591c72
parent ef5c44f63a4761cdd9aaffcb433901a631d92db5
3 files changed +8 -36
modified libpkg/pkg.h
@@ -918,8 +918,8 @@ void *pkg_plugin_func(struct pkg_plugin *p, const char *func);
 * @todo: Document
 */
typedef int(*pkg_plugin_callback)(void *data, struct pkgdb *db);
-
int pkg_plugins_hook(const char *pluginname, pkg_plugin_hook_t hook, pkg_plugin_callback callback);
int pkg_plugins_hook_run(pkg_plugin_hook_t hook, void *data, struct pkgdb *db);
+
int pkg_plugin_hook_register(struct pkg_plugin *p, pkg_plugin_hook_t hook, pkg_plugin_callback callback);

/**
 * Get the value of a configuration key
modified libpkg/plugins.c
@@ -46,7 +46,7 @@

struct plugin_hook {
	pkg_plugin_hook_t hook;				/* plugin hook type */
-
	pkg_plugin_callback callback;                          /* plugin callback function */
+
	pkg_plugin_callback callback;			/* plugin callback function */
	STAILQ_ENTRY(plugin_hook) next;
};

@@ -61,7 +61,6 @@ static STAILQ_HEAD(, pkg_plugin) ph = STAILQ_HEAD_INITIALIZER(ph);

static int pkg_plugin_free(void);
static int pkg_plugin_hook_free(struct pkg_plugin *p);
-
static int pkg_plugin_hook_register(struct pkg_plugin *p, pkg_plugin_hook_t hook, pkg_plugin_callback callback);
static int pkg_plugin_hook_exec(struct pkg_plugin *p, pkg_plugin_hook_t hook, void *data, struct pkgdb *db);
static int pkg_plugin_hook_list(struct pkg_plugin *p, struct plugin_hook **h);

@@ -107,7 +106,7 @@ pkg_plugin_free(void)
	return (EPKG_OK);
}

-
static int
+
int
pkg_plugin_hook_register(struct pkg_plugin *p, pkg_plugin_hook_t hook, pkg_plugin_callback callback)
{
	struct plugin_hook *new = NULL;
@@ -162,34 +161,6 @@ pkg_plugin_hook_list(struct pkg_plugin *p, struct plugin_hook **h)
}

int
-
pkg_plugins_hook(const char *pluginname, pkg_plugin_hook_t hook, pkg_plugin_callback callback)
-
{
-
	struct pkg_plugin *p = NULL;
-
	const char *pname = NULL;
-
	bool plugin_found = false;
-
	
-
	assert(pluginname != NULL);
-
	assert(callback != NULL);
-

-
	/* locate the plugin */
-
	while (pkg_plugins(&p) != EPKG_END) {
-
		pname = pkg_plugin_get(p, PKG_PLUGIN_NAME);
-
		if ((strcmp(pname, pluginname)) == 0) {
-
			pkg_plugin_hook_register(p, hook, callback);
-
			plugin_found = true;
-
		}
-
	}
-

-
	if (plugin_found == false) {
-
		pkg_emit_error("Plugin name '%s' was not found in the registry, cannot hook",
-
			       pluginname);
-
		return (EPKG_FATAL);
-
	}
-

-
	return (EPKG_OK);
-
}
-

-
int
pkg_plugins_hook_run(pkg_plugin_hook_t hook, void *data, struct pkgdb *db)
{
	struct pkg_plugin *p = NULL;
@@ -253,6 +224,7 @@ pkg_plugins_init(void)
		snprintf(pluginfile, MAXPATHLEN, "%s/%s.so", plugdir,
		    pkg_config_value(v));
		p = calloc(1, sizeof(struct pkg_plugin));
+
		STAILQ_INIT(&p->phooks);
		if ((p->lh = dlopen(pluginfile, RTLD_LAZY)) == NULL) {
			pkg_emit_error("Loading of plugin '%s' failed: %s",
			    pkg_config_value(v), dlerror());
modified plugins/stats/stats.c
@@ -54,21 +54,21 @@ init(struct pkg_plugin *p)
	pkg_plugin_set(p, PKG_PLUGIN_DESC, description);
	pkg_plugin_set(p, PKG_PLUGIN_VERSION, version);

-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGIN_HOOK_PRE_INSTALL, &plugin_stats_callback) != EPKG_OK) {
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_INSTALL, &plugin_stats_callback) != EPKG_OK) {
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
		return (EPKG_FATAL);
	}

-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGIN_HOOK_POST_INSTALL, &plugin_stats_callback) != EPKG_OK) {
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_POST_INSTALL, &plugin_stats_callback) != EPKG_OK) {
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
		return (EPKG_FATAL);
	}
-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGIN_HOOK_PRE_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
		return (EPKG_FATAL);
	}
	
-
	if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGIN_HOOK_POST_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
+
	if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_POST_DEINSTALL, &plugin_stats_callback) != EPKG_OK) {
		fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME);
		return (EPKG_FATAL);
	}