Skip to content

[wasm] Port CoreFoundation/Base.subproj/CFFileUtilities.c #4890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
38 changes: 31 additions & 7 deletions CoreFoundation/Base.subproj/CFFileUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <pwd.h>

#if !TARGET_OS_WASI
# include <pwd.h>
#endif

#include <fcntl.h>

#define statinfo stat
Expand Down Expand Up @@ -341,7 +345,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
FindClose(handle);
pathBuf[pathLength] = '\0';

#elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
#elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
uint8_t extBuff[CFMaxPathSize];
int extBuffInteriorDotCount = 0; //people insist on using extensions like ".trace.plist", so we need to know how many dots back to look :(

Expand Down Expand Up @@ -444,7 +448,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
dirURL = CFURLCreateFromFileSystemRepresentation(alloc, (uint8_t *)dirPath, pathLength, true);
releaseBase = true;
}
#if !defined(__OpenBSD__)
#if !defined(__OpenBSD__) && !TARGET_OS_WASI
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK || dp->d_type == DT_WHT) {
#else
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK) {
Expand All @@ -461,13 +465,13 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
}
}
#if TARGET_OS_LINUX
#if TARGET_OS_LINUX || TARGET_OS_WASI
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);
#else
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, dp->d_namlen, isDir, dirURL);
#endif
} else {
#if TARGET_OS_LINUX
#if TARGET_OS_LINUX || TARGET_OS_WASI
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, namelen, false, dirURL);
#else
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, dp->d_namlen, false, dirURL);
Expand Down Expand Up @@ -554,7 +558,7 @@ CF_PRIVATE SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean

if (modTime != NULL) {
if (fileExists) {
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
#if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
struct timespec ts = {statBuf.st_mtime, 0};
#else
struct timespec ts = statBuf.st_mtimespec;
Expand Down Expand Up @@ -1118,6 +1122,8 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
}
}
}
#elif TARGET_OS_WASI
CFIndex nameLen = strlen(dent->d_name);
#else
CFIndex nameLen = dent->d_namlen;
#endif
Expand All @@ -1130,7 +1136,25 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla

// This buffer has to be 1 bigger than the size of the one in the dirent so we can hold the extra '/' if it's required
// Be sure to initialize the first character to null, so that strlcat below works correctly
char fullPathToFile[sizeof(dent->d_name) + 1];
#if TARGET_OS_WASI
// wasi-libc's dirent.d_name is not a fixed-size array but a pointer, so we need to calculate
// the size of buffer at first.
size_t d_name_size = nameLen;
if (stuffToPrefix) {
for (CFIndex i = 0; i < CFArrayGetCount(stuffToPrefix); i++) {
CFStringRef onePrefix = CFArrayGetValueAtIndex(stuffToPrefix, i);
size_t prefixLen = CFStringGetLength(onePrefix);
// Add 1 for concatenating '/'
if (d_name_size > nameLen) {
d_name_size += 1;
}
d_name_size += prefixLen;
}
}
#else
size_t d_name_size = sizeof(dent->d_name);
#endif
char fullPathToFile[d_name_size + 1];
fullPathToFile[0] = 0;
CFIndex startAt = 0;

Expand Down