Codebase list realmd / scrub-obsolete/main service / realm-packages.c
scrub-obsolete/main

Tree @scrub-obsolete/main (Download .tar.gz)

realm-packages.c @scrub-obsolete/mainraw · history · blame

  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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/* realmd -- Realm configuration service
 *
 * Copyright 2012 Red Hat Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */

#include "config.h"

#include "realm-diagnostics.h"
#include "realm-daemon.h"
#include "realm-errors.h"
#include "realm-invocation.h"
#include "realm-options.h"
#include "realm-packages.h"
#include "realm-settings.h"

#include <glib/gi18n.h>

#define CALL_TIMEOUT (24 * 60 * 60 * 1000)

static gboolean
packages_check_paths (const gchar **paths,
                            GDBusMethodInvocation *invocation)
{
	gint i;

	g_return_val_if_fail (paths != NULL, FALSE);
	g_return_val_if_fail (invocation == NULL || G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);

	for (i = 0; paths[i] != NULL; i++) {
		if (!g_file_test (paths[i], G_FILE_TEST_EXISTS)) {
			realm_diagnostics_info (invocation, "Couldn't find file: %s", paths[i]);
			return FALSE;
		}
	}

	return TRUE;
}

static gchar *
packages_to_list (gchar **package_ids)
{
	GString *string;
	gchar **parts;
	gint i;

	string = g_string_new ("");
	for (i = 0; package_ids != NULL && package_ids[i] != NULL; i++) {
		parts = g_strsplit (package_ids[i], ";", 2);
		if (string->len)
			g_string_append (string, " ");
		g_string_append (string, parts[0]);
		g_strfreev (parts);
	}

	return g_string_free (string, FALSE);
}

typedef struct {
    GDBusConnection *connection;
    guint subscription;
    gchar *path;

    /* The method call */
    const gchar *method;
    GVariant *parameters;

    /* Package IDs seen when resolving */
    GHashTable *packages;

    GVariant *error_code;
} PackageTransaction;

static void
package_transaction_free (gpointer data)
{
	PackageTransaction *transaction = data;

	g_debug ("packages: freeing transtaction");

	if (transaction->subscription) {
		g_dbus_connection_signal_unsubscribe (transaction->connection,
		                                      transaction->subscription);
	}
	g_object_unref (transaction->connection);
	g_free (transaction->path);
	if (transaction->packages)
		g_hash_table_unref (transaction->packages);
	if (transaction->parameters)
		g_variant_unref (transaction->parameters);
	if (transaction->error_code)
		g_variant_unref (transaction->error_code);
	g_free (transaction);
}

static void
on_transaction_signal (GDBusConnection *connection,
                       const gchar *sender_name,
                       const gchar *object_path,
                       const gchar *interface_name,
                       const gchar *signal_name,
                       GVariant *parameters,
                       gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	PackageTransaction *transaction = g_task_get_task_data (task);
	const gchar *message;
	const gchar *id;
	const gchar *pos;
	guint code, percent;
	gboolean installed;
	gchar *package;
	gchar *string;

	g_debug ("packages: signal: %s %s", signal_name,
	         string = g_variant_print (parameters, FALSE));
	g_free (string);

	if (g_str_equal (signal_name, "ErrorCode")) {
		if (transaction->error_code)
			g_variant_unref (transaction->error_code);
		transaction->error_code = g_variant_ref (parameters);

	} else if (g_str_equal (signal_name, "Finished")) {
		g_dbus_connection_signal_unsubscribe (connection, transaction->subscription);
		transaction->subscription = 0;
		if (!g_task_had_error (task)) {
			if (transaction->error_code) {
				g_variant_get (transaction->error_code, "(u&s)", &code, &message);
				g_task_return_new_error (task, REALM_ERROR, REALM_ERROR_FAILED, "%s", message);
			} else {
				g_task_return_boolean (task, TRUE);
			}
		}
		g_object_unref (task);

	} else if (g_str_equal (signal_name, "Package")) {
		g_variant_get (parameters, "(u&s&s)", &code, &id, &message);

		if (!transaction->packages) {
			transaction->packages = g_hash_table_new_full (g_str_hash, g_str_equal,
			                                               g_free, g_free);
		}
		pos = strchr (id, ';');
		if (pos == NULL)
			pos = id + strlen (id);

		installed = (code == 1 /* PK_INFO_ENUM_INSTALLED */);
		package = g_strndup (id, pos - id);

		if (installed)
			id = "";

		if (installed || !g_hash_table_lookup (transaction->packages, package)) {
			g_hash_table_replace (transaction->packages, package, g_strdup (id));
			package = NULL;
		}

		g_free (package);

	} else if (g_str_equal (signal_name, "ItemProgress")) {
		g_variant_get (parameters, "(&suu)", &id, &code, &percent);
		g_debug ("packages: progress: %s %u %u", id, code, code);
	}
}

