Introduce a shared implementation for keyfile loading

Instead of copy&pasting the same code over again, use a common
implementation.

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This commit is contained in:
Steffen Jaeckel
2023-08-29 13:46:07 +02:00
parent b7f964fe64
commit ca2df180d8
13 changed files with 200 additions and 314 deletions

View File

@@ -55,6 +55,7 @@
#include "xmpp/capabilities.h"
static gchar* cache_loc;
static prof_keyfile_t caps_prof_keyfile;
static GKeyFile* cache;
static GHashTable* jid_to_ver;
@@ -72,14 +73,8 @@ void
caps_init(void)
{
log_info("Loading capabilities cache");
cache_loc = files_get_data_path(FILE_CAPSCACHE);
if (g_file_test(cache_loc, G_FILE_TEST_EXISTS)) {
g_chmod(cache_loc, S_IRUSR | S_IWUSR);
}
cache = g_key_file_new();
g_key_file_load_from_file(cache, cache_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
load_data_keyfile(&caps_prof_keyfile, FILE_CAPSCACHE);
cache = caps_prof_keyfile.keyfile;
jid_to_ver = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
jid_to_caps = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)caps_destroy);
@@ -347,7 +342,7 @@ caps_reset_ver(void)
void
caps_close(void)
{
g_key_file_free(cache);
free_keyfile(&caps_prof_keyfile);
cache = NULL;
g_hash_table_destroy(jid_to_ver);
g_hash_table_destroy(jid_to_caps);
@@ -456,8 +451,5 @@ caps_destroy(EntityCapabilities* caps)
static void
_save_cache(void)
{
gsize g_data_size;
auto_gchar gchar* g_cache_data = g_key_file_to_data(cache, &g_data_size, NULL);
g_file_set_contents(cache_loc, g_cache_data, g_data_size, NULL);
g_chmod(cache_loc, S_IRUSR | S_IWUSR);
save_keyfile(&caps_prof_keyfile);
}

View File

@@ -1102,31 +1102,18 @@ _xmpp_file_logger(void* const userdata, const xmpp_log_level_t xmpp_level, const
static void
_random_bytes_init(void)
{
auto_gchar gchar* rndbytes_loc = files_get_data_path(FILE_PROFANITY_IDENTIFIER);
GKeyFile* rndbytes;
prof_keyfile_t keyfile;
load_data_keyfile(&keyfile, FILE_PROFANITY_IDENTIFIER);
if (g_file_test(rndbytes_loc, G_FILE_TEST_EXISTS)) {
g_chmod(rndbytes_loc, S_IRUSR | S_IWUSR);
}
rndbytes = g_key_file_new();
g_key_file_load_from_file(rndbytes, rndbytes_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
if (g_key_file_has_group(rndbytes, "identifier")) {
profanity_instance_id = g_key_file_get_string(rndbytes, "identifier", "random_bytes", NULL);
if (g_key_file_has_group(keyfile.keyfile, "identifier")) {
profanity_instance_id = g_key_file_get_string(keyfile.keyfile, "identifier", "random_bytes", NULL);
} else {
profanity_instance_id = get_random_string(10);
g_key_file_set_string(rndbytes, "identifier", "random_bytes", profanity_instance_id);
gsize g_data_size;
auto_gchar gchar* g_accounts_data = g_key_file_to_data(rndbytes, &g_data_size, NULL);
auto_gchar gchar* base = g_path_get_dirname(rndbytes_loc);
auto_gchar gchar* true_loc = get_file_or_linked(rndbytes_loc, base);
g_file_set_contents(true_loc, g_accounts_data, g_data_size, NULL);
g_key_file_set_string(keyfile.keyfile, "identifier", "random_bytes", profanity_instance_id);
save_keyfile(&keyfile);
}
g_key_file_free(rndbytes);
free_keyfile(&keyfile);
}
static void