Skip to content

Commit 8ff7ba3

Browse files
darinfcompnerd
authored andcommitted
Fix the way known locations are generated on windows
Use GetAllUsersProfileDirectoryW and GetProfilesDirectoryW instead of hardcoding paths. Previously, the known locations were treated as UNC paths, which would typically fail to be read after ~5 second timeout.
1 parent 9cf3489 commit 8ff7ba3

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

CoreFoundation/Base.subproj/CFKnownLocations.c

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#include <assert.h>
1818

19+
#if TARGET_OS_WIN32
20+
#include <userenv.h>
21+
#endif
22+
1923
CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUser user, CFStringRef _Nullable username) {
2024
CFURLRef location = NULL;
2125

@@ -76,20 +80,48 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
7680
#elif TARGET_OS_WIN32
7781

7882
switch (user) {
79-
case _kCFKnownLocationUserAny:
80-
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("\\Users\\All Users\\AppData\\Local"), kCFURLWindowsPathStyle, true);
83+
case _kCFKnownLocationUserAny: {
84+
DWORD size = 0;
85+
GetAllUsersProfileDirectoryW(NULL, &size);
86+
87+
wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t));
88+
GetAllUsersProfileDirectoryW(path, &size);
89+
90+
CFStringRef allUsersPath = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1);
91+
free(path);
92+
93+
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, allUsersPath, kCFURLWindowsPathStyle, true);
94+
CFRelease(allUsersPath);
8195
break;
96+
}
8297
case _kCFKnownLocationUserCurrent:
8398
username = CFGetUserName();
8499
// fallthrough
85-
case _kCFKnownLocationUserByName:
86-
const char *user = CFStringGetCStringPtr(username, kCFStringEncodingUTF8);
87-
CFURLRef userdir = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (const unsigned char *)user, strlen(user), true);
88-
CFURLRef homedir = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\Users"), kCFURLWindowsPathStyle, true, userdir);
89-
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\AppData\\Local"), kCFURLWindowsPathStyle, true, homedir);
90-
CFRelease(homedir);
91-
CFRelease(userdir);
100+
case _kCFKnownLocationUserByName: {
101+
DWORD size = 0;
102+
GetProfilesDirectoryW(NULL, &size);
103+
104+
wchar_t* path = (wchar_t*)malloc(size * sizeof(wchar_t));
105+
GetProfilesDirectoryW(path, &size);
106+
107+
CFStringRef pathRef = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, path, size - 1);
108+
free(path);
109+
110+
CFURLRef profilesDir = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, pathRef, kCFURLWindowsPathStyle, true);
111+
CFRelease(pathRef);
112+
113+
CFURLRef usernameDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, profilesDir, username, true);
114+
CFURLRef appdataDir = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, usernameDir, CFSTR("AppData"), true);
115+
location = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, appdataDir, CFSTR("Local"), true);
116+
CFRelease(usernameDir);
117+
CFRelease(appdataDir);
118+
119+
CFRelease(profilesDir);
120+
if (user == _kCFKnownLocationUserCurrent) {
121+
CFRelease(username);
122+
}
92123
break;
124+
}
93125
}
94126

95127
#elif TARGET_OS_ANDROID

0 commit comments

Comments
 (0)