2 * Copyright(c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * November 9, 2000 Allan Nathanson <ajn@apple.com>
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCPreferencesInternal.h"
43 #include <sys/errno.h>
44 #include <sys/param.h>
46 __private_extern__ CFDataRef
47 __SCPSignatureFromStatbuf(const struct stat
*statBuf
)
49 CFMutableDataRef signature
;
50 SCPSignatureDataRef sig
;
52 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
53 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
54 sig
= (SCPSignatureDataRef
)CFDataGetBytePtr(signature
);
55 sig
->st_dev
= statBuf
->st_dev
;
56 sig
->st_ino
= statBuf
->st_ino
;
57 sig
->st_mtimespec
= statBuf
->st_mtimespec
;
58 sig
->st_size
= statBuf
->st_size
;
63 __private_extern__
char *
64 __SCPreferencesPath(CFAllocatorRef allocator
,
70 CFStringRef path
= NULL
;
74 if (prefsID
== NULL
) {
75 /* no user prefsID specified */
77 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
78 /* if absolute path */
79 path
= CFStringCreateCopy(allocator
, prefsID
);
82 * relative (to the user's preferences) path
84 char login
[MAXLOGNAME
+1];
87 bzero(&login
, sizeof(login
));
91 /* get current console user */
92 u
= SCDynamicStoreCopyConsoleUser(NULL
, NULL
, NULL
);
94 /* if could not get console user */
98 (void)_SC_cfstring_to_cstring(u
, login
, sizeof(login
), kCFStringEncodingASCII
);
101 /* use specified user */
102 (void)_SC_cfstring_to_cstring(user
, login
, sizeof(login
), kCFStringEncodingASCII
);
105 /* get password entry for user */
106 pwd
= getpwnam(login
);
108 /* if no home directory */
112 /* create prefs ID */
113 path
= CFStringCreateWithFormat(allocator
,
117 PREFS_DEFAULT_USER_DIR
,
121 if (prefsID
== NULL
) {
122 /* default preference ID */
123 path
= CFStringCreateWithFormat(allocator
,
126 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
127 useNewPrefs
? PREFS_DEFAULT_CONFIG
: PREFS_DEFAULT_CONFIG_OLD
);
128 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
129 /* if absolute path */
130 path
= CFStringCreateCopy(allocator
, prefsID
);
133 path
= CFStringCreateWithFormat(allocator
,
136 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
138 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
139 CFMutableStringRef newPath
;
141 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
142 CFStringReplace(newPath
,
143 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
152 * convert CFStringRef path to C-string path
154 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
155 if (pathStr
== NULL
) {
156 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("could not convert path to C string"));
165 SCPreferencesGetSignature(SCPreferencesRef session
)
167 SCPreferencesPrivateRef sessionPrivate
= (SCPreferencesPrivateRef
)session
;
169 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCPreferencesGetSignature:"));
171 sessionPrivate
->accessed
= TRUE
;
172 return sessionPrivate
->signature
;
176 __private_extern__ CFStringRef
177 _SCPNotificationKey(CFAllocatorRef allocator
,
183 CFStringRef key
= NULL
;
187 pathStr
= __SCPreferencesPath(allocator
, prefsID
, perUser
, user
, TRUE
);
188 if (pathStr
== NULL
) {
192 /* create notification key */
194 case kSCPreferencesKeyLock
:
197 case kSCPreferencesKeyCommit
:
200 case kSCPreferencesKeyApply
:
207 key
= CFStringCreateWithFormat(allocator
,
210 kSCDynamicStoreDomainPrefs
,
214 CFAllocatorDeallocate(NULL
, pathStr
);
220 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
222 SCPreferencesKeyType keyType
)
224 return _SCPNotificationKey(allocator
, prefsID
, FALSE
, NULL
, keyType
);
229 SCDynamicStoreKeyCreateUserPreferences(CFAllocatorRef allocator
,
232 SCPreferencesKeyType keyType
)
234 return _SCPNotificationKey(allocator
, prefsID
, TRUE
, user
, keyType
);