From 7403cdc60ca7799beccafd8775765760f1e25f13 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 3 Jun 2015 11:47:40 +0200 Subject: [PATCH 1/3] Added PathMakePath function. --- libfreerdp/crypto/certificate.c | 6 +-- server/shadow/shadow_server.c | 10 ++-- winpr/include/winpr/path.h | 2 + winpr/libwinpr/path/shell.c | 45 ++++++++++++++++ winpr/libwinpr/path/test/CMakeLists.txt | 3 +- winpr/libwinpr/path/test/TestPathMakePath.c | 57 ++++++++++++++++++++ winpr/libwinpr/smartcard/smartcard_inspect.c | 2 +- winpr/libwinpr/utils/wlog/BinaryAppender.c | 2 +- winpr/libwinpr/utils/wlog/FileAppender.c | 2 +- winpr/libwinpr/utils/wlog/Message.c | 2 +- 10 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 winpr/libwinpr/path/test/TestPathMakePath.c diff --git a/libfreerdp/crypto/certificate.c b/libfreerdp/crypto/certificate.c index fbe804d00..9e385fbe5 100644 --- a/libfreerdp/crypto/certificate.c +++ b/libfreerdp/crypto/certificate.c @@ -51,7 +51,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store) if (!PathFileExistsA(settings->ConfigPath)) { - if (!CreateDirectoryA(settings->ConfigPath, 0)) + if (!PathMakePathA(settings->ConfigPath, 0)) { WLog_ERR(TAG, "error creating directory '%s'", settings->ConfigPath); goto fail; @@ -64,7 +64,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store) if (!PathFileExistsA(certificate_store->path)) { - if (!CreateDirectoryA(certificate_store->path, 0)) + if (!PathMakePathA(certificate_store->path, 0)) { WLog_ERR(TAG, "error creating directory [%s]", certificate_store->path); goto fail; @@ -77,7 +77,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store) if (!PathFileExistsA(server_path)) { - if (!CreateDirectoryA(server_path, 0)) + if (!PathMakePathA(server_path, 0)) { WLog_ERR(TAG, "error creating directory [%s]", server_path); goto fail; diff --git a/server/shadow/shadow_server.c b/server/shadow/shadow_server.c index 6a985fb04..79568f1dd 100644 --- a/server/shadow/shadow_server.c +++ b/server/shadow/shadow_server.c @@ -445,7 +445,7 @@ int shadow_server_init_config_path(rdpShadowServer* server) if (userLibraryPath) { if (!PathFileExistsA(userLibraryPath) && - !CreateDirectoryA(userLibraryPath, 0)) + !PathMakePathA(userLibraryPath, 0)) { WLog_ERR(TAG, "Failed to create directory '%s'", userLibraryPath); free(userLibraryPath); @@ -457,7 +457,7 @@ int shadow_server_init_config_path(rdpShadowServer* server) if (userApplicationSupportPath) { if (!PathFileExistsA(userApplicationSupportPath) && - !CreateDirectoryA(userApplicationSupportPath, 0)) + !PathMakePathA(userApplicationSupportPath, 0)) { WLog_ERR(TAG, "Failed to create directory '%s'", userApplicationSupportPath); free(userLibraryPath); @@ -482,7 +482,7 @@ int shadow_server_init_config_path(rdpShadowServer* server) if (configHome) { if (!PathFileExistsA(configHome) && - !CreateDirectoryA(configHome, 0)) + !PathMakePathA(configHome, 0)) { WLog_ERR(TAG, "Failed to create directory '%s'", configHome); free(configHome); @@ -516,7 +516,7 @@ int shadow_server_init_certificate(rdpShadowServer* server) int makecert_argc = (sizeof(makecert_argv) / sizeof(char*)); if (!PathFileExistsA(server->ConfigPath) && - !CreateDirectoryA(server->ConfigPath, 0)) + !PathMakePathA(server->ConfigPath, 0)) { WLog_ERR(TAG, "Failed to create directory '%s'", server->ConfigPath); return -1; @@ -526,7 +526,7 @@ int shadow_server_init_certificate(rdpShadowServer* server) return -1; if (!PathFileExistsA(filepath) && - !CreateDirectoryA(filepath, 0)) + !PathMakePathA(filepath, 0)) { WLog_ERR(TAG, "Failed to create directory '%s'", filepath); free(filepath); diff --git a/winpr/include/winpr/path.h b/winpr/include/winpr/path.h index 36333e9b6..eba13296a 100644 --- a/winpr/include/winpr/path.h +++ b/winpr/include/winpr/path.h @@ -285,6 +285,8 @@ WINPR_API char* GetEnvironmentPath(char* name); WINPR_API char* GetEnvironmentSubPath(char* name, const char* path); WINPR_API char* GetCombinedPath(const char* basePath, const char* subPath); +WINPR_API BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes); + WINPR_API BOOL PathFileExistsA(LPCSTR pszPath); WINPR_API BOOL PathFileExistsW(LPCWSTR pszPath); diff --git a/winpr/libwinpr/path/shell.c b/winpr/libwinpr/path/shell.c index fe913db7e..69841a1c1 100644 --- a/winpr/libwinpr/path/shell.c +++ b/winpr/libwinpr/path/shell.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -420,6 +421,50 @@ char* GetCombinedPath(const char* basePath, const char* subPath) return path; } +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; + + if (!copy_org) + return FALSE; + + length = strlen(copy_org); + + /* Find first path element that exists. */ + while (copy) + { + if (!PathFileExistsA(copy)) + { + cur = strrchr(copy, delim); + if (cur) + *cur = '\0'; + } + else + break; + } + + /* Create directories. */ + while(copy) + { + if (!PathFileExistsA(copy)) + { + if (!CreateDirectoryA(copy, NULL)) + break; + } + if (strlen(copy) < length) + copy[strlen(copy)] = delim; + else + break; + } + free (copy_org); + + return PathFileExistsA(path); +} + BOOL PathFileExistsA(LPCSTR pszPath) { struct stat stat_info; diff --git a/winpr/libwinpr/path/test/CMakeLists.txt b/winpr/libwinpr/path/test/CMakeLists.txt index 8d0bc0ac9..974907e31 100644 --- a/winpr/libwinpr/path/test/CMakeLists.txt +++ b/winpr/libwinpr/path/test/CMakeLists.txt @@ -27,7 +27,8 @@ set(${MODULE_PREFIX}_TESTS TestPathCchStripToRoot.c TestPathCchStripPrefix.c TestPathCchRemoveFileSpec.c - TestPathShell.c) + TestPathShell.c + TestPathMakePath.c) create_test_sourcelist(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_DRIVER} diff --git a/winpr/libwinpr/path/test/TestPathMakePath.c b/winpr/libwinpr/path/test/TestPathMakePath.c new file mode 100644 index 000000000..081aa60aa --- /dev/null +++ b/winpr/libwinpr/path/test/TestPathMakePath.c @@ -0,0 +1,57 @@ +#include +#include +#include + +#include +#include + +int TestPathMakePath(int argc, char* argv[]) +{ + int x; + BOOL success; + char tmp[64]; + char* path; + char* base = GetKnownPath(KNOWN_PATH_TEMP); + if (!base) + { + fprintf(stderr, "Failed to get temporary directory!\n"); + return -1; + } + + srand(time(NULL)); + for (x=0; x<5; x++) + { + sprintf(tmp, "%08X", rand()); + path = GetCombinedPath(base, tmp); + free(base); + if (!path) + { + fprintf(stderr, "GetCombinedPath failed!\n"); + return -1; + } + + base = path; + } + + printf("Creating path %s\n", path); + success = PathMakePathA(path, NULL); + if (!success) + { + fprintf(stderr, "MakePath failed!\n"); + free (path); + return -1; + } + + success = PathFileExistsA(path); + if (!success) + { + fprintf(stderr, "MakePath lied about success!\n"); + free (path); + return -1; + } + free (path); + + printf("%s success!", __FUNCTION__); + return 0; +} + diff --git a/winpr/libwinpr/smartcard/smartcard_inspect.c b/winpr/libwinpr/smartcard/smartcard_inspect.c index 192a48ac1..c36fc87e5 100644 --- a/winpr/libwinpr/smartcard/smartcard_inspect.c +++ b/winpr/libwinpr/smartcard/smartcard_inspect.c @@ -1276,7 +1276,7 @@ void Inspect_InitLog() return; if (!PathFileExistsA(filepath)) - if (!CreateDirectoryA(filepath, NULL)) + if (!PathMakePathA(filepath, NULL)) return; if (!(g_Log = WLog_Get("WinSCard"))) diff --git a/winpr/libwinpr/utils/wlog/BinaryAppender.c b/winpr/libwinpr/utils/wlog/BinaryAppender.c index 6f8257274..4bc64270d 100644 --- a/winpr/libwinpr/utils/wlog/BinaryAppender.c +++ b/winpr/libwinpr/utils/wlog/BinaryAppender.c @@ -98,7 +98,7 @@ int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender) if (!PathFileExistsA(appender->FilePath)) { - if (!CreateDirectoryA(appender->FilePath, 0)) + if (!PathMakePathA(appender->FilePath, 0)) return -1; UnixChangeFileMode(appender->FilePath, 0xFFFF); } diff --git a/winpr/libwinpr/utils/wlog/FileAppender.c b/winpr/libwinpr/utils/wlog/FileAppender.c index c0d2295ae..64a9239be 100644 --- a/winpr/libwinpr/utils/wlog/FileAppender.c +++ b/winpr/libwinpr/utils/wlog/FileAppender.c @@ -95,7 +95,7 @@ int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender) if (!PathFileExistsA(appender->FilePath)) { - if (!CreateDirectoryA(appender->FilePath, 0)) + if (!PathMakePathA(appender->FilePath, 0)) return -1; UnixChangeFileMode(appender->FilePath, 0xFFFF); } diff --git a/winpr/libwinpr/utils/wlog/Message.c b/winpr/libwinpr/utils/wlog/Message.c index 303b1471d..c8567b92e 100644 --- a/winpr/libwinpr/utils/wlog/Message.c +++ b/winpr/libwinpr/utils/wlog/Message.c @@ -44,7 +44,7 @@ char* WLog_Message_GetOutputFileName(int id, const char* ext) if (!PathFileExistsA(FilePath)) { - if (!CreateDirectoryA(FilePath, NULL)) + if (!PathMakePathA(FilePath, NULL)) { free(FileName); free(FilePath); From db7753bc8bb3668266f1ade11790029fff421ef3 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 3 Jun 2015 12:35:45 +0200 Subject: [PATCH 2/3] Added RemoveDirectoryA, clean up test directories. --- winpr/include/winpr/file.h | 5 +++++ winpr/libwinpr/file/file.c | 15 +++++++++++++- winpr/libwinpr/path/test/TestPathMakePath.c | 22 +++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/winpr/include/winpr/file.h b/winpr/include/winpr/file.h index 80f89507f..45ec3e698 100644 --- a/winpr/include/winpr/file.h +++ b/winpr/include/winpr/file.h @@ -291,6 +291,9 @@ WINPR_API BOOL FindClose(HANDLE hFindFile); WINPR_API BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes); WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes); +WINPR_API BOOL RemoveDirectoryA(LPCSTR lpPathName); +WINPR_API BOOL RemoveDirectoryW(LPCWSTR lpPathName); + #ifdef __cplusplus } #endif @@ -302,6 +305,7 @@ WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecu #define FindFirstFileEx FindFirstFileExW #define FindNextFile FindNextFileW #define CreateDirectory CreateDirectoryW +#define RemoveDirectory RemoveDirectoryW #else #define CreateFile CreateFileA #define DeleteFile DeleteFileA @@ -309,6 +313,7 @@ WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecu #define FindFirstFileEx FindFirstFileExA #define FindNextFile FindNextFileA #define CreateDirectory CreateDirectoryA +#define RemoveDirectory RemoveDirectoryA #endif diff --git a/winpr/libwinpr/file/file.c b/winpr/libwinpr/file/file.c index 05fb444ba..291ee23e0 100644 --- a/winpr/libwinpr/file/file.c +++ b/winpr/libwinpr/file/file.c @@ -788,7 +788,20 @@ BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttribu BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) { - return TRUE; + return FALSE; +} + +BOOL RemoveDirectoryA(LPCSTR lpPathName) +{ + if (!rmdir(lpPathName)) + return TRUE; + + return FALSE; +} + +BOOL RemoveDirectoryW(LPCWSTR lpPathName) +{ + return FALSE; } #endif diff --git a/winpr/libwinpr/path/test/TestPathMakePath.c b/winpr/libwinpr/path/test/TestPathMakePath.c index 081aa60aa..e69b396bd 100644 --- a/winpr/libwinpr/path/test/TestPathMakePath.c +++ b/winpr/libwinpr/path/test/TestPathMakePath.c @@ -3,14 +3,18 @@ #include #include +#include #include int TestPathMakePath(int argc, char* argv[]) { int x; + size_t baseLen; BOOL success; char tmp[64]; char* path; + char* cur; + char delim = PathGetSeparatorA(0); char* base = GetKnownPath(KNOWN_PATH_TEMP); if (!base) { @@ -18,6 +22,7 @@ int TestPathMakePath(int argc, char* argv[]) return -1; } + baseLen = strlen(base); srand(time(NULL)); for (x=0; x<5; x++) { @@ -49,9 +54,22 @@ int TestPathMakePath(int argc, char* argv[]) free (path); return -1; } - free (path); - printf("%s success!", __FUNCTION__); + while (strlen(path) > baseLen) + { + if (!RemoveDirectoryA(path)) + { + fprintf(stderr, "RemoveDirectoryA %s failed!\n", path); + free (path); + return -1; + } + cur = strrchr(path, delim); + if (cur) + *cur = '\0'; + } + + free (path); + printf("%s success!\n", __FUNCTION__); return 0; } From 5b7a44cd4abcc1007514eda1beda46f7e950a104 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Tue, 9 Jun 2015 13:17:15 +0200 Subject: [PATCH 3/3] Removed CreateDirectoryA, handled by PathMakePath --- libfreerdp/core/settings.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/libfreerdp/core/settings.c b/libfreerdp/core/settings.c index 806d9bf03..ab5af8e42 100644 --- a/libfreerdp/core/settings.c +++ b/libfreerdp/core/settings.c @@ -485,9 +485,6 @@ rdpSettings* freerdp_settings_new(DWORD flags) settings->ConfigPath = GetCombinedPath( base, FREERDP_PRODUCT_STRING); - if (!PathFileExistsA(base)) - if (!CreateDirectoryA(base, NULL)) - settings->ConfigPath = NULL; } free (base); } else {