summaryrefslogtreecommitdiffstats
path: root/plugins/repos/gs-plugin-repos.c
blob: 8e57e8ba51d3d28edcc91c563ef94bacea6dbb9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* -*- 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>

#include "gs-plugin-repos.h"

/*
 * SECTION:
 * Plugin to set URLs and origin hostnames on repos and apps using data from
 * `/etc/yum.repos.d`
 *
 * This plugin is only useful on distributions which use `/etc/yum.repos.d`.
 *
 * It enumerates `/etc/yum.repos.d` in a worker thread and updates its internal
 * hash tables and state from that worker thread (while holding a lock).
 *
 * Other tasks on the plugin access the data synchronously, not using a worker
 * thread. Data accesses should be fast.
 */

struct _GsPluginRepos {
	GsPlugin	 parent;

	/* These hash tables are replaced by a worker thread. They are immutable
	 * once set, and will only be replaced with a new hash table instance.
	 * This means they are safe to access from the refine function in the
	 * main thread with a strong reference and no lock.
	 *
	 * @mutex must be held when getting a strong reference to them, or
	 * replacing them. */
	GHashTable	*fns;		/* origin : filename */
	GHashTable	*urls;		/* origin : url */

	GFileMonitor	*monitor;
	gchar		*reposdir;

	GMutex		 mutex;

	/* Used to cancel a pending update operation which is loading the repos
	 * data in a worker thread. */
	GCancellable	*update_cancellable; /* (nullable) (owned) */
};

G_DEFINE_TYPE (GsPluginRepos, gs_plugin_repos, GS_TYPE_PLUGIN)

static void
gs_plugin_repos_init (GsPluginRepos *self)
{
	GsPlugin *plugin = GS_PLUGIN (self);

	g_mutex_init (&self->mutex);

	/* for debugging and the self tests */
	self->reposdir = g_strdup (g_getenv ("GS_SELF_TEST_REPOS_DIR"));
	if (self->reposdir == NULL)
		self->reposdir = g_strdup ("/etc/yum.repos.d");

	/* plugin only makes sense if this exists at startup */
	if (!g_file_test (self->reposdir, G_FILE_TEST_EXISTS)) {
		gs_plugin_set_enabled (plugin, FALSE);
		return;
	}
}

static void
gs_plugin_repos_dispose (GObject *object)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (object);

	g_cancellable_cancel (self->update_cancellable);
	g_clear_object (&self->update_cancellable);
	g_clear_pointer (&self->reposdir, g_free);
	g_clear_pointer (&self->fns, g_hash_table_unref);
	g_clear_pointer (&self->urls, g_hash_table_unref);
	g_clear_object (&self->monitor);

	G_OBJECT_CLASS (gs_plugin_repos_parent_class)->dispose (object);
}

static void
gs_plugin_repos_finalize (GObject *object)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (object);

	g_mutex_clear (&self->mutex);

	G_OBJECT_CLASS (gs_plugin_repos_parent_class)->finalize (object);
}

/* Run in a worker thread; will take the mutex */
static gboolean
gs_plugin_repos_load (GsPluginRepos  *self,
                      GCancellable   *cancellable,
                      GError        **error)
{
	g_autoptr(GDir) dir = NULL;
	const gchar *fn;
	g_autoptr(GHashTable) new_filenames = NULL;
	g_autoptr(GHashTable) new_urls = NULL;
	g_autoptr(GMutexLocker) locker = NULL;

	new_filenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
	new_urls = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

	/* search all files */
	dir = g_dir_open (self->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 (self->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 *baseurl = NULL, *metalink = NULL;

			g_hash_table_insert (new_filenames,
			                     g_strdup (groups[i]),
			                     g_strdup (filename));

			baseurl = g_key_file_get_string (kf, groups[i], "baseurl", NULL);
			if (baseurl != NULL) {
				g_hash_table_insert (new_urls,
						     g_strdup (groups[i]),
						     g_steal_pointer (&baseurl));
				continue;
			}

			metalink = g_key_file_get_string (kf, groups[i], "metalink", NULL);
			if (metalink != NULL) {
				g_hash_table_insert (new_urls,
						     g_strdup (groups[i]),
						     g_steal_pointer (&metalink));
				continue;
			}
		}
	}

	/* success; replace the hash table pointers in the object while the lock
	 * is held */
	locker = g_mutex_locker_new (&self->mutex);

	g_clear_pointer (&self->fns, g_hash_table_unref);
	self->fns = g_steal_pointer (&new_filenames);
	g_clear_pointer (&self->urls, g_hash_table_unref);
	self->urls = g_steal_pointer (&new_urls);

	g_assert (self->fns != NULL && self->urls != NULL);

	return TRUE;
}

/* Run in a worker thread. */
static void
update_repos_thread_cb (GTask        *task,
                        gpointer      source_object,
                        gpointer      task_data,
                        GCancellable *cancellable)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (source_object);
	g_autoptr(GError) local_error = NULL;

	if (!gs_plugin_repos_load (self, cancellable, &local_error))
		g_task_return_error (task, g_steal_pointer (&local_error));
	else
		g_task_return_boolean (task, TRUE);
}

/* Run in the main thread. */
static void
gs_plugin_repos_changed_cb (GFileMonitor      *monitor,
                            GFile             *file,
                            GFile             *other_file,
                            GFileMonitorEvent  event_type,
                            gpointer           user_data)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (user_data);
	g_autoptr(GTask) task = NULL;

	/* Cancel any pending updates and schedule a new update of the repo data
	 * in a worker thread. */
	g_cancellable_cancel (self->update_cancellable);
	g_clear_object (&self->update_cancellable);
	self->update_cancellable = g_cancellable_new ();

	task = g_task_new (self, self->update_cancellable, NULL, NULL);
	g_task_set_source_tag (task, gs_plugin_repos_changed_cb);
	g_task_run_in_thread (task, update_repos_thread_cb);
}

