Skip to content

Commit 336a53f

Browse files
committed
Switch to a regular enum from a typed CFStringRef enum.
1 parent 066682d commit 336a53f

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

CoreFoundation/Base.subproj/CFKnownLocations.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515

1616
#include <assert.h>
1717

18-
CONST_STRING_DECL(_kCFKnownLocationUserAny, " == _kCFKnownLocationUserAny");
19-
CONST_STRING_DECL(_kCFKnownLocationUserCurrent, " == _kCFKnownLocationUserCurrent");
20-
21-
CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user) {
18+
CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) {
2219
CFURLRef location = NULL;
2320

2421
#if (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI)
@@ -31,18 +28,23 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
3128
- Current: $HOME/Library/Preferences
3229
*/
3330

34-
if (user == _kCFKnownLocationUserAny) {
35-
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true);
36-
} else {
37-
if (user == _kCFKnownLocationUserCurrent) {
38-
user = NULL;
31+
switch (user) {
32+
case _kCFKnownLocationUserAny:
33+
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true);
34+
break;
35+
36+
case _kCFKnownLocationUserCurrent:
37+
username = NULL;
38+
// passthrough to:
39+
case _kCFKnownLocationUserByName: {
40+
CFURLRef home = CFCopyHomeDirectoryURLForUser(username);
41+
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true, home);
42+
CFRelease(home);
43+
44+
break;
3945
}
40-
41-
CFURLRef home = CFCopyHomeDirectoryURLForUser(user);
42-
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true, home);
43-
CFRelease(home);
46+
4447
}
45-
4648
#elif !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
4749

4850
/*
@@ -53,15 +55,20 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
5355
- Current: $XDG_CONFIG_PATH (usually: $HOME/.config/).
5456
*/
5557

56-
if (user == _kCFKnownLocationUserAny) {
57-
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/usr/local/etc"), kCFURLPOSIXPathStyle, true);
58-
} else {
59-
assert(user == _kCFKnownLocationUserCurrent);
60-
61-
if (user == _kCFKnownLocationUserCurrent) {
58+
switch (user) {
59+
case _kCFKnownLocationUserAny:
60+
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/usr/local/etc"), kCFURLPOSIXPathStyle, true);
61+
break;
62+
63+
case _kCFKnownLocationUserByName:
64+
assert(username == NULL);
65+
// passthrough to:
66+
case _kCFKnownLocationUserCurrent: {
6267
CFStringRef path = _CFXDGCreateConfigHomePath();
6368
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, kCFURLPOSIXPathStyle, true);
6469
CFRelease(path);
70+
71+
break;
6572
}
6673
}
6774

CoreFoundation/Base.subproj/CFKnownLocations.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
CF_ASSUME_NONNULL_BEGIN
1717

18-
typedef CFStringRef CFKnownLocationUser CF_TYPED_EXTENSIBLE_ENUM;
19-
20-
extern const CFKnownLocationUser _kCFKnownLocationUserAny;
21-
22-
extern const CFKnownLocationUser _kCFKnownLocationUserCurrent;
18+
typedef CF_ENUM(CFIndex, CFKnownLocationUser) {
19+
_kCFKnownLocationUserAny,
20+
_kCFKnownLocationUserCurrent,
21+
_kCFKnownLocationUserByName,
22+
};
2323

2424
/* A note on support:
2525
@@ -28,13 +28,14 @@ extern const CFKnownLocationUser _kCFKnownLocationUserCurrent;
2828
- For platforms that use the XDG spec to identify a configuration path in a user's home, we cannot determine that path for any user other than the one we're currently running as.
2929
3030
So:
31-
- We're keeping that behavior when building Core Foundation for Darwin/ObjC for compatibility, hence the _EXTENSIBLE above; but
31+
- We're keeping that behavior when building Core Foundation for Darwin/ObjC for compatibility, hence the _EXTENSIBLE above; on those platforms, the …ByName enum will continue working to get locations for arbitrary usernames. But:
3232
- For Swift and any new platform, we are enforcing the documented constraint. Using a user value other than …Any or …Current above will assert (or return NULL if asserts are off).
3333
3434
See CFKnownLocations.c for a summary of what paths are returned.
3535
*/
3636

37-
extern CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user);
37+
// The username parameter is ignored for any user constant other than …ByName. …ByName with a NULL username is the same as …Current.
38+
extern CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username);
3839

3940
CF_ASSUME_NONNULL_END
4041

CoreFoundation/Preferences.subproj/CFPreferences.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ static CFURLRef _preferencesDirectoryForUserHostSafetyLevel(CFStringRef userName
193193
} else if (userName == kCFPreferencesCurrentUser) {
194194
user = _kCFKnownLocationUserCurrent;
195195
} else {
196-
user = userName;
196+
user = _kCFKnownLocationUserByName;
197197
}
198198

199-
CFURLRef base = _CFKnownLocationCreatePreferencesURLForUser(user);
199+
CFURLRef base = _CFKnownLocationCreatePreferencesURLForUser(user, userName);
200200

201201
if (hostName == kCFPreferencesCurrentHost) {
202202
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("ByHost"), kCFURLPOSIXPathStyle, true, base);

0 commit comments

Comments
 (0)