Debian Codegraph @

lib/packagekit-glib2/pk-offline.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2014 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <config.h>
23
24 #include <glib.h>
25 #include <errno.h>
26 #include <string.h>
27
28 #include "pk-offline.h"
29 #include "pk-offline-private.h"
30
31 /**
32 * SECTION:pk-offline
33 * @title: Offline Updates
34 * @short_description: Function to perform offline updates
35 *
36 * Functions for perform offline updates.
37 */
38
39 /**
40 * pk_offline_error_quark:
41 *
42 * An error quark for #PkOfflineError.
43 *
44 * Return value: an error quark.
45 *
46 * Since: 0.9.6
47 **/
48 G_DEFINE_QUARK (pk-offline-error-quark, pk_offline_error)
49
50 /**
51 * pk_offline_action_to_string:
52 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
53 *
54 * Converts the enumerated value to a string.
55 *
56 * Return value: string value, or %NULL for invalid
57 *
58 * Since: 0.9.6
59 **/
60 const gchar *
61 pk_offline_action_to_string (PkOfflineAction action)
62 {
63 if (action == PK_OFFLINE_ACTION_UNKNOWN)
64 return "unknown";
65 if (action == PK_OFFLINE_ACTION_REBOOT)
66 return "reboot";
67 if (action == PK_OFFLINE_ACTION_POWER_OFF)
68 return "power-off";
69 if (action == PK_OFFLINE_ACTION_UNSET)
70 return "unset";
71 return NULL;
72 }
73
74 /**
75 * pk_offline_action_from_string:
76 * @action: a string representation of a #PkOfflineAction, e.g. "reboot"
77 *
78 * Converts the string to the enumerated value.
79 *
80 * Return value: A #PkOfflineAction, or %PK_OFFLINE_ACTION_UNKNOWN for invalid
81 *
82 * Since: 0.9.6
83 **/
84 PkOfflineAction
85 pk_offline_action_from_string (const gchar *action)
86 {
87 if (g_strcmp0 (action, "unknown") == 0)
88 return PK_OFFLINE_ACTION_UNKNOWN;
89 if (g_strcmp0 (action, "reboot") == 0)
90 return PK_OFFLINE_ACTION_REBOOT;
91 if (g_strcmp0 (action, "power-off") == 0)
92 return PK_OFFLINE_ACTION_POWER_OFF;
93 if (g_strcmp0 (action, "unset") == 0)
94 return PK_OFFLINE_ACTION_UNSET;
95 return PK_OFFLINE_ACTION_UNKNOWN;
96 }
97
98 static GDBusCallFlags
99 pk_offline_flags_to_gdbus_call_flags (PkOfflineFlags flags)
100 {
101 if ((flags & PK_OFFLINE_FLAGS_INTERACTIVE) != 0)
102 return G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION;
103 return G_DBUS_CALL_FLAGS_NONE;
104 }
105
106 /**
107 * pk_offline_cancel:
108 * @cancellable: A #GCancellable or %NULL
109 * @error: A #GError or %NULL
110 *
111 * Cancels the offline operation that has been scheduled. If there is no
112 * scheduled offline operation then this method returns with success.
113 * The function always allows user interaction. To change the behavior,
114 * use pk_offline_cancel_with_flags().
115 *
116 * Return value: %TRUE for success, else %FALSE and @error set
117 *
118 * Since: 0.9.6
119 **/
120 gboolean
121 pk_offline_cancel (GCancellable *cancellable, GError **error)
122 {
123 return pk_offline_cancel_with_flags (PK_OFFLINE_FLAGS_INTERACTIVE, cancellable, error);
124 }
125
126 /**
127 * pk_offline_cancel_with_flags:
128 * @flags: bit-or of #PkOfflineFlags
129 * @cancellable: A #GCancellable or %NULL
130 * @error: A #GError or %NULL
131 *
132 * Cancels the offline operation that has been scheduled. If there is no
133 * scheduled offline operation then this method returns with success.
134 *
135 * Return value: %TRUE for success, else %FALSE and @error set
136 *
137 * Since: 1.2.5
138 **/
139 gboolean
140 pk_offline_cancel_with_flags (PkOfflineFlags flags, GCancellable *cancellable, GError **error)
141 {
142 g_autoptr(GDBusConnection) connection = NULL;
143 g_autoptr(GVariant) res = NULL;
144
145 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
146
147 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
148 if (connection == NULL)
149 return FALSE;
150 res = g_dbus_connection_call_sync (connection,
151 "org.freedesktop.PackageKit",
152 "/org/freedesktop/PackageKit",
153 "org.freedesktop.PackageKit.Offline",
154 "Cancel",
155 NULL,
156 NULL,
157 pk_offline_flags_to_gdbus_call_flags (flags),
158 -1,
159 cancellable,
160 error);
161 if (res == NULL)
162 return FALSE;
163 return TRUE;
164 }
165
166 /**
167 * pk_offline_clear_results:
168 * @cancellable: A #GCancellable or %NULL
169 * @error: A #GError or %NULL
170 *
171 * Clears the last offline operation report, which may be success or failure.
172 * If the report does not exist then this method returns success.
173 * The function always allows user interaction. To change the behavior,
174 * use pk_offline_clear_results_with_flags().
175 *
176 * Return value: %TRUE for success, else %FALSE and @error set
177 *
178 * Since: 0.9.6
179 **/
180 gboolean
181 pk_offline_clear_results (GCancellable *cancellable, GError **error)
182 {
183 return pk_offline_clear_results_with_flags (PK_OFFLINE_FLAGS_INTERACTIVE, cancellable, error);
184 }
185
186 /**
187 * pk_offline_clear_results_with_flags:
188 * @flags: bit-or of #PkOfflineFlags
189 * @cancellable: A #GCancellable or %NULL
190 * @error: A #GError or %NULL
191 *
192 * Clears the last offline operation report, which may be success or failure.
193 * If the report does not exist then this method returns success.
194 *
195 * Return value: %TRUE for success, else %FALSE and @error set
196 *
197 * Since: 1.2.5
198 **/
199 gboolean
200 pk_offline_clear_results_with_flags (PkOfflineFlags flags, GCancellable *cancellable, GError **error)
201 {
202 g_autoptr(GDBusConnection) connection = NULL;
203 g_autoptr(GVariant) res = NULL;
204
205 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
206
207 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
208 if (connection == NULL)
209 return FALSE;
210 res = g_dbus_connection_call_sync (connection,
211 "org.freedesktop.PackageKit",
212 "/org/freedesktop/PackageKit",
213 "org.freedesktop.PackageKit.Offline",
214 "ClearResults",
215 NULL,
216 NULL,
217 pk_offline_flags_to_gdbus_call_flags (flags),
218 -1,
219 cancellable,
220 error);
221 if (res == NULL)
222 return FALSE;
223 return TRUE;
224 }
225
226 /**
227 * pk_offline_trigger:
228 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
229 * @cancellable: A #GCancellable or %NULL
230 * @error: A #GError or %NULL
231 *
232 * Triggers the offline update so that the next reboot will perform the
233 * pending transaction.
234 * The function always allows user interaction. To change the behavior,
235 * use pk_offline_trigger_with_flags().
236 *
237 * Return value: %TRUE for success, else %FALSE and @error set
238 *
239 * Since: 0.9.6
240 **/
241 gboolean
242 pk_offline_trigger (PkOfflineAction action, GCancellable *cancellable, GError **error)
243 {
244 return pk_offline_trigger_with_flags (action, PK_OFFLINE_FLAGS_INTERACTIVE, cancellable, error);
245 }
246
247 /**
248 * pk_offline_trigger_with_flags:
249 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
250 * @flags: bit-or of #PkOfflineFlags
251 * @cancellable: A #GCancellable or %NULL
252 * @error: A #GError or %NULL
253 *
254 * Triggers the offline update so that the next reboot will perform the
255 * pending transaction.
256 *
257 * Return value: %TRUE for success, else %FALSE and @error set
258 *
259 * Since: 1.2.5
260 **/
261 gboolean
262 pk_offline_trigger_with_flags (PkOfflineAction action, PkOfflineFlags flags, GCancellable *cancellable, GError **error)
263 {
264 const gchar *tmp;
265 g_autoptr(GDBusConnection) connection = NULL;
266 g_autoptr(GVariant) res = NULL;
267
268 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
269
270 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
271 if (connection == NULL)
272 return FALSE;
273 tmp = pk_offline_action_to_string (action);
274 res = g_dbus_connection_call_sync (connection,
275 "org.freedesktop.PackageKit",
276 "/org/freedesktop/PackageKit",
277 "org.freedesktop.PackageKit.Offline",
278 "Trigger",
279 g_variant_new ("(s)", tmp),
280 NULL,
281 pk_offline_flags_to_gdbus_call_flags (flags),
282 -1,
283 cancellable,
284 error);
285 if (res == NULL)
286 return FALSE;
287 return TRUE;
288 }
289
290 /**
291 * pk_offline_trigger_upgrade:
292 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
293 * @cancellable: A #GCancellable or %NULL
294 * @error: A #GError or %NULL
295 *
296 * Triggers the offline system upgrade so that the next reboot will perform the
297 * pending transaction.
298 * The function always allows user interaction. To change the behavior,
299 * use pk_offline_trigger_upgrade_with_flags().
300 *
301 * Return value: %TRUE for success, else %FALSE and @error set
302 *
303 * Since: 1.0.12
304 **/
305 gboolean
306 pk_offline_trigger_upgrade (PkOfflineAction action, GCancellable *cancellable, GError **error)
307 {
308 return pk_offline_trigger_upgrade_with_flags (action, PK_OFFLINE_FLAGS_INTERACTIVE, cancellable, error);
309 }
310
311 /**
312 * pk_offline_trigger_upgrade_with_flags:
313 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
314 * @flags: bit-or of #PkOfflineFlags
315 * @cancellable: A #GCancellable or %NULL
316 * @error: A #GError or %NULL
317 *
318 * Triggers the offline system upgrade so that the next reboot will perform the
319 * pending transaction.
320 *
321 * Return value: %TRUE for success, else %FALSE and @error set
322 *
323 * Since: 1.2.5
324 **/
325 gboolean
326 pk_offline_trigger_upgrade_with_flags (PkOfflineAction action, PkOfflineFlags flags, GCancellable *cancellable, GError **error)
327 {
328 const gchar *tmp;
329 g_autoptr(GDBusConnection) connection = NULL;
330 g_autoptr(GVariant) res = NULL;
331
332 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
333
334 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
335 if (connection == NULL)
336 return FALSE;
337 tmp = pk_offline_action_to_string (action);
338 res = g_dbus_connection_call_sync (connection,
339 "org.freedesktop.PackageKit",
340 "/org/freedesktop/PackageKit",
341 "org.freedesktop.PackageKit.Offline",
342 "TriggerUpgrade",
343 g_variant_new ("(s)", tmp),
344 NULL,
345 pk_offline_flags_to_gdbus_call_flags (flags),
346 -1,
347 cancellable,
348 error);
349 if (res == NULL)
350 return FALSE;
351 return TRUE;
352 }
353
354 /**
355 * pk_offline_get_action:
356 * @error: A #GError or %NULL
357 *
358 * Gets the action that will be taken after the offline action has completed.
359 *
360 * An error is set if the the value %PK_OFFLINE_ACTION_UNKNOWN is returned.
361 *
362 * Return value: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
363 *
364 * Since: 0.9.6
365 **/
366 PkOfflineAction
367 pk_offline_get_action (GError **error)
368 {
369 PkOfflineAction action;
370 g_autoptr(GError) error_local = NULL;
371 g_autofree gchar *action_data = NULL;
372
373 g_return_val_if_fail (error == NULL || *error == NULL, PK_OFFLINE_ACTION_UNKNOWN);
374
375 /* is the trigger set? */
376 if (!g_file_test (PK_OFFLINE_TRIGGER_FILENAME, G_FILE_TEST_EXISTS) ||
377 !g_file_test (PK_OFFLINE_ACTION_FILENAME, G_FILE_TEST_EXISTS))
378 return PK_OFFLINE_ACTION_UNSET;
379
380 /* read data file */
381 if (!g_file_get_contents (PK_OFFLINE_ACTION_FILENAME,
382 &action_data, NULL, &error_local)) {
383 g_set_error (error,
384 PK_OFFLINE_ERROR,
385 PK_OFFLINE_ERROR_FAILED,
386 "Failed to open %s: %s",
387 PK_OFFLINE_ACTION_FILENAME,
388 error_local->message);
389 return PK_OFFLINE_ACTION_UNKNOWN;
390 }
391 action = pk_offline_action_from_string (action_data);
392 if (action == PK_OFFLINE_ACTION_UNKNOWN) {
393 g_set_error (error,
394 PK_OFFLINE_ERROR,
395 PK_OFFLINE_ERROR_INVALID_VALUE,
396 "Failed to parse '%s'", action_data);
397 }
398 return action;
399 }
400
401 /**
402 * pk_offline_get_prepared_sack:
403 * @error: A #GError or %NULL
404 *
405 * Gets a package sack of the packages in the prepared transaction.
406 *
407 * Return value: (transfer full): A new #PkPackageSack, or %NULL
408 *
409 * Since: 0.9.6
410 **/
411 PkPackageSack *
412 pk_offline_get_prepared_sack (GError **error)
413 {
414 guint i;
415 g_autoptr(PkPackageSack) sack = NULL;
416 g_auto(GStrv) package_ids = NULL;
417
418 /* get the list of packages */
419 package_ids = pk_offline_get_prepared_ids (error);
420 if (package_ids == NULL)
421 return NULL;
422
423 /* add them to the new array */
424 sack = pk_package_sack_new ();
425 for (i = 0; package_ids[i] != NULL; i++) {
426 if (!pk_package_sack_add_package_by_id (sack,
427 package_ids[i],
428 error))
429 return NULL;
430 }
431 return g_object_ref (sack);
432 }
433
434 /**
435 * pk_offline_get_prepared_ids:
436 * @error: A #GError or %NULL
437 *
438 * Gets the package-ids in the prepared transaction.
439 *
440 * Return value: (transfer full): array of package-ids, or %NULL
441 *
442 * Since: 0.9.6
443 **/
444 gchar **
445 pk_offline_get_prepared_ids (GError **error)
446 {
447 g_autoptr(GError) error_local = NULL;
448 g_autofree gchar *data = NULL;
449 g_auto(GStrv) prepared_ids = NULL;
450 gsize prepared_ids_size;
451 g_autoptr(GKeyFile) keyfile = NULL;
452
453 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
454
455 /* does exist? */
456 if (!g_file_test (PK_OFFLINE_PREPARED_FILENAME, G_FILE_TEST_EXISTS)) {
457 g_set_error (error,
458 PK_OFFLINE_ERROR,
459 PK_OFFLINE_ERROR_NO_DATA,
460 "No offline updates have been prepared");
461 return NULL;
462 }
463
464 /* read data file */
465 if (!g_file_get_contents (PK_OFFLINE_PREPARED_FILENAME,
466 &data, NULL, &error_local)) {
467 g_set_error (error,
468 PK_OFFLINE_ERROR,
469 PK_OFFLINE_ERROR_FAILED,
470 "Failed to read %s: %s",
471 PK_OFFLINE_PREPARED_FILENAME,
472 error_local->message);
473 return NULL;
474 }
475
476 keyfile = g_key_file_new ();
477 g_key_file_set_list_separator (keyfile, ',');
478 if (!g_key_file_load_from_data (keyfile, data, -1, G_KEY_FILE_NONE, &error_local)) {
479 /* fall back to previous plain text file format for backwards compatibility */
480 return g_strsplit (data, "\n", -1);
481 }
482
483 prepared_ids = g_key_file_get_string_list (keyfile, "update", "prepared_ids", &prepared_ids_size, error);
484
485 if (prepared_ids == NULL || prepared_ids_size == 0)
486 return NULL;
487
488 return g_steal_pointer (&prepared_ids);
489 }
490
491 /**
492 * pk_offline_get_prepared_upgrade_name:
493 * @error: A #GError or %NULL
494 *
495 * Gets the name of the prepared system upgrade in the prepared transaction.
496 *
497 * Return value: the name, or %NULL if unset, free with g_free()
498 *
499 * Since: 1.1.2
500 **/
501 gchar *
502 pk_offline_get_prepared_upgrade_name (GError **error)
503 {
504 gchar *name = NULL;
505
506 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
507
508 if (!pk_offline_get_prepared_upgrade (&name, NULL, error))
509 return NULL;
510
511 return name;
512 }
513
514 /**
515 * pk_offline_get_prepared_upgrade_version:
516 * @error: A #GError or %NULL
517 *
518 * Gets the version of the prepared system upgrade in the prepared transaction.
519 *
520 * Return value: the version, or %NULL if unset, free with g_free()
521 *
522 * Since: 1.0.12
523 **/
524 gchar *
525 pk_offline_get_prepared_upgrade_version (GError **error)
526 {
527 gchar *version = NULL;
528
529 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
530
531 if (!pk_offline_get_prepared_upgrade (NULL, &version, error))
532 return NULL;
533
534 return version;
535 }
536
537 /**
538 * pk_offline_get_prepared_monitor:
539 * @cancellable: A #GCancellable or %NULL
540 * @error: A #GError or %NULL
541 *
542 * Gets a file monitor for the prepared transaction.
543 *
544 * Return value: (transfer full): A #GFileMonitor, or %NULL
545 *
546 * Since: 0.9.6
547 **/
548 GFileMonitor *
549 pk_offline_get_prepared_monitor (GCancellable *cancellable, GError **error)
550 {
551 g_autoptr(GFile) file = NULL;
552 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
553 file = g_file_new_for_path (PK_OFFLINE_PREPARED_FILENAME);
554 return g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, error);
555 }
556
557 /**
558 * pk_offline_get_prepared_upgrade_monitor:
559 * @cancellable: A #GCancellable or %NULL
560 * @error: A #GError or %NULL
561 *
562 * Gets a file monitor for the prepared system upgrade transaction.
563 *
564 * Return value: (transfer full): A #GFileMonitor, or %NULL
565 *
566 * Since: 1.0.12
567 **/
568 GFileMonitor *
569 pk_offline_get_prepared_upgrade_monitor (GCancellable *cancellable, GError **error)
570 {
571 g_autoptr(GFile) file = NULL;
572 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
573 file = g_file_new_for_path (PK_OFFLINE_PREPARED_UPGRADE_FILENAME);
574 return g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, error);
575 }
576
577 /**
578 * pk_offline_get_action_monitor:
579 * @cancellable: A #GCancellable or %NULL
580 * @error: A #GError or %NULL
581 *
582 * Gets a file monitor for the trigger.
583 *
584 * Return value: (transfer full): A #GFileMonitor, or %NULL
585 *
586 * Since: 0.9.6
587 **/
588 GFileMonitor *
589 pk_offline_get_action_monitor (GCancellable *cancellable, GError **error)
590 {
591 g_autoptr(GFile) file = NULL;
592 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
593 file = g_file_new_for_path (PK_OFFLINE_ACTION_FILENAME);
594 return g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, error);
595 }
596
597 /**
598 * pk_offline_get_results_mtime:
599 * @error: A #GError or %NULL
600 *
601 * Gets the modification time of the prepared transaction.
602 *
603 * Return value: a unix time, or 0 for error.
604 *
605 * Since: 0.9.6
606 **/
607 guint64
608 pk_offline_get_results_mtime (GError **error)
609 {
610 g_autoptr(GError) error_local = NULL;
611 g_autoptr(GFile) file = NULL;
612 g_autoptr(GFileInfo) info = NULL;
613
614 g_return_val_if_fail (error == NULL || *error == NULL, 0);
615
616 file = g_file_new_for_path (PK_OFFLINE_RESULTS_FILENAME);
617 info = g_file_query_info (file,
618 G_FILE_ATTRIBUTE_TIME_MODIFIED,
619 G_FILE_QUERY_INFO_NONE,
620 NULL,
621 &error_local);
622 if (info == NULL) {
623 if (g_error_matches (error_local, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
624 g_set_error (error,
625 PK_OFFLINE_ERROR,
626 PK_OFFLINE_ERROR_NO_DATA,
627 "%s does not exist",
628 PK_OFFLINE_RESULTS_FILENAME);
629 return 0;
630 }
631 g_set_error (error,
632 PK_OFFLINE_ERROR,
633 PK_OFFLINE_ERROR_FAILED,
634 "Failed to read %s: %s",
635 PK_OFFLINE_RESULTS_FILENAME,
636 error_local->message);
637 return 0;
638 }
639 return g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
640 }
641
642 /**
643 * pk_offline_get_results:
644 * @error: A #GError or %NULL
645 *
646 * Gets the last result of the offline transaction.
647 *
648 * Return value: (transfer full): A #PkResults, or %NULL
649 *
650 * Since: 0.9.6
651 **/
652 PkResults *
653 pk_offline_get_results (GError **error)
654 {
655 gboolean ret;
656 gboolean success;
657 g_autoptr(GError) error_local = NULL;
658 g_autofree gchar *role_str = NULL;
659 g_autoptr(GKeyFile) file = NULL;
660 g_autoptr(PkError) pk_error = NULL;
661 g_autoptr(PkResults) results = NULL;
662 g_auto(GStrv) package_ids = NULL;
663 gsize package_ids_size;
664
665 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
666
667 /* does not exist */
668 if (!g_file_test (PK_OFFLINE_RESULTS_FILENAME, G_FILE_TEST_EXISTS)) {
669 g_set_error_literal (error,
670 PK_OFFLINE_ERROR,
671 PK_OFFLINE_ERROR_NO_DATA,
672 "no update results available");
673 return NULL;
674 }
675
676 /* load data */
677 file = g_key_file_new ();
678 g_key_file_set_list_separator (file, ',');
679 ret = g_key_file_load_from_file (file,
680 PK_OFFLINE_RESULTS_FILENAME,
681 G_KEY_FILE_NONE,
682 &error_local);
683 if (!ret) {
684 g_set_error (error,
685 PK_OFFLINE_ERROR,
686 PK_OFFLINE_ERROR_FAILED,
687 "results file invalid: %s",
688 error_local->message);
689 return NULL;
690 }
691
692 /* add error */
693 results = pk_results_new ();
694 success = g_key_file_get_boolean (file, PK_OFFLINE_RESULTS_GROUP,
695 "Success", NULL);
696 if (!success) {
697 g_autofree gchar *details = NULL;
698 g_autofree gchar *enum_str = NULL;
699 pk_error = pk_error_new ();
700 enum_str = g_key_file_get_string (file,
701 PK_OFFLINE_RESULTS_GROUP,
702 "ErrorCode",
703 NULL);
704 details = g_key_file_get_string (file,
705 PK_OFFLINE_RESULTS_GROUP,
706 "ErrorDetails",
707 NULL);
708 g_object_set (pk_error,
709 "code", pk_error_enum_from_string (enum_str),
710 "details", details,
711 NULL);
712 pk_results_set_error_code (results, pk_error);
713 pk_results_set_exit_code (results, PK_EXIT_ENUM_FAILED);
714 } else {
715 pk_results_set_exit_code (results, PK_EXIT_ENUM_SUCCESS);
716 }
717
718 /* set role */
719 role_str = g_key_file_get_string (file,
720 PK_OFFLINE_RESULTS_GROUP,
721 "Role",
722 NULL);
723 if (role_str != NULL)
724 pk_results_set_role (results, pk_role_enum_from_string (role_str));
725
726 /* add packages */
727 package_ids = g_key_file_get_string_list (file, PK_OFFLINE_RESULTS_GROUP,
728 "Packages", &package_ids_size, NULL);
729 if (package_ids == NULL || package_ids_size == 0)
730 goto out;
731
732 for (guint i = 0; package_ids[i] != NULL; i++) {
733 g_autoptr(PkPackage) pkg = NULL;
734 pkg = pk_package_new ();
735 pk_package_set_info (pkg, PK_INFO_ENUM_UPDATING);
736 if (!pk_package_set_id (pkg, package_ids[i], error))
737 return NULL;
738 pk_results_add_package (results, pkg);
739 }
740 out:
741 return g_object_ref (results);
742 }