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