Skip to content

Commit 1b4f09d

Browse files
committed
Merge changes from upstream swift-corelibs-foundation
2 parents 53857d5 + 44ee4ed commit 1b4f09d

23 files changed

+300
-94
lines changed

Diff for: Sources/CoreFoundation/CFFileUtilities.c

+31-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
#include <unistd.h>
3939
#include <dirent.h>
4040
#include <sys/types.h>
41-
#include <pwd.h>
41+
42+
#if !TARGET_OS_WASI
43+
# include <pwd.h>
44+
#endif
45+
4246
#include <fcntl.h>
4347

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

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

@@ -444,7 +448,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
444448
dirURL = CFURLCreateFromFileSystemRepresentation(alloc, (uint8_t *)dirPath, pathLength, true);
445449
releaseBase = true;
446450
}
447-
#if !defined(__OpenBSD__)
451+
#if !defined(__OpenBSD__) && !TARGET_OS_WASI
448452
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK || dp->d_type == DT_WHT) {
449453
#else
450454
if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK) {
@@ -461,13 +465,13 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
461465
isDir = ((statBuf.st_mode & S_IFMT) == S_IFDIR);
462466
}
463467
}
464-
#if TARGET_OS_LINUX
468+
#if TARGET_OS_LINUX || TARGET_OS_WASI
465469
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, namelen, isDir, dirURL);
466470
#else
467471
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase(alloc, (uint8_t *)dp->d_name, dp->d_namlen, isDir, dirURL);
468472
#endif
469473
} else {
470-
#if TARGET_OS_LINUX
474+
#if TARGET_OS_LINUX || TARGET_OS_WASI
471475
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, namelen, false, dirURL);
472476
#else
473477
fileURL = CFURLCreateFromFileSystemRepresentationRelativeToBase (alloc, (uint8_t *)dp->d_name, dp->d_namlen, false, dirURL);
@@ -554,7 +558,7 @@ CF_PRIVATE SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean
554558

555559
if (modTime != NULL) {
556560
if (fileExists) {
557-
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
561+
#if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
558562
struct timespec ts = {statBuf.st_mtime, 0};
559563
#else
560564
struct timespec ts = statBuf.st_mtimespec;
@@ -1118,6 +1122,8 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
11181122
}
11191123
}
11201124
}
1125+
#elif TARGET_OS_WASI
1126+
CFIndex nameLen = strlen(dent->d_name);
11211127
#else
11221128
CFIndex nameLen = dent->d_namlen;
11231129
#endif
@@ -1130,7 +1136,25 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
11301136

11311137
// 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
11321138
// Be sure to initialize the first character to null, so that strlcat below works correctly
1133-
char fullPathToFile[sizeof(dent->d_name) + 1];
1139+
#if TARGET_OS_WASI
1140+
// wasi-libc's dirent.d_name is not a fixed-size array but a pointer, so we need to calculate
1141+
// the size of buffer at first.
1142+
size_t d_name_size = nameLen;
1143+
if (stuffToPrefix) {
1144+
for (CFIndex i = 0; i < CFArrayGetCount(stuffToPrefix); i++) {
1145+
CFStringRef onePrefix = CFArrayGetValueAtIndex(stuffToPrefix, i);
1146+
size_t prefixLen = CFStringGetLength(onePrefix);
1147+
// Add 1 for concatenating '/'
1148+
if (d_name_size > nameLen) {
1149+
d_name_size += 1;
1150+
}
1151+
d_name_size += prefixLen;
1152+
}
1153+
}
1154+
#else
1155+
size_t d_name_size = sizeof(dent->d_name);
1156+
#endif
1157+
char fullPathToFile[d_name_size + 1];
11341158
fullPathToFile[0] = 0;
11351159
CFIndex startAt = 0;
11361160

Diff for: Sources/CoreFoundation/CFPlatform.c

+51-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <mach-o/dyld.h>
2323
#endif
2424

25+
#if TARGET_OS_WASI
26+
#include <wasi/libc-environ.h>
27+
#endif
28+
2529
#define _CFEmitInternalDiagnostics 0
2630

2731

@@ -62,11 +66,9 @@ int _CFArgc(void) { return *_NSGetArgc(); }
6266
#endif
6367

6468

65-
#if !TARGET_OS_WASI
6669
CF_PRIVATE Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
6770
return getcwd(path, maxlen) != NULL;
6871
}
69-
#endif
7072

