From f5a6f5d0a87ea00c6c4710c57f0d470171927877 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 7 Sep 2012 13:11:50 +0100 Subject: [PATCH 4/7] Default account backend: restructure to be able to look in multiple places This isolates all file-writing into _commit, and all file-reading into am_default_load_keyfile. Signed-off-by: Simon McVittie --- src/mcd-account-manager-default.c | 49 ++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/mcd-account-manager-default.c b/src/mcd-account-manager-default.c index 603ecd8..e7b9f06 100644 --- a/src/mcd-account-manager-default.c +++ b/src/mcd-account-manager-default.c @@ -612,6 +612,8 @@ _commit (const McpAccountStorage *self, if (!amd->save) return TRUE; + DEBUG ("Saving accounts to %s", amd->filename); + if (!_have_config (amd)) _create_config (amd); @@ -625,6 +627,31 @@ _commit (const McpAccountStorage *self, return rval; } +static void +am_default_load_keyfile (McdAccountManagerDefault *self, + const gchar *filename) +{ + GError *error = NULL; + + if (g_key_file_load_from_file (self->keyfile, filename, + G_KEY_FILE_KEEP_COMMENTS, &error)) + { + DEBUG ("Loaded accounts from %s", filename); + } + else + { + DEBUG ("Failed to load accounts from %s: %s", filename, error->message); + g_error_free (error); + + /* Start with a blank configuration, but do not save straight away; + * we don't want to overwrite a corrupt-but-maybe-recoverable + * configuration file with an empty one until given a reason to + * do so. */ + g_key_file_load_from_data (self->keyfile, INITIAL_CONFIG, -1, + G_KEY_FILE_KEEP_COMMENTS, NULL); + } +} + static GList * _list (const McpAccountStorage *self, const McpAccountManager *am) @@ -635,12 +662,26 @@ _list (const McpAccountStorage *self, GList *rval = NULL; McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self); - if (!_have_config (amd)) - _create_config (amd); + if (!amd->loaded && g_file_test (amd->filename, G_FILE_TEST_EXISTS)) + { + /* If the file exists, but loading it fails, we deliberately + * do not fall through to the "initial configuration" case, + * because we don't want to overwrite a corrupted file + * with an empty one until an actual write takes place. */ + am_default_load_keyfile (amd, amd->filename); + amd->loaded = TRUE; + } if (!amd->loaded) - amd->loaded = g_key_file_load_from_file (amd->keyfile, amd->filename, - G_KEY_FILE_KEEP_COMMENTS, NULL); + { + DEBUG ("Creating initial account data"); + g_key_file_load_from_data (amd->keyfile, INITIAL_CONFIG, -1, + G_KEY_FILE_KEEP_COMMENTS, NULL); + amd->loaded = TRUE; + /* create the placeholder file */ + amd->save = TRUE; + _commit (self, am, NULL); + } accounts = g_key_file_get_groups (amd->keyfile, &n); -- 1.7.10.4