Skip to content

Commit 3048047

Browse files
[wasm] Port use of dirent for wasi-libc
wasi-libc defines `d_name` as "flexible array member" which is not supported by ClangImporter yet. This patch replaces the use of `d_name` with a helper function `_direntName` to work around the issue.
1 parent b5988f5 commit 3048047

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ static inline int _direntNameLength(struct dirent *entry) {
576576
#endif
577577
}
578578

579+
static inline char *_direntName(struct dirent *entry) {
580+
return entry->d_name;
581+
}
582+
579583
// major() and minor() might be implemented as macros or functions.
580584
static inline unsigned int _dev_major(dev_t rdev) {
581585
return major(rdev);

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,8 @@ extension FileManager {
411411
errno = 0
412412
while let entry = readdir(dir) {
413413
let length = Int(_direntNameLength(entry))
414-
let entryName = withUnsafePointer(to: entry.pointee.d_name) { (ptr) -> String in
415-
let namePtr = UnsafeRawPointer(ptr).assumingMemoryBound(to: CChar.self)
416-
return string(withFileSystemRepresentation: namePtr, length: length)
417-
}
414+
let namePtr = UnsafeRawPointer(_direntName(entry)).assumingMemoryBound(to: CChar.self)
415+
let entryName = string(withFileSystemRepresentation: namePtr, length: length)
418416
if entryName != "." && entryName != ".." {
419417
let entryType = Int32(entry.pointee.d_type)
420418
try closure(entryName, entryType)

0 commit comments

Comments
 (0)