2 * Copyright(c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
41 #include <sys/errno.h>
42 #include <sys/param.h>
44 __private_extern__ CFDataRef
45 __SCPSignatureFromStatbuf(const struct stat
*statBuf
)
47 CFMutableDataRef signature
;
48 SCPSignatureDataRef sig
;
50 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
51 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
52 sig
= (SCPSignatureDataRef
)CFDataGetBytePtr(signature
);
53 sig
->st_dev
= statBuf
->st_dev
;
54 sig
->st_ino
= statBuf
->st_ino
;
55 sig
->st_mtimespec
= statBuf
->st_mtimespec
;
56 sig
->st_size
= statBuf
->st_size
;
61 __private_extern__
char *
62 __SCPreferencesPath(CFAllocatorRef allocator
,
68 CFStringRef path
= NULL
;
72 if (prefsID
== NULL
) {
73 /* no user prefsID specified */
75 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
76 /* if absolute path */
77 path
= CFStringCreateCopy(allocator
, prefsID
);
80 * relative (to the user's preferences) path
82 char login
[MAXLOGNAME
+1];
85 bzero(&login
, sizeof(login
));
89 /* get current console user */
90 u
= SCDynamicStoreCopyConsoleUser(NULL
, NULL
, NULL
);
92 /* if could not get console user */
96 (void)_SC_cfstring_to_cstring(u
, login
, sizeof(login
), kCFStringEncodingASCII
);
99 /* use specified user */
100 (void)_SC_cfstring_to_cstring(user
, login
, sizeof(login
), kCFStringEncodingASCII
);
103 /* get password entry for user */
104 pwd
= getpwnam(login
);
106 /* if no home directory */
110 /* create prefs ID */
111 path
= CFStringCreateWithFormat(allocator
,
115 PREFS_DEFAULT_USER_DIR
,
119 if (prefsID
== NULL
) {
120 /* default preference ID */
121 path
= CFStringCreateWithFormat(allocator
,
124 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
125 useNewPrefs
? PREFS_DEFAULT_CONFIG
: PREFS_DEFAULT_CONFIG_OLD
);
126 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
127 /* if absolute path */
128 path
= CFStringCreateCopy(allocator
, prefsID
);
131 path
= CFStringCreateWithFormat(allocator
,
134 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
136 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
137 CFMutableStringRef newPath
;
139 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
140 CFStringReplace(newPath
,
141 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
150 * convert CFStringRef path to C-string path
152 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
153 if (pathStr
== NULL
) {
154 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("could not convert path to C string"));
163 SCPreferencesGetSignature(SCPreferencesRef prefs
)
165 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
168 /* sorry, you must provide a session */
169 _SCErrorSet(kSCStatusNoPrefsSession
);
173 __SCPreferencesAccess(prefs
);
175 return prefsPrivate
->signature
;
179 __private_extern__ CFStringRef
180 _SCPNotificationKey(CFAllocatorRef allocator
,
189 CFStringRef storeKey
;
192 case kSCPreferencesKeyLock
:
193 keyStr
= CFSTR("lock");
195 case kSCPreferencesKeyCommit
:
196 keyStr
= CFSTR("commit");
198 case kSCPreferencesKeyApply
:
199 keyStr
= CFSTR("apply");
205 path
= __SCPreferencesPath(allocator
, prefsID
, perUser
, user
, TRUE
);
210 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
212 kCFStringEncodingASCII
,
215 storeKey
= CFStringCreateWithFormat(allocator
,
218 kSCDynamicStoreDomainPrefs
,
223 CFAllocatorDeallocate(NULL
, path
);
229 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
231 SCPreferencesKeyType keyType
)
233 return _SCPNotificationKey(allocator
, prefsID
, FALSE
, NULL
, keyType
);
238 SCDynamicStoreKeyCreateUserPreferences(CFAllocatorRef allocator
,
241 SCPreferencesKeyType keyType
)
243 return _SCPNotificationKey(allocator
, prefsID
, TRUE
, user
, keyType
);