about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSteffen Jaeckel <jaeckel-floss@eyet-services.de>2022-03-13 11:58:56 +0100
committerSteffen Jaeckel <jaeckel-floss@eyet-services.de>2022-03-13 14:15:02 +0100
commitb8e46552bffc559263e86b2dcc0331b3f47065b7 (patch)
treeedd714bd6487ae39a3d6b7c77f799391b5071151
parent764a7fb71b6cc401ed77233bbcd4d67201f9ca85 (diff)
downloadprofani-tty-b8e46552bffc559263e86b2dcc0331b3f47065b7.tar.gz
add `files_file_in_account_data_path()`
As all parts of the code invoking the `files_get_account_data_path()`
function did the same afterwards, a function has been added with the same
behavior.

1. create path
2. `mkdir` of that path
3. return final path

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
-rw-r--r--src/config/files.c21
-rw-r--r--src/config/files.h3
-rw-r--r--src/database.c22
-rw-r--r--src/omemo/omemo.c17
-rw-r--r--src/otr/otr.c36
-rw-r--r--src/pgp/gpg.c24
6 files changed, 52 insertions, 71 deletions
diff --git a/src/config/files.c b/src/config/files.c
index ce3f1196..8a241be5 100644
--- a/src/config/files.c
+++ b/src/config/files.c
@@ -38,6 +38,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
 #include <glib.h>
 
 #include "common.h"
@@ -191,6 +193,25 @@ files_get_account_data_path(const char* const specific_dir, const char* const ji
     return result;
 }
 
