Skip to content

Commit a6c748e

Browse files
committed
CoreFoundation: implement mapping from file descriptor to binary
This avoids an annoying warning in the Windows path that was introduced with the last update to Foundation. This implements the functionality on Windows as well.
1 parent 8a0ed08 commit a6c748e

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)