]> git.saurik.com Git - apple/configd.git/blob - Plugins/common/cache.c
configd-963.270.3.tar.gz
[apple/configd.git] / Plugins / common / cache.c
1 /*
2 * Copyright (c) 2003, 2004, 2006, 2011, 2015-2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * May 1, 2003 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #ifdef SC_LOG_HANDLE
33 #include <os/log.h>
34 os_log_t SC_LOG_HANDLE;
35 #endif //SC_LOG_HANDLE
36
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <SystemConfiguration/SystemConfiguration.h>
39 #include <SystemConfiguration/SCPrivate.h>
40
41 #include "cache.h"
42
43
44 static CFMutableDictionaryRef cached_keys = NULL;
45 static CFMutableDictionaryRef cached_set = NULL;
46 static CFMutableArrayRef cached_removals = NULL;
47 static CFMutableArrayRef cached_notifys = NULL;
48
49
50 __private_extern__
51 void
52 cache_open(void)
53 {
54 cached_keys = CFDictionaryCreateMutable(NULL,
55 0,
56 &kCFTypeDictionaryKeyCallBacks,
57 &kCFTypeDictionaryValueCallBacks);
58 cached_set = CFDictionaryCreateMutable(NULL,
59 0,
60 &kCFTypeDictionaryKeyCallBacks,
61 &kCFTypeDictionaryValueCallBacks);
62 cached_removals = CFArrayCreateMutable(NULL,
63 0,
64 &kCFTypeArrayCallBacks);
65 cached_notifys = CFArrayCreateMutable(NULL,
66 0,
67 &kCFTypeArrayCallBacks);
68
69 return;
70 }
71
72
73 __private_extern__
74 CFPropertyListRef
75 cache_SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key)
76 {
77 CFPropertyListRef value;
78
79 value = CFDictionaryGetValue(cached_set, key);
80 if (value) {
81 // if we have "set" a new value
82 return (CFRetain(value));
83 }
84
85 if (CFArrayContainsValue(cached_removals,
86 CFRangeMake(0, CFArrayGetCount(cached_removals)),
87 key)) {
88 // if we have "removed" the key
89 _SCErrorSet(kSCStatusNoKey);
90 return NULL;
91 }
92
93 value = CFDictionaryGetValue(cached_keys, key);
94 if (value) {
95 // if we have a cached value
96 return (CFRetain(value));
97 }
98
99 value = SCDynamicStoreCopyValue(store, key);
100 if (value) {
101 CFDictionarySetValue(cached_keys, key, value);
102 }
103
104 return value;
105 }
106
107
108 __private_extern__
109 void
110 cache_SCDynamicStoreSetValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef value)
111 {
112 #pragma unused(store)
113 CFIndex i;
114
115 i = CFArrayGetFirstIndexOfValue(cached_removals,
116 CFRangeMake(0, CFArrayGetCount(cached_removals)),
117 key);
118 if (i != kCFNotFound) {
119 // if previously "removed"
120 CFArrayRemoveValueAtIndex(cached_removals, i);
121 }
122
123 CFDictionarySetValue(cached_set, key, value);
124
125 return;
126 }
127
128 __private_extern__
129 void
130 cache_SCDynamicStoreRemoveValue(SCDynamicStoreRef store, CFStringRef key)
131 {
132 #pragma unused(store)
133 CFDictionaryRemoveValue(cached_set, key);
134
135 if (!CFArrayContainsValue(cached_removals,
136 CFRangeMake(0, CFArrayGetCount(cached_removals)),
137 key)) {
138 CFArrayAppendValue(cached_removals, key);
139 }
140
141 return;
142 }
143
144
145 __private_extern__
146 void
147 cache_SCDynamicStoreNotifyValue(SCDynamicStoreRef store, CFStringRef key)
148 {
149 #pragma unused(store)
150 if (!CFArrayContainsValue(cached_notifys,
151 CFRangeMake(0, CFArrayGetCount(cached_notifys)),
152 key)) {
153 CFArrayAppendValue(cached_notifys, key);
154 }
155
156 return;
157 }
158
159
160 __private_extern__
161 void
162 cache_write(SCDynamicStoreRef store)
163 {
164 if ((CFDictionaryGetCount(cached_set) > 0) ||
165 (CFArrayGetCount(cached_removals) > 0) ||
166 (CFArrayGetCount(cached_notifys) > 0)) {
167 if (!SCDynamicStoreSetMultiple(store,
168 cached_set,
169 cached_removals,
170 cached_notifys)) {
171 SC_log(LOG_NOTICE, "SCDynamicStoreSetMultiple() failed: %s",
172 SCErrorString(SCError()));
173 }
174 }
175
176 return;
177 }
178
179
180 __private_extern__
181 void
182 cache_close(void)
183 {
184 CFRelease(cached_keys);
185 CFRelease(cached_set);
186 CFRelease(cached_removals);
187 CFRelease(cached_notifys);
188
189 return;
190 }