Skip to content

Commit 8f114ba

Browse files
Add os(WASI) platform condition to FileHandle methods
Most of the file handle methods should be the same on WASI as on Linux. The only exception is file locking, which is not yet supported on WASI.
1 parent af33a1e commit 8f114ba

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Sources/Testing/Support/FileHandle.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct FileHandle: ~Copyable, Sendable {
141141
/// descriptor, `nil` is passed to `body`.
142142
borrowing func withUnsafePOSIXFileDescriptor<R>(_ body: (CInt?) throws -> R) rethrows -> R {
143143
try withUnsafeCFILEHandle { handle in
144-
#if SWT_TARGET_OS_APPLE || os(Linux)
144+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
145145
let fd = fileno(handle)
146146
#elseif os(Windows)
147147
let fd = _fileno(handle)
@@ -210,6 +210,8 @@ struct FileHandle: ~Copyable, Sendable {
210210
defer {
211211
_unlock_file(handle)
212212
}
213+
#elseif os(WASI)
214+
// No file locking on WASI yet.
213215
#else
214216
#warning("Platform-specific implementation missing: cannot lock a file handle")
215217
#endif
@@ -233,7 +235,7 @@ extension FileHandle {
233235
// If possible, reserve enough space in the resulting buffer to contain
234236
// the contents of the file being read.
235237
var size: Int?
236-
#if SWT_TARGET_OS_APPLE || os(Linux)
238+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
237239
withUnsafePOSIXFileDescriptor { fd in
238240
var s = stat()
239241
if let fd, 0 == fstat(fd, &s) {
@@ -371,7 +373,7 @@ extension FileHandle {
371373
extension FileHandle {
372374
/// Is this file handle a TTY or PTY?
373375
var isTTY: Bool {
374-
#if SWT_TARGET_OS_APPLE || os(Linux)
376+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
375377
// If stderr is a TTY and TERM is set, that's good enough for us.
376378
withUnsafePOSIXFileDescriptor { fd in
377379
if let fd, 0 != isatty(fd), let term = Environment.variable(named: "TERM"), !term.isEmpty {
@@ -397,7 +399,7 @@ extension FileHandle {
397399

398400
/// Is this file handle a pipe or FIFO?
399401
var isPipe: Bool {
400-
#if SWT_TARGET_OS_APPLE || os(Linux)
402+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
401403
withUnsafePOSIXFileDescriptor { fd in
402404
guard let fd else {
403405
return false

0 commit comments

Comments
 (0)