Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SHGetKnownFolderPath to get path to localappdata for current user #4998

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions CoreFoundation/Base.subproj/CFKnownLocations.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* CFKnownLocations.c
Copyright (c) 1999-2017, Apple Inc. and the Swift project authors

Portions Copyright (c) 2014-2017, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
Expand All @@ -18,65 +18,66 @@

#if TARGET_OS_WIN32
#include <userenv.h>
#include <shlobj_core.h>
#endif

CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) {
CFURLRef location = NULL;

#if TARGET_OS_MAC

/*
Building for a Darwin OS. (We use these paths on Swift builds as well, so that we can interoperate a little with Darwin's defaults(1) command and the other system facilities; but you want to use the system version of CF if possible on those platforms, which will talk to cfprefsd(8) and has stronger interprocess consistency guarantees.)

User:
- Any: /Library/Preferences
- Current: $HOME/Library/Preferences
*/

switch (user) {
case _kCFKnownLocationUserAny:
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true);
break;

case _kCFKnownLocationUserCurrent:
username = NULL;
// passthrough to:
case _kCFKnownLocationUserByName: {
CFURLRef home = CFCopyHomeDirectoryURLForUser(username);
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("/Library/Preferences"), kCFURLPOSIXPathStyle, true, home);
CFRelease(home);

break;
}

}
#elif !DEPLOYMENT_RUNTIME_OBJC && !TARGET_OS_WIN32 && !TARGET_OS_ANDROID

/*
Building for an OS that uses the FHS, BSD's hier(7), and/or the XDG specification for paths:

User:
- Any: /usr/local/etc/
- Current: $XDG_CONFIG_PATH (usually: $HOME/.config/).
*/

switch (user) {
case _kCFKnownLocationUserAny:
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("/usr/local/etc"), kCFURLPOSIXPathStyle, true);
break;

case _kCFKnownLocationUserByName:
assert(username == NULL);
// passthrough to:
case _kCFKnownLocationUserCurrent: {
CFStringRef path = _CFXDGCreateConfigHomePath();
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, kCFURLPOSIXPathStyle, true);
CFRelease(path);

break;
}
}

#elif TARGET_OS_WIN32

switch (user) {
Expand All @@ -96,30 +97,12 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
}
case _kCFKnownLocationUserCurrent:
username = CFGetUserName();
// fallthrough
break;
case _kCFKnownLocationUserByName: {
DWORD size = 0;
GetProfilesDirectoryW(NULL, &size);

wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t));
GetProfilesDirectoryW(path, &size);

CFStringRef pathRef = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1);
free(path);

CFURLRef profilesDir = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, pathRef, kCFURLWindowsPathStyle, true);
CFRelease(pathRef);

CFURLRef usernameDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, profilesDir, username, true);
CFURLRef appdataDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, usernameDir, CFSTR("AppData"), true);
location = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, appdataDir, CFSTR("Local"), true);
CFRelease(usernameDir);
CFRelease(appdataDir);

CFRelease(profilesDir);
if (user == _kCFKnownLocationUserCurrent) {
CFRelease(username);
}
wchar_t* path = NULL;
SHGetKnownFolderPath(&FOLDERID_LocalAppData, 0, NULL, &path);
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path, kCFURLWindowsPathStyle, true);
CoTaskMemFree(path);
break;
}
}
Expand Down Expand Up @@ -148,10 +131,10 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
CFRelease(userdir);

#else

#error For this platform, you need to define a preferences path for both 'any user' (i.e. installation-wide preferences) or the current user.

#endif

return location;
}