38
38
#include <unistd.h>
39
39
#include <dirent.h>
40
40
#include <sys/types.h>
41
+
42
+ #if !TARGET_OS_WASI
41
43
#include <pwd.h>
44
+ #else
45
+ #define WASI_D_NAME_SIZE 256
46
+ #endif
47
+
42
48
#include <fcntl.h>
43
49
44
50
#define statinfo stat
@@ -341,7 +347,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
341
347
FindClose (handle );
342
348
pathBuf [pathLength ] = '\0' ;
343
349
344
- #elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
350
+ #elif TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
345
351
uint8_t extBuff [CFMaxPathSize ];
346
352
int extBuffInteriorDotCount = 0 ; //people insist on using extensions like ".trace.plist", so we need to know how many dots back to look :(
347
353
@@ -437,7 +443,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
437
443
dirURL = CFURLCreateFromFileSystemRepresentation (alloc , (uint8_t * )dirPath , pathLength , true);
438
444
releaseBase = true;
439
445
}
440
- #if !defined(__OpenBSD__ )
446
+ #if !defined(__OpenBSD__ ) && ! TARGET_OS_WASI
441
447
if (dp -> d_type == DT_DIR || dp -> d_type == DT_UNKNOWN || dp -> d_type == DT_LNK || dp -> d_type == DT_WHT ) {
442
448
#else
443
449
if (dp -> d_type == DT_DIR || dp -> d_type == DT_UNKNOWN || dp -> d_type == DT_LNK ) {
@@ -454,13 +460,13 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
454
460
isDir = ((statBuf .st_mode & S_IFMT ) == S_IFDIR );
455
461
}
456
462
}
457
- #if TARGET_OS_LINUX
463
+ #if TARGET_OS_LINUX || TARGET_OS_WASI
458
464
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc , (uint8_t * )dp -> d_name , namelen , isDir , dirURL );
459
465
#else
460
466
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc , (uint8_t * )dp -> d_name , dp -> d_namlen , isDir , dirURL );
461
467
#endif
462
468
} else {
463
- #if TARGET_OS_LINUX
469
+ #if TARGET_OS_LINUX || TARGET_OS_WASI
464
470
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc , (uint8_t * )dp -> d_name , namelen , false, dirURL );
465
471
#else
466
472
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc , (uint8_t * )dp -> d_name , dp -> d_namlen , false, dirURL );
@@ -547,7 +553,7 @@ CF_PRIVATE SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean
547
553
548
554
if (modTime != NULL ) {
549
555
if (fileExists ) {
550
- #if TARGET_OS_WIN32 || TARGET_OS_LINUX
556
+ #if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
551
557
struct timespec ts = {statBuf .st_mtime , 0 };
552
558
#else
553
559
struct timespec ts = statBuf .st_mtimespec ;
@@ -1092,12 +1098,16 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
1092
1098
struct dirent * dent ;
1093
1099
if ((dirp = opendir (directoryPathBuf ))) {
1094
1100
while ((dent = readdir (dirp ))) {
1095
- #if TARGET_OS_LINUX
1101
+ #if TARGET_OS_LINUX || TARGET_OS_WASI
1096
1102
CFIndex nameLen = strlen (dent -> d_name );
1097
1103
if (dent -> d_type == DT_UNKNOWN ) {
1098
1104
// on some old file systems readdir may always fill d_type as DT_UNKNOWN (0), double check with stat
1099
1105
struct stat statBuf ;
1106
+ #if TARGET_OS_WASI
1107
+ char pathToStat [WASI_D_NAME_SIZE ];
1108
+ #else
1100
1109
char pathToStat [sizeof (dent -> d_name )];
1110
+ #endif
1101
1111
strncpy (pathToStat , directoryPathBuf , sizeof (pathToStat ));
1102
1112
strlcat (pathToStat , "/" , sizeof (pathToStat ));
1103
1113
strlcat (pathToStat , dent -> d_name , sizeof (pathToStat ));
@@ -1123,7 +1133,12 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
1123
1133
1124
1134
// 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
1125
1135
// Be sure to initialize the first character to null, so that strlcat below works correctly
1126
- char fullPathToFile [sizeof (dent -> d_name ) + 1 ];
1136
+ #if TARGET_OS_WASI
1137
+ size_t d_name_size = WASI_D_NAME_SIZE ;
1138
+ #else
1139
+ size_t d_name_size = sizeof (dent -> d_name );
1140
+ #endif
1141
+ char fullPathToFile [d_name_size + 1 ];
1127
1142
fullPathToFile [0 ] = 0 ;
1128
1143
CFIndex startAt = 0 ;
1129
1144
0 commit comments