about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDmitry Podgorny <pasis.ua@gmail.com>2013-08-03 14:14:30 +0300
committerDmitry Podgorny <pasis.ua@gmail.com>2013-08-03 14:14:30 +0300
commit6f498d1f69098742acc33d429c5e8dcee4edd86d (patch)
tree6a91c03cc8f726a6b616a2ccc23ea8cd8688ad75
parentc559d96d7739d499f9b747839e1115d28976306f (diff)
downloadprofani-tty-6f498d1f69098742acc33d429c5e8dcee4edd86d.tar.gz
refactored mkdir_recursive
Now this function returns result of operation. TRUE is success.
-rw-r--r--src/common.c32
-rw-r--r--src/common.h4
-rw-r--r--src/profanity.c12
3 files changed, 35 insertions, 13 deletions
diff --git a/src/common.c b/src/common.c
index e2ff0171..da33cc3d 100644
--- a/src/common.c
+++ b/src/common.c
@@ -25,12 +25,14 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 
 #include <curl/curl.h>
 #include <curl/easy.h>
 #include <glib.h>
 
+#include "log.h"
 #include "common.h"
 
 // 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);
 }
 
-void
+gboolean
 create_dir(char *name)
 {
-    int e;
     struct stat sb;
 
-    e = stat(name, &sb);
-    if (e != 0)
-        if (errno == ENOENT)
-            e = mkdir(name, S_IRWXU);
+    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;
+        }
+    }
+
+    return TRUE;
 }
 
-void
+gboolean
 mkdir_recursive(const char *dir)
 {
     int i;
+    gboolean result = TRUE;
+
     for (i = 1; i <= strlen(dir); i++) {
         if (dir[i] == '/' || dir[i] == '\0') {
             gchar *next_dir = g_strndup(dir, i);
-            create_dir(next_dir);
+            result = create_dir(next_dir);
             g_free(next_dir);
+            if (!result) {
+                break;
+            }
         }
     }
+
+    return result;
 }
 
 char *
diff --git a/src/common.h b/src/common.h
index f8ed82a5..469a63ec 100644
--- a/src/common.h
+++ b/src/common.h
@@ -72,8 +72,8 @@ typedef enum {
 
 gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos);
 void p_slist_free_full(GSList *items, GDestroyNotify free_func);
-void create_dir(char *name);
-void mkdir_recursive(const char *dir);
+gboolean create_dir(char *name);
+gboolean mkdir_recursive(const char *dir);
 char * str_replace(const char *string, const char *substr,
     const char *replacement);
 int str_contains(char str[], int size, char ch);
diff --git a/src/profanity.c b/src/profanity.c
index 9c729ca1..4e9e887c 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -664,9 +664,15 @@ _create_directories(void)
     GString *logs_dir = g_string_new(xdg_data);
     g_string_append(logs_dir, "/profanity/logs");
 
-    mkdir_recursive(themes_dir->str);
-    mkdir_recursive(chatlogs_dir->str);
-    mkdir_recursive(logs_dir->str);
+    if (!mkdir_recursive(themes_dir->str)) {
+        log_error("Error while creating directory %s", themes_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(chatlogs_dir, TRUE);