[gnome-online-accounts/gnome-3-18] utils: Avoid spurious key file updates



commit 003d5d156144b1b163cad1ca35c2e85a2fee5b33
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Jun 2 20:54:01 2016 +0200

    utils: Avoid spurious key file updates
    
    The gdbus-codegen-ed code doesn't use G_PARAM_EXPLICIT_NOTIFY. This
    causes spurious notifications which can lead to the key file being
    needlessly updated. Since The D-Bus objects are backed by the key
    file, and are updated every time the key file changes, this can cause
    an indefinite loop. Let's guard against that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=765994

 src/goabackend/goautils.c |   50 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 49 insertions(+), 1 deletions(-)
---
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index 6dbd53a..44a1c83 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -402,7 +402,8 @@ goa_utils_keyfile_remove_key (GoaAccount *account, const gchar *key)
       goto out;
     }
 
-  g_key_file_remove_key (key_file, group, key, NULL);
+  if (!g_key_file_remove_key (key_file, group, key, NULL))
+    goto out;
 
   error = NULL;
   if (!g_key_file_save_to_file (key_file, path, &error))
@@ -424,6 +425,8 @@ goa_utils_keyfile_set_boolean (GoaAccount *account, const gchar *key, gboolean v
 {
   GError *error;
   GKeyFile *key_file;
+  gboolean needs_update = FALSE;
+  gboolean old_value;
   gchar *group;
   gchar *path;
 
@@ -446,6 +449,27 @@ goa_utils_keyfile_set_boolean (GoaAccount *account, const gchar *key, gboolean v
       goto out;
     }
 
+  error = NULL;
+  old_value = g_key_file_get_boolean (key_file, group, key, &error);
+  if (error != NULL)
+    {
+      g_warning ("Error reading key %s from keyfile %s: %s (%s, %d)",
+                 key,
+                 path,
+                 error->message,
+                 g_quark_to_string (error->domain),
+                 error->code);
+      needs_update = TRUE;
+      g_error_free (error);
+    }
+  else if (old_value != value)
+    {
+      needs_update = TRUE;
+    }
+
+  if (!needs_update)
+    goto out;
+
   g_key_file_set_boolean (key_file, group, key, value);
 
   error = NULL;
@@ -468,7 +492,9 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
 {
   GError *error;
   GKeyFile *key_file;
+  gboolean needs_update = FALSE;
   gchar *group;
+  gchar *old_value = NULL;
   gchar *path;
 
   path = g_strdup_printf ("%s/goa-1.0/accounts.conf", g_get_user_config_dir ());
@@ -490,6 +516,27 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
       goto out;
     }
 
+  error = NULL;
+  old_value = g_key_file_get_string (key_file, group, key, &error);
+  if (error != NULL)
+    {
+      g_warning ("Error reading key %s from keyfile %s: %s (%s, %d)",
+                 key,
+                 path,
+                 error->message,
+                 g_quark_to_string (error->domain),
+                 error->code);
+      needs_update = TRUE;
+      g_error_free (error);
+    }
+  else if (g_strcmp0 (old_value, value) != 0)
+    {
+      needs_update = TRUE;
+    }
+
+  if (!needs_update)
+    goto out;
+
   g_key_file_set_string (key_file, group, key, value);
 
   error = NULL;
@@ -504,6 +551,7 @@ goa_utils_keyfile_set_string (GoaAccount *account, const gchar *key, const gchar
  out:
   g_key_file_free (key_file);
   g_free (group);
+  g_free (old_value);
   g_free (path);
 }
 


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