From 296a3ff2d5b0e5a6a2fbc0a577d98629b869e4cb Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 15 Nov 2013 15:00:56 +0000 Subject: McdAccountManagerDefault: track whether saving is needed correctly _commit (self, am, "just/one/account") would reset the global "dirty" flag, even if another account also needed writing out. I don't think this could actually happen in practice, but it seems best to fix it. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=27727 --- src/mcd-account-manager-default.c | 44 ++++++++++++++++++++++++++------------- src/mcd-account-manager-default.h | 1 - 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/mcd-account-manager-default.c b/src/mcd-account-manager-default.c index 0b9acc64..e42d1675 100644 --- a/src/mcd-account-manager-default.c +++ b/src/mcd-account-manager-default.c @@ -50,6 +50,8 @@ typedef struct { /* TRUE if the account doesn't really exist, but is here to stop us * loading it from a lower-priority file */ gboolean absent; + /* TRUE if this account needs saving */ + gboolean dirty; } McdDefaultStoredAccount; static GVariant * @@ -159,7 +161,6 @@ mcd_account_manager_default_init (McdAccountManagerDefault *self) self->directory = account_directory_in (g_get_user_data_dir ()); self->accounts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, stored_account_free); - self->save = FALSE; self->loaded = FALSE; } @@ -230,7 +231,7 @@ set_parameter (McpAccountStorage *self, g_variant_ref (val)); } - amd->save = TRUE; + sa->dirty = TRUE; return MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED; } @@ -269,7 +270,7 @@ set_attribute (McpAccountStorage *self, g_variant_ref (val)); } - amd->save = TRUE; + sa->dirty = TRUE; return MCP_ACCOUNT_STORAGE_SET_RESULT_CHANGED; } @@ -456,6 +457,9 @@ am_default_commit_one (McdAccountManagerDefault *self, gboolean ret; GError *error = NULL; + if (!sa->dirty) + return TRUE; + filename = account_file_in (g_get_user_data_dir (), account_name); DEBUG ("Saving account %s to %s", account_name, filename); @@ -498,6 +502,7 @@ am_default_commit_one (McdAccountManagerDefault *self, if (g_file_set_contents (filename, content_text, -1, &error)) { + sa->dirty = FALSE; ret = TRUE; } else @@ -524,9 +529,6 @@ _commit (const McpAccountStorage *self, GHashTableIter outer; gpointer account_p, sa_p; - if (!amd->save) - return TRUE; - DEBUG ("Saving accounts to %s", amd->directory); if (!mcd_ensure_directory (amd->directory, &error)) @@ -553,11 +555,6 @@ _commit (const McpAccountStorage *self, } } - if (all_succeeded) - { - amd->save = FALSE; - } - return all_succeeded; } @@ -605,7 +602,7 @@ am_default_load_keyfile (McdAccountManagerDefault *self, GStrv keys = g_key_file_get_keys (keyfile, account, &m, NULL); /* We're going to need to migrate this account. */ - self->save = TRUE; + sa->dirty = TRUE; for (j = 0; j < m; j++) { @@ -853,6 +850,7 @@ _list (const McpAccountStorage *self, GHashTableIter hash_iter; gchar *migrate_from = NULL; gpointer k, v; + gboolean save = FALSE; if (!amd->loaded) { @@ -928,7 +926,7 @@ _list (const McpAccountStorage *self, if (!am_default_load_keyfile (amd, migrate_from)) tp_clear_pointer (&migrate_from, g_free); amd->loaded = TRUE; - amd->save = TRUE; + save = TRUE; } else { @@ -940,10 +938,26 @@ _list (const McpAccountStorage *self, { DEBUG ("Creating initial account data"); amd->loaded = TRUE; - amd->save = TRUE; + save = TRUE; + } + + if (!save) + { + g_hash_table_iter_init (&hash_iter, amd->accounts); + + while (g_hash_table_iter_next (&hash_iter, NULL, &v)) + { + McdDefaultStoredAccount *sa = v; + + if (sa->dirty) + { + save = TRUE; + break; + } + } } - if (amd->save) + if (save) { DEBUG ("Saving initial or migrated account data"); diff --git a/src/mcd-account-manager-default.h b/src/mcd-account-manager-default.h index e16e062f..fb2cb75e 100644 --- a/src/mcd-account-manager-default.h +++ b/src/mcd-account-manager-default.h @@ -51,7 +51,6 @@ typedef struct { GObject parent; GHashTable *accounts; gchar *directory; - gboolean save; gboolean loaded; } _McdAccountManagerDefault; -- cgit v1.2.3