static void
on_method_done (GObject *source,
                GAsyncResult *result,
                gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	PackageTransaction *transaction = g_task_get_task_data (task);
	GError *error = NULL;
	GVariant *retval;

	retval = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error);

	if (error != NULL) {
		g_debug ("packages: call %s failed: %s", transaction->method, error->message);
		g_task_return_error (task, error);
	} else {
		g_debug ("packages: call %s completed", transaction->method);
		g_variant_unref (retval);
	}

	/* Not done until Finished signal */

	g_object_unref (task);
}

static void
on_set_hints (GObject *source,
              GAsyncResult *result,
              gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	PackageTransaction *transaction;
	GError *error = NULL;
	GVariant *retval;
	gchar *string;

	transaction = g_task_get_task_data (task);
	retval = g_dbus_connection_call_finish (transaction->connection, result, &error);

	if (error != NULL) {
		g_debug ("packages: call SetHints failed: %s", error->message);
		g_task_return_error (task, error);

	} else {
		g_variant_unref (retval);

		g_debug ("packages: call %s %s", transaction->method,
		         string = g_variant_print (transaction->parameters, FALSE));
		g_dbus_connection_call (transaction->connection,
		                        "org.freedesktop.PackageKit",
		                        transaction->path,
		                        "org.freedesktop.PackageKit.Transaction",
		                        transaction->method,
		                        transaction->parameters,
		                        G_VARIANT_TYPE ("()"),
		                        G_DBUS_CALL_FLAGS_NO_AUTO_START,
		                        CALL_TIMEOUT, g_task_get_cancellable (task),
		                        on_method_done, g_object_ref (task));
	}

	g_object_unref (task);
}

static void
on_create_transaction (GObject *source,
                       GAsyncResult *result,
                       gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	PackageTransaction *transaction;
	GError *error = NULL;
	GVariant *retval;

	const gchar *hints[] = { "interactive=false", "background=false", NULL };

	transaction = g_task_get_task_data (task);
	retval = g_dbus_connection_call_finish (transaction->connection, result, &error);

	if (error != NULL) {
		g_debug ("packages: CreateTransaction failed: %s", error->message);
		g_task_return_error (task, error);

	} else {
		g_variant_get (retval, "(o)", &transaction->path);
		g_variant_unref (retval);

		transaction->subscription =
			g_dbus_connection_signal_subscribe (transaction->connection,
			                                    "org.freedesktop.PackageKit",
			                                    "org.freedesktop.PackageKit.Transaction",
			                                    NULL,
			                                    transaction->path,
			                                    NULL,
			                                    G_DBUS_SIGNAL_FLAGS_NONE,
			                                    on_transaction_signal,
			                                    task, NULL);

		g_debug ("packages: SetHints call");
		g_dbus_connection_call (transaction->connection,
		                        "org.freedesktop.PackageKit",
		                        transaction->path,
		                        "org.freedesktop.PackageKit.Transaction",
		                        "SetHints",
		                        g_variant_new ("(^as)", hints),
		                        G_VARIANT_TYPE ("()"),
		                        G_DBUS_CALL_FLAGS_NO_AUTO_START,
		                        CALL_TIMEOUT, g_task_get_cancellable (task),
		                        on_set_hints, g_object_ref (task));
	}

	g_object_unref (task);
}

static void
package_transaction_create (const gchar *method,
                            GVariant *parameters,
                            GDBusConnection *connection,
                            GCancellable *cancellable,
                            GAsyncReadyCallback callback,
                            gpointer user_data)
{
	PackageTransaction *transaction;
	GTask *task;

	task = g_task_new (NULL, cancellable, callback, user_data);
	transaction = g_new0 (PackageTransaction, 1);
	transaction->method = method;
	transaction->parameters = g_variant_ref_sink (parameters);
	transaction->connection = g_object_ref (connection);
	g_task_set_task_data (task, transaction, package_transaction_free);

	g_debug ("packages: CreateTransaction call");

	g_dbus_connection_call (connection, "org.freedesktop.PackageKit",
	                        "/org/freedesktop/PackageKit",
	                        "org.freedesktop.PackageKit",
	                        "CreateTransaction",
	                        g_variant_new ("()"),
	                        G_VARIANT_TYPE ("(o)"),
	                        G_DBUS_CALL_FLAGS_NONE,
	                        CALL_TIMEOUT, cancellable,
	                        on_create_transaction, g_object_ref (task));
}

static void
packages_install_async (GDBusConnection *connection,
                        const gchar **package_ids,
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data)
{
	guint64 transaction_flags = 1 /* PK_TRANSACTION_FLAG_ENUM_ONLY_TRUSTED */;
	package_transaction_create ("InstallPackages", g_variant_new ("(t^as)", transaction_flags, package_ids),
	                            connection, cancellable, callback, user_data);
}

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

