[gnome-software] Fix error reporting in gs_utils_get_permission()



commit 80ee3ba3c3fe6a60f336f9f013ed78965b6890de
Author: Richard Hughes <richard hughsie com>
Date:   Thu Oct 19 14:02:38 2017 +0100

    Fix error reporting in gs_utils_get_permission()
    
    Allow the caller to handle the error, sometimes g_warning isn't what we want.

 lib/gs-utils.c                                  |   16 ++++++++++------
 lib/gs-utils.h                                  |    4 +++-
 plugins/flatpak/gs-plugin-flatpak.c             |    7 +++++--
 plugins/packagekit/gs-plugin-packagekit-proxy.c |    6 ++++--
 plugins/packagekit/gs-plugin-systemd-updates.c  |    3 ++-
 5 files changed, 24 insertions(+), 12 deletions(-)
---
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index 1ba2f58..257adc6 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -258,26 +258,30 @@ gs_utils_get_user_hash (GError **error)
 /**
  * gs_utils_get_permission:
  * @id: A PolicyKit ID, e.g. "org.gnome.Desktop"
+ * @cancellable: A #GCancellable, or %NULL
+ * @error: A #GError, or %NULL
  *
  * Gets a permission object for an ID.
  *
  * Returns: a #GPermission, or %NULL if this if not possible.
  **/
 GPermission *
-gs_utils_get_permission (const gchar *id)
+gs_utils_get_permission (const gchar *id, GCancellable *cancellable, GError **error)
 {
 #ifdef HAVE_POLKIT
        g_autoptr(GPermission) permission = NULL;
-       g_autoptr(GError) error = NULL;
-
-       permission = polkit_permission_new_sync (id, NULL, NULL, &error);
+       permission = polkit_permission_new_sync (id, NULL, cancellable, error);
        if (permission == NULL) {
-               g_warning ("Failed to create permission %s: %s", id, error->message);
+               g_prefix_error (error, "failed to create permission %s: ", id);
+               gs_utils_error_convert_gio (error);
                return NULL;
        }
        return g_steal_pointer (&permission);
 #else
-       g_debug ("no PolicyKit, so can't return GPermission for %s", id);
+       g_set_error (error,
+                    GS_PLUGIN_ERROR,
+                    GS_PLUGIN_ERROR_NOT_SUPPORTED,
+                    "no PolicyKit, so can't return GPermission for %s", id);
        return NULL;
 #endif
 }
diff --git a/lib/gs-utils.h b/lib/gs-utils.h
index b40a0d7..383fda9 100644
--- a/lib/gs-utils.h
+++ b/lib/gs-utils.h
@@ -61,7 +61,9 @@ gchar         *gs_utils_get_cache_filename    (const gchar    *kind,
                                                 GsUtilsCacheFlags flags,
                                                 GError         **error);
 gchar          *gs_utils_get_user_hash         (GError         **error);
-GPermission    *gs_utils_get_permission        (const gchar    *id);
+GPermission    *gs_utils_get_permission        (const gchar    *id,
+                                                GCancellable   *cancellable,
+                                                GError         **error);
 gboolean        gs_utils_strv_fnmatch          (gchar          **strv,
                                                 const gchar    *str);
 GDesktopAppInfo *gs_utils_get_desktop_app_info (const gchar    *id);
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index cd1eec5..e47d2f2 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -48,6 +48,7 @@ gs_plugin_initialize (GsPlugin *plugin)
 {
        GsPluginData *priv = gs_plugin_alloc_data (plugin, sizeof(GsPluginData));
        const gchar *action_id = "org.freedesktop.Flatpak.appstream-update";
+       g_autoptr(GError) error_local = NULL;
        g_autoptr(GPermission) permission = NULL;
 
        priv->flatpaks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
@@ -70,8 +71,10 @@ gs_plugin_initialize (GsPlugin *plugin)
 
        /* if we can't update the AppStream database system-wide don't even
         * pull the data as we can't do anything with it */
-       permission = gs_utils_get_permission (action_id);
-       if (permission != NULL) {
+       permission = gs_utils_get_permission (action_id, NULL, &error_local);
+       if (permission == NULL) {
+               g_debug ("no permission for %s: %s", action_id, error_local->message);
+       } else {
                priv->has_system_helper = g_permission_get_allowed (permission) ||
                                          g_permission_get_can_acquire (permission);
        }
diff --git a/plugins/packagekit/gs-plugin-packagekit-proxy.c b/plugins/packagekit/gs-plugin-packagekit-proxy.c
index f326ca9..27b2a8c 100644
--- a/plugins/packagekit/gs-plugin-packagekit-proxy.c
+++ b/plugins/packagekit/gs-plugin-packagekit-proxy.c
@@ -234,13 +234,15 @@ reload_proxy_settings (GsPlugin *plugin, GCancellable *cancellable)
        g_autofree gchar *proxy_socks = NULL;
        g_autofree gchar *no_proxy = NULL;
        g_autofree gchar *pac = NULL;
+       g_autoptr(GError) error = NULL;
        g_autoptr(GPermission) permission = NULL;
 
        /* only if we can achieve the action *without* an auth dialog */
        permission = gs_utils_get_permission ("org.freedesktop.packagekit."
-                                             "system-network-proxy-configure");
+                                             "system-network-proxy-configure",
+                                             cancellable, &error);
        if (permission == NULL) {
-               g_debug ("not setting proxy as no permission");
+               g_debug ("not setting proxy as no permission: %s", error->message);
                return;
        }
        if (!g_permission_get_allowed (permission)) {
diff --git a/plugins/packagekit/gs-plugin-systemd-updates.c b/plugins/packagekit/gs-plugin-systemd-updates.c
index 57beae9..ff3b3f1 100644
--- a/plugins/packagekit/gs-plugin-systemd-updates.c
+++ b/plugins/packagekit/gs-plugin-systemd-updates.c
@@ -132,7 +132,8 @@ gs_plugin_setup (GsPlugin *plugin, GCancellable *cancellable, GError **error)
 
        /* check if we have permission to trigger the update */
        priv->permission = gs_utils_get_permission (
-               "org.freedesktop.packagekit.trigger-offline-update");
+               "org.freedesktop.packagekit.trigger-offline-update",
+               NULL, NULL);
        if (priv->permission != NULL) {
                g_signal_connect (priv->permission, "notify",
                                  G_CALLBACK (gs_plugin_systemd_updates_permission_cb),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]