winpr: fix PathMakePathA and TestWLog
authorNorbert Federa <norbert.federa@thincast.com>
Mon, 13 Jun 2016 17:19:28 +0000 (19:19 +0200)
committerNorbert Federa <norbert.federa@thincast.com>
Mon, 13 Jun 2016 17:19:28 +0000 (19:19 +0200)
PathMakePathA:
- This function had an endless loop if no native delimiter was in the string
- Use SHCreateDirectoryExA on Windows
- Replaced old code with a new implementation

TestWLog:
- Windows has no "/tmp" by default
- Use GetKnownPath(KNOWN_PATH_TEMP) for the WLog "outputfilepath"

winpr/libwinpr/path/shell.c
winpr/libwinpr/utils/test/TestWLog.c

index dc075e7..5078e1d 100644 (file)
@@ -36,6 +36,8 @@
 
 #if defined(WIN32)
 #include <Shlobj.h>
+#else
+#include <errno.h>
 #endif
 
 static char* GetPath_XDG_CONFIG_HOME(void);
@@ -438,46 +440,35 @@ char* GetCombinedPath(const char* basePath, const char* subPath)
 
 BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes)
 {
-       size_t length;
-       const char delim = PathGetSeparatorA(0);
-       char* cur;
-       char* copy_org = _strdup(path);
-       char* copy = copy_org;
+#ifdef _WIN32
+       return (SHCreateDirectoryExA(NULL, path, lpAttributes) == ERROR_SUCCESS);
+#else
+       const char delim = PathGetSeparatorA(PATH_STYLE_NATIVE);
+       char* dup;
+       char* p;
 
-       if (!copy_org)
+       /* we only operate on a non-null, absolute path */
+       if (!path || *path != delim)
                return FALSE;
 
-       length = strlen(copy_org);
+       if (!(dup = _strdup(path)))
+               return FALSE;
 
-       /* Find first path element that exists. */
-       while (copy)
+       for (p = dup; p;)
        {
-               if (!PathFileExistsA(copy))
-               {
-                       cur = strrchr(copy, delim);
-                       if (cur)
-                               *cur = '\0';
-               }
-               else
-                       break;
-       }
+               if ((p = strchr(p + 1, delim)))
+                       *p = '\0';
 
-       /* Create directories. */
-       while(copy)
-       {
-               if (!PathFileExistsA(copy))
-               {
-                       if (!CreateDirectoryA(copy, NULL))
+               if (mkdir(dup, 0777) != 0)
+                       if (errno != EEXIST)
                                break;
-               }
-               if (strlen(copy) < length)
-                       copy[strlen(copy)] = delim;
-               else
-                       break;
+               if (p)
+                       *p = delim;
        }
-       free (copy_org);
 
-       return PathFileExistsA(path);
+       free(dup);
+       return (p == NULL);
+#endif
 }
 
 #if !defined(_WIN32) || defined(_UWP)
index 74e1c15..54b2ce4 100644 (file)
@@ -2,6 +2,7 @@
 #include <winpr/crt.h>
 #include <winpr/tchar.h>
 #include <winpr/path.h>
+#include <winpr/file.h>
 #include <winpr/wlog.h>
 
 int TestWLog(int argc, char* argv[])
@@ -11,6 +12,14 @@ int TestWLog(int argc, char* argv[])
        wLog* logB;
        wLogLayout* layout;
        wLogAppender* appender;
+       char* tmp_path;
+       char* wlog_file;
+
+        if (!(tmp_path = GetKnownPath(KNOWN_PATH_TEMP)))
+        {
+                fprintf(stderr, "Failed to get temporary directory!\n");
+                return -1;
+        }
 
        WLog_Init();
 
@@ -21,7 +30,7 @@ int TestWLog(int argc, char* argv[])
        appender = WLog_GetLogAppender(root);
        if(!WLog_ConfigureAppender(appender, "outputfilename", "test_w.log"))
                return 1;
-       if(!WLog_ConfigureAppender(appender, "outputfilepath", "/tmp/"))
+       if(!WLog_ConfigureAppender(appender, "outputfilepath", tmp_path))
                return 1;
 
        layout = WLog_GetLogLayout(root);
@@ -49,5 +58,11 @@ int TestWLog(int argc, char* argv[])
 
        WLog_Uninit();
 
+       if ((wlog_file = GetCombinedPath(tmp_path, "test_w.log")))
+       {
+               DeleteFileA(wlog_file);
+               free(wlog_file);
+       }
+
        return 0;
 }