static void
packages_resolve_async (GDBusConnection *connection,
                        const gchar **package_names,
                        GCancellable *cancellable,
                        GAsyncReadyCallback callback,
                        gpointer user_data)
{
	guint64 flags = 1 << 18 /* PK_FILTER_ENUM_ARCH */;
	flags |= 1 << 16 /* PK_FILTER_ENUM_NEWEST */;
	package_transaction_create ("Resolve", g_variant_new ("(t^as)", flags, package_names),
	                            connection, cancellable, callback, user_data);
}

static gchar **
packages_resolve_finish (GAsyncResult *result,
                         GError **error)
{
	GTask *task = G_TASK (result);
	PackageTransaction *transaction;
	gchar **requested;
	GPtrArray *packages;
	GHashTableIter iter;
	guint64 flags;
	gchar *missing;
	gchar *id;
	gint i;

	if (!g_task_propagate_boolean (task, error))
		return NULL;

	transaction = g_task_get_task_data (task);
	g_variant_get (transaction->parameters, "(t^a&s)", &flags, &requested);

	/*
	 * In an unexpected move, Resolve() does not fail or provide
	 * any feedback when a requested package does not exist.
	 *
	 * So we make a note of the ones we requested here, to compare against
	 * what we get back.
	 */

	packages = g_ptr_array_new ();
	for (i = 0; requested[i] != NULL; i++) {
		if (!g_hash_table_lookup (transaction->packages, requested[i]))
			g_ptr_array_add (packages, requested[i]);
	}

	missing = NULL;
	if (packages->len) {
		g_ptr_array_add (packages, NULL);
		missing = packages_to_list ((gchar **)packages->pdata);
		g_set_error (error, REALM_ERROR, REALM_ERROR_INTERNAL,
		             _("The following packages are not available for installation: %s"), missing);
		g_free (missing);
	}
	g_ptr_array_free (packages, TRUE);

	if (missing) {
		return NULL;
	}

	packages = g_ptr_array_new ();
	g_hash_table_iter_init (&iter, transaction->packages);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&id)) {
		if (!g_str_equal (id, "")) {
			g_hash_table_iter_steal (&iter);
			g_ptr_array_add (packages, id);
		}
	}

	g_ptr_array_add (packages, NULL);
	return (gchar **)g_ptr_array_free (packages, FALSE);
}

typedef struct {
	GDBusConnection *connection;
	GDBusMethodInvocation *invocation;
	gchar **packages;
	gboolean automatic;
} InstallClosure;

static void
install_closure_free (gpointer data)
{
	InstallClosure *install = data;
	g_clear_object (&install->invocation);
	g_clear_object (&install->connection);
	g_strfreev (install->packages);
	g_free (install);
}

static void
on_install_installed (GObject *source,
                      GAsyncResult *result,
                      gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	GError *error = NULL;

	packages_install_finish (result, &error);
	if (error == NULL) {
		g_task_return_boolean (task, TRUE);
	} else {
		g_task_return_error (task, error);
	}

	g_object_unref (task);
}

static void
on_install_resolved (GObject *source,
                     GAsyncResult *result,
                     gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	InstallClosure *install = g_task_get_task_data (task);
	gchar **package_ids = NULL;
	GHashTable *names;
	GCancellable *cancellable;
	GError *error = NULL;
	gchar *remote;
	gchar *missing;

	names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	package_ids = packages_resolve_finish (result, &error);

	if (error == NULL) {
		missing = packages_to_list (package_ids);
		if (package_ids == NULL || *package_ids == NULL) {
			g_task_return_boolean (task, TRUE);

		} else if (!install->automatic) {
			g_set_error (&error, REALM_ERROR, REALM_ERROR_FAILED,
			             _("Necessary packages are not installed: %s"), missing);

		} else {

			/* String should match that in realm-client.c */
			realm_diagnostics_info (install->invocation, "%s: %s",
			                        _("Installing necessary packages"), missing);
			cancellable = realm_invocation_get_cancellable (install->invocation);
			packages_install_async (install->connection,
			                        (const gchar **)package_ids, cancellable,
			                        on_install_installed, g_object_ref (task));
		}

		g_free (missing);
	}

	if (error != NULL) {
		/*
		 * This is after our first interaction with package-kit. If it's
		 * not installed then we'll get a standard DBus error that it
		 * couldn't activate the service.
		 *
		 * So translate that into something useful for the caller. If
		 * PackageKit is not installed, then it is assumed that the
		 * distro or administrator wants to take full control over the
		 * installation of packages.
		 */
		if (error->domain == G_DBUS_ERROR) {
			remote = g_dbus_error_get_remote_error (error);
			if (remote && g_str_equal (remote, "org.freedesktop.DBus.Error.ServiceUnknown")) {
				g_dbus_error_strip_remote_error (error);
				realm_diagnostics_error (install->invocation, error, "PackageKit not available");
				g_clear_error (&error);
				missing = packages_to_list (install->packages);
				g_set_error (&error, REALM_ERROR, REALM_ERROR_FAILED,
				             _("Necessary packages are not installed: %s"), missing);
				g_free (missing);
			}
			g_free (remote);
		}

		g_task_return_error (task, error);
	}

	g_hash_table_unref (names);
	g_strfreev (package_ids);
	g_object_unref (task);
}

