2 * Copyright (c) 2000-2006, 2008, 2011, 2012, 2016 Apple 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 "SCPreferencesInternal.h"
38 static CF_RETURNS_RETAINED CFMutableArrayRef
39 normalizePath(CFStringRef path
)
41 CFMutableArrayRef elements
;
43 CFArrayRef tmpElements
;
45 if (!isA_CFString(path
)) {
46 _SCErrorSet(kSCStatusInvalidArgument
);
50 if (!CFStringHasPrefix(path
, CFSTR("/"))) {
51 /* if no root separator */
55 tmpElements
= CFStringCreateArrayBySeparatingStrings(NULL
, path
, CFSTR("/"));
56 elements
= CFArrayCreateMutableCopy(NULL
, 0, tmpElements
);
57 CFRelease(tmpElements
);
59 /* remove empty path components */
60 n
= CFArrayGetCount(elements
);
62 CFStringRef pathElement
;
64 pathElement
= CFArrayGetValueAtIndex(elements
, n
);
65 if (CFStringGetLength(pathElement
) == 0) {
66 CFArrayRemoveValueAtIndex(elements
, n
);
75 getPath(SCPreferencesRef prefs
, CFStringRef path
, CFDictionaryRef
*entity
)
78 CFMutableArrayRef elements
;
84 CFDictionaryRef value
= NULL
;
86 elements
= normalizePath(path
);
87 if (elements
== NULL
) {
88 _SCErrorSet(kSCStatusNoKey
);
94 nElements
= CFArrayGetCount(elements
);
97 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
99 __SCPreferencesAccess(prefs
);
101 *entity
= prefsPrivate
->prefs
;
106 for (i
= 0; i
< nElements
; i
++) {
107 element
= CFArrayGetValueAtIndex(elements
, i
);
109 value
= SCPreferencesGetValue(prefs
, CFArrayGetValueAtIndex(elements
, 0));
111 value
= CFDictionaryGetValue(value
, element
);
114 /* if path component does not exist */
115 _SCErrorSet(kSCStatusNoKey
);
119 if (!isA_CFDictionary(value
)) {
120 /* if path component not a dictionary */
121 _SCErrorSet(kSCStatusNoKey
);
125 if ((i
< nElements
- 1) &&
126 CFDictionaryGetValueIfPresent(value
, kSCResvLink
, (const void **)&link
)) {
128 * if not the last path component and this
131 CFMutableArrayRef linkElements
;
133 if (++nLinks
> MAXLINKS
) {
134 /* if we are chasing our tail */
135 _SCErrorSet(kSCStatusMaxLink
);
139 linkElements
= normalizePath(link
);
140 if (linkElements
== NULL
) {
141 /* if the link is bad */
142 _SCErrorSet(kSCStatusNoKey
);
146 CFArrayAppendArray(linkElements
,
148 CFRangeMake(i
+ 1, nElements
-i
- 1));
150 elements
= linkElements
;
167 setPath(SCPreferencesRef prefs
, CFStringRef path
, CFDictionaryRef entity
)
170 CFMutableArrayRef elements
;
175 CFDictionaryRef newEntity
= NULL
;
176 CFDictionaryRef node
= NULL
;
177 CFMutableArrayRef nodes
= NULL
;
180 if ((entity
!= NULL
) && !isA_CFDictionary(entity
)) {
181 _SCErrorSet(kSCStatusInvalidArgument
);
185 elements
= normalizePath(path
);
186 if (elements
== NULL
) {
187 _SCErrorSet(kSCStatusNoKey
);
193 nElements
= CFArrayGetCount(elements
);
196 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
198 __SCPreferencesAccess(prefs
);
200 if (prefsPrivate
->prefs
!= NULL
) {
201 CFRelease(prefsPrivate
->prefs
);
204 if (entity
== NULL
) {
205 prefsPrivate
->prefs
= CFDictionaryCreateMutable(NULL
,
207 &kCFTypeDictionaryKeyCallBacks
,
208 &kCFTypeDictionaryValueCallBacks
);
210 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, entity
);
213 prefsPrivate
->changed
= TRUE
;
218 nodes
= CFArrayCreateMutable(NULL
, nElements
- 1, &kCFTypeArrayCallBacks
);
219 for (i
= 0; i
< nElements
- 1; i
++) {
220 element
= CFArrayGetValueAtIndex(elements
, i
);
222 node
= SCPreferencesGetValue(prefs
, element
);
224 node
= CFDictionaryGetValue(node
, element
);
229 /* if path component exists */
230 CFArrayAppendValue(nodes
, node
);
232 /* if path component does not exist */
233 node
= CFDictionaryCreate(NULL
,
237 &kCFTypeDictionaryKeyCallBacks
,
238 &kCFTypeDictionaryValueCallBacks
);
239 CFArrayAppendValue(nodes
, node
);
243 if (!isA_CFDictionary(node
)) {
244 _SCErrorSet(kSCStatusNoKey
);
248 if ((i
< nElements
- 1) &&
249 CFDictionaryGetValueIfPresent(node
, kSCResvLink
, (const void **)&link
)) {
251 * if not the last path component and this
254 CFMutableArrayRef linkElements
;
256 if (++nLinks
> MAXLINKS
) {
257 /* if we are chasing our tail */
258 _SCErrorSet(kSCStatusMaxLink
);
262 linkElements
= normalizePath(link
);
263 if (linkElements
== NULL
) {
264 /* if the link is bad */
265 _SCErrorSet(kSCStatusNoKey
);
269 CFArrayAppendArray(linkElements
,
271 CFRangeMake(i
+ 1, nElements
-i
- 1));
273 elements
= linkElements
;
282 * make sure that the last component doesn't step on top
283 * of a non-dictionary component.
285 element
= CFArrayGetValueAtIndex(elements
, nElements
- 1);
287 node
= CFArrayGetValueAtIndex(nodes
, nElements
- 2);
288 node
= CFDictionaryGetValue(node
, element
);
290 node
= SCPreferencesGetValue(prefs
, element
);
292 if ((node
!= NULL
) && !isA_CFDictionary(node
)) {
293 // we won't step on a non-dictionary component
294 _SCErrorSet(kSCStatusInvalidArgument
);
298 if (entity
!= NULL
) {
299 newEntity
= CFRetain(entity
);
301 for (i
= nElements
- 1; i
>= 0; i
--) {
302 element
= CFArrayGetValueAtIndex(elements
, i
);
304 if (newEntity
!= NULL
) {
305 ok
= SCPreferencesSetValue(prefs
, element
, newEntity
);
307 ok
= SCPreferencesRemoveValue(prefs
, element
);
310 CFMutableDictionaryRef newNode
;
312 node
= CFArrayGetValueAtIndex(nodes
, i
- 1);
313 newNode
= CFDictionaryCreateMutableCopy(NULL
, 0, node
);
314 if (newEntity
!= NULL
) {
315 CFDictionarySetValue(newNode
, element
, newEntity
);
316 CFRelease(newEntity
);
318 CFDictionaryRemoveValue(newNode
, element
);
319 if (CFDictionaryGetCount(newNode
) == 0) {
320 // prune the (now empty) parent
328 if (newEntity
!= NULL
) {
329 CFRelease(newEntity
);
334 if (nodes
!= NULL
) CFRelease(nodes
);
341 SCPreferencesPathCreateUniqueChild(SCPreferencesRef prefs
,
345 CFStringRef newPath
= NULL
;
346 CFDictionaryRef newDict
;
348 CFDictionaryRef entity
;
351 /* sorry, you must provide a session */
352 _SCErrorSet(kSCStatusNoPrefsSession
);
356 if (getPath(prefs
, prefix
, &entity
)) {
357 // if prefix path exists
358 if (CFDictionaryContainsKey(entity
, kSCResvLink
)) {
359 /* the path is a link... */
360 _SCErrorSet(kSCStatusFailed
);
363 } else if (SCError() != kSCStatusNoKey
) {
364 // if any error except for a missing prefix path component
368 uuid
= CFUUIDCreate(NULL
);
369 child
= CFUUIDCreateString(NULL
, uuid
);
370 newPath
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@/%@"), prefix
, child
);
374 /* save a new/empty dictionary */
375 newDict
= CFDictionaryCreate(NULL
,
377 &kCFTypeDictionaryKeyCallBacks
,
378 &kCFTypeDictionaryValueCallBacks
);
379 assert(newDict
!= NULL
);
381 if (!setPath(prefs
, newPath
, newDict
)) {
392 SCPreferencesPathGetValue(SCPreferencesRef prefs
,
395 CFDictionaryRef entity
;
396 CFStringRef entityLink
;
399 /* sorry, you must provide a session */
400 _SCErrorSet(kSCStatusNoPrefsSession
);
404 if (!getPath(prefs
, path
, &entity
)) {
408 if (isA_CFDictionary(entity
) &&
409 (CFDictionaryGetValueIfPresent(entity
, kSCResvLink
, (const void **)&entityLink
))) {
410 /* if this is a dictionary AND it is a link */
411 if (!getPath(prefs
, entityLink
, &entity
)) {
412 /* if it was a bad link */
422 SCPreferencesPathGetLink(SCPreferencesRef prefs
,
425 CFDictionaryRef entity
;
426 CFStringRef entityLink
;
429 /* sorry, you must provide a session */
430 _SCErrorSet(kSCStatusNoPrefsSession
);
434 if (!getPath(prefs
, path
, &entity
)) {
438 if (isA_CFDictionary(entity
) &&
439 (CFDictionaryGetValueIfPresent(entity
, kSCResvLink
, (const void **)&entityLink
))) {
440 /* if this is a dictionary AND it is a link */
449 SCPreferencesPathSetValue(SCPreferencesRef prefs
,
451 CFDictionaryRef value
)
456 /* sorry, you must provide a session */
457 _SCErrorSet(kSCStatusNoPrefsSession
);
461 #define NETPREF_NEEDS_REPAIR
462 #ifdef NETPREF_NEEDS_REPAIR
463 if (CFEqual(path
, CFSTR("/CurrentSet")) && isA_CFString(value
)) {
464 static Boolean warned
= FALSE
;
466 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesPathSetValue(, %@, ) called with non-dictionary value\n"), path
);
469 return SCPreferencesSetValue(prefs
, CFSTR("CurrentSet"), value
);
471 #endif // NETPREF_NEEDS_REPAIR
473 if (!isA_CFDictionary(value
)) {
474 #ifdef NETPREF_NEEDS_REPAIR
475 SCPrint(TRUE
, stderr
, CFSTR("SCPreferencesPathSetValue(, %@, ) called with non-dictionary value\n"), path
);
476 #endif // NETPREF_NEEDS_REPAIR
477 _SCErrorSet(kSCStatusInvalidArgument
);
481 ok
= setPath(prefs
, path
, value
);
487 SCPreferencesPathSetLink(SCPreferencesRef prefs
,
491 CFMutableDictionaryRef dict
;
492 CFDictionaryRef entity
;
496 /* sorry, you must provide a session */
497 _SCErrorSet(kSCStatusNoPrefsSession
);
501 if (!isA_CFString(link
)) {
502 _SCErrorSet(kSCStatusInvalidArgument
);
506 if (!getPath(prefs
, link
, &entity
)) {
511 dict
= CFDictionaryCreateMutable(NULL
,
513 &kCFTypeDictionaryKeyCallBacks
,
514 &kCFTypeDictionaryValueCallBacks
);
515 CFDictionarySetValue(dict
, kSCResvLink
, link
);
516 ok
= setPath(prefs
, path
, dict
);
524 SCPreferencesPathRemoveValue(SCPreferencesRef prefs
,
527 CFMutableArrayRef elements
= NULL
;
529 CFDictionaryRef value
;
532 /* sorry, you must provide a session */
533 _SCErrorSet(kSCStatusNoPrefsSession
);
537 if (!getPath(prefs
, path
, &value
)) {
542 elements
= normalizePath(path
);
543 if (elements
== NULL
) {
544 _SCErrorSet(kSCStatusNoKey
);
548 ok
= setPath(prefs
, path
, NULL
);