Menu

[r214]: / trunk / ext / build / config_win.c  Maximize  Restore  History

Download this file

352 lines (299 with data), 11.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* config_win.c : parsing configuration data from the registry
*
* ====================================================================
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
*/
#include "svn_private_config.h"
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#ifdef APR_HAVE_IPV6
#include "winsock2.h"
#include "Ws2tcpip.h"
#include "Wspiapi.h"
#endif
#include <windows.h>
#include <shlobj.h>
#include <apr_file_info.h>
/* FIXME: We're using an internal APR header here, which means we
have to build Subversion with APR sources. This being Win32-only,
that should be fine for now, but a better solution must be found in
combination with issue #850. */
#include <arch/win32/apr_arch_utf8.h>
#include "svn_error.h"
#include "svn_path.h"
#include "svn_pools.h"
#include "svn_utf.h"
svn_error_t *
svn_config__win_config_path(const char **folder, int system_path,
apr_pool_t *pool)
{
/* ### Adding CSIDL_FLAG_CREATE here, because those folders really
must exist. I'm not too sure about the SHGFP_TYPE_CURRENT
semancics, though; maybe we should use ..._DEFAULT instead? */
const int csidl = ((system_path ? CSIDL_COMMON_APPDATA : CSIDL_APPDATA)
| CSIDL_FLAG_CREATE);
int style;
apr_status_t apr_err = apr_filepath_encoding(&style, pool);
if (apr_err)
return svn_error_wrap_apr(apr_err,
"Can't determine the native path encoding");
if (style == APR_FILEPATH_ENCODING_UTF8)
{
WCHAR folder_ucs2[MAX_PATH];
apr_size_t inwords, outbytes, outlength;
char *folder_utf8;
if (S_OK != SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT,
folder_ucs2))
goto no_folder_path;
/* ### When mapping from UCS-2 to UTF-8, we need at most 3 bytes
per wide char, plus extra space for the nul terminator. */
inwords = lstrlenW(folder_ucs2);
outbytes = outlength = 3 * (inwords + 1);
folder_utf8 = apr_palloc(pool, outlength);
apr_err = apr_conv_ucs2_to_utf8(folder_ucs2, &inwords,
folder_utf8, &outbytes);
if (!apr_err && (inwords > 0 || outbytes == 0))
apr_err = APR_INCOMPLETE;
if (apr_err)
return svn_error_wrap_apr(apr_err,
"Can't convert config path to UTF-8");
/* Note that apr_conv_ucs2_to_utf8 does _not_ terminate the
outgoing buffer. */
folder_utf8[outlength - outbytes] = '\0';
*folder = folder_utf8;
}
else if (style == APR_FILEPATH_ENCODING_LOCALE)
{
char folder_ansi[MAX_PATH];
if (S_OK != SHGetFolderPathA(NULL, csidl, NULL, SHGFP_TYPE_CURRENT,
folder_ansi))
goto no_folder_path;
SVN_ERR(svn_utf_cstring_to_utf8(folder, folder_ansi, pool));
}
else
{
/* There is no third option on Windows; we should never get here. */
return svn_error_createf(APR_EINVAL, NULL,
"Unknown native path encoding (%d)", style);
}
*folder = svn_path_internal_style(*folder, pool);
return SVN_NO_ERROR;
no_folder_path:
return svn_error_create(SVN_ERR_BAD_FILENAME, NULL,
(system_path
? "Can't determine the system config path"
: "Can't determine the user's config path"));
}
/* Convert UTF8, a UTF-8 encoded string, to UCS2, a UCS-2 encoded
string, using POOL for temporary allocations. */
static svn_error_t *
utf8_to_ucs2(WCHAR **ucs2, const char *utf8, apr_pool_t *pool)
{
apr_size_t inbytes, outwords, outlength;
apr_status_t apr_err;
inbytes = lstrlenA(utf8);
outwords = outlength = inbytes + 1; /* Include terminating null. */
*ucs2 = apr_palloc(pool, outwords * sizeof(WCHAR));
apr_err = apr_conv_utf8_to_ucs2(utf8, &inbytes, *ucs2, &outwords);
if (!apr_err && (inbytes > 0 || outwords == 0))
apr_err = APR_INCOMPLETE;
if (apr_err)
return svn_error_wrap_apr(apr_err, "Can't convert config path to UCS-2");
/* Note that apr_conv_utf8_to_ucs2 does _not_ terminate the
outgoing buffer. */
(*ucs2)[outlength - outwords] = L'\0';
return SVN_NO_ERROR;
}
#include "config_impl.h"
svn_error_t *
svn_config__open_file(FILE **pfile,
const char *filename,
const char *mode,
apr_pool_t *pool)
{
int style;
apr_status_t apr_err = apr_filepath_encoding(&style, pool);
if (apr_err)
return svn_error_wrap_apr(apr_err,
"Can't determine the native path encoding");
if (style == APR_FILEPATH_ENCODING_UTF8)
{
WCHAR *filename_ucs2;
WCHAR *mode_ucs2;
SVN_ERR(utf8_to_ucs2(&filename_ucs2, filename, pool));
SVN_ERR(utf8_to_ucs2(&mode_ucs2, mode, pool));
*pfile = _wfopen(filename_ucs2, mode_ucs2);
}
else if (style == APR_FILEPATH_ENCODING_LOCALE)
{
const char *filename_native;
SVN_ERR(svn_utf_cstring_from_utf8(&filename_native, filename, pool));
*pfile = fopen(filename_native, mode);
}
else
{
/* There is no third option on Windows; we should never get here. */
return svn_error_createf(APR_EINVAL, NULL,
"Unknown native path encoding (%d)", style);
}
return SVN_NO_ERROR;
}
/* ### These constants are insanely large, but (a) we want to avoid
reallocating strings if possible, and (b) the realloc logic might
not actually work -- you never know with Win32 ... */
#define SVN_REG_DEFAULT_NAME_SIZE 2048
#define SVN_REG_DEFAULT_VALUE_SIZE 8192
static svn_error_t *
parse_section(svn_config_t *cfg, HKEY hkey, const char *section,
svn_stringbuf_t *option, svn_stringbuf_t *value)
{
DWORD option_len, type, index;
LONG err;
/* Start with a reasonable size for the buffers. */
svn_stringbuf_ensure(option, SVN_REG_DEFAULT_NAME_SIZE);
svn_stringbuf_ensure(value, SVN_REG_DEFAULT_VALUE_SIZE);
for (index = 0; ; ++index)
{
option_len = option->blocksize;
err = RegEnumValue(hkey, index, option->data, &option_len,
NULL, &type, NULL, NULL);
if (err == ERROR_NO_MORE_ITEMS)
break;
if (err == ERROR_INSUFFICIENT_BUFFER)
{
svn_stringbuf_ensure(option, option_len);
err = RegEnumValue(hkey, index, option->data, &option_len,
NULL, &type, NULL, NULL);
}
if (err != ERROR_SUCCESS)
return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
"Can't enumerate registry values");
/* Ignore option names that start with '#', see
http://subversion.tigris.org/issues/show_bug.cgi?id=671 */
if (type == REG_SZ && option->data[0] != '#')
{
DWORD value_len = value->blocksize;
err = RegQueryValueEx(hkey, option->data, NULL, NULL,
value->data, &value_len);
if (err == ERROR_MORE_DATA)
{
svn_stringbuf_ensure(value, value_len);
err = RegQueryValueEx(hkey, option->data, NULL, NULL,
value->data, &value_len);
}
if (err != ERROR_SUCCESS)
return svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
"Can't read registry value data");
svn_config_set(cfg, section, option->data, value->data);
}
}
return SVN_NO_ERROR;
}
/*** Exported interface. ***/
svn_error_t *
svn_config__parse_registry(svn_config_t *cfg, const char *file,
svn_boolean_t must_exist, apr_pool_t *pool)
{
apr_pool_t *subpool;
svn_stringbuf_t *section, *option, *value;
svn_error_t *svn_err = SVN_NO_ERROR;
HKEY base_hkey, hkey;
DWORD index;
LONG err;
if (0 == strncmp(file, SVN_REGISTRY_HKLM, SVN_REGISTRY_HKLM_LEN))
{
base_hkey = HKEY_LOCAL_MACHINE;
file += SVN_REGISTRY_HKLM_LEN;
}
else if (0 == strncmp(file, SVN_REGISTRY_HKCU, SVN_REGISTRY_HKCU_LEN))
{
base_hkey = HKEY_CURRENT_USER;
file += SVN_REGISTRY_HKCU_LEN;
}
else
{
return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
"Unrecognised registry path '%s'",
svn_path_local_style(file, pool));
}
err = RegOpenKeyEx(base_hkey, file, 0,
KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
&hkey);
if (err != ERROR_SUCCESS)
{
const int is_enoent = APR_STATUS_IS_ENOENT(APR_FROM_OS_ERROR(err));
if (!is_enoent)
return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
"Can't open registry key '%s'",
svn_path_local_style(file, pool));
else if (must_exist && is_enoent)
return svn_error_createf(SVN_ERR_BAD_FILENAME, NULL,
"Can't find registry key '%s'",
svn_path_local_style(file, pool));
else
return SVN_NO_ERROR;
}
subpool = svn_pool_create(pool);
section = svn_stringbuf_create("", subpool);
option = svn_stringbuf_create("", subpool);
value = svn_stringbuf_create("", subpool);
/* The top-level values belong to the [DEFAULT] section */
svn_err = parse_section(cfg, hkey, SVN_CONFIG__DEFAULT_SECTION,
option, value);
if (svn_err)
goto cleanup;
/* Now enumerate the rest of the keys. */
svn_stringbuf_ensure(section, SVN_REG_DEFAULT_NAME_SIZE);
for (index = 0; ; ++index)
{
DWORD section_len = section->blocksize;
FILETIME last_write_time;
HKEY sub_hkey;
err = RegEnumKeyEx(hkey, index, section->data, &section_len,
NULL, NULL, NULL, &last_write_time);
if (err == ERROR_NO_MORE_ITEMS)
break;
if (err == ERROR_MORE_DATA)
{
svn_stringbuf_ensure(section, section_len);
err = RegEnumKeyEx(hkey, index, section->data, &section_len,
NULL, NULL, NULL, &last_write_time);
}
if (err != ERROR_SUCCESS)
{
svn_err = svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
"Can't enumerate registry keys");
goto cleanup;
}
err = RegOpenKeyEx(hkey, section->data, 0,
KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
&sub_hkey);
if (err != ERROR_SUCCESS)
{
svn_err = svn_error_create(SVN_ERR_MALFORMED_FILE, NULL,
"Can't open existing subkey");
goto cleanup;
}
svn_err = parse_section(cfg, sub_hkey, section->data, option, value);
RegCloseKey(sub_hkey);
if (svn_err)
goto cleanup;
}
cleanup:
RegCloseKey(hkey);
svn_pool_destroy(subpool);
return svn_err;
}
#endif /* WIN32 */
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.