2 * Copyright (c) 2003, 2004, 2006, 2011, 2015-2017 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 * May 1, 2003 Allan Nathanson <ajn@apple.com>
34 os_log_t SC_LOG_HANDLE
;
35 #endif //SC_LOG_HANDLE
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <SystemConfiguration/SystemConfiguration.h>
39 #include <SystemConfiguration/SCPrivate.h>
44 static CFMutableDictionaryRef cached_keys
= NULL
;
45 static CFMutableDictionaryRef cached_set
= NULL
;
46 static CFMutableArrayRef cached_removals
= NULL
;
47 static CFMutableArrayRef cached_notifys
= NULL
;
54 cached_keys
= CFDictionaryCreateMutable(NULL
,
56 &kCFTypeDictionaryKeyCallBacks
,
57 &kCFTypeDictionaryValueCallBacks
);
58 cached_set
= CFDictionaryCreateMutable(NULL
,
60 &kCFTypeDictionaryKeyCallBacks
,
61 &kCFTypeDictionaryValueCallBacks
);
62 cached_removals
= CFArrayCreateMutable(NULL
,
64 &kCFTypeArrayCallBacks
);
65 cached_notifys
= CFArrayCreateMutable(NULL
,
67 &kCFTypeArrayCallBacks
);
75 cache_SCDynamicStoreCopyValue(SCDynamicStoreRef store
, CFStringRef key
)
77 CFPropertyListRef value
;
79 value
= CFDictionaryGetValue(cached_set
, key
);
81 // if we have "set" a new value
82 return (CFRetain(value
));
85 if (CFArrayContainsValue(cached_removals
,
86 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
88 // if we have "removed" the key
89 _SCErrorSet(kSCStatusNoKey
);
93 value
= CFDictionaryGetValue(cached_keys
, key
);
95 // if we have a cached value
96 return (CFRetain(value
));
99 value
= SCDynamicStoreCopyValue(store
, key
);
101 CFDictionarySetValue(cached_keys
, key
, value
);
110 cache_SCDynamicStoreSetValue(SCDynamicStoreRef store
, CFStringRef key
, CFPropertyListRef value
)
112 #pragma unused(store)
115 i
= CFArrayGetFirstIndexOfValue(cached_removals
,
116 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
118 if (i
!= kCFNotFound
) {
119 // if previously "removed"
120 CFArrayRemoveValueAtIndex(cached_removals
, i
);
123 CFDictionarySetValue(cached_set
, key
, value
);
130 cache_SCDynamicStoreRemoveValue(SCDynamicStoreRef store
, CFStringRef key
)
132 #pragma unused(store)
133 CFDictionaryRemoveValue(cached_set
, key
);
135 if (!CFArrayContainsValue(cached_removals
,
136 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
138 CFArrayAppendValue(cached_removals
, key
);
147 cache_SCDynamicStoreNotifyValue(SCDynamicStoreRef store
, CFStringRef key
)
149 #pragma unused(store)
150 if (!CFArrayContainsValue(cached_notifys
,
151 CFRangeMake(0, CFArrayGetCount(cached_notifys
)),
153 CFArrayAppendValue(cached_notifys
, key
);
162 cache_write(SCDynamicStoreRef store
)
164 if ((CFDictionaryGetCount(cached_set
) > 0) ||
165 (CFArrayGetCount(cached_removals
) > 0) ||
166 (CFArrayGetCount(cached_notifys
) > 0)) {
167 if (!SCDynamicStoreSetMultiple(store
,
171 SC_log(LOG_NOTICE
, "SCDynamicStoreSetMultiple() failed: %s",
172 SCErrorString(SCError()));
184 CFRelease(cached_keys
);
185 CFRelease(cached_set
);
186 CFRelease(cached_removals
);
187 CFRelease(cached_notifys
);