From f75f90145cf15b38df56df508c185573164efc6a Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 7 Sep 2012 13:06:41 +0100 Subject: [PATCH 5/7] Default account backend: include directory creation in _commit Also check for errors - we can at least give a useful warning. Signed-off-by: Simon McVittie --- src/mcd-account-manager-default.c | 49 ++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/mcd-account-manager-default.c b/src/mcd-account-manager-default.c index e7b9f06..dcf8c95 100644 --- a/src/mcd-account-manager-default.c +++ b/src/mcd-account-manager-default.c @@ -23,6 +23,7 @@ #include #include "mcd-account-manager-default.h" #include "mcd-debug.h" +#include "mcd-misc.h" #define PLUGIN_NAME "default-gkeyfile" #define PLUGIN_PRIORITY MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_DEFAULT @@ -380,25 +381,6 @@ mcd_account_manager_default_class_init (McdAccountManagerDefaultClass *cls) DEBUG ("mcd_account_manager_default_class_init"); } -static gboolean -_have_config (McdAccountManagerDefault *self) -{ - DEBUG ("checking for %s", self->filename); - return g_file_test (self->filename, G_FILE_TEST_EXISTS); -} - -static void -_create_config (McdAccountManagerDefault *self) -{ - gchar *dir = g_path_get_dirname (self->filename); - - DEBUG (""); - g_mkdir_with_parents (dir, 0700); - g_free (dir); - g_file_set_contents (self->filename, INITIAL_CONFIG, -1, NULL); - DEBUG ("created %s", self->filename); -} - /* We happen to know that the string MC gave us is "sufficiently escaped" to * put it in the keyfile as-is. */ static gboolean @@ -608,18 +590,39 @@ _commit (const McpAccountStorage *self, gchar *data; McdAccountManagerDefault *amd = MCD_ACCOUNT_MANAGER_DEFAULT (self); gboolean rval = FALSE; + gchar *dir; + GError *error = NULL; if (!amd->save) return TRUE; + dir = g_path_get_dirname (amd->filename); + DEBUG ("Saving accounts to %s", amd->filename); - if (!_have_config (amd)) - _create_config (amd); + if (!mcd_ensure_directory (dir, &error)) + { + g_warning ("%s", error->message); + g_error_free (error); + /* fall through anyway: writing to the file will fail, but it does + * give us a chance to commit to the keyring too */ + } + + g_free (dir); data = g_key_file_to_data (amd->keyfile, &n, NULL); - rval = g_file_set_contents (amd->filename, data, n, NULL); - amd->save = !rval; + rval = g_file_set_contents (amd->filename, data, n, &error); + + if (rval) + { + amd->save = FALSE; + } + else + { + g_warning ("%s", error->message); + g_error_free (error); + } + g_free (data); _keyring_commit (self, am, account); -- 1.7.10.4