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



commit 064ab19b59ec75e227272a87939f32ed180b867d
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 567666f..739c9cf 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -421,7 +421,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))
@@ -443,6 +444,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;
 
@@ -465,6 +468,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;
@@ -487,7 +511,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 ());
@@ -509,6 +535,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;
@@ -523,6 +570,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]