2 * Copyright (c) 2000-2006, 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 SCDynamicStoreSetMultiple(SCDynamicStoreRef store
,
40 CFDictionaryRef keysToSet
,
41 CFArrayRef keysToRemove
,
42 CFArrayRef keysToNotify
)
44 SCDynamicStorePrivateRef storePrivate
;
46 CFDataRef xmlSet
= NULL
; /* key/value pairs to set (XML serialized) */
47 xmlData_t mySetRef
= NULL
; /* key/value pairs to set (serialized) */
49 CFDataRef xmlRemove
= NULL
; /* keys to remove (XML serialized) */
50 xmlData_t myRemoveRef
= NULL
; /* keys to remove (serialized) */
51 CFIndex myRemoveLen
= 0;
52 CFDataRef xmlNotify
= NULL
; /* keys to notify (XML serialized) */
53 xmlData_t myNotifyRef
= NULL
; /* keys to notify (serialized) */
54 CFIndex myNotifyLen
= 0;
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 FALSE
; /* you must have an open session to play */
72 /* serialize the key/value pairs to set*/
73 if (keysToSet
!= NULL
) {
74 CFDictionaryRef newInfo
;
77 newInfo
= _SCSerializeMultiple(keysToSet
);
78 if (newInfo
== NULL
) {
79 _SCErrorSet(kSCStatusInvalidArgument
);
83 ok
= _SCSerialize(newInfo
, &xmlSet
, (void **)&mySetRef
, &mySetLen
);
86 _SCErrorSet(kSCStatusInvalidArgument
);
91 /* serialize the keys to remove */
92 if (keysToRemove
!= NULL
) {
93 if (!_SCSerialize(keysToRemove
, &xmlRemove
, (void **)&myRemoveRef
, &myRemoveLen
)) {
94 if (xmlSet
!= NULL
) CFRelease(xmlSet
);
95 _SCErrorSet(kSCStatusInvalidArgument
);
100 /* serialize the keys to notify */
101 if (keysToNotify
!= NULL
) {
102 if (!_SCSerialize(keysToNotify
, &xmlNotify
, (void **)&myNotifyRef
, &myNotifyLen
)) {
103 if (xmlSet
!= NULL
) CFRelease(xmlSet
);
104 if (xmlRemove
!= NULL
) CFRelease(xmlRemove
);
105 _SCErrorSet(kSCStatusInvalidArgument
);
112 /* send the keys and patterns, fetch the associated result from the server */
113 status
= configset_m(storePrivate
->server
,
115 (mach_msg_type_number_t
)mySetLen
,
117 (mach_msg_type_number_t
)myRemoveLen
,
119 (mach_msg_type_number_t
)myNotifyLen
,
122 if (__SCDynamicStoreCheckRetryAndHandleError(store
,
125 "SCDynamicStoreSetMultiple configset_m()")) {
130 if (xmlSet
!= NULL
) CFRelease(xmlSet
);
131 if (xmlRemove
!= NULL
) CFRelease(xmlRemove
);
132 if (xmlNotify
!= NULL
) CFRelease(xmlNotify
);
134 if (sc_status
!= kSCStatusOK
) {
135 _SCErrorSet(sc_status
);
143 SCDynamicStoreSetValue(SCDynamicStoreRef store
, CFStringRef key
, CFPropertyListRef value
)
145 SCDynamicStorePrivateRef storePrivate
;
146 kern_return_t status
;
147 CFDataRef utfKey
; /* serialized key */
150 CFDataRef xmlData
; /* serialized data */
157 store
= __SCDynamicStoreNullSession();
159 /* sorry, you must provide a session */
160 _SCErrorSet(kSCStatusNoStoreSession
);
165 storePrivate
= (SCDynamicStorePrivateRef
)store
;
166 if (storePrivate
->server
== MACH_PORT_NULL
) {
167 /* sorry, you must have an open session to play */
168 _SCErrorSet(kSCStatusNoStoreServer
);
172 if (storePrivate
->cache_active
) {
173 if (storePrivate
->cached_removals
!= NULL
) {
176 i
= CFArrayGetFirstIndexOfValue(storePrivate
->cached_removals
,
177 CFRangeMake(0, CFArrayGetCount(storePrivate
->cached_removals
)),
179 if (i
!= kCFNotFound
) {
180 // if previously "removed"
181 CFArrayRemoveValueAtIndex(storePrivate
->cached_removals
, i
);
185 if (storePrivate
->cached_set
== NULL
) {
186 storePrivate
->cached_set
= CFDictionaryCreateMutable(NULL
,
188 &kCFTypeDictionaryKeyCallBacks
,
189 &kCFTypeDictionaryValueCallBacks
);
191 CFDictionarySetValue(storePrivate
->cached_set
, key
, value
);
195 /* serialize the key */
196 if (!_SCSerializeString(key
, &utfKey
, (void **)&myKeyRef
, &myKeyLen
)) {
197 _SCErrorSet(kSCStatusInvalidArgument
);
201 /* serialize the data */
202 if (!_SCSerialize(value
, &xmlData
, (void **)&myDataRef
, &myDataLen
)) {
204 _SCErrorSet(kSCStatusInvalidArgument
);
210 /* send the key & data to the server, get new instance id */
211 status
= configset(storePrivate
->server
,
213 (mach_msg_type_number_t
)myKeyLen
,
215 (mach_msg_type_number_t
)myDataLen
,
220 if (__SCDynamicStoreCheckRetryAndHandleError(store
,
223 "SCDynamicStoreSetValue configset()")) {
231 if (sc_status
!= kSCStatusOK
) {
232 _SCErrorSet(sc_status
);