Root/
Source at commit 7 created 14 years 4 months ago. By rekursor, Added a version file in the bin directory, refreshed the binaries for 10.5 and 10.6 | |
---|---|
1 | /*␊ |
2 | * property_list.cpp␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/10.␊ |
5 | *␊ |
6 | */␊ |
7 | ␊ |
8 | #include "property_list.h"␊ |
9 | #include <string.h>␊ |
10 | ␊ |
11 | /****************************************************************************/␊ |
12 | /**␊ |
13 | * Simple parsing and splitting args function, does handle env. vars ␊ |
14 | */␊ |
15 | static void parseArgs(const char * args, const char* argv[], int argvlen)␊ |
16 | {␊ |
17 | ␉// Create a list of args fron the space delimited args parameters␊ |
18 | ␉int n=0;␊ |
19 | ␉if (args && *args) ␊ |
20 | ␉{␊ |
21 | ␉␉bool skipSpaces = true;␊ |
22 | ␉␉bool prevCharwasDelim =false;␊ |
23 | ␉␉char * buf = strdup(args);␊ |
24 | ␉␉char lastPriorityDelim=' ';␊ |
25 | ␉␉␊ |
26 | ␉␉printf("Parsing <%s>\n", args);␊ |
27 | ␉␉␊ |
28 | ␉␉int k=0;␊ |
29 | ␉␉for (int i=0; i< strlen(args); i++)␊ |
30 | ␉␉{␊ |
31 | ␉␉␉switch (args[i]) ␊ |
32 | ␉␉␉{␊ |
33 | ␉␉␉␉case ' ': case '\t':␊ |
34 | ␉␉␉␉␉if(!skipSpaces) {␊ |
35 | ␉␉␉␉␉␉buf[k++]=args[i];␊ |
36 | ␉␉␉␉␉}␊ |
37 | ␉␉␉␉␉else {␊ |
38 | ␉␉␉␉␉␉prevCharwasDelim = true; // // keep a track of delim to further split tokens␊ |
39 | ␉␉␉␉␉}␊ |
40 | ␉␉␉␉␉break;␊ |
41 | ␉␉␉␉case '\0':␊ |
42 | ␉␉␉␉case '\'': ␊ |
43 | ␉␉␉␉case '"': ␊ |
44 | ␉␉␉␉␉if (!skipSpaces &&lastPriorityDelim!=args[i])␊ |
45 | ␉␉␉␉␉{ // consider the imbricated delim as a simple char␊ |
46 | ␉␉␉␉␉␉buf[k++]=args[i];␊ |
47 | ␉␉␉␉␉␉break;␊ |
48 | ␉␉␉␉␉}␊ |
49 | ␉␉␉␉␉␊ |
50 | ␉␉␉␉␉if (!skipSpaces) // end of priority substr␊ |
51 | ␉␉␉␉␉{␊ |
52 | ␉␉␉␉␉␉skipSpaces = true;␊ |
53 | ␉␉␉␉␉␉lastPriorityDelim=' ';␊ |
54 | ␉␉␉␉␉␉buf[k]='\0'; ␊ |
55 | ␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the priority substr␊ |
56 | ␉␉␉␉␉␉k=0; // reset the buf index for next token␊ |
57 | ␉␉␉␉␉␉buf[0]='\0';␊ |
58 | ␉␉␉␉␉}␊ |
59 | ␉␉␉␉␉else␊ |
60 | ␉␉␉␉␉{ // start of priority substr␊ |
61 | ␉␉␉␉␉␉skipSpaces=false;␊ |
62 | ␉␉␉␉␉␉lastPriorityDelim=args[i];␊ |
63 | ␉␉␉␉␉␉if(k>0) // previous string to store first␊ |
64 | ␉␉␉␉␉␉{␊ |
65 | ␉␉␉␉␉␉␉buf[k++]='\0'; // finish the substr␊ |
66 | ␉␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the substr␊ |
67 | ␉␉␉␉␉␉␉k=0; // rest the index for next token␊ |
68 | ␉␉␉␉␉␉␉buf[0]='\0';␊ |
69 | ␉␉␉␉␉␉}␊ |
70 | ␉␉␉␉␉␉//if (args[i] !='\0') buf[k++] = args[i]; // start the new priority substr␊ |
71 | ␉␉␉␉␉}␊ |
72 | ␉␉␉␉␉break;␊ |
73 | ␉␉␉␉default: // non delimiters chars␊ |
74 | ␉␉␉␉␉if(skipSpaces && prevCharwasDelim)␊ |
75 | ␉␉␉␉␉{␊ |
76 | ␉␉␉␉␉␉if(k>0) // a previous substr was before the space␊ |
77 | ␉␉␉␉␉␉{ // store previous token␊ |
78 | ␉␉␉␉␉␉␉buf[k++]='\0'; // finish the substr␊ |
79 | ␉␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the substr␊ |
80 | ␉␉␉␉␉␉␉k=0; // rest the index for next token␊ |
81 | ␉␉␉␉␉␉␉buf[0]='\0';␊ |
82 | ␉␉␉␉␉␉}␊ |
83 | ␉␉␉␉␉}␊ |
84 | ␉␉␉␉␉prevCharwasDelim = false;␊ |
85 | ␉␉␉␉␉buf[k++]=args[i];␊ |
86 | ␉␉␉␉␉break;␊ |
87 | ␉␉␉}␊ |
88 | ␉␉}␊ |
89 | ␉␉␊ |
90 | ␉␉if(*buf) ␊ |
91 | ␉␉{␊ |
92 | ␉␉␉buf[k]='\0';␊ |
93 | ␉␉␉argv[n++]=strdup(buf); // copy the last buffer␉␊ |
94 | ␉␉}␊ |
95 | ␉␉free((void*) buf);␊ |
96 | ␉}␊ |
97 | ␉argv[n]=NULL;␊ |
98 | }␊ |
99 | ␊ |
100 | /****************************************************************************/␊ |
101 | bool executePrivilegedCmd(AuthorizationRef auth, ␊ |
102 | ␉␉␉␉␉␉␉␉ const char* pathToTool, ␊ |
103 | ␉␉␉␉␉␉␉␉ const char* args, ␊ |
104 | ␉␉␉␉␉␉␉␉ AuthorizationFlags flags)␊ |
105 | {␊ |
106 | ␉const char * argv[16];␊ |
107 | ␊ |
108 | ␉parseArgs(args, argv, sizeof(argv)/sizeof(const char*));␊ |
109 | ␊ |
110 | ␉// Execute Cmd␊ |
111 | ␉OSStatus status = AuthorizationExecuteWithPrivileges(auth, pathToTool,flags, (char**)argv, NULL);␊ |
112 | ␉␊ |
113 | ␉␊ |
114 | ␉// Cleanup mem␊ |
115 | ␉for (int i=0; argv[i]; i++) free ((void*) argv[i]);␊ |
116 | ␊ |
117 | return status ? false : true;␊ |
118 | }␊ |
119 | ␊ |
120 | ␊ |
121 | /****************************************************************************/␊ |
122 | /**␊ |
123 | * Write a property list from a file␊ |
124 | */␊ |
125 | bool WritePropertyListToFile( CFPropertyListRef propertyList, CFURLRef fileURL )␊ |
126 | {␊ |
127 | ␉CFDataRef xmlData;␊ |
128 | ␉Boolean status;␊ |
129 | ␉SInt32 errorCode;␊ |
130 | ␉␊ |
131 | ␉// Convert the property list into XML data.␊ |
132 | ␉xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList );␊ |
133 | ␉␊ |
134 | ␉// Write the XML data to the file.␊ |
135 | ␉status = CFURLWriteDataAndPropertiesToResource (␊ |
136 | ␉␉␉␉␉␉␉␉␉␉␉␉␉fileURL, // URL to use␊ |
137 | ␉␉␉␉␉␉␉␉␉␉␉␉␉xmlData, // data to write␊ |
138 | ␉␉␉␉␉␉␉␉␉␉␉␉␉NULL,␊ |
139 | ␉␉␉␉␉␉␉␉␉␉␉␉␉&errorCode);␊ |
140 | ␉CFRelease(xmlData);␊ |
141 | ␉␊ |
142 | ␉return status ? true: false;␊ |
143 | }␊ |
144 | ␊ |
145 | /****************************************************************************/␊ |
146 | /**␊ |
147 | * Read a property list from a file␊ |
148 | */␊ |
149 | static CFPropertyListRef CreatePropertyListFromFile( CFURLRef fileURL, CFStringRef * errorString ) {␊ |
150 | ␉CFPropertyListRef propertyList;␊ |
151 | ␉CFDataRef resourceData;␊ |
152 | ␉Boolean status;␊ |
153 | ␉SInt32 errorCode;␊ |
154 | ␉␊ |
155 | ␉// Read the XML file.␊ |
156 | ␉status = CFURLCreateDataAndPropertiesFromResource(␊ |
157 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ kCFAllocatorDefault,␊ |
158 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ fileURL,␊ |
159 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &resourceData, // place to put file data␊ |
160 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
161 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
162 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &errorCode);␊ |
163 | ␉if (!status) return NULL;␊ |
164 | ␉␊ |
165 | ␉// Reconstitute the dictionary using the XML data.␊ |
166 | ␉propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,␊ |
167 | ␉␉␉␉␉␉␉␉␉␉␉␉ resourceData,␊ |
168 | ␉␉␉␉␉␉␉␉␉␉␉␉ kCFPropertyListMutableContainersAndLeaves,␊ |
169 | ␉␉␉␉␉␉␉␉␉␉␉␉ errorString);␊ |
170 | ␊ |
171 | ␉␉␉␉␉␉␊ |
172 | ␉CFRelease( resourceData );␊ |
173 | ␉return propertyList;␊ |
174 | }␊ |
175 | ␊ |
176 | /****************************************************************************/␊ |
177 | static void show(CFStringRef formatString, ...) {␊ |
178 | CFStringRef resultString;␊ |
179 | CFDataRef data;␊ |
180 | va_list argList;␊ |
181 | ␉␊ |
182 | va_start(argList, formatString);␊ |
183 | resultString = CFStringCreateWithFormatAndArguments(NULL, NULL, formatString, argList);␊ |
184 | va_end(argList);␊ |
185 | ␉␊ |
186 | data = CFStringCreateExternalRepresentation(NULL, resultString, CFStringGetSystemEncoding(), '?');␊ |
187 | ␉␊ |
188 | if (data != NULL) {␊ |
189 | printf ("%.*s\n\n", (int)CFDataGetLength(data), CFDataGetBytePtr(data));␊ |
190 | CFRelease(data);␊ |
191 | }␊ |
192 | ␉␊ |
193 | CFRelease(resultString);␊ |
194 | }␊ |
195 | /****************************************************************************/␊ |
196 | /*␊ |
197 | * Always return a valid static const string, if the CFSTR is not valid, then we return ""␊ |
198 | */␊ |
199 | static const char * CfsToCs(CFStringRef inRef)␊ |
200 | {␊ |
201 | ␉static char buffer[512]="";␊ |
202 | ␉if(CFStringGetFileSystemRepresentation (inRef, buffer, sizeof(buffer)))␊ |
203 | ␉ return buffer;␊ |
204 | ␉return "";␊ |
205 | }␊ |
206 | ␉␊ |
207 | /****************************************************************************/␊ |
208 | /**␊ |
209 | * Creates a PropertyList Incstance␊ |
210 | */␊ |
211 | PropertyList::~PropertyList()␊ |
212 | {␊ |
213 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
214 | ␉if (_proplistRef) CFRelease(_proplistRef);␊ |
215 | ␊ |
216 | }␊ |
217 | ␊ |
218 | /****************************************************************************/␊ |
219 | /**␊ |
220 | * Open a properlist from a path name␊ |
221 | */␊ |
222 | bool PropertyList::open(const char * path, CFStringRef * errorString, AuthorizationRef auth, AuthorizationFlags flags)␊ |
223 | {␊ |
224 | ␉bool ret = false;␊ |
225 | ␉␊ |
226 | ␉if (!path || !*path) return false;␊ |
227 | ␊ |
228 | ␉if (auth)␊ |
229 | ␉{ // give write temporary write rights to the file␊ |
230 | ␉␉std::string args = " 0777 "+ _propFilePath;␊ |
231 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
232 | ␉␉␉return false;␊ |
233 | ␉}␊ |
234 | ␊ |
235 | ␉_propFilePath = path;␊ |
236 | ␊ |
237 | ␉CFStringRef cfPath = CFStringCreateWithCString (kCFAllocatorDefault, path, kCFStringEncodingUTF8);␊ |
238 | ␉CFURLRef inURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,␊ |
239 | ␉␉␉␉␉␉␉␉␉␉␉cfPath, ␊ |
240 | ␉␉␉␉␉␉␉␉␉␉␉kCFURLPOSIXPathStyle, // interpret as POSIX path␊ |
241 | ␉␉␉␉␉␉␉␉␉␉␉false ); // is it a directory?␊ |
242 | ␉CFRelease(cfPath);␊ |
243 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
244 | ␉_CFURLRef = inURL;␊ |
245 | ␉␊ |
246 | ␉if (_CFURLRef) ␊ |
247 | ␉{␊ |
248 | ␉␉_proplistRef = CreatePropertyListFromFile(inURL, errorString);␊ |
249 | ␉␉␊ |
250 | ␉␉if (_proplistRef) ␊ |
251 | ␉␉␉ret= true;␊ |
252 | ␉}␊ |
253 | ␉if (auth)␊ |
254 | ␉{ // give write temporary write rights to the file␊ |
255 | ␉␉std::string args = " 0644 "+ _propFilePath;␊ |
256 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
257 | ␉␉␉ret= false;␊ |
258 | ␉}␊ |
259 | ␉␊ |
260 | ␉return ret;␊ |
261 | }␊ |
262 | ␊ |
263 | /****************************************************************************/␊ |
264 | /**␊ |
265 | * Save the current property list␊ |
266 | */␊ |
267 | bool PropertyList::save(AuthorizationRef auth, AuthorizationFlags flags)␊ |
268 | {␊ |
269 | ␉bool ret=false;␊ |
270 | ␉if (auth)␊ |
271 | ␉{ // give write temporary write rights to the file␊ |
272 | ␉␉std::string args = " 0777 "+ _propFilePath;␊ |
273 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
274 | ␉␉␉return false;␊ |
275 | ␉}␊ |
276 | ␉if (_proplistRef && _CFURLRef) ␊ |
277 | ␉␉ret= WritePropertyListToFile(_proplistRef,_CFURLRef);␊ |
278 | ␉if (auth)␊ |
279 | ␉{ // give write temporary write rights to the file␊ |
280 | ␉␉std::string args = " 0644 "+ _propFilePath;␊ |
281 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
282 | ␉␉␉return false;␊ |
283 | ␉}␊ |
284 | ␉return ret;␊ |
285 | }␊ |
286 | ␊ |
287 | /****************************************************************************/␊ |
288 | /**␊ |
289 | * Extract a sring value from a property list key, ␊ |
290 | * value returned is not owned by caller, copy it necessary.␊ |
291 | */␊ |
292 | const char * PropertyList::getStringForKey(const char *key)␊ |
293 | {␊ |
294 | ␉if (!_proplistRef) ␉return 0;␊ |
295 | ␉CFDictionaryRef myDict = ␊ |
296 | ␉(CFGetTypeID(_proplistRef) == CFDictionaryGetTypeID()) ? (CFDictionaryRef) _proplistRef : NULL;␊ |
297 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
298 | ␉CFStringRef myString;␊ |
299 | ␉␊ |
300 | ␉if (myDict && CFDictionaryGetValueIfPresent(myDict, cfKey, (const void **) &myString))␊ |
301 | ␉{␊ |
302 | ␉␉// show(CFSTR("Key '%@' found , value = <%@>\n"), cfKey, !myString ? CFSTR("") : myString);␊ |
303 | ␉␉CFRelease(cfKey);␊ |
304 | ␉␉return CfsToCs(myString);␊ |
305 | ␉}␊ |
306 | ␉␊ |
307 | ␉if(cfKey) CFRelease(cfKey);␊ |
308 | ␉return NULL;␊ |
309 | }␊ |
310 | /****************************************************************************/␊ |
311 | /**␊ |
312 | * replace a sring value from a property list key, ␊ |
313 | * value returned is not owned by caller, copy it necessary.␊ |
314 | */␊ |
315 | bool PropertyList::setStringForKey(const char *key, const char * value)␊ |
316 | {␊ |
317 | ␉if (!_proplistRef) ␉return false;␊ |
318 | ␉bool ret = false;␊ |
319 | ␉CFMutableDictionaryRef myDict = ␊ |
320 | ␉(CFGetTypeID(_proplistRef) == CFDictionaryGetTypeID()) ? (CFMutableDictionaryRef) _proplistRef : NULL;␊ |
321 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
322 | ␉CFStringRef myString = CFStringCreateWithCString (kCFAllocatorDefault, value, kCFStringEncodingUTF8);␊ |
323 | ␉␊ |
324 | ␉if (myDict) {␊ |
325 | ␉␉CFDictionaryReplaceValue(myDict, cfKey, myString);␊ |
326 | ␉␉ret=true;␉␊ |
327 | ␉} ␊ |
328 | ␉if(cfKey) CFRelease(cfKey);␊ |
329 | ␉if(myString) CFRelease(myString);␊ |
330 | ␉return ret;␊ |
331 | }␊ |
332 | ␊ |
333 |