Skip to content

Commit 675ce56

Browse files
authored
Merge pull request #3147 from compnerd/reverse-lookup
CoreFoundation: implement mapping from file descriptor to binary
2 parents 51bb354 + a6c748e commit 675ce56

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

CoreFoundation/PlugIn.subproj/CFBundle_Internal.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,29 @@ static bool _CFGetPathFromFileDescriptor(int fd, char *path) {
458458
#else
459459

460460
static bool _CFGetPathFromFileDescriptor(int fd, char *path) {
461-
#warning This platform does not have a way to go back from an open file descriptor to a path.
462-
return false;
461+
HANDLE hFile = _get_osfhandle(fd);
462+
if (hFile == INVALID_HANDLE_VALUE)
463+
return false;
464+
465+
DWORD dwLength = GetFinalPathNameByHandleW(hFile, NULL, 0, 0);
466+
467+
WCHAR *wszPath = (WCHAR *)malloc(dwLength);
468+
if (wszPath == NULL)
469+
return false;
470+
471+
if (GetFinalPathNameByHandleW(hFile, wszPath, dwLength, 0) != dwLength - 1) {
472+
free(wszPath);
473+
return false;
474+
}
475+
476+
CFStringRef location =
477+
CFStringCreateWithBytes(kCFAllocatorSystemDefault,
478+
(const UInt8 *)wszPath, dwLength - 1,
479+
kCFStringEncodingUTF16, false);
480+
path = strdup(CFStringGetCStringPtr(location, kCFStringEncodingUTF8));
481+
CFRelease(location);
482+
free(wszPath);
483+
return true;
463484
}
464485

465486
#endif

0 commit comments

Comments
 (0)