2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2020 Apple 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 "SCPreferencesInternal.h"
35 #include "SCNetworkConfigurationInternal.h"
40 #include <sys/errno.h>
41 #include <sys/param.h>
43 __private_extern__ CF_RETURNS_RETAINED CFDataRef
44 __SCPSignatureFromStatbuf(const struct stat
*statBuf
)
46 CFMutableDataRef signature
;
47 SCPSignatureDataRef sig
;
49 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
50 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
52 /* ALIGN: CFDataGetBytePtr aligns to at least 8 bytes */
53 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
55 sig
->st_dev
= statBuf
->st_dev
;
56 sig
->st_ino
= statBuf
->st_ino
;
57 sig
->tv_sec
= statBuf
->st_mtimespec
.tv_sec
;
58 sig
->tv_nsec
= statBuf
->st_mtimespec
.tv_nsec
;
59 sig
->st_size
= statBuf
->st_size
;
66 __SCPreferencesGetNetworkConfigurationFlags(SCPreferencesRef prefs
)
68 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
70 return (prefs
!= NULL
) ? prefsPrivate
->nc_flags
: 0;
75 __SCPreferencesSetNetworkConfigurationFlags(SCPreferencesRef prefs
, uint32_t nc_flags
)
77 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
80 prefsPrivate
->nc_flags
= nc_flags
;
87 __private_extern__
char *
88 __SCPreferencesPath(CFAllocatorRef allocator
,
92 CFStringRef path
= NULL
;
95 if (prefsID
== NULL
) {
96 /* default preference ID */
97 path
= CFStringCreateWithFormat(allocator
,
100 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
101 useNewPrefs
? PREFS_DEFAULT_CONFIG
: PREFS_DEFAULT_CONFIG_OLD
);
102 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
103 /* if absolute path */
104 path
= CFStringCreateCopy(allocator
, prefsID
);
107 path
= CFStringCreateWithFormat(allocator
,
110 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
112 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
113 CFMutableStringRef newPath
;
115 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
116 CFStringReplace(newPath
,
117 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
125 * convert CFStringRef path to C-string path
127 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
128 if (pathStr
== NULL
) {
131 pathLen
= CFStringGetMaximumSizeOfFileSystemRepresentation(path
);
132 pathStr
= CFAllocatorAllocate(NULL
, pathLen
, 0);
133 if (!CFStringGetFileSystemRepresentation(path
, pathStr
, pathLen
)) {
134 SC_log(LOG_INFO
, "could not convert path to C string");
135 CFAllocatorDeallocate(NULL
, pathStr
);
147 __SCPreferencesIsEmpty(SCPreferencesRef prefs
)
149 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
151 assert(prefs
!= NULL
);
152 __SCPreferencesAccess(prefs
);
154 if ((prefsPrivate
->prefs
== NULL
) ||
155 (CFDictionaryGetCount(prefsPrivate
->prefs
) == 0)) {
165 __SCPreferencesPrefsSize(SCPreferencesRef prefs
)
167 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
168 SCPSignatureDataRef sig
;
171 signature
= prefsPrivate
->signature
;
172 if (signature
== NULL
) {
176 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
183 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs
)
186 Boolean isDefault
= FALSE
;
187 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
189 curPath
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
190 if (curPath
!= NULL
) {
193 defPath
= __SCPreferencesPath(NULL
,
195 (prefsPrivate
->newPath
== NULL
));
196 if (defPath
!= NULL
) {
197 if (strcmp(curPath
, defPath
) == 0) {
200 CFAllocatorDeallocate(NULL
, defPath
);
207 SCPreferencesGetSignature(SCPreferencesRef prefs
)
209 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
212 /* sorry, you must provide a session */
213 _SCErrorSet(kSCStatusNoPrefsSession
);
217 __SCPreferencesAccess(prefs
);
219 return prefsPrivate
->signature
;
223 __private_extern__ CF_RETURNS_RETAINED CFStringRef
224 _SCPNotificationKey(CFAllocatorRef allocator
,
231 CFStringRef storeKey
;
234 case kSCPreferencesKeyLock
:
235 keyStr
= CFSTR("lock");
237 case kSCPreferencesKeyCommit
:
238 keyStr
= CFSTR("commit");
240 case kSCPreferencesKeyApply
:
241 keyStr
= CFSTR("apply");
247 path
= __SCPreferencesPath(allocator
, prefsID
, TRUE
);
252 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
254 kCFStringEncodingASCII
,
257 storeKey
= CFStringCreateWithFormat(allocator
,
260 kSCDynamicStoreDomainPrefs
,
265 CFAllocatorDeallocate(NULL
, path
);
271 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
273 SCPreferencesKeyType keyType
)
275 return _SCPNotificationKey(allocator
, prefsID
, keyType
);