1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

refactored mkdir_recursive

Now this function returns result of operation. TRUE is success.
This commit is contained in:
Dmitry Podgorny 2013-08-03 14:14:30 +03:00
parent c559d96d77
commit 6f498d1f69
3 changed files with 35 additions and 13 deletions

View File

@ -25,12 +25,14 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <glib.h> #include <glib.h>
#include "log.h"
#include "common.h" #include "common.h"
// assume malloc stores at most 8 bytes for size of allocated memory // assume malloc stores at most 8 bytes for size of allocated memory
@ -69,29 +71,43 @@ p_slist_free_full(GSList *items, GDestroyNotify free_func)
g_slist_free (items); g_slist_free (items);
} }
void gboolean
create_dir(char *name) create_dir(char *name)
{ {
int e;
struct stat sb; struct stat sb;
e = stat(name, &sb); if (stat(name, &sb) != 0) {
if (e != 0) if (errno != ENOENT || mkdir(name, S_IRWXU) != 0) {
if (errno == ENOENT) return FALSE;
e = mkdir(name, S_IRWXU); }
} else {
if ((sb.st_mode & S_IFDIR) != S_IFDIR) {
log_debug("create_dir: %s exists and is not a directory!", name);
return FALSE;
}
}
return TRUE;
} }
void gboolean
mkdir_recursive(const char *dir) mkdir_recursive(const char *dir)
{ {
int i; int i;
gboolean result = TRUE;
for (i = 1; i <= strlen(dir); i++) { for (i = 1; i <= strlen(dir); i++) {
if (dir[i] == '/' || dir[i] == '\0') { if (dir[i] == '/' || dir[i] == '\0') {
gchar *next_dir = g_strndup(dir, i); gchar *next_dir = g_strndup(dir, i);
create_dir(next_dir); result = create_dir(next_dir);
g_free(next_dir); g_free(next_dir);
if (!result) {
break;
} }
} }
}
return result;
} }
char * char *

View File

@ -72,8 +72,8 @@ typedef enum {
gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos); gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos);
void p_slist_free_full(GSList *items, GDestroyNotify free_func); void p_slist_free_full(GSList *items, GDestroyNotify free_func);
void create_dir(char *name); gboolean create_dir(char *name);
void mkdir_recursive(const char *dir); gboolean mkdir_recursive(const char *dir);
char * str_replace(const char *string, const char *substr, char * str_replace(const char *string, const char *substr,
const char *replacement); const char *replacement);
int str_contains(char str[], int size, char ch); int str_contains(char str[], int size, char ch);

View File

@ -664,9 +664,15 @@ _create_directories(void)
GString *logs_dir = g_string_new(xdg_data); GString *logs_dir = g_string_new(xdg_data);
g_string_append(logs_dir, "/profanity/logs"); g_string_append(logs_dir, "/profanity/logs");
mkdir_recursive(themes_dir->str); if (!mkdir_recursive(themes_dir->str)) {
mkdir_recursive(chatlogs_dir->str); log_error("Error while creating directory %s", themes_dir->str);
mkdir_recursive(logs_dir->str); }
if (!mkdir_recursive(chatlogs_dir->str)) {
log_error("Error while creating directory %s", chatlogs_dir->str);
}
if (!mkdir_recursive(logs_dir->str)) {
log_error("Error while creating directory %s", logs_dir->str);
}
g_string_free(themes_dir, TRUE); g_string_free(themes_dir, TRUE);
g_string_free(chatlogs_dir, TRUE); g_string_free(chatlogs_dir, TRUE);