Skip to content
Open
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
21 changes: 15 additions & 6 deletions Sources/CoreFoundation/CFFileUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,24 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
#endif
Boolean isDir = (dp->d_type == DT_DIR);
if (!isDir) {
// Ugh; must stat.
char subdirPath[CFMaxPathLength];
struct statinfo statBuf;
cf_strlcpy(subdirPath, dirPath, sizeof(subdirPath));
cf_strlcat(subdirPath, "/", sizeof(subdirPath));
cf_strlcat(subdirPath, dp->d_name, sizeof(subdirPath));
if (stat(subdirPath, &statBuf) == 0) {
#if TARGET_OS_WASI
// WASI doesn't support dirfd/fstatat, fall back to stat

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaxDesiatov just confirming - is this correct?

if (pathLength + 1 + namelen < CFMaxPathLength) {
char subdirPath[CFMaxPathLength];
cf_strlcpy(subdirPath, dirPath, sizeof(subdirPath));
cf_strlcat(subdirPath, "/", sizeof(subdirPath));
cf_strlcat(subdirPath, dp->d_name, sizeof(subdirPath));
if (stat(subdirPath, &statBuf) == 0) {
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
}
}
#else
int dirfd_fd = dirfd(dirp);
if (dirfd_fd >= 0 && fstatat(dirfd_fd, dp->d_name, &statBuf, 0) == 0) {
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
}
#endif
}
#if TARGET_OS_LINUX || TARGET_OS_WASI
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);
Expand Down