Debian Codegraph @

lib/packagekit-glib2/pk-offline-private.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 <glib/gstdio.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #include "pk-offline.h"
30 #include "pk-offline-private.h"
31
32 /*
33 * pk_offline_auth_set_action:
34 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
35 * @error: A #GError or %NULL
36 *
37 * Sets the action to be done after the offline action has been performed.
38 *
39 * Return value: %TRUE for success, else %FALSE and @error set
40 *
41 * Since: 0.9.6
42 **/
43 gboolean
44 pk_offline_auth_set_action (PkOfflineAction action, GError **error)
45 {
46 const gchar *action_str;
47 g_autoptr(GError) error_local = NULL;
48
49 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
50
51 if (action == PK_OFFLINE_ACTION_UNKNOWN) {
52 g_set_error (error,
53 PK_OFFLINE_ERROR,
54 PK_OFFLINE_ERROR_INVALID_VALUE,
55 "Failed to set unknown %i", action);
56 return FALSE;
57 }
58 if (action == PK_OFFLINE_ACTION_UNSET)
59 return pk_offline_auth_cancel (error);
60
61 action_str = pk_offline_action_to_string (action);
62 if (action_str == NULL) {
63 g_set_error (error,
64 PK_OFFLINE_ERROR,
65 PK_OFFLINE_ERROR_FAILED,
66 "Failed to convert %i", action);
67 return FALSE;
68 }
69 if (!g_file_set_contents (PK_OFFLINE_ACTION_FILENAME,
70 action_str, -1, &error_local)) {
71 g_set_error (error,
72 PK_OFFLINE_ERROR,
73 PK_OFFLINE_ERROR_FAILED,
74 "failed to write file: %s",
75 error_local->message);
76 return FALSE;
77 }
78 return TRUE;
79 }
80
81 /*
82 * pk_offline_auth_cancel:
83 * @error: A #GError or %NULL
84 *
85 * Cancels the offline operation that has been scheduled. If there is no
86 * scheduled offline operation then this method returns with success.
87 *
88 * Return value: %TRUE for success, else %FALSE and @error set
89 *
90 * Since: 0.9.6
91 **/
92 gboolean
93 pk_offline_auth_cancel (GError **error)
94 {
95 g_autoptr(GError) error_local = NULL;
96 g_autoptr(GFile) file1 = NULL;
97 g_autoptr(GFile) file2 = NULL;
98
99 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
100
101 file1 = g_file_new_for_path (PK_OFFLINE_TRIGGER_FILENAME);
102 if (!g_file_query_exists (file1, NULL))
103 return TRUE;
104 if (!g_file_delete (file1, NULL, &error_local)) {
105 g_set_error (error,
106 PK_OFFLINE_ERROR,
107 PK_OFFLINE_ERROR_FAILED,
108 "Cannot delete %s: %s",
109 PK_OFFLINE_TRIGGER_FILENAME,
110 error_local->message);
111 return FALSE;
112 }
113
114 file2 = g_file_new_for_path (PK_OFFLINE_ACTION_FILENAME);
115 if (g_file_query_exists (file2, NULL) &&
116 !g_file_delete (file2, NULL, &error_local)) {
117 g_set_error (error,
118 PK_OFFLINE_ERROR,
119 PK_OFFLINE_ERROR_FAILED,
120 "Cannot delete %s: %s",
121 PK_OFFLINE_ACTION_FILENAME,
122 error_local->message);
123 return FALSE;
124 }
125
126 g_debug ("Offline update cancelled");
127 return TRUE;
128 }
129
130 /*
131 * pk_offline_auth_clear_results:
132 * @error: A #GError or %NULL
133 *
134 * Creates the last offline operation report, which may be success or failure.
135 * If the report does not exist then this method returns success.
136 *
137 * Return value: %TRUE for success, else %FALSE and @error set
138 *
139 * Since: 0.9.6
140 **/
141 gboolean
142 pk_offline_auth_clear_results (GError **error)
143 {
144 g_autoptr(GError) error_local = NULL;
145 g_autoptr(GFile) file = NULL;
146
147 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
148
149 /* not existing is success */
150 if (!g_file_test (PK_OFFLINE_RESULTS_FILENAME, G_FILE_TEST_EXISTS))
151 return TRUE;
152
153 file = g_file_new_for_path (PK_OFFLINE_RESULTS_FILENAME);
154 if (!g_file_delete (file, NULL, &error_local)) {
155 g_set_error (error,
156 PK_OFFLINE_ERROR,
157 PK_OFFLINE_ERROR_FAILED,
158 "Cannot delete %s: %s",
159 PK_OFFLINE_RESULTS_FILENAME,
160 error_local->message);
161 return FALSE;
162 }
163 return TRUE;
164 }
165
166 /*
167 * pk_offline_auth_invalidate:
168 * @error: A #GError or %NULL
169 *
170 * Invalidates the offline operation. This is normally done when the package
171 * cache has been refreshed, or a package listed in the prepared transaction
172 * is manually installed or removed.
173 *
174 * Return value: %TRUE for success, else %FALSE and @error set
175 *
176 * Since: 0.9.6
177 **/
178 gboolean
179 pk_offline_auth_invalidate (GError **error)
180 {
181 g_autoptr(GError) error_local = NULL;
182 g_autoptr(GFile) file1 = NULL;
183 g_autoptr(GFile) file2 = NULL;
184
185 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
186
187 /* cancel the pending update */
188 if (!pk_offline_auth_cancel (error))
189 return FALSE;
190
191 /* delete the prepared file */
192 file1 = g_file_new_for_path (PK_OFFLINE_PREPARED_FILENAME);
193 if (g_file_query_exists (file1, NULL) &&
194 !g_file_delete (file1, NULL, &error_local)) {
195 g_set_error (error,
196 PK_OFFLINE_ERROR,
197 PK_OFFLINE_ERROR_FAILED,
198 "Cannot delete %s: %s",
199 PK_OFFLINE_PREPARED_FILENAME,
200 error_local->message);
201 return FALSE;
202 }
203
204 /* delete the prepared system upgrade file */
205 file2 = g_file_new_for_path (PK_OFFLINE_PREPARED_UPGRADE_FILENAME);
206 if (g_file_query_exists (file2, NULL) &&
207 !g_file_delete (file2, NULL, &error_local)) {
208 g_set_error (error,
209 PK_OFFLINE_ERROR,
210 PK_OFFLINE_ERROR_FAILED,
211 "Cannot delete %s: %s",
212 PK_OFFLINE_PREPARED_UPGRADE_FILENAME,
213 error_local->message);
214 return FALSE;
215 }
216
217 return TRUE;
218 }
219
220 static gboolean
221 pk_offline_auth_trigger_prepared_file (PkOfflineAction action, const gchar *prepared_file, GError **error)
222 {
223 gint rc;
224
225 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
226
227 /* check the prepared update exists */
228 if (!g_file_test (prepared_file, G_FILE_TEST_EXISTS)) {
229 g_set_error (error,
230 PK_OFFLINE_ERROR,
231 PK_OFFLINE_ERROR_NO_DATA,
232 "Prepared update not found: %s",
233 prepared_file);
234 return FALSE;
235 }
236
237 /* triggering a new update clears the status from any previous one */
238 if (!pk_offline_auth_clear_results (error))
239 return FALSE;
240
241 /* set the action type */
242 if (!pk_offline_auth_set_action (action, error))
243 return FALSE;
244
245 /* delete any existing triggers we might have */
246 g_unlink (PK_OFFLINE_TRIGGER_FILENAME);
247
248 /* create symlink for the systemd-system-update-generator */
249 rc = symlink (prepared_file, PK_OFFLINE_TRIGGER_FILENAME);
250 if (rc < 0) {
251 g_set_error (error,
252 PK_OFFLINE_ERROR,
253 PK_OFFLINE_ERROR_FAILED,
254 "Failed to create symlink: %s",
255 strerror (errno));
256 return FALSE;
257 }
258 return TRUE;
259 }
260
261 /*
262 * pk_offline_auth_trigger:
263 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
264 * @error: A #GError or %NULL
265 *
266 * Triggers the offline update so that the next reboot will perform the
267 * pending transaction.
268 *
269 * Return value: %TRUE for success, else %FALSE and @error set
270 *
271 * Since: 0.9.6
272 **/
273 gboolean
274 pk_offline_auth_trigger (PkOfflineAction action, GError **error)
275 {
276 return pk_offline_auth_trigger_prepared_file (action, PK_OFFLINE_PREPARED_FILENAME, error);
277 }
278
279 /*
280 * pk_offline_auth_trigger_upgrade:
281 * @action: a #PkOfflineAction, e.g. %PK_OFFLINE_ACTION_REBOOT
282 * @error: A #GError or %NULL
283 *
284 * Triggers the offline system upgrade so that the next reboot will perform the
285 * pending transaction.
286 *
287 * Return value: %TRUE for success, else %FALSE and @error set
288 *
289 * Since: 1.0.12
290 **/
291 gboolean
292 pk_offline_auth_trigger_upgrade (PkOfflineAction action, GError **error)
293 {
294 return pk_offline_auth_trigger_prepared_file (action, PK_OFFLINE_PREPARED_UPGRADE_FILENAME, error);
295 }
296
297 /*
298 * pk_offline_auth_set_prepared_ids:
299 * @package_ids: Array of package-ids
300 * @error: A #GError or %NULL
301 *
302 * Saves the package-ids to a prepared transaction file.
303 *
304 * Return value: %TRUE for success, else %FALSE and @error set
305 *
306 * Since: 0.9.6
307 **/
308 gboolean
309 pk_offline_auth_set_prepared_ids (gchar **package_ids, GError **error)
310 {
311 g_autoptr(GKeyFile) keyfile = NULL;
312
313 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
314
315 keyfile = g_key_file_new ();
316 g_key_file_set_list_separator (keyfile, ',');
317 g_key_file_set_string_list (keyfile,
318 "update",
319 "prepared_ids",
320 (const gchar**)package_ids,
321 g_strv_length (package_ids));
322 return g_key_file_save_to_file (keyfile, PK_OFFLINE_PREPARED_FILENAME, error);
323 }
324
325 /*
326 * pk_offline_auth_set_prepared_upgrade:
327 * @name: Distro name to upgrade to
328 * @release_ver: Distro version to upgrade to
329 * @error: A #GError or %NULL
330 *
331 * Saves the distro name and version to upgrade to a prepared transaction file.
332 *
333 * Return value: %TRUE for success, else %FALSE and @error set
334 *
335 * Since: 1.1.2
336 **/
337 gboolean
338 pk_offline_auth_set_prepared_upgrade (const gchar *name, const gchar *release_ver, GError **error)
339 {
340 g_autoptr(GKeyFile) keyfile = NULL;
341
342 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
343
344 keyfile = g_key_file_new ();
345 g_key_file_set_string (keyfile, "update", "name", name);
346 g_key_file_set_string (keyfile, "update", "releasever", release_ver);
347 return g_key_file_save_to_file (keyfile, PK_OFFLINE_PREPARED_UPGRADE_FILENAME, error);
348 }
349
350 /*
351 * pk_offline_get_prepared_upgrade:
352 * @name: (out): Return location for the distro name
353 * @release_ver: (out): Return location for the distro version
354 * @error: A #GError or %NULL
355 *
356 * Gets details about a prepared system upgrade transaction.
357 *
358 * Return value: %TRUE for success, else %FALSE and @error set
359 *
360 * Since: 1.1.2
361 **/
362 gboolean
363 pk_offline_get_prepared_upgrade (gchar **name, gchar **release_ver, GError **error)
364 {
365 g_autoptr(GError) error_local = NULL;
366 g_autofree gchar *data = NULL;
367 g_autoptr(GKeyFile) keyfile = NULL;
368
369 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
370
371 /* does exist? */
372 if (!g_file_test (PK_OFFLINE_PREPARED_UPGRADE_FILENAME, G_FILE_TEST_EXISTS)) {
373 g_set_error (error,
374 PK_OFFLINE_ERROR,
375 PK_OFFLINE_ERROR_NO_DATA,
376 "No offline system upgrades have been prepared");
377 return FALSE;
378 }
379
380 /* read data file */
381 if (!g_file_get_contents (PK_OFFLINE_PREPARED_UPGRADE_FILENAME,
382 &data, NULL, &error_local)) {
383 g_set_error (error,
384 PK_OFFLINE_ERROR,
385 PK_OFFLINE_ERROR_FAILED,
386 "Failed to read %s: %s",
387 PK_OFFLINE_PREPARED_UPGRADE_FILENAME,
388 error_local->message);
389 return FALSE;
390 }
391
392 keyfile = g_key_file_new ();
393 if (!g_key_file_load_from_data (keyfile, data, -1, G_KEY_FILE_NONE, error)) {
394 return FALSE;
395 }
396
397 if (name != NULL) {
398 *name = g_key_file_get_string (keyfile, "update", "name", error);
399 if (*name == NULL)
400 return FALSE;
401 }
402
403 if (release_ver != NULL) {
404 *release_ver = g_key_file_get_string (keyfile, "update", "releasever", error);
405 if (*release_ver == NULL)
406 return FALSE;
407 }
408
409 return TRUE;
410 }
411
412 /*
413 * pk_offline_auth_set_results:
414 * @results: A #PkResults
415 * @error: A #GError or %NULL
416 *
417 * Saves the transaction results to a file.
418 *
419 * Return value: %TRUE for success, else %FALSE and @error set
420 *
421 * Since: 0.9.6
422 **/
423 gboolean
424 pk_offline_auth_set_results (PkResults *results, GError **error)
425 {
426 PkPackage *package;
427 PkRoleEnum role;
428 g_autoptr(GError) error_local = NULL;
429 g_autofree gchar *data = NULL;
430 g_autoptr(GKeyFile) key_file = NULL;
431 g_autoptr(PkError) pk_error = NULL;
432 g_autoptr(GPtrArray) packages = NULL;
433
434 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
435
436 key_file = g_key_file_new ();
437 g_key_file_set_list_separator (key_file, ',');
438 pk_error = pk_results_get_error_code (results);
439 if (pk_error != NULL) {
440 g_key_file_set_boolean (key_file,
441 PK_OFFLINE_RESULTS_GROUP,
442 "Success",
443 FALSE);
444 g_key_file_set_string (key_file,
445 PK_OFFLINE_RESULTS_GROUP,
446 "ErrorCode",
447 pk_error_enum_to_string (pk_error_get_code (pk_error)));
448 g_key_file_set_string (key_file,
449 PK_OFFLINE_RESULTS_GROUP,
450 "ErrorDetails",
451 pk_error_get_details (pk_error));
452 } else {
453 g_key_file_set_boolean (key_file,
454 PK_OFFLINE_RESULTS_GROUP,
455 "Success",
456 TRUE);
457 }
458
459 /* save role */
460 role = pk_results_get_role (results);
461 if (role != PK_ROLE_ENUM_UNKNOWN) {
462 g_key_file_set_string (key_file,
463 PK_OFFLINE_RESULTS_GROUP,
464 "Role",
465 pk_role_enum_to_string (role));
466 }
467
468 /* save packages if any set */
469 packages = pk_results_get_package_array (results);
470 if (packages->len > 0) {
471 g_autoptr(GHashTable) known_pkgids = g_hash_table_new (g_str_hash, g_str_equal);
472 g_autoptr(GPtrArray) deduped_pkgids = NULL;
473
474 for (guint i = 0; i < packages->len; i++) {
475 const gchar *pkgid;
476 package = g_ptr_array_index (packages, i);
477 switch (pk_package_get_info (package)) {
478 case PK_INFO_ENUM_UPDATING:
479 case PK_INFO_ENUM_INSTALLING:
480 pkgid = pk_package_get_id (package);
481
482 /* deduplicate entries in case the backend has emitted them multiple times */
483 g_hash_table_add (known_pkgids, (gpointer) pkgid);
484 break;
485 default:
486 break;
487 }
488 }
489
490 deduped_pkgids = g_hash_table_steal_all_keys (known_pkgids);
491 g_key_file_set_string_list (key_file,
492 PK_OFFLINE_RESULTS_GROUP,
493 "Packages",
494 (const gchar**)deduped_pkgids->pdata,
495 deduped_pkgids->len);
496 }
497
498 /* write file */
499 data = g_key_file_to_data (key_file, NULL, &error_local);
500 if (data == NULL) {
501 g_set_error (error,
502 PK_OFFLINE_ERROR,
503 PK_OFFLINE_ERROR_FAILED,
504 "failed to get keyfile data: %s",
505 error_local->message);
506 return FALSE;
507 }
508 if (!g_file_set_contents (PK_OFFLINE_RESULTS_FILENAME,
509 data, -1, &error_local)) {
510 g_set_error (error,
511 PK_OFFLINE_ERROR,
512 PK_OFFLINE_ERROR_FAILED,
513 "failed to write file: %s",
514 error_local->message);
515 return FALSE;
516 }
517 return TRUE;
518 }