changeset 550:504a8b9297a3

Fix log directory creation.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 12 Nov 2012 14:30:59 +0200
parents dd7b58eca06d
children 89fafb218396
files main.c
diffstat 1 files changed, 76 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Mon Nov 12 14:18:26 2012 +0200
+++ b/main.c	Mon Nov 12 14:30:59 2012 +0200
@@ -1579,45 +1579,6 @@
 }
 
 
-BOOL nn_mkdir_rec(const char *cpath)
-{
-    char save, *path = th_strdup(cpath);
-    size_t start = 0, end;
-    BOOL res = FALSE;
-
-    do
-    {
-        for (save = 0, end = start; path[end] != 0; end++)
-        if (path[end] == SET_DIR_SEPARATOR)
-        {
-            save = path[end];
-            path[end] = 0;
-            break;
-        }
-
-        if (path[start] != 0)
-        {
-#ifdef __WIN32
-            if (!CreateDirectory(path, NULL))
-                goto error;
-#else
-            if (mkdir(path, 711) < 0)
-                goto error;
-#endif
-        }
-
-        path[end] = save;
-        start = end + 1;
-    } while (save != 0);
-
-    res = TRUE;
-
-error:
-    th_free(path);
-    return res;
-}
-
-
 BOOL nn_stat_path(const char *path, BOOL *isDirectory, BOOL *isWritable, BOOL *isReadable)
 {
 #ifdef __WIN32
@@ -1640,6 +1601,55 @@
 }
 
 
+BOOL nn_mkdir_rec(const char *cpath)
+{
+    char save, *path = th_strdup(cpath);
+    size_t start = 0, end;
+    BOOL res = FALSE, exists, isDir, isWritable, isReadable;
+
+    do
+    {
+        for (save = 0, end = start; path[end] != 0; end++)
+        if (path[end] == SET_DIR_SEPARATOR)
+        {
+            save = path[end];
+            path[end] = 0;
+            break;
+        }
+        
+
+        if (path[start] != 0)
+        {
+            exists = nn_stat_path(path, &isDir, &isWritable, &isReadable);
+            if (exists && !isDir)
+                goto error;
+
+            if (!exists)
+            {
+#ifdef __WIN32
+            if (!CreateDirectory(path, NULL))
+                goto error;
+#else
+            if (mkdir(path, 0x1c9) < 0)
+                goto error;
+#endif
+            }
+        }
+
+        path[end] = save;
+        start = end + 1;
+    } while (save != 0);
+
+    res = TRUE;
+
+error:
+    th_free(path);
+    return res;
+}
+
+
+
+
 int main(int argc, char *argv[])
 {
     char *tmpStr;
@@ -1769,6 +1779,33 @@
             setHomeDir, SET_DIR_SEPARATOR, SET_LOG_DIR, SET_DIR_SEPARATOR);
     }
 
+    {
+        BOOL isDir, isWritable, isReadable;
+        if (nn_stat_path(optLogPath, &isDir, &isWritable, &isReadable))
+        {
+            if (!isDir)
+            {
+                THERR("The log file path '%s' is not a directory.\n",
+                    optLogPath);
+                goto err_exit;
+            }
+            else
+            if (!isWritable)
+            {
+                THERR("The log file path '%s' is not writable.\n",
+                    optLogPath);
+                goto err_exit;
+            }
+        }
+        else
+        if (!nn_mkdir_rec(optLogPath))
+        {
+            THERR("Could not create log file directory '%s'.\n",
+                optLogPath);
+            goto err_exit;
+        }
+    }
+
     // Parse command line arguments
     argsOK = th_args_process(argc, argv, optList, optListN,
                              argHandleOpt, argHandleFile, FALSE);
@@ -1807,32 +1844,6 @@
     if (!optDaemon && !nnwin_init(SET_DELAY))
         goto err_exit;
 
-    {
-        BOOL isDir, isWritable, isReadable;
-        if (nn_stat_path(optLogPath, &isDir, &isWritable, &isReadable))
-        {
-            if (!isDir)
-            {
-                errorMsg("The log file path '%s' is not a directory.\n",
-                    optLogPath);
-                goto err_exit;
-            }
-            else
-            if (!isWritable)
-            {
-                errorMsg("The log file path '%s' is not writable.\n",
-                    optLogPath);
-                goto err_exit;
-            }
-        }
-        else
-        if (!nn_mkdir_rec(optLogPath))
-        {
-            errorMsg("Could not create log file directory '%s'.\n",
-                optLogPath);
-            goto err_exit;
-        }
-    }
 
     if (cursesInit)
     {