static void
lookup_required_files_and_packages (const gchar **package_sets,
                                    gchar ***result_packages,
                                    gchar ***result_files,
                                    gboolean *result_unconditional)
{
	GHashTable *settings;
	GHashTableIter iter;
	GPtrArray *packages;
	GPtrArray *files;
	gboolean unconditional;
	gchar *section;
	gchar *package;
	gchar *file;
	gint i;

	unconditional = FALSE;
	packages = g_ptr_array_new_with_free_func (g_free);
	files = g_ptr_array_new_with_free_func (g_free);

	for (i = 0; package_sets[i] != NULL; i++) {
		section = g_strdup_printf ("%s-packages", package_sets[i]);
		settings = realm_settings_section (section);
		if (settings == NULL) {
			g_critical ("No section found in settings: %s", section);
			return;
		}
		g_free (section);

		g_hash_table_iter_init (&iter, settings);
		while (g_hash_table_iter_next (&iter, (gpointer *)&package, (gpointer *)&file)) {
			file = g_strstrip (g_strdup (file));
			if (g_str_equal (file, "")) {
				g_free (file);
				unconditional = TRUE;
			} else {
				g_ptr_array_add (files, file);
			}
			package = g_strstrip (g_strdup (package));
			if (g_str_equal (package, ""))
				g_free (package);
			else
				g_ptr_array_add (packages, package);
		}
	}

	if (result_packages) {
		g_ptr_array_add (packages, NULL);
		*result_packages = (gchar **)g_ptr_array_free (packages, FALSE);
	} else {
		g_ptr_array_free (packages, TRUE);
	}

	if (result_files) {
		g_ptr_array_add (files, NULL);
		*result_files = (gchar **)g_ptr_array_free (files, FALSE);
	} else {
		g_ptr_array_free (files, TRUE);
	}

	if (result_unconditional)
		*result_unconditional = unconditional;
}

gchar **
realm_packages_expand_sets (const gchar **package_sets)
{
	gchar **packages = NULL;

	g_return_val_if_fail (package_sets != NULL, NULL);

	lookup_required_files_and_packages (package_sets, &packages, NULL, NULL);
	return packages;
}

void
realm_packages_install_async (const gchar **package_sets,
                              GDBusMethodInvocation *invocation,
                              GDBusConnection *connection,
                              GAsyncReadyCallback callback,
                              gpointer user_data)
{
	GTask *task;
	InstallClosure *install;
	gboolean unconditional;
	gchar **required_files;
	GCancellable *cancellable;
	gchar *string;
	gboolean have;

	g_return_if_fail (package_sets != NULL);
	g_return_if_fail (G_IS_DBUS_CONNECTION (connection));

	task = g_task_new (NULL, NULL, callback, user_data);
	install = g_new0 (InstallClosure, 1);
	install->automatic = realm_options_automatic_install ();
	install->invocation = invocation ? g_object_ref (invocation) : NULL;
	install->connection = g_object_ref (connection);
	g_task_set_task_data (task, install, install_closure_free);

	lookup_required_files_and_packages (package_sets, &install->packages, &required_files, &unconditional);

	if (realm_daemon_is_install_mode ()) {
		have = TRUE;
		realm_diagnostics_info (invocation, "Assuming packages are installed");

	} else if (unconditional) {
		have = FALSE;
		realm_diagnostics_info (invocation, "Unconditionally checking packages");

	} else {
		have = packages_check_paths ((const gchar **)required_files, invocation);
		if (required_files[0] != NULL) {
			string = g_strjoinv (", ", required_files);
			realm_diagnostics_info (invocation, "Required files: %s", string);
			g_free (string);
		}
	}

	g_strfreev (required_files);

	if (have) {
		g_task_return_boolean (task, TRUE);

	} else {
		realm_diagnostics_info (invocation, "Resolving required packages");

		cancellable = realm_invocation_get_cancellable (install->invocation);
		packages_resolve_async (connection, (const gchar **)install->packages, cancellable,
		                        on_install_resolved, g_object_ref (task));
	}

	g_object_unref (task);
}

gboolean
realm_packages_install_finish (GAsyncResult *result,
                               GError **error)
{
	if (g_task_propagate_boolean (G_TASK (result), error))
		return FALSE;

	return TRUE;
}