1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

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>
This commit is contained in:
Steffen Jaeckel 2022-03-13 11:58:56 +01:00
parent 764a7fb71b
commit b8e46552bf
6 changed files with 54 additions and 73 deletions

View File

@ -38,6 +38,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h> #include <glib.h>
#include "common.h" #include "common.h"
@ -191,6 +193,25 @@ files_get_account_data_path(const char* const specific_dir, const char* const ji
return result; 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* static char*
_files_get_xdg_config_home(void) _files_get_xdg_config_home(void)
{ {

View File

@ -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_log_file(const char* const log_file);
gchar* files_get_inputrc_file(void); 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 #endif

View File

@ -56,27 +56,7 @@ static prof_msg_type_t _get_message_type_type(const char* const type);
static char* static char*
_get_db_filename(ProfAccount* account) _get_db_filename(ProfAccount* account)
{ {
gchar* database_dir = files_get_account_data_path(DIR_DATABASE, account->jid); return files_file_in_account_data_path(DIR_DATABASE, account->jid, "chatlog.db");
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;
} }
gboolean gboolean

View File

@ -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.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); 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); omemo_ctx.identity_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.identity_filename, "/identity.txt"); 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); omemo_ctx.known_devices_filename = g_string_new(omemo_dir);
g_string_append(omemo_ctx.known_devices_filename, "/known_devices.txt"); 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); g_free(omemo_dir);
omemo_devicelist_subscribe(); omemo_devicelist_subscribe();

View File

@ -129,19 +129,20 @@ static void
cb_write_fingerprints(void* opdata) cb_write_fingerprints(void* opdata)
{ {
gcry_error_t err = 0; gcry_error_t err = 0;
gchar* otr_dir = files_get_account_data_path(DIR_OTR, jid); gchar* fpsfilename = files_file_in_account_data_path(DIR_OTR, jid, "fingerprints.txt");
if (!fpsfilename) {
GString* fpsfilename = g_string_new(otr_dir); log_error("Failed to create fingerprints file");
g_string_append(fpsfilename, "/fingerprints.txt");
err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
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 create fingerprints file");
return;
} }
g_free(otr_dir); err = otrl_privkey_write_fingerprints(user_state, fpsfilename);
g_string_free(fpsfilename, TRUE); if (err != GPG_ERR_NO_ERROR) {
log_error("Failed to write fingerprints file");
cons_show_error("Failed to write fingerprints file");
}
g_free(fpsfilename);
} }
static void static void
@ -212,12 +213,10 @@ otr_on_connect(ProfAccount* account)
jid = strdup(account->jid); jid = strdup(account->jid);
log_info("Loading OTR key for %s", jid); log_info("Loading 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 (!otr_dir) {
if (!mkdir_recursive(otr_dir)) { log_error("Could not create directory for account %s.", jid);
log_error("Could not create %s for account %s.", otr_dir, jid); cons_show_error("Could not create directory for account %s.", jid);
cons_show_error("Could not create %s for account %s.", otr_dir, jid);
g_free(otr_dir);
return; return;
} }
@ -381,12 +380,11 @@ otr_keygen(ProfAccount* account)
jid = strdup(account->jid); jid = strdup(account->jid);
log_info("Generating OTR key for %s", 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)) { if (!otr_dir) {
log_error("Could not create %s for account %s.", otr_dir, jid); log_error("Could not create directory for account %s.", jid);
cons_show_error("Could not create %s for account %s.", otr_dir, jid); cons_show_error("Could not create directory for account %s.", jid);
g_free(otr_dir);
return; return;
} }

View File

@ -161,26 +161,12 @@ p_gpg_close(void)
void void
p_gpg_on_connect(const char* const barejid) p_gpg_on_connect(const char* const barejid)
{ {
gchar* pubsfile = files_get_account_data_path(DIR_PGP, barejid); pubsloc = files_file_in_account_data_path(DIR_PGP, barejid, "pubkeys");
if (!pubsloc) {
// mkdir if doesn't exist for account log_error("Could not create directory for account %s.", barejid);
errno = 0; cons_show_error("Could not create directory for account %s.", barejid);
int res = g_mkdir_with_parents(pubsfile, S_IRWXU); return;
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);
} }
}
// 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)) { if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
g_chmod(pubsloc, S_IRUSR | S_IWUSR); g_chmod(pubsloc, S_IRUSR | S_IWUSR);