From 705b6f7806bb2bab00dded8beed8c43f4190bb93 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sun, 13 Mar 2022 12:02:59 +0100 Subject: [PATCH] use `g_mkdir_with_parents()` instead of home-baked solution Signed-off-by: Steffen Jaeckel --- src/command/cmd_defs.c | 2 +- src/common.c | 36 ++++-------------------------------- src/common.h | 3 +-- src/config/files.c | 15 +++++++-------- tests/unittests/helpers.c | 4 ++-- 5 files changed, 15 insertions(+), 45 deletions(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index e56b3dc8..750e533d 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -3004,7 +3004,7 @@ command_mangen(void) cmds = g_list_insert_sorted(cmds, pcmd, (GCompareFunc)_cmp_command); } - mkdir_recursive("docs"); + create_dir("docs"); GDateTime* now = g_date_time_new_now_local(); gchar* date = g_date_time_format(now, "%F"); diff --git a/src/common.c b/src/common.c index 6020b8c3..5e0fabf6 100644 --- a/src/common.c +++ b/src/common.c @@ -71,43 +71,15 @@ struct curl_data_t static size_t _data_callback(void* ptr, size_t size, size_t nmemb, void* data); gboolean -create_dir(char* name) +create_dir(const char* name) { - struct stat sb; - - if (stat(name, &sb) != 0) { - if (errno != ENOENT || mkdir(name, S_IRWXU) != 0) { - return FALSE; - } - } else { - if ((sb.st_mode & S_IFDIR) != S_IFDIR) { - log_debug("create_dir: %s exists and is not a directory!", name); - return FALSE; - } + if (g_mkdir_with_parents(name, S_IRWXU) != 0) { + log_error("Failed to create directory at '%s' with error '%s'", name, strerror(errno)); + return FALSE; } - return TRUE; } -gboolean -mkdir_recursive(const char* dir) -{ - gboolean result = TRUE; - - for (int i = 1; i <= strlen(dir); i++) { - if (dir[i] == '/' || dir[i] == '\0') { - gchar* next_dir = g_strndup(dir, i); - result = create_dir(next_dir); - g_free(next_dir); - if (!result) { - break; - } - } - } - - return result; -} - gboolean copy_file(const char* const sourcepath, const char* const targetpath, const gboolean overwrite_existing) { diff --git a/src/common.h b/src/common.h index bd33bf90..b97ef401 100644 --- a/src/common.h +++ b/src/common.h @@ -80,8 +80,7 @@ typedef enum { RESOURCE_XA } resource_presence_t; -gboolean create_dir(char* name); -gboolean mkdir_recursive(const char* dir); +gboolean create_dir(const char* name); gboolean copy_file(const char* const src, const char* const target, const gboolean overwrite_existing); char* str_replace(const char* string, const char* substr, const char* replacement); gboolean strtoi_range(char* str, int* saveptr, int min, int max, char** err_msg); diff --git a/src/config/files.c b/src/config/files.c index 8a241be5..0f073b8b 100644 --- a/src/config/files.c +++ b/src/config/files.c @@ -67,19 +67,19 @@ files_create_directories(void) GString* plugins_dir = g_string_new(xdg_data); g_string_append(plugins_dir, "/profanity/plugins"); - if (!mkdir_recursive(themes_dir->str)) { + if (!create_dir(themes_dir->str)) { log_error("Error while creating directory %s", themes_dir->str); } - if (!mkdir_recursive(icons_dir->str)) { + if (!create_dir(icons_dir->str)) { log_error("Error while creating directory %s", icons_dir->str); } - if (!mkdir_recursive(chatlogs_dir->str)) { + if (!create_dir(chatlogs_dir->str)) { log_error("Error while creating directory %s", chatlogs_dir->str); } - if (!mkdir_recursive(logs_dir->str)) { + if (!create_dir(logs_dir->str)) { log_error("Error while creating directory %s", logs_dir->str); } - if (!mkdir_recursive(plugins_dir->str)) { + if (!create_dir(plugins_dir->str)) { log_error("Error while creating directory %s", plugins_dir->str); } @@ -122,7 +122,7 @@ files_get_log_file(const char* const log_file) if (log_file) { gchar* log_path = g_path_get_dirname(log_file); - if (!mkdir_recursive(log_path)) { + if (!create_dir(log_path)) { log_error("Error while creating directory %s", log_path); } g_free(log_path); @@ -198,8 +198,7 @@ files_file_in_account_data_path(const char* const specific_dir, const char* cons { 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)); + if (!create_dir(data_path)) { g_free(data_path); return NULL; } diff --git a/tests/unittests/helpers.c b/tests/unittests/helpers.c index e309b559..bc4f8db3 100644 --- a/tests/unittests/helpers.c +++ b/tests/unittests/helpers.c @@ -16,7 +16,7 @@ void create_config_dir(void** state) { setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); - if (!mkdir_recursive("./tests/files/xdg_config_home/profanity")) { + if (!create_dir("./tests/files/xdg_config_home/profanity")) { assert_true(FALSE); } } @@ -32,7 +32,7 @@ void create_data_dir(void** state) { setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1); - if (!mkdir_recursive("./tests/files/xdg_data_home/profanity")) { + if (!create_dir("./tests/files/xdg_data_home/profanity")) { assert_true(FALSE); } }