libwinpr-path: add path detection functions

This commit is contained in:
Marc-André Moreau 2013-03-22 00:42:09 -04:00
parent 93a752b546
commit edf6e7258d
11 changed files with 362 additions and 140 deletions

View File

@ -29,14 +29,9 @@ extern "C" {
#endif
FREERDP_API void freerdp_mkdir(char* path);
FREERDP_API BOOL freerdp_check_file_exists(char* file);
FREERDP_API char* freerdp_get_home_path(rdpSettings* settings);
FREERDP_API char* freerdp_get_config_path(rdpSettings* settings);
FREERDP_API char* freerdp_get_current_path(rdpSettings* settings);
FREERDP_API char* freerdp_construct_path(char* base_path, char* relative_path);
FREERDP_API char* freerdp_append_shared_library_suffix(char* file_path);
FREERDP_API char* freerdp_get_parent_path(char* base_path, int depth);
FREERDP_API BOOL freerdp_path_contains_separator(char* path);
FREERDP_API void freerdp_detect_paths(rdpSettings* settings);
#ifdef __cplusplus

View File

@ -48,7 +48,7 @@ void certificate_store_init(rdpCertificateStore* certificate_store)
config_path = freerdp_get_config_path(settings);
certificate_store->path = freerdp_construct_path(config_path, (char*) certificate_store_dir);
if (freerdp_check_file_exists(certificate_store->path) == FALSE)
if (PathFileExistsA(certificate_store->path) == FALSE)
{
freerdp_mkdir(certificate_store->path);
printf("creating directory %s\n", certificate_store->path);
@ -56,7 +56,7 @@ void certificate_store_init(rdpCertificateStore* certificate_store)
certificate_store->file = freerdp_construct_path(config_path, (char*) certificate_known_hosts_file);
if (freerdp_check_file_exists(certificate_store->file) == FALSE)
if (PathFileExistsA(certificate_store->file) == FALSE)
{
certificate_store->fp = fopen((char*) certificate_store->file, "w+");

View File

@ -59,7 +59,7 @@ endif()
set_complex_link_libraries(VARIABLE ${MODULE_PREFIX}_LIBS
MONOLITHIC ${MONOLITHIC_BUILD}
MODULE winpr
MODULES winpr-crt winpr-synch winpr-thread winpr-utils)
MODULES winpr-crt winpr-synch winpr-thread winpr-utils winpr-path)
if(MONOLITHIC_BUILD)
set(FREERDP_LIBS ${FREERDP_LIBS} ${${MODULE_PREFIX}_LIBS} PARENT_SCOPE)

View File

@ -27,6 +27,7 @@
#include <sys/stat.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <freerdp/types.h>
#include <freerdp/settings.h>
@ -44,25 +45,13 @@
#ifndef _WIN32
#define PATH_SEPARATOR_STR "/"
#define PATH_SEPARATOR_CHR '/'
#define HOME_ENV_VARIABLE "HOME"
#else
#define PATH_SEPARATOR_STR "\\"
#define PATH_SEPARATOR_CHR '\\'
#define HOME_ENV_VARIABLE "HOME"
#endif
#ifdef _WIN32
#define SHARED_LIB_SUFFIX ".dll"
#elif defined(__APPLE__)
#define SHARED_LIB_SUFFIX ".dylib"
#else
#define SHARED_LIB_SUFFIX ".so"
#endif
#define FREERDP_CONFIG_DIR ".freerdp"
#define PARENT_PATH ".." PATH_SEPARATOR_STR
void freerdp_mkdir(char* path)
{
#ifndef _WIN32
@ -72,31 +61,9 @@ void freerdp_mkdir(char* path)
#endif
}
BOOL freerdp_check_file_exists(char* file)
{
struct stat stat_info;
if (stat(file, &stat_info) != 0)
return FALSE;
return TRUE;
}
char* freerdp_get_home_path(rdpSettings* settings)
{
char* home_env = NULL;
if (settings->HomePath == NULL)
{
home_env = getenv(HOME_ENV_VARIABLE);
if (home_env)
settings->HomePath = _strdup(home_env);
}
if (settings->HomePath == NULL)
settings->HomePath = _strdup("/");
settings->HomePath = GetKnownPath(KNOWN_PATH_HOME);
return settings->HomePath;
}
@ -108,20 +75,12 @@ char* freerdp_get_config_path(rdpSettings* settings)
settings->ConfigPath = (char*) malloc(strlen(settings->HomePath) + sizeof(FREERDP_CONFIG_DIR) + 2);
sprintf(settings->ConfigPath, "%s" PATH_SEPARATOR_STR "%s", settings->HomePath, FREERDP_CONFIG_DIR);
if (!freerdp_check_file_exists(settings->ConfigPath))
if (!PathFileExistsA(settings->ConfigPath))
freerdp_mkdir(settings->ConfigPath);
return settings->ConfigPath;
}
char* freerdp_get_current_path(rdpSettings* settings)
{
if (settings->CurrentPath == NULL)
settings->CurrentPath = getcwd(NULL, 0);
return settings->CurrentPath;
}
char* freerdp_construct_path(char* base_path, char* relative_path)
{
char* path;
@ -139,89 +98,6 @@ char* freerdp_construct_path(char* base_path, char* relative_path)
return path;
}
char* freerdp_append_shared_library_suffix(char* file_path)
{
char* p;
char* path = NULL;
int file_path_length;
int shared_lib_suffix_length;
if (file_path == NULL)
return NULL;
file_path_length = strlen(file_path);
shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);
if (file_path_length >= shared_lib_suffix_length)
{
p = &file_path[file_path_length - shared_lib_suffix_length];
if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
{
path = malloc(file_path_length + shared_lib_suffix_length + 1);
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
}
else
{
path = _strdup(file_path);
}
}
else
{
path = malloc(file_path_length + shared_lib_suffix_length + 1);
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
}
return path;
}
char* freerdp_get_parent_path(char* base_path, int depth)
{
int i;
char* p;
char* path;
int length;
int base_length;
if (base_path == NULL)
return NULL;
if (depth <= 0)
return _strdup(base_path);
base_length = strlen(base_path);
p = &base_path[base_length];
for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
{
if (base_path[i] == PATH_SEPARATOR_CHR)
{
p = &base_path[i];
depth--;
}
}
length = (p - base_path);
path = (char*) malloc(length + 1);
memcpy(path, base_path, length);
path[length] = '\0';
return path;
}
BOOL freerdp_path_contains_separator(char* path)
{
if (path == NULL)
return FALSE;
if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
return FALSE;
return TRUE;
}
void freerdp_detect_paths(rdpSettings* settings)
{
freerdp_get_home_path(settings);

View File

@ -546,7 +546,7 @@ void* xf_peer_main_loop(void* arg)
server_file_path = freerdp_construct_path(settings->ConfigPath, "server");
if (!freerdp_check_file_exists(server_file_path))
if (!PathFileExistsA(server_file_path))
freerdp_mkdir(server_file_path);
settings->CertificateFile = freerdp_construct_path(server_file_path, "server.crt");

View File

@ -250,4 +250,34 @@ WINPR_API PCWSTR PathGetSharedLibraryExtensionW(unsigned long dwFlags);
#endif
/**
* Shell Path Functions
*/
#ifdef _WIN32
#include <shlwapi.h>
#endif
#define KNOWN_PATH_HOME 1
#define KNOWN_PATH_TEMP 2
#define KNOWN_PATH_DATA 3
#define KNOWN_PATH_CONFIG 4
#define KNOWN_PATH_CACHE 5
#define KNOWN_PATH_RUNTIME 6
WINPR_API char* GetKnownPath(int id);
#ifndef _WIN32
WINPR_API BOOL PathFileExistsA(LPCSTR pszPath);
WINPR_API BOOL PathFileExistsW(LPCWSTR pszPath);
#ifdef UNICODE
#define PathFileExists PathFileExistsW
#else
#define PathFileExists PathFileExistsA
#endif
#endif
#endif /* WINPR_PATH_H */

View File

@ -132,7 +132,22 @@ BOOL NeedCurrentDirectoryForExePathW(LPCWSTR ExeName)
DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
{
return 0;
int length;
char* env = NULL;
env = getenv(lpName);
if (!env)
return 0;
length = strlen(env);
if ((length + 1 > nSize) || (!lpBuffer))
return length + 1;
CopyMemory(lpBuffer, env, length + 1);
return length;
}
DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)

