From 2b43acf9fd5d2062f3f4154f0c7cf95819e976ea Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 6 Apr 2017 16:19:57 -0400 Subject: [PATCH] Add bt_plugin_find_component_class() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch also renames bt_plugin_create_from_name() to bt_plugin_find() which seems more intuitive. bt_plugin_find_component_class() simply calls bt_plugin_find() and then gets one of its component classes. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/plugin/plugin.h | 6 +++- lib/plugin/plugin.c | 26 +++++++++++++++- tests/lib/test_plugin.c | 49 ++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/include/babeltrace/plugin/plugin.h b/include/babeltrace/plugin/plugin.h index 1d1725408..31baf5766 100644 --- a/include/babeltrace/plugin/plugin.h +++ b/include/babeltrace/plugin/plugin.h @@ -51,7 +51,11 @@ enum bt_plugin_status { BT_PLUGIN_STATUS_NOMEM = -4, }; -extern struct bt_plugin *bt_plugin_create_from_name(const char *plugin_name); +extern struct bt_plugin *bt_plugin_find(const char *plugin_name); + +extern struct bt_component_class *bt_plugin_find_component_class( + const char *plugin_name, const char *component_class_name, + enum bt_component_class_type component_class_type); extern struct bt_plugin **bt_plugin_create_all_from_file(const char *path); diff --git a/lib/plugin/plugin.c b/lib/plugin/plugin.c index 90f77cb5a..0651ae72f 100644 --- a/lib/plugin/plugin.c +++ b/lib/plugin/plugin.c @@ -131,7 +131,7 @@ void free_plugins(struct bt_plugin **plugins) { } } -struct bt_plugin *bt_plugin_create_from_name(const char *plugin_name) +struct bt_plugin *bt_plugin_find(const char *plugin_name) { const char *system_plugin_dir; char *home_plugin_dir = NULL; @@ -246,6 +246,30 @@ end: return plugin; } +struct bt_component_class *bt_plugin_find_component_class( + const char *plugin_name, const char *comp_cls_name, + enum bt_component_class_type comp_cls_type) +{ + struct bt_plugin *plugin = NULL; + struct bt_component_class *comp_cls = NULL; + + if (!plugin_name || !comp_cls_name) { + goto end; + } + + plugin = bt_plugin_find(plugin_name); + if (!plugin) { + goto end; + } + + comp_cls = bt_plugin_get_component_class_by_name_and_type( + plugin, comp_cls_name, comp_cls_type); + +end: + bt_put(plugin); + return comp_cls; +} + /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ static struct dirent *alloc_dirent(const char *path) diff --git a/tests/lib/test_plugin.c b/tests/lib/test_plugin.c index 1b576104c..fdc16c5bb 100644 --- a/tests/lib/test_plugin.c +++ b/tests/lib/test_plugin.c @@ -32,7 +32,7 @@ #include "tap/tap.h" #include "common.h" -#define NR_TESTS 51 +#define NR_TESTS 58 #define NON_EXISTING_PATH "/this/hopefully/does/not/exist/5bc75f8d-0dba-4043-a509-d7984b97e42b.so" /* Those symbols are written to by some test plugins */ @@ -217,15 +217,15 @@ static void test_sfs(const char *plugin_dir) diag("> putting the plugin object here"); BT_PUT(plugin); - sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null); + sink_component = bt_component_create(sink_comp_class, NULL, NULL); ok(sink_component, "bt_component_create() still works after the plugin object is destroyed"); BT_PUT(sink_component); BT_PUT(source_comp_class); - sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null); + sink_component = bt_component_create(sink_comp_class, NULL, NULL); ok(sink_component, "bt_component_create() still works after the source component class object is destroyed"); BT_PUT(sink_component); BT_PUT(filter_comp_class); - sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null); + sink_component = bt_component_create(sink_comp_class, NULL, NULL); ok(sink_component, "bt_component_create() still works after the filter component class object is destroyed"); BT_PUT(sink_comp_class); BT_PUT(sink_component); @@ -265,26 +265,49 @@ static void test_create_all_from_dir(const char *plugin_dir) free(plugins); } -static void test_create_from_name(const char *plugin_dir) +static void test_find(const char *plugin_dir) { struct bt_plugin *plugin; + struct bt_component_class *comp_cls_sink; + struct bt_component_class *comp_cls_source; char *plugin_path; - ok(!bt_plugin_create_from_name(NULL), - "bt_plugin_create_from_name() handles NULL"); - ok(!bt_plugin_create_from_name(NON_EXISTING_PATH), - "bt_plugin_create_from_name() returns NULL with an unknown plugin name"); + ok(!bt_plugin_find(NULL), + "bt_plugin_find() handles NULL"); + ok(!bt_plugin_find(NON_EXISTING_PATH), + "bt_plugin_find() returns NULL with an unknown plugin name"); plugin_path = malloc(PATH_MAX * 5); assert(plugin_path); sprintf(plugin_path, "%s:/ec1d09e5-696c-442e-b1c3-f9c6cf7f5958:::%s:8db46494-a398-466a-9649-c765ae077629:", NON_EXISTING_PATH, plugin_dir); setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1); - plugin = bt_plugin_create_from_name("test_minimal"); + plugin = bt_plugin_find("test_minimal"); ok(plugin, - "bt_plugin_create_from_name() succeeds with a plugin name it can find"); + "bt_plugin_find() succeeds with a plugin name it can find"); ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0, - "bt_plugin_create_from_name() finds the correct plugin for a given name"); + "bt_plugin_find() finds the correct plugin for a given name"); BT_PUT(plugin); + comp_cls_sink = bt_plugin_find_component_class(NULL, "sink", + BT_COMPONENT_CLASS_TYPE_SINK); + ok(!comp_cls_sink, "bt_plugin_find_component_class() handles NULL (plugin name)"); + comp_cls_sink = bt_plugin_find_component_class("test_sfs", NULL, + BT_COMPONENT_CLASS_TYPE_SINK); + ok(!comp_cls_sink, "bt_plugin_find_component_class() handles NULL (component class name)"); + comp_cls_sink = bt_plugin_find_component_class("test_sfs", "sink2", + BT_COMPONENT_CLASS_TYPE_SINK); + ok(!comp_cls_sink, "bt_plugin_find_component_class() fails with an unknown component class name"); + comp_cls_sink = bt_plugin_find_component_class("test_sfs", "sink", + BT_COMPONENT_CLASS_TYPE_SINK); + ok(comp_cls_sink, "bt_plugin_find_component_class() succeeds with valid parameters"); + ok(strcmp(bt_component_class_get_name(comp_cls_sink), "sink") == 0, + "bt_plugin_find_component_class() returns the appropriate component class (sink)"); + comp_cls_source = bt_plugin_find_component_class("test_sfs", "source", + BT_COMPONENT_CLASS_TYPE_SOURCE); + ok(comp_cls_sink, "bt_plugin_find_component_class() succeeds with another component class name (same plugin)"); + ok(strcmp(bt_component_class_get_name(comp_cls_source), "source") == 0, + "bt_plugin_find_component_class() returns the appropriate component class (source)"); + BT_PUT(comp_cls_sink); + BT_PUT(comp_cls_source); free(plugin_path); } @@ -305,7 +328,7 @@ int main(int argc, char **argv) test_minimal(plugin_dir); test_sfs(plugin_dir); test_create_all_from_dir(plugin_dir); - test_create_from_name(plugin_dir); + test_find(plugin_dir); ret = exit_status(); end: return ret; -- 2.34.1