From 7729f3dcde01d2efd33ae277421185d048965a04 Mon Sep 17 00:00:00 2001 From: Jonny Lamb Date: Tue, 3 Jul 2012 15:37:43 +0100 Subject: account migrator: use XDG directories instead of ~/.mission-control Thanks for William Jon McCann for the original patch on fd.o#35896 for migrating the old accounts.cfg file to an XDG directory. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=35896 Signed-off-by: Jonny Lamb --- src/mcd-account-manager-gvariant.c | 18 +++- src/mcd-account-manager.c | 21 +--- src/mcd-account-migrator.c | 110 ++++++++++++++++++--- src/mcd-account.c | 15 +-- tests/twisted/Makefile.am | 2 +- tests/twisted/account-manager/avatar-persist.py | 6 +- tests/twisted/account-manager/avatar-refresh.py | 6 +- tests/twisted/account-manager/gvariant-accounts.py | 7 +- tests/twisted/account-manager/make-valid.py | 10 +- tests/twisted/account-manager/migrate-accounts.py | 12 ++- tests/twisted/crash-recovery/crash-recovery.py | 5 +- tests/twisted/dispatcher/create-at-startup.py | 6 +- 12 files changed, 149 insertions(+), 69 deletions(-) diff --git a/src/mcd-account-manager-gvariant.c b/src/mcd-account-manager-gvariant.c index 8068cd72..cf188a7d 100644 --- a/src/mcd-account-manager-gvariant.c +++ b/src/mcd-account-manager-gvariant.c @@ -20,12 +20,14 @@ #include "config.h" +#include #include #include #include "mcd-account-manager-gvariant.h" #include "mcd-debug.h" +#include "mcd-misc.h" #define PLUGIN_NAME "gvariant" #define PLUGIN_PRIORITY MCP_ACCOUNT_STORAGE_PLUGIN_PRIO_DEFAULT @@ -75,19 +77,24 @@ account_free (gpointer data) static void mcd_account_manager_gvariant_init (McdAccountManagerGVariant *self) { + gint ret; + DEBUG (""); self->loaded = FALSE; - /* TODO: use this instead of MC_ACCOUNT_DIR self->directory = g_build_filename ( g_get_user_config_dir (), "mission-control", "accounts", NULL); - */ - - /* TODO: this will only work in the test environment for now. */ - self->directory = g_build_filename (g_getenv ("MC_ACCOUNT_DIR"), NULL); self->accounts = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, account_free); + + if (!g_file_test (self->directory, G_FILE_TEST_EXISTS)) + { + ret = g_mkdir_with_parents (self->directory, 0700); + if (ret == -1) + g_warning ("Failed to create account directory: %s", + g_strerror (errno)); + } } static void @@ -286,6 +293,7 @@ _commit_one (McdAccountManagerGVariant *self, /* now save to disk */ str = g_variant_print (variant, TRUE); rval = g_file_set_contents (a->filename, str, -1, NULL); + _mcd_chmod_private (a->filename); /* clean up */ g_variant_unref (variant); diff --git a/src/mcd-account-manager.c b/src/mcd-account-manager.c index 45a50e68..f8c47bf4 100644 --- a/src/mcd-account-manager.c +++ b/src/mcd-account-manager.c @@ -1555,24 +1555,6 @@ mcd_account_manager_class_init (McdAccountManagerClass *klass) G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); } -static const gchar * -get_connections_cache_dir (void) -{ - const gchar *from_env = g_getenv ("MC_ACCOUNT_DIR"); - - if (from_env != NULL) - { - return from_env; - } - - if ((ACCOUNTS_CACHE_DIR)[0] != '\0') - { - return ACCOUNTS_CACHE_DIR; - } - - return g_get_user_cache_dir (); -} - static void mcd_account_manager_init (McdAccountManager *account_manager) { @@ -1597,7 +1579,8 @@ mcd_account_manager_init (McdAccountManager *account_manager) priv->accounts = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, unref_account); - priv->account_connections_dir = g_strdup (get_connections_cache_dir ()); + priv->account_connections_dir = g_build_filename (g_get_user_cache_dir (), + "mission-control", "accounts", NULL); priv->account_connections_file = g_build_filename (priv->account_connections_dir, ".mc_connections", NULL); diff --git a/src/mcd-account-migrator.c b/src/mcd-account-migrator.c index 9fa3fe18..5a1ae94b 100644 --- a/src/mcd-account-migrator.c +++ b/src/mcd-account-migrator.c @@ -21,11 +21,15 @@ #include "config.h" +#include #include #include +#include + #include "mcd-account-migrator.h" +#include "mcd-misc.h" typedef struct { @@ -74,6 +78,23 @@ get_account_conf_filename (const gchar *filename) return g_build_filename (base, filename, NULL); } +static gchar * +get_new_account_dir (void) +{ + return g_build_filename ( + g_get_user_config_dir (), "mission-control", "accounts", NULL); +} + +static void +create_new_account_dir (void) +{ + gchar *account_dir = get_new_account_dir (); + + g_mkdir_with_parents (account_dir, 0700); + + g_free (account_dir); +} + static GKeyFile * parse_old_accounts_cfg (void) { @@ -97,10 +118,66 @@ clear_accounts_cfg (void) /* TODO */ g_file_set_contents (filename, "# Accounts have been migrated", -1, NULL); + _mcd_chmod_private (filename); g_free (filename); } +/* man I can't wait until this file can be deleted */ +static void +move_everything_else (void) +{ + gchar *old_dir = get_account_conf_filename (NULL); + gchar *new_dir = get_new_account_dir (); + GFile *old = g_file_new_for_path (old_dir); + GFileEnumerator *enumerator; + static const gchar * const files_to_move[] = { + ".mc_connections", + NULL + }; + + enumerator = g_file_enumerate_children (old, + G_FILE_ATTRIBUTE_STANDARD_NAME, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + NULL, NULL); + + if (enumerator != NULL) + { + GFileInfo *info; + + while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) + { + const gchar *tmp = g_file_info_get_name (info); + gchar *old_filename; + gchar *new_filename; + gint ret; + + old_filename = g_build_filename (old_dir, tmp, NULL); + new_filename = g_build_filename (new_dir, tmp, NULL); + + if (!g_file_test (old_filename, G_FILE_TEST_IS_REGULAR) + || tp_strv_contains (files_to_move, tmp)) + { + ret = g_rename (old_filename, new_filename); + + if (ret == -1) + g_warning ("failed to move directory '%s': %s", + old_filename, g_strerror (errno)); + } + + g_free (old_filename); + g_free (new_filename); + g_object_unref (info); + } + + g_object_unref (enumerator); + } + + g_object_unref (old); + g_free (old_dir); + g_free (new_dir); +} + static gboolean migrate_one_account (gpointer user_data); /* it's not you it's me */ @@ -330,9 +407,11 @@ save_new_account_file (MigrateData *data, GVariant *variant) { gchar *str = g_variant_print (variant, TRUE); - gchar *account_dir = get_account_conf_filename (NULL); + gchar *account_dir = get_new_account_dir (); gchar *filename, *tmp; + g_mkdir_with_parents (account_dir, 0700); + tmp = escape_path (*(data->current_group)); filename = g_build_filename (account_dir, tmp, NULL); g_free (tmp); @@ -341,6 +420,9 @@ save_new_account_file (MigrateData *data, if (!g_file_test (filename, G_FILE_TEST_EXISTS)) g_file_set_contents (filename, str, -1, NULL); + _mcd_chmod_private (filename); + + g_free (filename); g_free (account_dir); g_free (str); } @@ -442,21 +524,23 @@ mcd_account_migrator_do_your_thing (void) data->dbus = tp_dbus_daemon_dup (NULL); data->keyfile = parse_old_accounts_cfg (); - if (data->keyfile == NULL) - goto out; - - data->groups = g_key_file_get_groups (data->keyfile, NULL); - data->current_group = data->groups; + if (data->keyfile != NULL) + { + data->groups = g_key_file_get_groups (data->keyfile, NULL); + data->current_group = data->groups; + } - if (data->current_group == NULL) - goto out; + if (data->current_group != NULL) + { + create_new_account_dir (); - g_idle_add (migrate_one_account, data); - g_main_loop_run (data->loop); + g_idle_add (migrate_one_account, data); + g_main_loop_run (data->loop); - /* I hope it worked lolololololol */ - clear_accounts_cfg (); + /* I hope it worked lolololololol */ + clear_accounts_cfg (); + move_everything_else (); + } -out: migrate_data_free (data); } diff --git a/src/mcd-account.c b/src/mcd-account.c index 644ad158..0252747c 100644 --- a/src/mcd-account.c +++ b/src/mcd-account.c @@ -663,19 +663,8 @@ load_manager (McdAccount *account) static gchar * get_account_data_path (McdAccountPrivate *priv) { - const gchar *base; - - base = g_getenv ("MC_ACCOUNT_DIR"); - if (!base) - base = ACCOUNTS_DIR; - if (!base) - return NULL; - - if (base[0] == '~') - return g_build_filename (g_get_home_dir(), base + 1, - priv->unique_name, NULL); - else - return g_build_filename (base, priv->unique_name, NULL); + return g_build_filename (g_get_user_config_dir (), + "mission-control", "accounts", priv->unique_name, NULL); } static void diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am index 69f3fe01..3f6fd836 100644 --- a/tests/twisted/Makefile.am +++ b/tests/twisted/Makefile.am @@ -10,7 +10,6 @@ TWISTED_BASIC_TESTS = \ account-manager/create-with-properties.py \ account-manager/enable-auto-connect.py \ account-manager/enable.py \ - account-manager/migrate-accounts.py \ account-manager/nickname.py \ account-manager/param-types.py \ account-manager/presence.py \ @@ -87,6 +86,7 @@ TWISTED_SEPARATE_TESTS = \ account-manager/device-idle.py \ account-manager/gvariant-accounts.py \ account-manager/make-valid.py \ + account-manager/migrate-accounts.py \ crash-recovery/crash-recovery.py \ dispatcher/create-at-startup.py diff --git a/tests/twisted/account-manager/avatar-persist.py b/tests/twisted/account-manager/avatar-persist.py index e3496335..0a7c1802 100644 --- a/tests/twisted/account-manager/avatar-persist.py +++ b/tests/twisted/account-manager/avatar-persist.py @@ -41,9 +41,11 @@ def preseed(): accounts_dir = os.environ['MC_ACCOUNT_DIR'] escaped = account_id.replace('/', '_') - filename = accounts_dir + '/' + escaped - account = open(filename, 'w') + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename)) + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, 'manager': <'fakecm'>, diff --git a/tests/twisted/account-manager/avatar-refresh.py b/tests/twisted/account-manager/avatar-refresh.py index 6792c4fa..5fa132e0 100644 --- a/tests/twisted/account-manager/avatar-refresh.py +++ b/tests/twisted/account-manager/avatar-refresh.py @@ -41,9 +41,11 @@ def preseed(): accounts_dir = os.environ['MC_ACCOUNT_DIR'] escaped = account_id.replace('/', '_') - filename = accounts_dir + '/' + escaped - account = open(filename, 'w') + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename)) + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, 'manager': <'fakecm'>, diff --git a/tests/twisted/account-manager/gvariant-accounts.py b/tests/twisted/account-manager/gvariant-accounts.py index 50c05cf7..6666e186 100644 --- a/tests/twisted/account-manager/gvariant-accounts.py +++ b/tests/twisted/account-manager/gvariant-accounts.py @@ -34,7 +34,10 @@ def preseed(): accounts_dir = os.environ['MC_ACCOUNT_DIR'] escaped = account1_id.replace('/', '_') - filename = accounts_dir + '/' + escaped + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename), 0700) + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, @@ -114,7 +117,7 @@ def test(q, bus, unused): accounts_dir = os.environ['MC_ACCOUNT_DIR'] name = 'fakecm/fakeprotocol/dontdivert_40bar_2ecom0' - second_filename = os.path.join(accounts_dir, name.replace('/', '_')) + second_filename = accounts_dir + '/mission-control/accounts/' + name.replace('/', '_') assert not os.path.exists(second_filename) path = am.CreateAccount('fakecm', 'fakeprotocol', 'Display name', parameters, {}) diff --git a/tests/twisted/account-manager/make-valid.py b/tests/twisted/account-manager/make-valid.py index 6023749d..a95650d4 100644 --- a/tests/twisted/account-manager/make-valid.py +++ b/tests/twisted/account-manager/make-valid.py @@ -43,9 +43,11 @@ def preseed(): # The passwords are missing, so the accounts can't connect yet. escaped = account1_id.replace('/', '_') - filename = accounts_dir + '/' + escaped - account = open(filename, 'w') + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename)) + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, 'manager': <'fakecm'>, @@ -64,9 +66,9 @@ def preseed(): account.close() escaped = account2_id.replace('/', '_') - filename = accounts_dir + '/' + escaped - account = open(filename, 'w') + filename = accounts_dir + '/mission-control/accounts/' + escaped + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, 'manager': <'fakecm'>, diff --git a/tests/twisted/account-manager/migrate-accounts.py b/tests/twisted/account-manager/migrate-accounts.py index 0fd7f001..314ca862 100644 --- a/tests/twisted/account-manager/migrate-accounts.py +++ b/tests/twisted/account-manager/migrate-accounts.py @@ -32,9 +32,9 @@ account_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint' def preseed(): - accounts_dir = os.environ['MC_ACCOUNT_DIR'] + old_accounts_dir = os.environ['MC_ACCOUNT_DIR'] - accounts_cfg = open(accounts_dir + '/accounts.cfg', 'w') + accounts_cfg = open(old_accounts_dir + '/accounts.cfg', 'w') accounts_cfg.write(r"""# Telepathy accounts [%s] @@ -83,11 +83,13 @@ def test(q, bus, unused): assertEquals([], props['Supersedes']) assertEquals(True, props['Valid']) - accounts_dir = os.environ['MC_ACCOUNT_DIR'] - accounts = keyfile_read(accounts_dir + '/accounts.cfg') + old_accounts_dir = os.environ['MC_ACCOUNT_DIR'] + new_accounts_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], + 'mission-control', 'accounts') + accounts = keyfile_read(old_accounts_dir + '/accounts.cfg') assertEquals({None: {}}, accounts) - filename = os.path.join(accounts_dir, account_id.replace('/', '_')) + filename = os.path.join(new_accounts_dir, account_id.replace('/', '_')) assert os.path.exists(filename) s = file(filename).read() diff --git a/tests/twisted/crash-recovery/crash-recovery.py b/tests/twisted/crash-recovery/crash-recovery.py index 0f831d4d..22621e5c 100644 --- a/tests/twisted/crash-recovery/crash-recovery.py +++ b/tests/twisted/crash-recovery/crash-recovery.py @@ -37,7 +37,10 @@ def preseed(): account_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint' escaped = account_id.replace('/', '_') - filename = accounts_dir + '/' + escaped + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename)) + account = open(filename, 'w') # As a regression test for part of fd.o #28557, the password starts and diff --git a/tests/twisted/dispatcher/create-at-startup.py b/tests/twisted/dispatcher/create-at-startup.py index 8c1ff278..3cf348f7 100644 --- a/tests/twisted/dispatcher/create-at-startup.py +++ b/tests/twisted/dispatcher/create-at-startup.py @@ -38,9 +38,11 @@ def preseed(): account_id = 'fakecm/fakeprotocol/jc_2edenton_40unatco_2eint' escaped = account_id.replace('/', '_') - filename = accounts_dir + '/' + escaped - account = open(filename, 'w') + filename = accounts_dir + '/mission-control/accounts/' + escaped + + os.makedirs(os.path.dirname(filename)) + account = open(filename, 'w') account.write("""{ 'id': <'%s'>, 'manager': <'fakecm'>, -- cgit v1.2.3