View File

@ -19,7 +19,8 @@ set(MODULE_NAME "winpr-path")
set(MODULE_PREFIX "WINPR_PATH")
set(${MODULE_PREFIX}_SRCS
path.c)
path.c
shell.c)
add_complex_library(MODULE ${MODULE_NAME} TYPE "OBJECT"
MONOLITHIC ${MONOLITHIC_BUILD}
@ -30,7 +31,7 @@ set_target_properties(${MODULE_NAME} PROPERTIES VERSION ${WINPR_VERSION_FULL} SO
if(MONOLITHIC_BUILD)
else()
target_link_libraries(${MODULE_NAME} winpr-crt winpr-heap)
target_link_libraries(${MODULE_NAME} winpr-crt winpr-heap winpr-environment)
install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

272
winpr/libwinpr/path/shell.c Normal file
View File

@ -0,0 +1,272 @@
/**
* WinPR: Windows Portable Runtime
* Path Functions
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <winpr/crt.h>
#include <winpr/heap.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
#include <winpr/path.h>
/**
* SHGetKnownFolderPath function:
* http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188/
*/
/**
* XDG Base Directory Specification:
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*/
char* GetEnvAlloc(LPCSTR lpName)
{
DWORD length;
char* env = NULL;
length = GetEnvironmentVariableA(lpName, NULL, 0);
if (length > 0)
{
env = malloc(length + 1);
GetEnvironmentVariableA(lpName, env, length + 1);
env[length] = '\0';
}
return env;
}
char* GetPath_HOME()
{
char* path = NULL;
#ifdef _WIN32
path = GetEnvAlloc("UserProfile");
#else
path = GetEnvAlloc("HOME");
#endif
return path;
}
char* GetPath_TEMP()
{
char* path = NULL;
#ifdef _WIN32
path = GetEnvAlloc("TEMP");
#else
path = GetEnvAlloc("TMPDIR");
if (!path)
path = _strdup("/tmp");
#endif
return path;
}
char* GetPath_XDG_DATA_HOME()
{
char* path = NULL;
char* home = NULL;
/**
* There is a single base directory relative to which user-specific data files should be written.
* This directory is defined by the environment variable $XDG_DATA_HOME.
*
* $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored.
* If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
*/
path = GetEnvAlloc("XDG_DATA_HOME");
if (path)
return path;
home = GetPath_HOME();
path = (char*) malloc(strlen(home) + strlen("/.local/share") + 1);
sprintf(path, "%s%s", home, "/.local/share");
free(home);
return path;
}
char* GetPath_XDG_CONFIG_HOME()
{
char* path = NULL;
char* home = NULL;
/**
* There is a single base directory relative to which user-specific configuration files should be written.
* This directory is defined by the environment variable $XDG_CONFIG_HOME.
*
* $XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored.
* If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.
*/
path = GetEnvAlloc("XDG_CONFIG_HOME");
if (path)
return path;
home = GetPath_HOME();
path = (char*) malloc(strlen(home) + strlen("/.config") + 1);
sprintf(path, "%s%s", home, "/.config");
free(home);
return path;
}
char* GetPath_XDG_CACHE_HOME()
{
char* path = NULL;
char* home = NULL;
/**
* There is a single base directory relative to which user-specific non-essential (cached) data should be written.
* This directory is defined by the environment variable $XDG_CACHE_HOME.
*
* $XDG_CACHE_HOME defines the base directory relative to which user specific non-essential data files should be stored.
* If $XDG_CACHE_HOME is either not set or empty, a default equal to $HOME/.cache should be used.
*/
path = GetEnvAlloc("XDG_CACHE_HOME");
if (path)
return path;
home = GetPath_HOME();
path = (char*) malloc(strlen(home) + strlen("/.cache") + 1);
sprintf(path, "%s%s", home, "/.cache");
free(home);
return path;
}
char* GetPath_XDG_RUNTIME_DIR()
{
char* path = NULL;
/**
* There is a single base directory relative to which user-specific runtime files and other file objects should be placed.
* This directory is defined by the environment variable $XDG_RUNTIME_DIR.
*
* $XDG_RUNTIME_DIR defines the base directory relative to which user-specific non-essential runtime files and other
* file objects (such as sockets, named pipes, ...) should be stored. The directory MUST be owned by the user,
* and he MUST be the only one having read and write access to it. Its Unix access mode MUST be 0700.
*
* The lifetime of the directory MUST be bound to the user being logged in. It MUST be created when the user first
* logs in and if the user fully logs out the directory MUST be removed. If the user logs in more than once he should
* get pointed to the same directory, and it is mandatory that the directory continues to exist from his first login
* to his last logout on the system, and not removed in between. Files in the directory MUST not survive reboot or a
* full logout/login cycle.
*
* The directory MUST be on a local file system and not shared with any other system. The directory MUST by fully-featured
* by the standards of the operating system. More specifically, on Unix-like operating systems AF_UNIX sockets,
* symbolic links, hard links, proper permissions, file locking, sparse files, memory mapping, file change notifications,
* a reliable hard link count must be supported, and no restrictions on the file name character set should be imposed.
* Files in this directory MAY be subjected to periodic clean-up. To ensure that your files are not removed, they should
* have their access time timestamp modified at least once every 6 hours of monotonic time or the 'sticky' bit should be
* set on the file.
*
* If $XDG_RUNTIME_DIR is not set applications should fall back to a replacement directory with similar capabilities and
* print a warning message. Applications should use this directory for communication and synchronization purposes and
* should not place larger files in it, since it might reside in runtime memory and cannot necessarily be swapped out to disk.
*/
path = GetEnvAlloc("XDG_RUNTIME_DIR");
if (path)
return path;
path = GetPath_TEMP();
return path;
}
char* GetKnownPath(int id)
{
char* path = NULL;
switch (id)
{
case KNOWN_PATH_HOME:
path = GetPath_HOME();
break;
case KNOWN_PATH_TEMP:
path = GetPath_TEMP();
break;
case KNOWN_PATH_DATA:
path = GetPath_XDG_DATA_HOME();
break;
case KNOWN_PATH_CONFIG:
path = GetPath_XDG_CONFIG_HOME();
break;
case KNOWN_PATH_CACHE:
path = GetPath_XDG_CACHE_HOME();
break;
case KNOWN_PATH_RUNTIME:
path = GetPath_XDG_RUNTIME_DIR();
break;
default:
path = NULL;
break;
}
return path;
}
#ifndef _WIN32
BOOL PathFileExistsA(LPCSTR pszPath)
{
struct stat stat_info;
if (stat(pszPath, &stat_info) != 0)
return FALSE;
return TRUE;
}
BOOL PathFileExistsW(LPCWSTR pszPath)
{
return FALSE;
}
#endif

View File

@ -26,7 +26,8 @@ set(${MODULE_PREFIX}_TESTS
TestPathCchSkipRoot.c
TestPathCchStripToRoot.c
TestPathCchStripPrefix.c
TestPathCchRemoveFileSpec.c)
TestPathCchRemoveFileSpec.c
TestPathShell.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}

View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>
int TestPathShell(int argc, char* argv[])
{
char* path;
path = GetKnownPath(KNOWN_PATH_HOME);
printf("KNOWN_PATH_HOME: %s\n", path);
path = GetKnownPath(KNOWN_PATH_TEMP);
printf("KNOWN_PATH_TEMP: %s\n", path);
path = GetKnownPath(KNOWN_PATH_DATA);
printf("KNOWN_PATH_DATA: %s\n", path);
path = GetKnownPath(KNOWN_PATH_CONFIG);
printf("KNOWN_PATH_CONFIG: %s\n", path);
path = GetKnownPath(KNOWN_PATH_CACHE);
printf("KNOWN_PATH_CACHE: %s\n", path);
path = GetKnownPath(KNOWN_PATH_RUNTIME);
printf("KNOWN_PATH_RUNTIME: %s\n", path);
return 0;
}