+gchar*
+files_file_in_account_data_path(const char* const specific_dir, const char* const jid, const char* const file_name)
+{
+    gchar* data_path = files_get_account_data_path(specific_dir, jid);
+
+    if (g_mkdir_with_parents(data_path, S_IRWXU) != 0) {
+        log_error("Failed to create directory at '%s' with error '%s'", data_path, strerror(errno));
+        g_free(data_path);
+        return NULL;
+    }
+
+    if (!file_name) {
+        return data_path;
+    }
+    gchar* filename = g_strdup_printf("%s/%s", data_path, file_name);
+    g_free(data_path);
+    return filename;
+}
+
 static char*
 _files_get_xdg_config_home(void)
 {
diff --git a/src/config/files.h b/src/config/files.h
index a6b5a730..c0000ce1 100644
--- a/src/config/files.h
+++ b/src/config/files.h
@@ -69,4 +69,7 @@ gchar* files_get_account_data_path(const char* const specific_dir, const char* c
 gchar* files_get_log_file(const char* const log_file);
 gchar* files_get_inputrc_file(void);
 
+gchar*
+files_file_in_account_data_path(const char* const specific_dir, const char* const jid, const char* const file_name);
+
 #endif
diff --git a/src/database.c b/src/database.c
index c04dcff9..4656ae79 100644
--- a/src/database.c
+++ b/src/database.c
@@ -56,27 +56,7 @@ static prof_msg_type_t _get_message_type_type(const char* const type);
 static char*
 _get_db_filename(ProfAccount* account)
 {
-    gchar* database_dir = files_get_account_data_path(DIR_DATABASE, account->jid);
-
-    int res = g_mkdir_with_parents(database_dir, S_IRWXU);
-    if (res == -1) {
-        const char* errmsg = strerror(errno);
-        if (errmsg) {
-            log_error("DATABASE: error creating directory: %s, %s", database_dir, errmsg);
-        } else {
-            log_error("DATABASE: creating directory: %s", database_dir);
-        }
-        g_free(database_dir);
-        return NULL;
-    }
-
-    GString* chatlog_filename = g_string_new(database_dir);
-    g_string_append(chatlog_filename, "/chatlog.db");
-    gchar* result = g_strdup(chatlog_filename->str);
-    g_string_free(chatlog_filename, TRUE);
-    g_free(database_dir);
-
-    return result;
+    return files_file_in_account_data_path(DIR_DATABASE, account->jid, "chatlog.db");
 }
 
 gboolean
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 87599de0..c46714d6 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -231,7 +231,11 @@ omemo_on_connect(ProfAccount* account)
     omemo_ctx.device_list_handler = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
     omemo_ctx.known_devices = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_g_hash_table_free);
 
-    gchar* omemo_dir = files_get_account_data_path(DIR_OMEMO, account->jid);
+    gchar* omemo_dir = files_file_in_account_data_path(DIR_OMEMO, account->jid, NULL);
+    if (!omemo_dir) {
+        log_error("[OMEMO] failed creating directory");
+        return;
+    }
 
     omemo_ctx.identity_filename = g_string_new(omemo_dir);
     g_string_append(omemo_ctx.identity_filename, "/identity.txt");
@@ -242,17 +246,6 @@ omemo_on_connect(ProfAccount* account)
     omemo_ctx.known_devices_filename = g_string_new(omemo_dir);
     g_string_append(omemo_ctx.known_devices_filename, "/known_devices.txt");
 
-    errno = 0;
-    int res = g_mkdir_with_parents(omemo_dir, S_IRWXU);
-    if (res == -1) {
-        const char* errmsg = strerror(errno);
-        if (errmsg) {
-            log_error("[OMEMO] error creating directory: %s, %s", omemo_dir, errmsg);
-        } else {
-            log_error("[OMEMO] creating directory: %s", omemo_dir);
-        }
-    }
-
     g_free(omemo_dir);
 
     omemo_devicelist_subscribe();
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 5f45e23c..562914a2 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -129,19 +129,20 @@ static void
 cb_write_fingerprints(void* opdata)
 {
     gcry_error_t err = 0;
-    gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
-
-    GString* fpsfilename = g_string_new(otr_dir);
-    g_string_append(fpsfilename, "/fingerprints.txt");
+    gchar* fpsfilename = files_file_in_account_data_path(DIR_OTR, jid, "fingerprints.txt");
+    if (!fpsfilename) {
+        log_error("Failed to create fingerprints file");
+        cons_show_error("Failed to create fingerprints file");
+        return;
+    }
 
-    err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
+    err = otrl_privkey_write_fingerprints(user_state, fpsfilename);
     if (err != GPG_ERR_NO_ERROR) {
         log_error("Failed to write fingerprints file");
-        cons_show_error("Failed to create fingerprints file");
+        cons_show_error("Failed to write fingerprints file");
     }
 
-    g_free(otr_dir);
-    g_string_free(fpsfilename, TRUE);
+    g_free(fpsfilename);
 }
 
 static void
@@ -212,12 +213,10 @@ otr_on_connect(ProfAccount* account)
     jid = strdup(account->jid);
     log_info("Loading OTR key for %s", jid);
 
-    gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
-
-    if (!mkdir_recursive(otr_dir)) {
-        log_error("Could not create %s for account %s.", otr_dir, jid);
-        cons_show_error("Could not create %s for account %s.", otr_dir, jid);
-        g_free(otr_dir);
+    gchar* otr_dir = files_file_in_account_data_path(DIR_OTR, jid, NULL);
+    if (!otr_dir) {
+        log_error("Could not create directory for account %s.", jid);
+        cons_show_error("Could not create directory for account %s.", jid);
         return;
     }
 
@@ -381,12 +380,11 @@ otr_keygen(ProfAccount* account)
     jid = strdup(account->jid);
     log_info("Generating OTR key for %s", jid);
 
-    gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid);
+    gchar* otr_dir = files_file_in_account_data_path(DIR_OTR, jid, NULL);
 
-    if (!mkdir_recursive(otr_dir)) {
-        log_error("Could not create %s for account %s.", otr_dir, jid);
-        cons_show_error("Could not create %s for account %s.", otr_dir, jid);
-        g_free(otr_dir);
+    if (!otr_dir) {
+        log_error("Could not create directory for account %s.", jid);
+        cons_show_error("Could not create directory for account %s.", jid);
         return;
     }
 
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index 549f9d3b..a1300f80 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -161,27 +161,13 @@ p_gpg_close(void)
 void
 p_gpg_on_connect(const char* const barejid)
 {
-    gchar* pubsfile = files_get_account_data_path(DIR_PGP, barejid);
-
-    // mkdir if doesn't exist for account
-    errno = 0;
-    int res = g_mkdir_with_parents(pubsfile, S_IRWXU);
-    if (res == -1) {
-        const char* errmsg = strerror(errno);
-        if (errmsg) {
-            log_error("Error creating directory: %s, %s", pubsfile, errmsg);
-        } else {
-            log_error("Error creating directory: %s", pubsfile);
-        }
+    pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
+    if (!pubsloc) {
+        log_error("Could not create directory for account %s.", barejid);
+        cons_show_error("Could not create directory for account %s.", barejid);
+        return;
     }
 
-    // create or read publickeys
-    GString* pubtmp = g_string_new(pubsfile);
-    g_string_append(pubtmp, "/pubkeys");
-    pubsloc = pubtmp->str;
-    g_string_free(pubtmp, FALSE);
-    g_free(pubsfile);
-
     if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
         g_chmod(pubsloc, S_IRUSR | S_IWUSR);
     }