From be7c4f5a0051b61252001c5ae6cf46eed63b3bc0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 26 Oct 2014 20:43:05 +0000 Subject: [PATCH] Follow symlinks for profrc and accounts files --- src/common.c | 25 +++++++++++++++++++++++++ src/common.h | 2 ++ src/config/accounts.c | 9 ++++++++- src/config/preferences.c | 9 ++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/common.c b/src/common.c index 9fa53e72..8b0f186f 100644 --- a/src/common.c +++ b/src/common.c @@ -522,3 +522,28 @@ _data_callback(void *ptr, size_t size, size_t nmemb, void *data) return realsize; } + +char* +get_file_or_linked(char *loc, char *basedir) +{ + char *true_loc = NULL; + + // check for symlink + if (g_file_test(loc, G_FILE_TEST_IS_SYMLINK)) { + true_loc = g_file_read_link(loc, NULL); + + // if relative, add basedir + if (!g_str_has_prefix(true_loc, "/") && !g_str_has_prefix(true_loc, "~")) { + GString *base_str = g_string_new(basedir); + g_string_append(base_str, true_loc); + free(true_loc); + true_loc = base_str->str; + g_string_free(base_str, FALSE); + } + // use given location + } else { + true_loc = strdup(loc); + } + + return true_loc; +} diff --git a/src/common.h b/src/common.h index 49d6504f..6497e666 100644 --- a/src/common.h +++ b/src/common.h @@ -116,4 +116,6 @@ char * create_unique_id(char *prefix); int cmp_win_num(gconstpointer a, gconstpointer b); int get_next_available_win_num(GList *used); +char* get_file_or_linked(char *loc, char *basedir); + #endif diff --git a/src/config/accounts.c b/src/config/accounts.c index 0bea55b4..b5e27127 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -808,9 +808,16 @@ _save_accounts(void) { gsize g_data_size; gchar *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL); - g_file_set_contents(accounts_loc, g_accounts_data, g_data_size, NULL); + gchar *xdg_data = xdg_get_data_home(); + GString *base_str = g_string_new(xdg_data); + g_string_append(base_str, "/profanity/"); + gchar *true_loc = get_file_or_linked(accounts_loc, base_str->str); + g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL); g_chmod(accounts_loc, S_IRUSR | S_IWUSR); + g_free(xdg_data); + free(true_loc); g_free(g_accounts_data); + g_string_free(base_str, TRUE); } static gchar * diff --git a/src/config/preferences.c b/src/config/preferences.c index 63c226f6..9f084951 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -406,9 +406,16 @@ _save_prefs(void) { gsize g_data_size; gchar *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL); - g_file_set_contents(prefs_loc, g_prefs_data, g_data_size, NULL); + gchar *xdg_config = xdg_get_config_home(); + GString *base_str = g_string_new(xdg_config); + g_string_append(base_str, "/profanity/"); + gchar *true_loc = get_file_or_linked(prefs_loc, base_str->str); + g_file_set_contents(true_loc, g_prefs_data, g_data_size, NULL); g_chmod(prefs_loc, S_IRUSR | S_IWUSR); + g_free(xdg_config); + free(true_loc); g_free(g_prefs_data); + g_string_free(base_str, TRUE); } static gchar *