Skip to content

Commit 2ce09f8

Browse files
committed
Prefer fstatat + dirfd to cf_strlcat and then stat
Prefer fstatat + dirfd to cf_strlcat and then stat
1 parent 040e214 commit 2ce09f8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Sources/CoreFoundation/CFFileUtilities.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,22 @@ 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));
464465
if (stat(subdirPath, &statBuf) == 0) {
465466
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
466467
}
468+
#else
469+
int dirfd_fd = dirfd(dirp);
470+
if (dirfd_fd >= 0 && fstatat(dirfd_fd, dp->d_name, &statBuf, 0) == 0) {
471+
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
472+
}
473+
#endif
467474
}
468475
#if TARGET_OS_LINUX || TARGET_OS_WASI
469476
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);

0 commit comments

Comments
 (0)