Skip to content

Base.subproject: correct home directory handling on Windows #4692

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

Merged
merged 1 commit into from
Jan 20, 2023
Merged
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
56 changes: 22 additions & 34 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,40 +582,28 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
return CFCopyHomeDirectoryURL();
}

CFStringRef home = NULL;
LPUSER_INFO_1 pBuffer = NULL;
DWORD dwEntriesRead = 0;
DWORD dwEntries = 0;
DWORD dwToken = 0;
NTSTATUS nStatus;
do {
nStatus = NetUserEnum(NULL, 1, FILTER_NORMAL_ACCOUNT, (LPBYTE*)&pBuffer,
MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwEntries,
&dwToken);
if (nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) {
for (unsigned uiEntry = 0; !home && uiEntry < dwEntriesRead; ++uiEntry) {
CFStringRef name =
CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault,
pBuffer[uiEntry].usri1_name,
kCFStringEncodingUTF16,
kCFAllocatorNull);

if (CFEqual(uName, name)) {
home = pBuffer[uiEntry].usri1_home_dir
? CFStringCreateWithCString(kCFAllocatorSystemDefault,
pBuffer[uiEntry].usri1_home_dir,
kCFStringEncodingUTF16)
: CFSTR("");
}

CFRelease(name);
}
}
NetApiBufferFree(pBuffer);
pBuffer = NULL;
} while (nStatus == ERROR_MORE_DATA);

return home;
CFIndex ulLength = CFStringGetLength(uName);

UniChar *buffer = calloc(ulLength + 1, sizeof(UniChar));
if (buffer == NULL)
return NULL;
CFStringGetCharacters(uName, CFRangeMake(0, ulLength), buffer);

CFURLRef url = NULL;
LPUSER_INFO_1 lpUI1 = NULL;
if (NetUserGetInfo(NULL, buffer, 1, (LPBYTE*)&lpUI1) == NERR_Success) {
CFStringRef path =
CFStringCreateWithCharacters(kCFAllocatorSystemDefault,
lpUI1->usri1_home_dir,
wcslen(lpUI1->usri1_home_dir));
url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, path,
kCFURLWindowsPathStyle, true);
CFRelease(path);
NetApiBufferFree(lpUI1);
}
free(buffer);

return url;
#else
#error Dont know how to compute users home directories on this platform
#endif
Expand Down