OUT-573: Fix custom paths for user folder on Windows sync
authorThomas
Fri, 13 Sep 2019 15:06:14 +0200
branchsync
changeset 4061b242b0e23479
parent 4060 09f80c60c1c9
child 4062 ff1743d684d2
child 4066 6d4d7ea7823e
OUT-573: Fix custom paths for user folder on Windows
src/platform_windows.cpp
     1.1 --- a/src/platform_windows.cpp	Fri Sep 06 10:38:53 2019 +0200
     1.2 +++ b/src/platform_windows.cpp	Fri Sep 13 15:06:14 2019 +0200
     1.3 @@ -25,8 +25,8 @@
     1.4  #define LOCAL_DB_FILENAME "management.db"
     1.5  #define SYSTEM_DB_FILENAME "system.db"
     1.6  #define KEYS_DB "keys.db"
     1.7 -#define USER_FOLDER_PATH pEpUserFolderPath().c_str()
     1.8 -#define SYSTEM_FOLDER_PATH pEpSystemFolderPath().c_str()
     1.9 +#define USER_FOLDER_PATH _per_user_directory()
    1.10 +#define SYSTEM_FOLDER_PATH _per_machine_directory()
    1.11  
    1.12  #ifndef WC_ERR_INVALID_CHARS
    1.13  #define WC_ERR_INVALID_CHARS      0x00000080  // error for invalid chars
    1.14 @@ -140,72 +140,68 @@
    1.15  	return path;
    1.16  }
    1.17  
    1.18 -static inline string pEpSystemFolderPath(void)
    1.19 +const char *_per_machine_directory(void)
    1.20  {
    1.21 -	static TCHAR tPath[PATH_BUF_SIZE];
    1.22 -	string path = PER_MACHINE_DIRECTORY;
    1.23 +	static string path;
    1.24 +	if (path.length())
    1.25 +		return path.c_str();
    1.26 +
    1.27 +	TCHAR tPath[PATH_BUF_SIZE];
    1.28  
    1.29  	// Get SystemFolder Registry value and use if available
    1.30  	bool result = readRegistryString(HKEY_CURRENT_USER,
    1.31  		TEXT("SOFTWARE\\pEp"), TEXT("SystemFolder"), tPath,
    1.32  		PATH_BUF_SIZE, NULL);
    1.33  
    1.34 -	if (result)
    1.35 -		path = utf8_string(tPath);
    1.36 +	// If not Registry value was found, use default
    1.37 +	if (!result) {
    1.38 +		DWORD length = ExpandEnvironmentStringsW(utf16_string(string(PER_MACHINE_DIRECTORY)).c_str(),
    1.39 +			tPath, PATH_BUF_SIZE);
    1.40 +		assert(length);
    1.41 +		if (length == 0)
    1.42 +			throw bad_alloc(); // BUG: there are other errors possible beside out of memory
    1.43 +	}
    1.44  
    1.45 -	return path;
    1.46 +	path = utf8_string(wstring(tPath));
    1.47 +	return path.c_str();
    1.48  }
    1.49  
    1.50 -static inline string pEpUserFolderPath(void)
    1.51 +const char *_per_user_directory(void)
    1.52  {
    1.53 -	static TCHAR tPath[PATH_BUF_SIZE];
    1.54 -	string path = PER_USER_DIRECTORY;
    1.55 +	static string path;
    1.56 +	if (path.length())
    1.57 +		return path.c_str();
    1.58 +
    1.59 +	TCHAR tPath[PATH_BUF_SIZE];
    1.60  
    1.61  	// Get UserFolder Registry value and use if available
    1.62  	bool result = readRegistryString(HKEY_CURRENT_USER,
    1.63  		TEXT("SOFTWARE\\pEp"), TEXT("UserFolder"), tPath,
    1.64  		PATH_BUF_SIZE, NULL);
    1.65  
    1.66 -	if (result)
    1.67 -		path = utf8_string(tPath);
    1.68 -	
    1.69 -	return path;
    1.70 +	// If not Registry value was found, use default
    1.71 +	if (!result) {
    1.72 +		DWORD length = ExpandEnvironmentStringsW(utf16_string(string(PER_USER_DIRECTORY)).c_str(),
    1.73 +			tPath, PATH_BUF_SIZE);
    1.74 +		assert(length);
    1.75 +		if (length == 0)
    1.76 +			throw bad_alloc(); // BUG: there are other errors possible beside out of memory
    1.77 +	}
    1.78 +
    1.79 +	path = utf8_string(wstring(tPath));
    1.80 +	return path.c_str();
    1.81  }
    1.82  
    1.83  extern "C" {
    1.84  
    1.85  DYNAMIC_API const char *per_user_directory(void)
    1.86  {
    1.87 -    static string path;
    1.88 -    if (path.length())
    1.89 -        return path.c_str();
    1.90 -
    1.91 -    TCHAR tPath[PATH_BUF_SIZE];
    1.92 -    DWORD length = ExpandEnvironmentStringsW(utf16_string(string(PER_USER_DIRECTORY)).c_str(),
    1.93 -            tPath, PATH_BUF_SIZE);
    1.94 -	assert(length);
    1.95 -    if (length == 0)
    1.96 -        throw bad_alloc(); // BUG: there are other errors possible beside out of memory
    1.97 -
    1.98 -    path = utf8_string(wstring(tPath));
    1.99 -    return path.c_str();
   1.100 +	return _per_user_directory();
   1.101  }
   1.102  
   1.103  DYNAMIC_API const char *per_machine_directory(void)
   1.104  {
   1.105 -    static string path;
   1.106 -    if (path.length())
   1.107 -        return path.c_str();
   1.108 -
   1.109 -    TCHAR tPath[PATH_BUF_SIZE];
   1.110 -    DWORD length = ExpandEnvironmentStringsW(utf16_string(string(PER_MACHINE_DIRECTORY)).c_str(),
   1.111 -            tPath, PATH_BUF_SIZE);
   1.112 -	assert(length);
   1.113 -    if (length == 0)
   1.114 -        throw bad_alloc(); // BUG: there are other errors possible beside out of memory
   1.115 -
   1.116 -    path = utf8_string(wstring(tPath));
   1.117 -    return path.c_str();
   1.118 +	return _per_machine_directory();
   1.119  }
   1.120  
   1.121  void *dlopen(const char *filename, int flag) {
   1.122 @@ -278,7 +274,7 @@
   1.123  const char *windoze_local_db(void) {
   1.124  	static string path;
   1.125  	if (path.length() == 0)
   1.126 -        path = managementPath(PER_USER_DIRECTORY, LOCAL_DB_FILENAME);
   1.127 +        path = managementPath(USER_FOLDER_PATH, LOCAL_DB_FILENAME);
   1.128      return path.c_str();
   1.129  }
   1.130