xref: /trunk/main/registry/source/registry.cxx (revision 51134e9e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_registry.hxx"
26 
27 #include <registry/registry.h>
28 #include <registry/registry.hxx>
29 #include <osl/process.h>
30 
31 #include "keyimpl.hxx"
32 #include "regimpl.hxx"
33 #include "regkey.hxx"
34 
35 #if defined(WIN32) || defined(WNT) || defined(OS2)
36 #include <io.h>
37 #endif
38 
39 #include <string.h>
40 #if defined(UNX)
41 #include <stdlib.h>
42 #include <unistd.h>
43 #endif
44 
45 #if defined ( GCC ) && ( defined ( SCO ) )
46 ORealDynamicLoader* ODynamicLoader<Registry_Api>::m_pLoader = NULL;
47 #endif
48 
49 extern "C" {
50 
51 //*********************************************************************
52 //	acquire
53 //
acquire(RegHandle hReg)54 static void REGISTRY_CALLTYPE acquire(RegHandle hReg)
55 {
56 	ORegistry* pReg = (ORegistry*) hReg;
57 
58 	if (pReg != NULL)
59 		pReg->acquire();
60 }
61 
62 
63 //*********************************************************************
64 //	release
65 //
release(RegHandle hReg)66 static void REGISTRY_CALLTYPE release(RegHandle hReg)
67 {
68 	ORegistry* pReg = (ORegistry*) hReg;
69 
70 	if (pReg)
71 	{
72 		if (pReg->release() == 0)
73 		{
74 			delete pReg;
75 			hReg = NULL;
76 		}
77 	}
78 }
79 
80 
81 //*********************************************************************
82 //	getName
83 //
getName(RegHandle hReg,rtl_uString ** pName)84 static RegError REGISTRY_CALLTYPE getName(RegHandle hReg, rtl_uString** pName)
85 {
86 	ORegistry* 	pReg;
87 
88 	if (hReg)
89 	{
90 		pReg = (ORegistry*)hReg;
91 		if ( pReg->isOpen() )
92 		{
93 			rtl_uString_assign(pName, pReg->getName().pData);
94 			return REG_NO_ERROR;
95 		} else
96 		{
97 			rtl_uString_new(pName);
98 			return REG_REGISTRY_NOT_OPEN;
99 		}
100 	}
101 
102 	rtl_uString_new(pName);
103 	return REG_INVALID_REGISTRY;
104 }
105 
106 
107 //*********************************************************************
108 //	isReadOnly
109 //
isReadOnly(RegHandle hReg)110 static sal_Bool REGISTRY_CALLTYPE isReadOnly(RegHandle hReg)
111 {
112 	if (hReg)
113 		return ((ORegistry*)hReg)->isReadOnly();
114 	else
115 		return sal_False;
116 }
117 
118 
119 //*********************************************************************
120 //	createRegistry
121 //
createRegistry(rtl_uString * registryName,RegHandle * phRegistry)122 static RegError REGISTRY_CALLTYPE createRegistry(rtl_uString* registryName,
123 											     RegHandle* phRegistry)
124 {
125 	RegError ret;
126 
127 	ORegistry* pReg = new ORegistry();
128 	if ((ret = pReg->initRegistry(registryName, REG_CREATE)))
129 	{
130 		*phRegistry = NULL;
131 		return ret;
132 	}
133 
134 	*phRegistry = pReg;
135 
136 	return REG_NO_ERROR;
137 }
138 
139 //*********************************************************************
140 //	openRootKey
141 //
openRootKey(RegHandle hReg,RegKeyHandle * phRootKey)142 static RegError REGISTRY_CALLTYPE openRootKey(RegHandle hReg,
143 											  RegKeyHandle* phRootKey)
144 {
145 	ORegistry* pReg;
146 
147 	if (hReg)
148 	{
149 		pReg = (ORegistry*)hReg;
150 		if (!pReg->isOpen())
151 			return REG_REGISTRY_NOT_OPEN;
152 	} else
153 	{
154 		phRootKey = NULL;
155 		return REG_INVALID_REGISTRY;
156 	}
157 
158 	*phRootKey = pReg->getRootKey();
159 
160 	return REG_NO_ERROR;
161 }
162 
163 
164 //*********************************************************************
165 //	openRegistry
166 //
openRegistry(rtl_uString * registryName,RegHandle * phRegistry,RegAccessMode accessMode)167 static RegError REGISTRY_CALLTYPE openRegistry(rtl_uString* registryName,
168 											   RegHandle* phRegistry,
169 											   RegAccessMode accessMode)
170 {
171 	RegError _ret;
172 
173 	ORegistry* pReg = new ORegistry();
174 	if ((_ret = pReg->initRegistry(registryName, accessMode)))
175 	{
176 		*phRegistry = NULL;
177 		delete pReg;
178 		return _ret;
179 	}
180 
181 
182 	*phRegistry = pReg;
183 
184 	return REG_NO_ERROR;
185 }
186 
187 //*********************************************************************
188 //	closeRegistry
189 //
closeRegistry(RegHandle hReg)190 static RegError REGISTRY_CALLTYPE closeRegistry(RegHandle hReg)
191 {
192 	ORegistry	*pReg;
193 
194 	if (hReg)
195 	{
196 		pReg = (ORegistry*)hReg;
197 		if (!pReg->isOpen())
198 			return REG_REGISTRY_NOT_OPEN;
199 
200 		RegError ret = REG_NO_ERROR;
201 		if (pReg->release() == 0)
202 		{
203 			delete(pReg);
204 			hReg = NULL;
205 		}
206 		else
207 			ret = pReg->closeRegistry();
208 
209 		return ret;
210 	} else
211 	{
212 		return REG_INVALID_REGISTRY;
213 	}
214 }
215 
216 
217 //*********************************************************************
218 //	destroyRegistry
219 //
destroyRegistry(RegHandle hReg,rtl_uString * registryName)220 static RegError REGISTRY_CALLTYPE destroyRegistry(RegHandle hReg,
221 												  rtl_uString* registryName)
222 {
223 	ORegistry	*pReg;
224 
225 	if (hReg)
226 	{
227 		pReg = (ORegistry*)hReg;
228 		if (!pReg->isOpen())
229 			return REG_INVALID_REGISTRY;
230 
231 		RegError ret = pReg->destroyRegistry(registryName);
232 		if (!ret)
233 		{
234 			if (!registryName->length)
235 			{
236 				delete(pReg);
237 				hReg = NULL;
238 			}
239 		}
240 		return ret;
241 	} else
242 	{
243 		return REG_INVALID_REGISTRY;
244 	}
245 }
246 
247 
248 //*********************************************************************
249 //	loadRegKey
250 //
loadKey(RegHandle hReg,RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName)251 static RegError REGISTRY_CALLTYPE loadKey(RegHandle hReg,
252 								   		   RegKeyHandle hKey,
253 										   rtl_uString* keyName,
254 										   rtl_uString* regFileName)
255 {
256 
257 	ORegistry* pReg = static_cast< ORegistry* >(hReg);
258 	if (!pReg)
259 		return REG_INVALID_REGISTRY;
260 
261 	if (!pReg->isOpen())
262 		return REG_REGISTRY_NOT_OPEN;
263 
264 	ORegKey* pKey = static_cast< ORegKey* >(hKey);
265 	if (!pKey)
266 		return REG_INVALID_KEY;
267 
268 	if (pKey->getRegistry() != pReg)
269 		return REG_INVALID_KEY;
270 	if (pKey->isDeleted())
271 		return REG_INVALID_KEY;
272 	if (pKey->isReadOnly())
273 		return REG_REGISTRY_READONLY;
274 
275 
276 	ORegKey* pNewKey = 0;
277 	RegError _ret = pKey->openKey(keyName, (RegKeyHandle*)&pNewKey);
278 	if (_ret == REG_NO_ERROR)
279 	{
280 		pKey->releaseKey(pNewKey);
281 		pKey->deleteKey(keyName);
282 	}
283 
284 	_ret = pKey->createKey(keyName, (RegKeyHandle*)&pNewKey);
285 	if (_ret != REG_NO_ERROR)
286 		return _ret;
287 
288 	_ret = pReg->loadKey(pNewKey, regFileName);
289 	if (_ret != REG_NO_ERROR)
290 	{
291 		pKey->releaseKey(pNewKey);
292 		pKey->deleteKey(keyName);
293 		return _ret;
294 	}
295 
296 	return pKey->closeKey(pNewKey);
297 }
298 
299 //*********************************************************************
300 //	saveKey
301 //
saveKey(RegHandle hReg,RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName)302 static RegError REGISTRY_CALLTYPE saveKey(RegHandle hReg,
303 								   		   RegKeyHandle hKey,
304 										   rtl_uString* keyName,
305 										   rtl_uString* regFileName)
306 {
307 
308 	ORegistry* pReg = static_cast< ORegistry* >(hReg);
309 	if (!pReg)
310 		return REG_INVALID_REGISTRY;
311 
312 	if (!pReg->isOpen())
313 		return REG_REGISTRY_NOT_OPEN;
314 
315 	ORegKey* pKey = static_cast< ORegKey* >(hKey);
316 	if (!pKey)
317 		return REG_INVALID_KEY;
318 
319 	if (pKey->getRegistry() != pReg)
320 		return REG_INVALID_KEY;
321 	if (pKey->isDeleted())
322 		return REG_INVALID_KEY;
323 
324 	ORegKey* pNewKey = 0;
325 	RegError _ret = pKey->openKey(keyName, (RegKeyHandle*)&pNewKey);
326 	if (_ret != REG_NO_ERROR)
327 		return _ret;
328 
329 	_ret = pReg->saveKey(pNewKey, regFileName);
330 	if (_ret != REG_NO_ERROR)
331 	{
332 		(void) pKey->releaseKey(pNewKey);
333 		return _ret;
334 	}
335 
336 	return pKey->releaseKey(pNewKey);
337 }
338 
339 //*********************************************************************
340 //	mergeKey
341 //
mergeKey(RegHandle hReg,RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName,sal_Bool bWarnings,sal_Bool bReport)342 static RegError REGISTRY_CALLTYPE mergeKey(RegHandle hReg,
343 								   		   RegKeyHandle hKey,
344 										   rtl_uString* keyName,
345 										   rtl_uString* regFileName,
346 										   sal_Bool bWarnings,
347 										   sal_Bool bReport)
348 {
349 	ORegistry* pReg = static_cast< ORegistry* >(hReg);
350 	if (!pReg)
351 		return REG_INVALID_REGISTRY;
352 	if (!pReg->isOpen())
353 		return REG_REGISTRY_NOT_OPEN;
354 
355 	ORegKey* pKey = static_cast< ORegKey* >(hKey);
356 	if (!pKey)
357 		return REG_INVALID_KEY;
358 	if (pKey->getRegistry() != pReg)
359 		return REG_INVALID_KEY;
360 	if (pKey->isDeleted())
361 		return REG_INVALID_KEY;
362 	if (pKey->isReadOnly())
363 		return REG_REGISTRY_READONLY;
364 
365 	if (keyName->length)
366 	{
367 		ORegKey* pNewKey = 0;
368 		RegError _ret = pKey->createKey(keyName, (RegKeyHandle*)&pNewKey);
369 		if (_ret != REG_NO_ERROR)
370 			return _ret;
371 
372 		_ret = pReg->loadKey(pNewKey, regFileName, bWarnings, bReport);
373 		if (_ret == REG_MERGE_ERROR || (_ret == REG_MERGE_CONFLICT && bWarnings))
374 		{
375 			if (pNewKey != pKey)
376 				(void) pKey->closeKey(pNewKey);
377 			else
378 				(void) pKey->releaseKey(pNewKey);
379 			return _ret;
380 		}
381 
382 		return (pNewKey != pKey) ? pKey->closeKey(pNewKey) : pKey->releaseKey(pNewKey);
383 	}
384 
385 	return pReg->loadKey(pKey, regFileName, bWarnings, bReport);
386 }
387 
388 //*********************************************************************
389 //	dumpRegistry
390 //
dumpRegistry(RegHandle hReg,RegKeyHandle hKey)391 static RegError REGISTRY_CALLTYPE dumpRegistry(RegHandle hReg,
392 											   RegKeyHandle hKey)
393 {
394 	ORegistry* pReg = static_cast< ORegistry* >(hReg);
395 	if (!pReg)
396 		return REG_INVALID_REGISTRY;
397 	if (!pReg->isOpen())
398 		return REG_REGISTRY_NOT_OPEN;
399 
400 	ORegKey* pKey = static_cast< ORegKey* >(hKey);
401 	if (!pKey)
402 		return REG_INVALID_KEY;
403 	if (pKey->getRegistry() != pReg)
404 		return REG_INVALID_KEY;
405 	if (pKey->isDeleted())
406 		return REG_INVALID_KEY;
407 
408 	return pReg->dumpRegistry(hKey);
409 }
410 
411 //*********************************************************************
412 //	initRegistry_Api
413 //
initRegistry_Api(void)414 Registry_Api* REGISTRY_CALLTYPE initRegistry_Api(void)
415 {
416 	static Registry_Api aApi= {&acquire,
417 							   &release,
418 							   &isReadOnly,
419 							   &openRootKey,
420 							   &getName,
421 							   &createRegistry,
422 							   &openRegistry,
423 							   &closeRegistry,
424 							   &destroyRegistry,
425 							   &loadKey,
426 							   &saveKey,
427 							   &mergeKey,
428 							   &dumpRegistry,
429 							   &acquireKey,
430 							   &releaseKey,
431 							   &isKeyReadOnly,
432 							   &getKeyName,
433 							   &createKey,
434 							   &openKey,
435 							   &openSubKeys,
436 							   &closeSubKeys,
437 							   &deleteKey,
438 							   &closeKey,
439 							   &setValue,
440 							   &setLongListValue,
441 							   &setStringListValue,
442 							   &setUnicodeListValue,
443 							   &getValueInfo,
444 							   &getValue,
445 							   &getLongListValue,
446 							   &getStringListValue,
447 							   &getUnicodeListValue,
448 							   &freeValueList,
449 							   &createLink,
450 							   &deleteLink,
451 							   &getKeyType,
452 							   &getLinkTarget,
453 							   &getResolvedKeyName,
454 							   &getKeyNames,
455 							   &freeKeyNames};
456 
457 	return (&aApi);
458 }
459 
460 }
461 
462 //*********************************************************************
463 //	reg_loadRegKey
464 //
reg_loadKey(RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName)465 RegError REGISTRY_CALLTYPE reg_loadKey(RegKeyHandle hKey,
466 									   rtl_uString* keyName,
467 									   rtl_uString* regFileName)
468 {
469 	ORegKey *pKey;
470 
471 	if (hKey)
472 		pKey = (ORegKey*)hKey;
473 	else
474 		return REG_INVALID_KEY;
475 
476 	return loadKey(pKey->getRegistry(), hKey, keyName, regFileName);
477 }
478 
479 //*********************************************************************
480 //	reg_saveKey
481 //
reg_saveKey(RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName)482 RegError REGISTRY_CALLTYPE reg_saveKey(RegKeyHandle hKey,
483 									   rtl_uString* keyName,
484 									   rtl_uString* regFileName)
485 {
486 	ORegKey *pKey;
487 
488 	if (hKey)
489 		pKey = (ORegKey*)hKey;
490 	else
491 		return REG_INVALID_KEY;
492 
493 	return saveKey(pKey->getRegistry(), hKey, keyName, regFileName);
494 }
495 
496 //*********************************************************************
497 //	reg_mergeKey
498 //
reg_mergeKey(RegKeyHandle hKey,rtl_uString * keyName,rtl_uString * regFileName,sal_Bool bWarnings,sal_Bool bReport)499 RegError REGISTRY_CALLTYPE reg_mergeKey(RegKeyHandle hKey,
500 									    rtl_uString* keyName,
501 									    rtl_uString* regFileName,
502 										sal_Bool bWarnings,
503 										sal_Bool bReport)
504 {
505 	ORegKey *pKey;
506 
507 	if (hKey)
508 		pKey = (ORegKey*)hKey;
509 	else
510 		return REG_INVALID_KEY;
511 
512 	return mergeKey(pKey->getRegistry(), hKey, keyName, regFileName, bWarnings, bReport);
513 }
514 
515 //*********************************************************************
516 //	reg_createRegistry
517 //
reg_createRegistry(rtl_uString * registryName,RegHandle * phRegistry)518 RegError REGISTRY_CALLTYPE reg_createRegistry(rtl_uString* registryName,
519 											  RegHandle* phRegistry)
520 {
521 	RegError ret;
522 
523 	ORegistry* pReg = new ORegistry();
524 	if ((ret = pReg->initRegistry(registryName, REG_CREATE)))
525 	{
526 		*phRegistry = NULL;
527 		return ret;
528 	}
529 
530 	*phRegistry = pReg;
531 
532 	return REG_NO_ERROR;
533 }
534 
535 //*********************************************************************
536 //	reg_openRootKey
537 //
reg_openRootKey(RegHandle hRegistry,RegKeyHandle * phRootKey)538 RegError REGISTRY_CALLTYPE reg_openRootKey(RegHandle hRegistry,
539 										  RegKeyHandle* phRootKey)
540 {
541 	return openRootKey(hRegistry, phRootKey);
542 }
543 
544 
545 //*********************************************************************
546 //	reg_getName
547 //
reg_getName(RegHandle hRegistry,rtl_uString ** pName)548 RegError REGISTRY_CALLTYPE reg_getName(RegHandle hRegistry, rtl_uString** pName)
549 {
550 	return getName(hRegistry, pName);
551 }
552 
553 
554 //*********************************************************************
555 //	reg_isReadOnly
556 //
reg_isReadOnly(RegHandle hRegistry)557 sal_Bool REGISTRY_CALLTYPE reg_isReadOnly(RegHandle hRegistry)
558 {
559 	return isReadOnly(hRegistry);
560 }
561 
562 
563 //*********************************************************************
564 //	reg_openRegistry
565 //
reg_openRegistry(rtl_uString * registryName,RegHandle * phRegistry,RegAccessMode accessMode)566 RegError REGISTRY_CALLTYPE reg_openRegistry(rtl_uString* registryName,
567 											RegHandle* phRegistry,
568 											RegAccessMode accessMode)
569 {
570 	RegError _ret;
571 
572 	ORegistry* pReg = new ORegistry();
573 	if ((_ret = pReg->initRegistry(registryName, accessMode)))
574 	{
575 		*phRegistry = NULL;
576 		return _ret;
577 	}
578 
579 	*phRegistry = pReg;
580 
581 	return REG_NO_ERROR;
582 }
583 
584 //*********************************************************************
585 //	reg_closeRegistry
586 //
reg_closeRegistry(RegHandle hRegistry)587 RegError REGISTRY_CALLTYPE reg_closeRegistry(RegHandle hRegistry)
588 {
589 	ORegistry* pReg;
590 
591 	if (hRegistry)
592 	{
593 		pReg = (ORegistry*)hRegistry;
594 		delete(pReg);
595 		return REG_NO_ERROR;
596 	} else
597 	{
598 		return REG_REGISTRY_NOT_OPEN;
599 	}
600 }
601 
602 
603 //*********************************************************************
604 //	reg_destroyRegistry
605 //
reg_destroyRegistry(RegHandle hRegistry,rtl_uString * registryName)606 RegError REGISTRY_CALLTYPE reg_destroyRegistry(RegHandle hRegistry,
607 											   rtl_uString* registryName)
608 {
609 	return destroyRegistry(hRegistry, registryName);
610 }
611 
612 
613 //*********************************************************************
614 //	reg_dumpRegistry
615 //
reg_dumpRegistry(RegKeyHandle hKey)616 RegError REGISTRY_CALLTYPE reg_dumpRegistry(RegKeyHandle hKey)
617 {
618 	ORegKey *pKey;
619 
620 	if (hKey)
621 		pKey = (ORegKey*)hKey;
622 	else
623 		return REG_INVALID_KEY;
624 
625 	return dumpRegistry(pKey->getRegistry(), hKey);
626 }
627 
628 
629