7173
#if TARGET_OS_WIN32
7274
// Returns the path to the CF DLL, which we can then use to find resources like char sets
@@ -103,7 +105,6 @@ CF_PRIVATE const wchar_t *_CFDLLPath(void) {
103105
}
104106
#endif // TARGET_OS_WIN32
105107

106-
#if !TARGET_OS_WASI
107108
static const char *__CFProcessPath = NULL;
108109
static const char *__CFprogname = NULL;
109110

@@ -186,6 +187,31 @@ const char *_CFProcessPath(void) {
186187
__CFprogname = __CFProcessPath;
187188
}
188189
return __CFProcessPath;
190+
#elif TARGET_OS_WASI
191+
__wasi_errno_t err;
192+
size_t argc;
193+
size_t argv_buf_size;
194+
err = __wasi_args_sizes_get(&argc, &argv_buf_size);
195+
if (err != 0) {
196+
__CFProcessPath = "";
197+
__CFprogname = __CFProcessPath;
198+
return __CFProcessPath;
199+
}
200+
char *argv_buf = malloc(argv_buf_size);
201+
char **argv = calloc(argc, sizeof(char *));
202+
err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf);
203+
if (err != 0) {
204+
__CFProcessPath = "";
205+
__CFprogname = __CFProcessPath;
206+
free(argv_buf);
207+
free(argv);
208+
return __CFProcessPath;
209+
}
210+
_CFSetProgramNameFromPath(argv[0]);
211+
free(argv_buf);
212+
free(argv);
213+
return __CFProcessPath;
214+
189215
#else // TARGET_OS_BSD
190216
char *argv0 = NULL;
191217

@@ -248,7 +274,6 @@ const char *_CFProcessPath(void) {
248274
return __CFProcessPath;
249275
#endif
250276
}
251-
#endif // TARGET_OS_WASI
252277

253278
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
254279
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
@@ -273,7 +298,6 @@ Boolean _CFIsMainThread(void) {
273298
}
274299
#endif // TARGET_OS_LINUX
275300

