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 | ␉/// release the current proplist if it exists␊ |
44 | ␉void cleanup();␊ |
45 | ␊ |
46 | ␉/// Open a property list␊ |
47 | ␉bool open(const char *propListPath, bool createIfNotExist,␊ |
48 | ␉␉␉ AuthorizationRef auth=NULL, ␊ |
49 | ␉␉␉ AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
50 | ␊ |
51 | ␉/// Save a property list␊ |
52 | ␉bool save(AuthorizationRef auth=NULL, ␊ |
53 | ␉␉␉ AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
54 | ␉␊ |
55 | ␉/// Return last error status, used in open()␊ |
56 | ␉CFStringRef lastError() const { return _errorString;}␊ |
57 | ␉␊ |
58 | ␉/// Get a corresponding string content from a key spec␊ |
59 | ␉const char * getStringForKey(const char *key);␊ |
60 | ␉const char * getStringForKey(const std::string& key)␊ |
61 | ␉{␊ |
62 | ␉␉std::string str= key;␊ |
63 | ␉␉return getStringForKey(str.c_str());␊ |
64 | ␉}␊ |
65 | ␉␊ |
66 | ␉/// Set a content from a string, at specified key␊ |
67 | ␉bool setStringForKey(const char* key, const char* value);␊ |
68 | ␉bool setStringForKey(const std::string& key, const std::string& value)␊ |
69 | ␉{␊ |
70 | ␉␉return setStringForKey(key.c_str(), value.c_str());␊ |
71 | ␉}␊ |
72 | ␉␊ |
73 | ␉/// Remove a key from the Proplist␊ |
74 | ␉bool removeKeyAndValue(const char *key);␊ |
75 | ␊ |
76 | ␉/// Get the property list path␊ |
77 | ␉const char * propFilePath() const {␉return _propFilePath.c_str(); }␊ |
78 | ␊ |
79 | ␉/// permit to execute a priviledge chmod to access system files␊ |
80 | ␉static bool chmodFile(const char * path, const char * chmodMask,␊ |
81 | ␉␉␉␉␉␉ AuthorizationRef auth, AuthorizationFlags flags=kAuthorizationFlagDefaults);␊ |
82 | protected:␊ |
83 | ␉CFPropertyListRef _proplistRef;␊ |
84 | ␉CFURLRef _CFURLRef;␊ |
85 | ␉std::string _propFilePath; // keep a track of the proplist filename␊ |
86 | ␉CFStringRef _errorString;␊ |
87 | };␊ |
88 | ␊ |
89 | #endif␊ |
90 | |