Skip to content

Commit de6482e

Browse files
committed
[android] Improve CFKnownLocations.
The usage of CFKnownLocations from Swift Foundation seems to be reduced to both _kCFKnownLocationUserAny and _kCFKnownLocationUserCurrent. From the public API of Swift Foundation there's no way to get to _kCFKnownLocationUserByName. Supporting UserAny is, therefore, necessary. There are some ways of accessing _kCFKnownLocationUserByName from the CF API, but it seems that the API only allows Current or Any, even if they are not enforced. In any case, Android do not have Unix usernames, so ByName doesn't make sense, and should be always the same as UserCurrent. The change in code makes every code path fall back to the current user (which should be also any user, and any user by name, because the platforms limitations). Additionally, instead of picking up a CFFIXEDUSE_HOME, request the home directory from CFUtilities, since it deals with HOME, HOMEDIR and CFFIXED_HOME appropiedly, since HOME is necessary for many other things. The current code will have aborted the test suite, while the modified code, passes the test suite for UserDefaults.
1 parent 7b72fbd commit de6482e

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

CoreFoundation/Base.subproj/CFKnownLocations.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <CoreFoundation/CFString.h>
1313
#include "CFPriv.h"
1414
#include "CFInternal.h"
15+
#include "CFUtilities.h"
1516

1617
#include <assert.h>
1718

@@ -94,21 +95,15 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
9495
#elif TARGET_OS_ANDROID
9596

9697
switch (user) {
97-
case _kCFKnownLocationUserAny:
98-
case _kCFKnownLocationUserByName:
99-
abort();
100-
case _kCFKnownLocationUserCurrent: {
101-
const char *buffer = getenv("CFFIXED_USER_HOME");
102-
if (buffer == NULL || *buffer == '\0') {
103-
CFLog(__kCFLogAssertion, CFSTR("CFFIXED_USER_HOME is unset"));
104-
HALT;
105-
}
106-
107-
CFURLRef userdir = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (const unsigned char *)buffer, strlen(buffer), true);
108-
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("/Apple/Library/Preferences"), kCFURLPOSIXPathStyle, true, userdir);
109-
CFRelease(userdir);
110-
break;
111-
}
98+
case _kCFKnownLocationUserAny:
99+
case _kCFKnownLocationUserByName:
100+
// fallthrough
101+
case _kCFKnownLocationUserCurrent: {
102+
CFURLRef userdir = CFCopyHomeDirectoryURL();
103+
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true, userdir);
104+
CFRelease(userdir);
105+
break;
106+
}
112107
}
113108

114109
#else

0 commit comments

Comments
 (0)