276-
#if !TARGET_OS_WASI
277301
CF_PRIVATE CFStringRef _CFProcessNameString(void) {
278302
static CFStringRef __CFProcessNameString = NULL;
279303
if (!__CFProcessNameString) {
@@ -292,7 +316,6 @@ CF_PRIVATE CFStringRef _CFProcessNameString(void) {
292316
}
293317
return __CFProcessNameString;
294318
}
295-
#endif // !TARGET_OS_WASI
296319

297320
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
298321

@@ -387,16 +410,20 @@ static CFURLRef _CFCopyHomeDirURLForUser(const char *username, bool fallBackToHo
387410

388411
#endif
389412

390-
#if !TARGET_OS_WASI
391413
#define CFMaxHostNameLength 256
392414
#define CFMaxHostNameSize (CFMaxHostNameLength+1)
393415

394416
CF_PRIVATE CFStringRef _CFStringCreateHostName(void) {
417+
#if TARGET_OS_WASI
418+
// WASI doesn't have a concept of a hostname
419+
return CFSTR("");
420+
#else
395421
char myName[CFMaxHostNameSize];
396422

397423
// return @"" instead of nil a la CFUserName() and Ali Ozer
398424
if (0 != gethostname(myName, CFMaxHostNameSize)) return CFSTR("");
399425
return CFStringCreateWithCString(kCFAllocatorSystemDefault, myName, kCFPlatformInterfaceStringEncoding);
426+
#endif
400427
}
401428

402429
/* These are sanitized versions of the above functions. We might want to eliminate the above ones someday.
@@ -433,6 +460,8 @@ CF_EXPORT CFStringRef CFCopyUserName(void) {
433460
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, cname, kCFPlatformInterfaceStringEncoding);
434461
}
435462
}
463+
#elif TARGET_OS_WASI
464+
// WASI does not have user concept
436465
#else
437466
#error "Please add an implementation for CFCopyUserName() that copies the account username"
438467
#endif
@@ -462,6 +491,8 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void) {
462491
GetUserNameExW(NameDisplay, (LPWSTR)wszBuffer, &ulLength);
463492

464493
result = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)wszBuffer, ulLength);
494+
#elif TARGET_OS_WASI
495+
// WASI does not have user concept
465496
#else
466497
#error "Please add an implementation for CFCopyFullUserName() that copies the full (display) user name"
467498
#endif
@@ -528,6 +559,9 @@ CFURLRef CFCopyHomeDirectoryURL(void) {
528559
if (testPath) CFRelease(testPath);
529560

530561
return retVal;
562+
#elif TARGET_OS_WASI
563+
// WASI does not have user concept
564+
return NULL;
531565
#else
532566
#error Dont know how to compute users home directories on this platform
533567
#endif
@@ -659,6 +693,9 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
659693
CFAllocatorDeallocate(kCFAllocatorSystemDefault, pwszUserName);
660694

661695
return url;
696+
#elif TARGET_OS_WASI
697+
// WASI does not have user concept
698+
return NULL;
662699
#else
663700
#error Dont know how to compute users home directories on this platform
664701
#endif
@@ -667,7 +704,6 @@ CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName) {
667704

668705
#undef CFMaxHostNameLength
669706
#undef CFMaxHostNameSize
670-
#endif // !TARGET_OS_WASI
671707

672708
#if TARGET_OS_WIN32
673709
CF_INLINE CFIndex strlen_UniChar(const UniChar* p) {
@@ -1625,9 +1661,11 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) {
16251661
#if DEPLOYMENT_RUNTIME_SWIFT
16261662
#include <fcntl.h>
16271663

1628-
extern void swift_retain(void *);
1664+
extern void *swift_retain(void *);
16291665
extern void swift_release(void *);
16301666

1667+
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
1668+
16311669
#if TARGET_OS_WIN32
16321670
typedef struct _CFThreadSpecificData {
16331671
CFTypeRef value;
@@ -1806,14 +1844,17 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
18061844
#endif
18071845
return -1;
18081846
}
1847+
#endif // SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
18091848

18101849
CF_EXPORT char **_CFEnviron(void) {
18111850
#if TARGET_OS_MAC
18121851
return *_NSGetEnviron();
18131852
#elif TARGET_OS_WIN32
18141853
return _environ;
1854+
#elif TARGET_OS_WASI
1855+
return __wasilibc_get_environ();
18151856
#else
1816-
#if TARGET_OS_BSD || TARGET_OS_WASI
1857+
#if TARGET_OS_BSD
18171858
extern char **environ;
18181859
#endif
18191860
return environ;

Diff for: Sources/CoreFoundation/CFRuntime.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ __kCFRetainEvent = 28,
5252
__kCFReleaseEvent = 29
5353
};
5454

55-
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
55+
#if TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
5656
#include <malloc.h>
5757
#elif TARGET_OS_BSD
5858
#include <stdlib.h> // malloc()
@@ -1162,7 +1162,7 @@ _CFThreadRef _CF_pthread_main_thread_np(void) {
11621162

11631163

11641164

1165-
#if TARGET_OS_LINUX || TARGET_OS_BSD
1165+
#if TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
11661166
static void __CFInitialize(void) __attribute__ ((constructor));
11671167
#endif
11681168
#if TARGET_OS_WIN32
@@ -1194,9 +1194,13 @@ void __CFInitialize(void) {
11941194
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
11951195
GetCurrentProcess(), &_CFMainPThread, 0, FALSE,
11961196
DUPLICATE_SAME_ACCESS);
1197-
#else
1197+
#elif _POSIX_THREADS
11981198
// move this next line up into the #if above after Foundation gets off this symbol. Also: <rdar://problem/39622745> Stop using _CFMainPThread
11991199
_CFMainPThread = pthread_self();
1200+
#elif TARGET_OS_WASI
1201+
_CFMainPThread = kNilPthreadT;
1202+
#else
1203+
#error Dont know how to get the main thread on this platform
12001204
#endif
12011205

12021206
#if TARGET_OS_WIN32
@@ -1407,7 +1411,7 @@ int DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID pReserved ) {
14071411
#endif
14081412

14091413
#if DEPLOYMENT_RUNTIME_SWIFT
1410-
extern void swift_retain(void *);
1414+
extern void *swift_retain(void *);
14111415
#endif
14121416

14131417
// For "tryR==true", a return of NULL means "failed".

Diff for: Sources/CoreFoundation/CFURL.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <stdlib.h>
2323
#include <stdio.h>
2424
#include <string.h>
25-
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
25+
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
2626
#if TARGET_OS_OSX
2727
#include "CFNumberFormatter.h"
2828
#endif
@@ -53,9 +53,7 @@ static CFStringRef WindowsPathToURLPath(CFStringRef path, CFAllocatorRef alloc,
5353
static CFStringRef POSIXPathToURLPath(CFStringRef path, CFAllocatorRef alloc, Boolean isDirectory, Boolean isAbsolute, Boolean *posixAndUrlPathsMatch) CF_RETURNS_RETAINED;
5454
static CFStringRef CreateStringFromFileSystemRepresentationByAddingPercentEscapes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, Boolean isDirectory, Boolean isAbsolute, Boolean windowsPath, Boolean *addedPercentEncoding) CF_RETURNS_RETAINED;
5555
CFStringRef CFURLCreateStringWithFileSystemPath(CFAllocatorRef allocator, CFURLRef anURL, CFURLPathStyle fsType, Boolean resolveAgainstBase) CF_RETURNS_RETAINED;
56-
#if !TARGET_OS_WASI
5756
CF_EXPORT CFURLRef _CFURLCreateCurrentDirectoryURL(CFAllocatorRef allocator) CF_RETURNS_RETAINED;
58-
#endif
5957
#if TARGET_OS_MAC
6058
static Boolean _CFURLHasFileURLScheme(CFURLRef url, Boolean *hasScheme);
6159
#endif
@@ -2189,13 +2187,11 @@ static CFURLRef _CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStrin
21892187
// if fileSystemPath is an absolute path, ignore baseURL (if provided)
21902188
baseURL = NULL;
21912189
}
2192-
#if !TARGET_OS_WASI
21932190
else if ( baseURL == NULL ) {
21942191
// if fileSystemPath is a relative path and no baseURL is provided, use the current working directory
21952192
baseURL = _CFURLCreateCurrentDirectoryURL(allocator);
21962193
releaseBaseURL = true;
21972194
}
2198-
#endif
21992195

22002196
// override isDirectory if the path is to root
22012197
if ( !isDirectory && (len == 1) && (CFStringGetCharacterAtIndex(urlString, 0) == '/') ) {
@@ -2282,7 +2278,7 @@ static CFURLRef _CFURLCreateWithFileSystemRepresentation(CFAllocatorRef allocato
22822278
#endif
22832279
struct __CFURL *result = NULL;
22842280
if (bufLen > 0) {
2285-
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
2281+
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
22862282
Boolean isAbsolute = bufLen && (*buffer == '/');
22872283
Boolean addedPercentEncoding;
22882284
Boolean releaseBaseURL = false;
@@ -2292,14 +2288,12 @@ static CFURLRef _CFURLCreateWithFileSystemRepresentation(CFAllocatorRef allocato
22922288
// if buffer contains an absolute path, ignore baseURL (if provided)
22932289
baseURL = NULL;
22942290
isFileReferencePath = _fileSystemRepresentationHasFileIDPrefix(buffer, bufLen);
2295-
}
2296-
#if !TARGET_OS_WASI
2297-
else if ( baseURL == NULL ) {
2291+
} else if ( baseURL == NULL ) {
22982292
// if buffer contains a relative path and no baseURL is provided, use the current working directory
22992293
baseURL = _CFURLCreateCurrentDirectoryURL(allocator);
23002294
releaseBaseURL = true;
23012295
}
2302-
#endif
2296+
23032297
CFStringRef urlString = CreateStringFromFileSystemRepresentationByAddingPercentEscapes(allocator, buffer, bufLen, isDirectory, isAbsolute, false /*windowsPath*/, &addedPercentEncoding);
23042298
if ( urlString ) {
23052299
// allocate the URL object with the appropriate number of ranges
@@ -4367,7 +4361,6 @@ static CFStringRef _resolveFileSystemPaths(CFStringRef relativePath, CFStringRef
43674361
return _resolvedPath(buf, buf + baseLen + relLen, pathDelimiter, false, true, alloc);
43684362
}
43694363

4370-
#if !TARGET_OS_WASI
43714364
CFURLRef _CFURLCreateCurrentDirectoryURL(CFAllocatorRef allocator) {
43724365
CFURLRef url = NULL;
43734366
// CFMaxPathSize is OK here since we're getting the path from the file system
@@ -4377,7 +4370,6 @@ CFURLRef _CFURLCreateCurrentDirectoryURL(CFAllocatorRef allocator) {
43774370
}
43784371
return url;
43794372
}
4380-
#endif
43814373

43824374
CFURLRef CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle fsType, Boolean isDirectory) {
43834375
CFURLRef result;

0 commit comments

Comments
 (0)