2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
44 __SCDynamicStoreCopyKeyList(SCDynamicStoreRef store
, CFStringRef key
, Boolean isRegex
, CFArrayRef
*subKeys
)
46 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
47 CFMutableArrayRef keyArray
;
51 CFDictionaryRef storeValue
;
53 if (_configd_verbose
) {
54 SCLog(TRUE
, LOG_DEBUG
, CFSTR("__SCDynamicStoreCopyKeyList:"));
55 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" key = %@"), key
);
56 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" isRegex = %s"), isRegex
? "TRUE" : "FALSE");
59 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
60 return kSCStatusNoStoreSession
; /* you must have an open session to play */
63 storeCnt
= CFDictionaryGetCount(storeData
);
64 keyArray
= CFArrayCreateMutable(NULL
, storeCnt
, &kCFTypeArrayCallBacks
);
69 if (!patternCompile(key
, &preg
, &reErrStr
)) {
70 CFArrayAppendValue(keyArray
, reErrStr
);
72 *subKeys
= CFArrayCreateCopy(NULL
, keyArray
);
74 return kSCStatusFailed
;
80 const void * storeKeys_q
[N_QUICK
];
81 const void ** storeKeys
= storeKeys_q
;
82 const void * storeValues_q
[N_QUICK
];
83 const void ** storeValues
= storeValues_q
;
85 if (storeCnt
> (CFIndex
)(sizeof(storeKeys_q
) / sizeof(CFStringRef
))) {
86 storeKeys
= CFAllocatorAllocate(NULL
, storeCnt
* sizeof(CFStringRef
), 0);
87 storeValues
= CFAllocatorAllocate(NULL
, storeCnt
* sizeof(CFStringRef
), 0);
90 CFDictionaryGetKeysAndValues(storeData
, storeKeys
, storeValues
);
91 for (i
= 0; i
< storeCnt
; i
++) {
92 storeStr
= (CFStringRef
)storeKeys
[i
];
93 storeValue
= (CFDictionaryRef
)storeValues
[i
];
96 * only return those keys which match the regular
97 * expression specified in the provided key.
101 char storeKey_q
[256];
102 char * storeKey
= storeKey_q
;
103 CFIndex storeKeyLen
= CFStringGetLength(storeStr
) + 1;
105 if (storeKeyLen
> (CFIndex
)sizeof(storeKey_q
))
106 storeKey
= CFAllocatorAllocate(NULL
, storeKeyLen
, 0);
107 if (_SC_cfstring_to_cstring(storeStr
, storeKey
, storeKeyLen
, kCFStringEncodingASCII
) == NULL
) {
108 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not convert store key to C string"));
109 if (storeKey
!= storeKey_q
) CFAllocatorDeallocate(NULL
, storeKey
);
113 reError
= regexec(&preg
, storeKey
, 0, NULL
, 0);
116 /* we've got a match */
117 if (CFDictionaryContainsKey(storeValue
, kSCDData
))
118 CFArrayAppendValue(keyArray
, storeStr
);
127 reErrStrLen
= regerror(reError
,
131 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("regexec(): %s"), reErrBuf
);
135 if (storeKey
!= storeKey_q
) CFAllocatorDeallocate(NULL
, storeKey
);
138 * only return those keys which are prefixed by the
139 * provided key string and have data.
141 if (((CFStringGetLength(key
) == 0) || CFStringHasPrefix(storeStr
, key
)) &&
142 CFDictionaryContainsKey(storeValue
, kSCDData
)) {
143 CFArrayAppendValue(keyArray
, storeStr
);
148 if (storeKeys
!= storeKeys_q
) {
149 CFAllocatorDeallocate(NULL
, storeKeys
);
150 CFAllocatorDeallocate(NULL
, storeValues
);
166 _configlist(mach_port_t server
,
167 xmlData_t keyRef
, /* raw XML bytes */
168 mach_msg_type_number_t keyLen
,
170 xmlDataOut_t
*listRef
, /* raw XML bytes */
171 mach_msg_type_number_t
*listLen
,
175 CFStringRef key
; /* key (un-serialized) */
176 serverSessionRef mySession
= getSession(server
);
178 CFArrayRef subKeys
; /* array of CFStringRef's */
180 if (_configd_verbose
) {
181 SCLog(TRUE
, LOG_DEBUG
, CFSTR("List keys in configuration database."));
182 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
188 /* un-serialize the key */
189 if (!_SCUnserializeString(&key
, NULL
, (void *)keyRef
, keyLen
)) {
190 *sc_status
= kSCStatusFailed
;
194 if (!isA_CFString(key
)) {
195 *sc_status
= kSCStatusInvalidArgument
;
201 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
206 *sc_status
= __SCDynamicStoreCopyKeyList(mySession
->store
, key
, isRegex
!= 0, &subKeys
);
208 if (*sc_status
!= kSCStatusOK
) {
212 /* serialize the list of keys */
213 ok
= _SCSerialize(subKeys
, NULL
, (void **)listRef
, (CFIndex
*)listLen
);
216 *sc_status
= kSCStatusFailed
;