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



commit 83f3381c79e8643c7eee2b0e1b56bea96b6a5309
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 b06e4ae..bce46f6 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -407,7 +407,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))
@@ -429,6 +430,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;
 
@@ -451,6 +454,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;
@@ -473,7 +497,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 ());
@@ -495,6 +521,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;
@@ -509,6 +556,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]