static void
gs_plugin_repos_setup_async (GsPlugin            *plugin,
                             GCancellable        *cancellable,
                             GAsyncReadyCallback  callback,
                             gpointer             user_data)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (plugin);
	g_autoptr(GFile) file = g_file_new_for_path (self->reposdir);
	g_autoptr(GTask) task = NULL;
	g_autoptr(GError) local_error = NULL;

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_repos_setup_async);

	/* watch for changes in the main thread */
	self->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, cancellable, &local_error);
	if (self->monitor == NULL) {
		gs_utils_error_convert_gio (&local_error);
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	g_signal_connect (self->monitor, "changed",
			  G_CALLBACK (gs_plugin_repos_changed_cb), self);

	/* Set up the repos at startup. */
	g_task_run_in_thread (task, update_repos_thread_cb);
}

static gboolean
gs_plugin_repos_setup_finish (GsPlugin      *plugin,
                              GAsyncResult  *result,
                              GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_plugin_repos_shutdown_async (GsPlugin            *plugin,
                                GCancellable        *cancellable,
                                GAsyncReadyCallback  callback,
                                gpointer             user_data)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (plugin);
	g_autoptr(GTask) task = NULL;

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_repos_shutdown_async);

	/* Cancel any ongoing update operations. */
	g_cancellable_cancel (self->update_cancellable);

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_repos_shutdown_finish (GsPlugin      *plugin,
                                 GAsyncResult  *result,
                                 GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
refine_app (GsApp               *app,
            GsPluginRefineFlags  flags,
            GHashTable          *filenames,
            GHashTable          *urls)
{
	const gchar *tmp;

	/* not required */
	if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME) == 0)
		return;
	if (gs_app_get_origin_hostname (app) != NULL)
		return;

	/* make sure we don't end up refining flatpak repos */
	if (gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_PACKAGE)
		return;

	/* find hostname */
	switch (gs_app_get_kind (app)) {
	case AS_COMPONENT_KIND_REPOSITORY:
		if (gs_app_get_id (app) == NULL)
			return;
		tmp = g_hash_table_lookup (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;
		tmp = g_hash_table_lookup (urls, gs_app_get_origin (app));
		if (tmp != NULL)
			gs_app_set_origin_hostname (app, tmp);
		else {
			GHashTableIter iter;
			gpointer key, value;
			const gchar *origin;

			origin = gs_app_get_origin (app);

			/* Some repos, such as rpmfusion, can have set the name with a distribution
			   number in the appstream file, thus check those specifically */
			g_hash_table_iter_init (&iter, urls);
			while (g_hash_table_iter_next (&iter, &key, &value)) {
				if (g_str_has_prefix (origin, key)) {
					const gchar *rest = origin + strlen (key);
					while (*rest == '-' || (*rest >= '0' && *rest <= '9'))
						rest++;
					if (!*rest) {
						gs_app_set_origin_hostname (app, value);
						break;
					}
				}
			}
		}
		break;
	}

	/* find filename */
	switch (gs_app_get_kind (app)) {
	case AS_COMPONENT_KIND_REPOSITORY:
		if (gs_app_get_id (app) == NULL)
			return;
		tmp = g_hash_table_lookup (filenames, gs_app_get_id (app));
		if (tmp != NULL)
			gs_app_set_metadata (app, "repos::repo-filename", tmp);
		break;
	default:
		break;
	}
}

static void
gs_plugin_repos_refine_async (GsPlugin            *plugin,
                              GsAppList           *list,
                              GsPluginRefineFlags  flags,
                              GCancellable        *cancellable,
                              GAsyncReadyCallback  callback,
                              gpointer             user_data)
{
	GsPluginRepos *self = GS_PLUGIN_REPOS (plugin);
	g_autoptr(GHashTable) filenames = NULL;  /* (element-type utf8 filename) mapping origin to filename */
	g_autoptr(GHashTable) urls = NULL;  /* (element-type utf8 utf8) mapping origin to URL */
	g_autoptr(GMutexLocker) locker = NULL;
	g_autoptr(GTask) task = NULL;

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_repos_refine_async);

	/* nothing to do here */
	if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ORIGIN_HOSTNAME) == 0) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* Grab a reference to the object’s state so it can be accessed without
	 * holding the lock throughout, to keep the critical section small. */
	locker = g_mutex_locker_new (&self->mutex);
	filenames = g_hash_table_ref (self->fns);
	urls = g_hash_table_ref (self->urls);
	g_clear_pointer (&locker, g_mutex_locker_free);

	/* Update each of the apps */
	for (guint i = 0; i < gs_app_list_length (list); i++) {
		GsApp *app = gs_app_list_index (list, i);

		refine_app (app, flags, filenames, urls);
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_repos_refine_finish (GsPlugin      *plugin,
                               GAsyncResult  *result,
                               GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_plugin_repos_class_init (GsPluginReposClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);

	object_class->dispose = gs_plugin_repos_dispose;
	object_class->finalize = gs_plugin_repos_finalize;

	plugin_class->setup_async = gs_plugin_repos_setup_async;
	plugin_class->setup_finish = gs_plugin_repos_setup_finish;
	plugin_class->shutdown_async = gs_plugin_repos_shutdown_async;
	plugin_class->shutdown_finish = gs_plugin_repos_shutdown_finish;
	plugin_class->refine_async = gs_plugin_repos_refine_async;
	plugin_class->refine_finish = gs_plugin_repos_refine_finish;
}

GType
gs_plugin_query_type (void)
{
	return GS_TYPE_PLUGIN_REPOS;
}