1 | /*␊ |
2 | * property_list.h␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/10.␊ |
5 | *␊ |
6 | */␊ |
7 | #ifndef __CHPROPERTYLIST_LIST_H␊ |
8 | #define __CHPROPERTYLIST_LIST_H␊ |
9 | ␊ |
10 | #include <CoreFoundation/CFPropertyList.h>␊ |
11 | #include <CoreFoundation/CFURLAccess.h>␊ |
12 | #include <Security/Authorization.h>␊ |
13 | #include <string>␊ |
14 | ␊ |
15 | //----------------------------------------------------------------------------␊ |
16 | ␊ |
17 | static const bool CreateIfNotExist = true;␊ |
18 | static const bool DontCreate = false;␊ |
19 | ␊ |
20 | ␊ |
21 | //----------------------------------------------------------------------------␊ |
22 | /// Run priviledged command for cmds like property chmods and restard command␊ |
23 | //----------------------------------------------------------------------------␊ |
24 | bool executePrivilegedCmd(AuthorizationRef auth, ␊ |
25 | ␉␉␉␉␉␉ const char* pathToTool, ␊ |
26 | ␉␉␉␉␉␉ const char* args=NULL, ␊ |
27 | ␉␉␉␉␉␉ AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
28 | ␊ |
29 | ␊ |
30 | //----------------------------------------------------------------------------␊ |
31 | /// Simple PropertyList Abstraction␊ |
32 | //----------------------------------------------------------------------------␊ |
33 | class PropertyList␊ |
34 | {␊ |
35 | ␉␊ |
36 | public:␊ |
37 | ␉// Create a property list␊ |
38 | ␉PropertyList() : _proplistRef(0), _CFURLRef(0) {␉}␊ |
39 | ␉virtual ~PropertyList();␊ |
40 | ␉␊ |
41 | ␉/// Tell if a valid property list successfully opened ␊ |
42 | ␉bool isValid() const { return _proplistRef!=NULL;}␊ |
43 | ␊ |
44 | ␉/// Open a property list␊ |
45 | ␉bool open(const char *propListPath, bool createIfNotExist,␊ |
46 | ␉␉␉ AuthorizationRef auth=NULL, ␊ |
47 | ␉␉␉ AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
48 | ␊ |
49 | ␉/// Save a property list␊ |
50 | ␉bool save(AuthorizationRef auth=NULL, ␊ |
51 | ␉␉␉ AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
52 | ␉␊ |
53 | ␉/// Return last error status, used in open()␊ |
54 | ␉CFStringRef lastError() const { return _errorString;}␊ |
55 | ␉␊ |
56 | ␉/// Get a corresponding string content from a key spec␊ |
57 | ␉const char * getStringForKey(const char *key);␊ |
58 | ␉const char * getStringForKey(const std::string& key)␊ |
59 | ␉{␊ |
60 | ␉␉std::string str= key;␊ |
61 | ␉␉return getStringForKey(str.c_str());␊ |
62 | ␉}␊ |
63 | ␉␊ |
64 | ␉/// Set a content from a string, at specified key␊ |
65 | ␉bool setStringForKey(const char* key, const char* value);␊ |
66 | ␉bool setStringForKey(const std::string& key, const std::string& value)␊ |
67 | ␉{␊ |
68 | ␉␉return setStringForKey(key.c_str(), value.c_str());␊ |
69 | ␉}␊ |
70 | ␉␊ |
71 | ␉/// Remove a key from the Proplist␊ |
72 | ␉bool removeKeyAndValue(const char *key);␊ |
73 | ␊ |
74 | ␉/// Get the property list path␊ |
75 | ␉const char * propFilePath() const {␉return _propFilePath.c_str(); }␊ |
76 | ␊ |
77 | ␉/// permit to execute a priviledge chmod to access system files␊ |
78 | ␉static bool chmodFile(const char * path, const char * chmodMask,␊ |
79 | ␉␉␉␉␉␉ AuthorizationRef auth, AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
80 | protected:␊ |
81 | ␉CFPropertyListRef _proplistRef;␊ |
82 | ␉CFURLRef _CFURLRef;␊ |
83 | ␉std::string _propFilePath; // keep a track of the proplist filename␊ |
84 | ␉CFStringRef _errorString;␊ |
85 | };␊ |
86 | ␊ |
87 | #endif␊ |
88 | |