2 * Copyright (c) 2000-2005, 2009-2011, 2013, 2016-2019 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 * March 24, 2000 Allan Nathanson <ajn@apple.com>
34 #include "SCDynamicStoreInternal.h"
35 #include "config.h" /* MiG generated file */
39 SCDynamicStoreCopyMultiple(SCDynamicStoreRef store
,
43 SCDynamicStorePrivateRef storePrivate
;
45 CFDataRef xmlKeys
= NULL
; /* keys (XML serialized) */
46 xmlData_t myKeysRef
= NULL
; /* keys (serialized) */
47 CFIndex myKeysLen
= 0;
48 CFDataRef xmlPatterns
= NULL
; /* patterns (XML serialized) */
49 xmlData_t myPatternsRef
= NULL
; /* patterns (serialized) */
50 CFIndex myPatternsLen
= 0;
51 xmlDataOut_t xmlDictRef
= NULL
; /* dict (serialized) */
52 mach_msg_type_number_t xmlDictLen
= 0;
53 CFDictionaryRef dict
= NULL
; /* dict (un-serialized) */
54 CFDictionaryRef expDict
= NULL
; /* dict (un-serialized / expanded) */
58 store
= __SCDynamicStoreNullSession();
60 /* sorry, you must provide a session */
61 _SCErrorSet(kSCStatusNoStoreSession
);
66 storePrivate
= (SCDynamicStorePrivateRef
)store
;
67 if (storePrivate
->server
== MACH_PORT_NULL
) {
68 _SCErrorSet(kSCStatusNoStoreServer
);
69 return NULL
; /* you must have an open session to play */
72 /* serialize the keys */
74 if (!_SCSerialize(keys
, &xmlKeys
, (void **)&myKeysRef
, &myKeysLen
)) {
75 _SCErrorSet(kSCStatusFailed
);
80 /* serialize the patterns */
81 if (patterns
!= NULL
) {
82 if (!_SCSerialize(patterns
, &xmlPatterns
, (void **)&myPatternsRef
, &myPatternsLen
)) {
83 if (xmlKeys
!= NULL
) CFRelease(xmlKeys
);
84 _SCErrorSet(kSCStatusFailed
);
91 /* send the keys and patterns, fetch the associated result from the server */
92 status
= configget_m(storePrivate
->server
,
94 (mach_msg_type_number_t
)myKeysLen
,
96 (mach_msg_type_number_t
)myPatternsLen
,
101 if (__SCDynamicStoreCheckRetryAndHandleError(store
,
104 "SCDynamicStoreCopyMultiple configget_m()")) {
108 if (sc_status
!= kSCStatusOK
) {
109 if (xmlDictRef
!= NULL
) {
110 (void) vm_deallocate(mach_task_self(), (vm_address_t
)xmlDictRef
, xmlDictLen
);
112 _SCErrorSet(sc_status
);
116 /* un-serialize the dictionary */
117 if (!_SCUnserialize((CFPropertyListRef
*)&dict
, NULL
, xmlDictRef
, xmlDictLen
)) {
118 _SCErrorSet(kSCStatusFailed
);
122 expDict
= _SCUnserializeMultiple(dict
);
128 if (xmlKeys
!= NULL
) CFRelease(xmlKeys
);
129 if (xmlPatterns
!= NULL
) CFRelease(xmlPatterns
);
136 SCDynamicStoreCopyValue(SCDynamicStoreRef store
, CFStringRef key
)
138 SCDynamicStorePrivateRef storePrivate
;
139 kern_return_t status
;
140 CFDataRef utfKey
; /* key (XML serialized) */
141 xmlData_t myKeyRef
; /* key (serialized) */
143 xmlDataOut_t xmlDataRef
= NULL
; /* data (serialized) */
144 mach_msg_type_number_t xmlDataLen
= 0;
145 CFPropertyListRef data
; /* data (un-serialized) */
150 store
= __SCDynamicStoreNullSession();
152 /* sorry, you must provide a session */
153 _SCErrorSet(kSCStatusNoStoreSession
);
158 storePrivate
= (SCDynamicStorePrivateRef
)store
;
159 if (storePrivate
->server
== MACH_PORT_NULL
) {
160 _SCErrorSet(kSCStatusNoStoreServer
);
161 return NULL
; /* you must have an open session to play */
164 if (storePrivate
->cache_active
) {
165 if ((storePrivate
->cached_set
!= NULL
) &&
166 CFDictionaryGetValueIfPresent(storePrivate
->cached_set
, key
, (const void **)&data
)) {
167 // if we have "set" a new value
168 return (CFRetain(data
));
171 if ((storePrivate
->cached_removals
!= NULL
) &&
172 CFArrayContainsValue(storePrivate
->cached_removals
,
173 CFRangeMake(0, CFArrayGetCount(storePrivate
->cached_removals
)),
175 // if we have "removed" the key
176 _SCErrorSet(kSCStatusNoKey
);
180 if ((storePrivate
->cached_keys
!= NULL
) &&
181 CFDictionaryGetValueIfPresent(storePrivate
->cached_keys
, key
, (const void **)&data
)) {
182 // if we have a cached value
183 return (CFRetain(data
));
187 /* serialize the key */
188 if (!_SCSerializeString(key
, &utfKey
, (void **)&myKeyRef
, &myKeyLen
)) {
189 _SCErrorSet(kSCStatusFailed
);
195 /* send the key & fetch the associated data from the server */
196 status
= configget(storePrivate
->server
,
198 (mach_msg_type_number_t
)myKeyLen
,
204 if (__SCDynamicStoreCheckRetryAndHandleError(store
,
207 "SCDynamicStoreCopyValue configget()")) {
214 if (sc_status
!= kSCStatusOK
) {
215 if (xmlDataRef
!= NULL
) {
216 (void) vm_deallocate(mach_task_self(), (vm_address_t
)xmlDataRef
, xmlDataLen
);
218 _SCErrorSet(sc_status
);
222 /* un-serialize the data */
223 if (!_SCUnserialize(&data
, NULL
, xmlDataRef
, xmlDataLen
)) {
224 _SCErrorSet(kSCStatusFailed
);
228 if (storePrivate
->cache_active
&& (data
!= NULL
)) {
229 if (storePrivate
->cached_keys
== NULL
) {
230 storePrivate
->cached_keys
= CFDictionaryCreateMutable(NULL
,
232 &kCFTypeDictionaryKeyCallBacks
,
233 &kCFTypeDictionaryValueCallBacks
);
235 CFDictionarySetValue(storePrivate
->cached_keys
, key
, data
);