2 * Copyright(c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 16, 2000 Allan Nathanson <ajn@apple.com>
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCValidation.h>
36 #include <SystemConfiguration/SCPrivate.h>
37 #include "SCPreferencesInternal.h"
42 normalizePath(CFStringRef path
)
44 CFArrayRef tmpElements
;
45 CFMutableArrayRef elements
;
49 if (!CFStringHasPrefix(path
, CFSTR("/"))) {
50 /* if no root separator */
54 tmpElements
= CFStringCreateArrayBySeparatingStrings(NULL
, path
, CFSTR("/"));
55 elements
= CFArrayCreateMutableCopy(NULL
, 0, tmpElements
);
56 CFRelease(tmpElements
);
58 /* remove empty path components */
59 nElements
= CFArrayGetCount(elements
);
60 for (i
= nElements
; i
> 0; i
--) {
61 CFStringRef pathElement
;
63 pathElement
= CFArrayGetValueAtIndex(elements
, i
-1);
64 if (CFStringGetLength(pathElement
) == 0) {
65 CFArrayRemoveValueAtIndex(elements
, i
-1);
80 getPath(SCPreferencesRef prefs
, CFStringRef path
, CFDictionaryRef
*entity
)
89 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
90 CFDictionaryRef value
= NULL
;
92 elements
= normalizePath(path
);
93 if (elements
== NULL
) {
94 _SCErrorSet(kSCStatusNoKey
);
98 __SCPreferencesAccess(prefs
);
102 nElements
= CFArrayGetCount(elements
);
103 for (i
= 0; i
< nElements
; i
++) {
104 element
= CFArrayGetValueAtIndex(elements
, i
);
106 value
= CFDictionaryGetValue(prefsPrivate
->prefs
,
107 CFArrayGetValueAtIndex(elements
, 0));
109 value
= CFDictionaryGetValue(value
, element
);
112 /* if path component does not exist */
113 _SCErrorSet(kSCStatusNoKey
);
117 if (!isA_CFDictionary(value
)) {
118 /* if path component not a dictionary */
119 _SCErrorSet(kSCStatusNoKey
);
123 if ((i
< nElements
-1) &&
124 CFDictionaryGetValueIfPresent(value
, kSCResvLink
, (const void **)&link
)) {
126 * if not the last path component and this
129 CFArrayRef linkElements
;
130 CFMutableArrayRef newElements
;
132 if (++nLinks
> MAXLINKS
) {
133 /* if we are chasing our tail */
134 _SCErrorSet(kSCStatusMaxLink
);
138 linkElements
= normalizePath(link
);
139 if (linkElements
== NULL
) {
140 /* if the link is bad */
141 _SCErrorSet(kSCStatusNoKey
);
145 newElements
= CFArrayCreateMutableCopy(NULL
, 0, linkElements
);
146 CFArrayAppendArray(newElements
,
148 CFRangeMake(i
+1, nElements
-i
-1));
150 elements
= newElements
;
167 setPath(SCPreferencesRef prefs
, CFStringRef path
, CFDictionaryRef entity
)
175 CFDictionaryRef newEntity
= NULL
;
176 CFDictionaryRef node
= NULL
;
177 CFMutableArrayRef nodes
;
179 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
181 elements
= normalizePath(path
);
182 if (elements
== NULL
) {
183 _SCErrorSet(kSCStatusNoKey
);
187 __SCPreferencesAccess(prefs
);
191 nElements
= CFArrayGetCount(elements
);
192 nodes
= CFArrayCreateMutable(NULL
, nElements
-1, &kCFTypeArrayCallBacks
);
193 for (i
= 0; i
< nElements
- 1; i
++) {
194 element
= CFArrayGetValueAtIndex(elements
, i
);
196 node
= CFDictionaryGetValue(prefsPrivate
->prefs
, element
);
198 node
= CFDictionaryGetValue(node
, element
);
203 /* if path component exists */
204 CFArrayAppendValue(nodes
, node
);
206 /* if path component does not exist */
207 node
= CFDictionaryCreate(NULL
,
211 &kCFTypeDictionaryKeyCallBacks
,
212 &kCFTypeDictionaryValueCallBacks
);
213 CFArrayAppendValue(nodes
, node
);
217 if (!isA_CFDictionary(node
)) {
218 _SCErrorSet(kSCStatusNoKey
);
222 if ((i
< nElements
-1) &&
223 CFDictionaryGetValueIfPresent(node
, kSCResvLink
, (const void **)&link
)) {
225 * if not the last path component and this
228 CFArrayRef linkElements
;
229 CFMutableArrayRef newElements
;
231 if (++nLinks
> MAXLINKS
) {
232 /* if we are chasing our tail */
233 _SCErrorSet(kSCStatusMaxLink
);
237 linkElements
= normalizePath(link
);
238 if (linkElements
== NULL
) {
239 /* if the link is bad */
240 _SCErrorSet(kSCStatusNoKey
);
244 newElements
= CFArrayCreateMutableCopy(NULL
, 0, linkElements
);
245 CFArrayAppendArray(newElements
,
247 CFRangeMake(i
+1, nElements
-i
-1));
249 elements
= newElements
;
257 newEntity
= CFRetain(entity
);
259 for (i
= nElements
- 1; i
>= 0; i
--) {
260 element
= CFArrayGetValueAtIndex(elements
, i
);
263 CFDictionarySetValue(prefsPrivate
->prefs
, element
, newEntity
);
265 CFDictionaryRemoveValue(prefsPrivate
->prefs
, element
);
267 prefsPrivate
->changed
= TRUE
;
270 CFMutableDictionaryRef newNode
;
272 node
= CFArrayGetValueAtIndex(nodes
, i
-1);
273 newNode
= CFDictionaryCreateMutableCopy(NULL
, 0, node
);
275 CFDictionarySetValue(newNode
, element
, newEntity
);
276 CFRelease(newEntity
);
278 CFDictionaryRemoveValue(newNode
, element
);
284 CFRelease(newEntity
);
296 SCPreferencesPathCreateUniqueChild(SCPreferencesRef prefs
,
300 CFStringRef newPath
= NULL
;
301 CFMutableDictionaryRef newDict
= NULL
;
303 CFDictionaryRef entity
;
306 /* sorry, you must provide a session */
307 _SCErrorSet(kSCStatusNoPrefsSession
);
311 if (getPath(prefs
, prefix
, &entity
)) {
312 // if prefix path exists
313 if (CFDictionaryContainsKey(entity
, kSCResvLink
)) {
314 /* the path is a link... */
315 _SCErrorSet(kSCStatusFailed
);
318 } else if (SCError() != kSCStatusNoKey
) {
319 // if any error except for a missing prefix path component
323 uuid
= CFUUIDCreate(NULL
);
324 child
= CFUUIDCreateString(NULL
, uuid
);
325 newPath
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@/%@"), prefix
, child
);
329 /* save the new dictionary */
330 newDict
= CFDictionaryCreateMutable(NULL
,
332 &kCFTypeDictionaryKeyCallBacks
,
333 &kCFTypeDictionaryValueCallBacks
);
334 if (!setPath(prefs
, newPath
, newDict
)) {
345 SCPreferencesPathGetValue(SCPreferencesRef prefs
,
348 CFDictionaryRef entity
;
349 CFStringRef entityLink
;
352 /* sorry, you must provide a session */
353 _SCErrorSet(kSCStatusNoPrefsSession
);
357 if (!getPath(prefs
, path
, &entity
)) {
361 if (isA_CFDictionary(entity
) &&
362 (CFDictionaryGetValueIfPresent(entity
, kSCResvLink
, (const void **)&entityLink
))) {
363 /* if this is a dictionary AND it is a link */
364 if (!getPath(prefs
, entityLink
, &entity
)) {
365 /* if it was a bad link */
375 SCPreferencesPathGetLink(SCPreferencesRef prefs
,
378 CFDictionaryRef entity
;
379 CFStringRef entityLink
;
382 /* sorry, you must provide a session */
383 _SCErrorSet(kSCStatusNoPrefsSession
);
387 if (!getPath(prefs
, path
, &entity
)) {
391 if (isA_CFDictionary(entity
) &&
392 (CFDictionaryGetValueIfPresent(entity
, kSCResvLink
, (const void **)&entityLink
))) {
393 /* if this is a dictionary AND it is a link */
402 SCPreferencesPathSetValue(SCPreferencesRef prefs
,
404 CFDictionaryRef value
)
409 /* sorry, you must provide a session */
410 _SCErrorSet(kSCStatusNoPrefsSession
);
415 _SCErrorSet(kSCStatusInvalidArgument
);
419 ok
= setPath(prefs
, path
, value
);
425 SCPreferencesPathSetLink(SCPreferencesRef prefs
,
429 CFMutableDictionaryRef dict
;
430 CFDictionaryRef entity
;
434 /* sorry, you must provide a session */
435 _SCErrorSet(kSCStatusNoPrefsSession
);
440 _SCErrorSet(kSCStatusInvalidArgument
);
444 if (!getPath(prefs
, link
, &entity
)) {
449 dict
= CFDictionaryCreateMutable(NULL
,
451 &kCFTypeDictionaryKeyCallBacks
,
452 &kCFTypeDictionaryValueCallBacks
);
453 CFDictionaryAddValue(dict
, kSCResvLink
, link
);
454 ok
= setPath(prefs
, path
, dict
);
462 SCPreferencesPathRemoveValue(SCPreferencesRef prefs
,
465 CFArrayRef elements
= NULL
;
467 CFDictionaryRef value
;
470 /* sorry, you must provide a session */
471 _SCErrorSet(kSCStatusNoPrefsSession
);
475 if (!getPath(prefs
, path
, &value
)) {
480 elements
= normalizePath(path
);
481 if (elements
== NULL
) {
482 _SCErrorSet(kSCStatusNoKey
);
486 ok
= setPath(prefs
, path
, NULL
);