diff options
Diffstat (limited to 'plugins/repos')
-rw-r--r-- | plugins/repos/gs-plugin-repos.c | 247 | ||||
-rw-r--r-- | plugins/repos/gs-self-test.c | 82 | ||||
-rw-r--r-- | plugins/repos/meson.build | 41 | ||||
-rw-r--r-- | plugins/repos/tests/yum.repos.d/utopia.repo | 5 |
4 files changed, 375 insertions, 0 deletions
diff --git a/plugins/repos/gs-plugin-repos.c b/plugins/repos/gs-plugin-repos.c new file mode 100644 index 0000000..a35fc80 --- /dev/null +++ b/plugins/repos/gs-plugin-repos.c @@ -0,0 +1,247 @@ +/* -*- 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> + * Copyright (C) 2017-2018 Kalev Lember <klember@redhat.com> + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include <config.h> + +#include <gnome-software.h> + +struct GsPluginData { + GHashTable *fns; /* origin : filename */ + GHashTable *urls; /* origin : url */ + GFileMonitor *monitor; + GMutex mutex; + gchar *reposdir; + gboolean valid; +}; + +void +gs_plugin_initialize (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData)); + + g_mutex_init (&priv->mutex); + + /* for debugging and the self tests */ + priv->reposdir = g_strdup (g_getenv ("GS_SELF_TEST_REPOS_DIR")); + if (priv->reposdir == NULL) + priv->reposdir = g_strdup ("/etc/yum.repos.d"); + + /* plugin only makes sense if this exists at startup */ + if (!g_file_test (priv->reposdir, G_FILE_TEST_EXISTS)) { + gs_plugin_set_enabled (plugin, FALSE); + return; + } + + /* we also watch this for changes */ + priv->fns = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + priv->urls = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + /* need application IDs */ + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "packagekit-refine"); + gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "rpm-ostree"); +} + +void +gs_plugin_destroy (GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_free (priv->reposdir); + if (priv->fns != NULL) + g_hash_table_unref (priv->fns); + if (priv->urls != NULL) + g_hash_table_unref (priv->urls); + if (priv->monitor != NULL) + g_object_unref (priv->monitor); + g_mutex_clear (&priv->mutex); +} + +/* mutex must be held */ +static gboolean +gs_plugin_repos_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GDir) dir = NULL; + const gchar *fn; + + /* already valid */ + if (priv->valid) + return TRUE; + + /* clear existing */ + g_hash_table_remove_all (priv->fns); + g_hash_table_remove_all (priv->urls); + + /* search all files */ + dir = g_dir_open (priv->reposdir, 0, error); + if (dir == NULL) { + gs_utils_error_convert_gio (error); + return FALSE; + } + while ((fn = g_dir_read_name (dir)) != NULL) { + g_autofree gchar *filename = NULL; + g_auto(GStrv) groups = NULL; + g_autoptr(GKeyFile) kf = g_key_file_new (); + guint i; + + /* not a repo */ + if (!g_str_has_suffix (fn, ".repo")) + continue; + + /* load file */ + filename = g_build_filename (priv->reposdir, fn, NULL); + if (!g_key_file_load_from_file (kf, filename, + G_KEY_FILE_NONE, + error)) { + gs_utils_error_convert_gio (error); + return FALSE; + } + + /* we can have multiple repos in one file */ + groups = g_key_file_get_groups (kf, NULL); + for (i = 0; groups[i] != NULL; i++) { + g_autofree gchar *tmp = NULL; + + g_hash_table_insert (priv->fns, + g_strdup (groups[i]), + g_strdup (filename)); + + tmp = g_key_file_get_string (kf, groups[i], "baseurl", NULL); + if (tmp != NULL) { + g_hash_table_insert (priv->urls, + g_strdup (groups[i]), + g_strdup (tmp)); + continue; + } + + tmp = g_key_file_get_string (kf, groups[i], "metalink", NULL); + if (tmp != NULL) { + g_hash_table_insert (priv->urls, + g_strdup (groups[i]), + g_strdup (tmp)); + continue; + } + } + } + + /* success */ + priv->valid = TRUE; + return TRUE; +} + +static void +gs_plugin_repos_changed_cb (GFileMonitor *monitor, + GFile *file, + GFile *other_file, + GFileMonitorEvent event_type, + GsPlugin *plugin) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + priv->valid = FALSE; +} + +gboolean +gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GFile) file = g_file_new_for_path (priv->reposdir); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex); + + /* watch for changes */ + priv->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, cancellable, error); + if (priv->monitor == NULL) { + gs_utils_error_convert_gio (error); + return FALSE; + } + g_signal_connect (priv->monitor, "changed", + G_CALLBACK (gs_plugin_repos_changed_cb), plugin); + + /* unconditionally at startup */ + return gs_plugin_repos_setup (plugin, cancellable, error); +} + +static gboolean +refine_app_locked (GsPlugin *plugin, + GsApp *app, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + const gchar *tmp; + + /* not required */ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME) == 0) + return TRUE; + if (gs_app_get_origin_hostname (app) != NULL) + return TRUE; + + /* make sure we don't end up refining flatpak repos */ + if (gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_PACKAGE) + return TRUE; + + /* ensure valid */ + if (!gs_plugin_repos_setup (plugin, cancellable, error)) + return FALSE; + + /* find hostname */ + switch (gs_app_get_kind (app)) { + case AS_APP_KIND_SOURCE: + if (gs_app_get_id (app) == NULL) + return TRUE; + tmp = g_hash_table_lookup (priv->urls, gs_app_get_id (app)); + if (tmp != NULL) + gs_app_set_url (app, AS_URL_KIND_HOMEPAGE, tmp); + break; + default: + if (gs_app_get_origin (app) == NULL) + return TRUE; + tmp = g_hash_table_lookup (priv->urls, gs_app_get_origin (app)); + if (tmp != NULL) + gs_app_set_origin_hostname (app, tmp); + break; + } + + /* find filename */ + switch (gs_app_get_kind (app)) { + case AS_APP_KIND_SOURCE: + if (gs_app_get_id (app) == NULL) + return TRUE; + tmp = g_hash_table_lookup (priv->fns, gs_app_get_id (app)); + if (tmp != NULL) + gs_app_set_metadata (app, "repos::repo-filename", tmp); + break; + default: + break; + } + + return TRUE; +} + +gboolean +gs_plugin_refine (GsPlugin *plugin, + GsAppList *list, + GsPluginRefineFlags flags, + GCancellable *cancellable, + GError **error) +{ + GsPluginData *priv = gs_plugin_get_data (plugin); + g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&priv->mutex); + + /* nothing to do here */ + if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME) == 0) + return TRUE; + + for (guint i = 0; i < gs_app_list_length (list); i++) { + GsApp *app = gs_app_list_index (list, i); + if (!refine_app_locked (plugin, app, flags, cancellable, error)) + return FALSE; + } + + return TRUE; +} diff --git a/plugins/repos/gs-self-test.c b/plugins/repos/gs-self-test.c new file mode 100644 index 0000000..ff5b3e7 --- /dev/null +++ b/plugins/repos/gs-self-test.c @@ -0,0 +1,82 @@ +/* -*- 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_repos_func (GsPluginLoader *plugin_loader) +{ + gboolean ret; + g_autoptr(GsApp) app = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GsPluginJob) plugin_job = NULL; + + /* get the extra bits */ + app = gs_app_new ("testrepos.desktop"); + gs_app_set_origin (app, "utopia"); + gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE); + plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_REFINE, + "app", app, + "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME, + NULL); + ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error); + gs_test_flush_main_context (); + g_assert_no_error (error); + g_assert (ret); + g_assert_cmpstr (gs_app_get_origin_hostname (app), ==, "people.freedesktop.org"); +} + +int +main (int argc, char **argv) +{ + gboolean ret; + g_autofree gchar *reposdir = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GsPluginLoader) plugin_loader = NULL; + const gchar *allowlist[] = { + "repos", + 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); + + /* dummy data */ + reposdir = gs_test_get_filename (TESTDATADIR, "yum.repos.d"); + g_assert (reposdir != NULL); + g_setenv ("GS_SELF_TEST_REPOS_DIR", reposdir, 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); + 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/repos", + plugin_loader, + (GTestDataFunc) gs_plugins_repos_func); + + return g_test_run (); +} diff --git a/plugins/repos/meson.build b/plugins/repos/meson.build new file mode 100644 index 0000000..201b137 --- /dev/null +++ b/plugins/repos/meson.build @@ -0,0 +1,41 @@ +cargs = ['-DG_LOG_DOMAIN="GsPluginRepos"'] +cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"'] + +shared_module( + 'gs_plugin_repos', + sources : 'gs-plugin-repos.c', + include_directories : [ + include_directories('../..'), + include_directories('../../lib'), + ], + install : true, + install_dir: plugin_dir, + c_args : cargs, + dependencies : plugin_libs, + link_with : [ + libgnomesoftware + ] +) + +if get_option('tests') + cargs += ['-DTESTDATADIR="' + join_paths(meson.current_source_dir(), 'tests') + '"'] + e = executable( + 'gs-self-test-repos', + 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-repos', e, suite: ['plugins', 'repos'], env: test_env) +endif diff --git a/plugins/repos/tests/yum.repos.d/utopia.repo b/plugins/repos/tests/yum.repos.d/utopia.repo new file mode 100644 index 0000000..e912ec4 --- /dev/null +++ b/plugins/repos/tests/yum.repos.d/utopia.repo @@ -0,0 +1,5 @@ +[utopia] +name=utopia for Fedora $releasever +baseurl=http://people.freedesktop.org/~hughsient/fedora/$releasever/x86_64/ +enabled=1 +gpgcheck=0 |