diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:18:46 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 15:18:46 +0000 |
commit | 56294d30a82ec2da6f9ce399740c1ef65a9ddef4 (patch) | |
tree | bbe3823e41495d026ba8edc6eeaef166edb7e2a2 /plugins/modalias | |
parent | Initial commit. (diff) | |
download | gnome-software-56294d30a82ec2da6f9ce399740c1ef65a9ddef4.tar.xz gnome-software-56294d30a82ec2da6f9ce399740c1ef65a9ddef4.zip |
Adding upstream version 3.38.1.upstream/3.38.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'plugins/modalias')
-rw-r--r-- | plugins/modalias/gs-plugin-modalias.c | 151 | ||||
-rw-r--r-- | plugins/modalias/gs-self-test.c | 116 | ||||
-rw-r--r-- | plugins/modalias/meson.build | 42 |
3 files changed, 309 insertions, 0 deletions
diff --git a/plugins/modalias/gs-plugin-modalias.c b/plugins/modalias/gs-plugin-modalias.c new file mode 100644 index 0000000..d4e27a2 --- /dev/null +++ b/plugins/modalias/gs-plugin-modalias.c @@ -0,0 +1,151 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2016 Richard Hughes <richard@hughsie.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <fnmatch.h> +#include <gudev/gudev.h> + +#include <gnome-software.h> + +struct GsPluginData { + GUdevClient *client; + GPtrArray *devices; +}; + +static void +gs_plugin_modalias_uevent_cb (GUdevClient *client, + const gchar *action, + GUdevDevice *device, + GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + if (g_strcmp0 (action, "add") == 0 || + g_strcmp0 (action, "remove") == 0) { + g_debug ("invalidating devices as '%s' sent action '%s'", + g_udev_device_get_sysfs_path (device), + action); + g_ptr_array_set_size (priv->devices, 0); + } +} + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_BEFORE, "icons"); + priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + priv->client = g_udev_client_new (NULL); + g_signal_connect (priv->client, "uevent", + G_CALLBACK (gs_plugin_modalias_uevent_cb), plugin); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_object_unref (priv->client); + g_ptr_array_unref (priv->devices); +} + +static void +gs_plugin_modalias_ensure_devices (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GList) list = NULL; + + /* already set */ + if (priv->devices->len > 0) + return; + + /* get the devices, and assume ownership of each */ + list = g_udev_client_query_by_subsystem (priv->client, NULL); + for (GList *l = list; l != NULL; l = l->next) { + GUdevDevice *device = G_UDEV_DEVICE (l->data); + if (g_udev_device_get_sysfs_attr (device, "modalias") == NULL) { + g_object_unref (device); + continue; + } + g_ptr_array_add (priv->devices, device); + } + g_debug ("%u devices with modalias", priv->devices->len); +} + +static gboolean +gs_plugin_modalias_matches (GsPlugin *plugin, const gchar *modalias) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + + gs_plugin_modalias_ensure_devices (plugin); + for (guint i = 0; i < priv->devices->len; i++) { + GUdevDevice *device = g_ptr_array_index (priv->devices, i); + const gchar *modalias_tmp; + + /* get the (optional) device modalias */ + modalias_tmp = g_udev_device_get_sysfs_attr (device, "modalias"); + if (modalias_tmp == NULL) + continue; + if (fnmatch (modalias, modalias_tmp, 0) == 0) { + g_debug ("matched %s against %s", modalias_tmp, modalias); + return TRUE; + } + } + return FALSE; +} + +static gboolean +refine_app (GsPlugin *plugin, + GsApp *app, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + GPtrArray *provides; + guint i; + + /* not required */ + if (gs_app_get_icons(app)->len > 0) + return TRUE; + if (gs_app_get_kind (app) != AS_APP_KIND_DRIVER) + return TRUE; + + /* do any of the modaliases match any installed hardware */ + provides = gs_app_get_provides (app); + for (i = 0 ; i < provides->len; i++) { + AsProvide *prov = g_ptr_array_index (provides, i); + if (as_provide_get_kind (prov) != AS_PROVIDE_KIND_MODALIAS) + continue; + if (gs_plugin_modalias_matches (plugin, as_provide_get_value (prov))) { + g_autoptr(AsIcon) ic = NULL; + ic = as_icon_new (); + as_icon_set_kind (ic, AS_ICON_KIND_STOCK); + as_icon_set_name (ic, "emblem-system-symbolic"); + gs_app_add_icon (app, ic); + gs_app_add_quirk (app, GS_APP_QUIRK_NOT_LAUNCHABLE); + break; + } + } + return TRUE; +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + if (!refine_app (plugin, app, flags, cancellable, error)) + return FALSE; + } + + return TRUE; +} diff --git a/plugins/modalias/gs-self-test.c b/plugins/modalias/gs-self-test.c new file mode 100644 index 0000000..5cbb39c --- /dev/null +++ b/plugins/modalias/gs-self-test.c @@ -0,0 +1,116 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * vi:set noexpandtab tabstop=8 shiftwidth=8: + * + * Copyright (C) 2013-2017 Richard Hughes <richard@hughsie.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include "config.h" + +#include "gnome-software-private.h" + +#include "gs-test.h" + +static void +gs_plugins_modalias_func (GsPluginLoader *plugin_loader) +{ + GsApp *app; + g_autoptr(GError) error = NULL; + g_autoptr(GsAppList) list = NULL; + g_autoptr(GsPluginJob) plugin_job = NULL; + + /* get search result based on addon keyword */ + plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_SEARCH, + "search", "colorhug2", + "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_CATEGORIES, + NULL); + list = gs_plugin_loader_job_process (plugin_loader, plugin_job, NULL, &error); + gs_test_flush_main_context (); + g_assert_no_error (error); + g_assert (list != NULL); + + /* make sure there is one entry, the parent app */ + g_assert_cmpint (gs_app_list_length (list), ==, 1); + app = gs_app_list_index (list, 0); + g_assert_cmpstr (gs_app_get_id (app), ==, "com.hughski.ColorHug2.driver"); + g_assert_cmpint (gs_app_get_kind (app), ==, AS_APP_KIND_DRIVER); + g_assert (gs_app_has_category (app, "Addon")); + g_assert (gs_app_has_category (app, "Driver")); +} + +int +main (int argc, char **argv) +{ + g_autofree gchar *tmp_root = NULL; + gboolean ret; + int retval; + g_autofree gchar *xml = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GsPluginLoader) plugin_loader = NULL; + const gchar *allowlist[] = { + "appstream", + "dummy", + "modalias", + NULL + }; + + g_test_init (&argc, &argv, +#if GLIB_CHECK_VERSION(2, 60, 0) + G_TEST_OPTION_ISOLATE_DIRS, +#endif + NULL); + g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); + g_setenv ("GS_SELF_TEST_DUMMY_ENABLE", "1", TRUE); + + xml = g_strdup_printf ("<?xml version=\"1.0\"?>\n" + "<components version=\"0.9\">\n" + " <component type=\"driver\">\n" + " <id>com.hughski.ColorHug2.driver</id>\n" + " <name>ColorHug2</name>\n" + " <summary>ColorHug2 Colorimeter Driver</summary>\n" + " <pkgname>colorhug-client</pkgname>\n" + " <provides>\n" + " <modalias>pci:*</modalias>\n" + " </provides>\n" + " </component>\n" + " <info>\n" + " <scope>system</scope>\n" + " </info>\n" + "</components>\n"); + g_setenv ("GS_SELF_TEST_APPSTREAM_XML", xml, TRUE); + + /* Use a common cache directory for all tests, since the appstream + * plugin uses it and cannot be reinitialised for each test. */ + tmp_root = g_dir_make_tmp ("gnome-software-modalias-test-XXXXXX", NULL); + g_assert (tmp_root != NULL); + g_setenv ("GS_SELF_TEST_CACHEDIR", tmp_root, TRUE); + + /* only critical and error are fatal */ + g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); + + /* we can only load this once per process */ + plugin_loader = gs_plugin_loader_new (); + gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR); + gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR_CORE); + gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR_DUMMY); + ret = gs_plugin_loader_setup (plugin_loader, + (gchar**) allowlist, + NULL, + NULL, + &error); + g_assert_no_error (error); + g_assert (ret); + + /* plugin tests go here */ + g_test_add_data_func ("/gnome-software/plugins/modalias", + plugin_loader, + (GTestDataFunc) gs_plugins_modalias_func); + + retval = g_test_run (); + + /* Clean up. */ + gs_utils_rmtree (tmp_root, NULL); + + return retval; +} diff --git a/plugins/modalias/meson.build b/plugins/modalias/meson.build new file mode 100644 index 0000000..22cac6d --- /dev/null +++ b/plugins/modalias/meson.build @@ -0,0 +1,42 @@ +cargs = ['-DG_LOG_DOMAIN="GsPluginModalias"'] +cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"'] +cargs += ['-DLOCALPLUGINDIR_CORE="' + meson.current_build_dir() + '/../core"'] +cargs += ['-DLOCALPLUGINDIR_DUMMY="' + meson.current_build_dir() + '/../dummy"'] + +shared_module( + 'gs_plugin_modalias', +sources : 'gs-plugin-modalias.c', + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : [ plugin_libs, gudev ], + link_with : [ + libgnomesoftware + ] +) + +if get_option('tests') + e = executable( + 'gs-self-test-modalias', + compiled_schemas, + sources : [ + 'gs-self-test.c' + ], + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + dependencies : [ + plugin_libs, + ], + link_with : [ + libgnomesoftware + ], + c_args : cargs, + ) + test('gs-self-test-modalias', e, suite: ['plugins', 'modalias'], env: test_env) +endif |