Skip to content

Commit b7d2fd4

Browse files
committed
Prefer fstatat + dirfd to cf_strlcat and then stat
1 parent 5550995 commit b7d2fd4

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Sources/CoreFoundation/CFFileUtilities.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,24 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
455455
#endif
456456
Boolean isDir = (dp->d_type == DT_DIR);
457457
if (!isDir) {
458-
// Ugh; must stat.
459-
char subdirPath[CFMaxPathLength];
460458
struct statinfo statBuf;
459+
#if TARGET_OS_WASI
460+
// WASI doesn't support dirfd/fstatat, fall back to stat
461+
char subdirPath[CFMaxPathLength];
461462
cf_strlcpy(subdirPath, dirPath, sizeof(subdirPath));
462463
cf_strlcat(subdirPath, "/", sizeof(subdirPath));
463464
cf_strlcat(subdirPath, dp->d_name, sizeof(subdirPath));
464-
if (stat(subdirPath, &statBuf) == 0) {
465+
if (stat(subdirPath, &statBuf) == 0)
466+
{
467+
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
468+
}
469+
#else
470+
int dirfd_fd = dirfd(dirp);
471+
if (dirfd_fd >= 0 && fstatat(dirfd_fd, dp->d_name, &statBuf, 0) == 0)
472+
{
465473
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
466474
}
475+
#endif
467476
}
468477
#if TARGET_OS_LINUX || TARGET_OS_WASI
469478
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);

0 commit comments

Comments
 (0)