Files
swift-nio/Sources/NIOPosix/Windows.swift
scottmarchant 5bf0267df4 build: Elide NIOPosix for WASI platforms only (#3485)
### Motivation:

Packages that consume NIOPosix should be able to compile to WASI
platforms without special configurations. This change elides the
NIOPosix source from WASI platforms to simplify configuration.

Without this change:

```swift
dependencies: [
    // Without this PR, downstream packages must maintain exhaustive platform list to exclude `.wasi`:
    .product(
        name: "NIOPosix",
        package: "swift-nio",
        condition: .when(platforms: [
            .macOS,
            .macCatalyst,
            .iOS,
            .tvOS,
            .watchOS,
            .visionOS,
            .driverKit,
            .linux,
            .windows,
            .android,
            .openbsd,
            // .wasi // <-- Need to exclude this, because there is no exclusion list api for SPM conditionals
        ])
    ),
]
```

With this change:

```swift
dependencies: [
    // Without this PR, downstream packages can consume NIOPosix simply:
    .product(name: "NIOPosix", package: "swift-nio"),
]
```

### Modifications:

- Add compiler directives (`#if !os(WASI)`) to all source files in
NIOPosix

### Result:

Downstream packages can compile to wasm without manually excluding the
NIOPosix dependency.

### Testing performed

- Verified `swift build --swift-sdk wasm32-unknown-wasip1 --target
NIOPosix` compiles, which demonstrates proper elision of source files in
NIOPosix that aren't wasm-ready.
- Confirmed GitHub [checks
pass](https://github.com/PassiveLogic/swift-nio/actions/runs/21151341738).
2026-01-23 16:39:32 +00:00

139 lines
4.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if !os(WASI)
#if os(Windows)
import NIOCore
import WinSDK
import CNIOWindows
typealias ssize_t = SSIZE_T
let missingPipeSupportWindows = "Unimplemented: NIOPosix does not support PipeChannel on Windows"
// overwrite the windows write method, as the one without underscore is deprecated.
// also we can use this to downcast the count Int to UInt32
func write(_ fd: Int32, _ ptr: UnsafeRawPointer?, _ count: Int) -> Int32 {
_write(fd, ptr, UInt32(clamping: count))
}
var errno: Int32 {
CNIOWindows_errno()
}
extension NIOCore.Windows {
/// Call this to get a string representation from an error code that was returned from `GetLastError`.
static func makeErrorMessageFromCode(_ errorCode: DWORD) -> String? {
var errorMsg = UnsafeMutablePointer<CHAR>?.none
CNIOWindows_FormatGetLastError(errorCode, &errorMsg)
if let errorMsg {
let result = String(cString: errorMsg)
LocalFree(errorMsg)
return result
} else {
// we could check GetLastError here again. But that feels quite recursive.
return nil
}
}
static func recv(
socket: SOCKET,
pointer: UnsafeMutableRawPointer,
size: Int32,
flags: Int32
) throws -> IOResult<Int> {
let result = WinSDK.recv(socket, pointer.assumingMemoryBound(to: CChar.self), size, flags)
if result == WinSDK.SOCKET_ERROR {
throw IOError(winsock: WSAGetLastError(), reason: "accept")
}
return .processed(Int(result))
}
static func pread(
descriptor: CInt,
pointer: UnsafeMutableRawPointer,
size: size_t,
offset: off_t
) throws -> IOResult<ssize_t> {
var overlapped = OVERLAPPED()
// off_t is Int32 anyway. Therefore high is always zero.
precondition(off_t.self == Int32.self)
overlapped.OffsetHigh = 0
overlapped.Offset = UInt32(offset)
let file = HANDLE(bitPattern: _get_osfhandle(descriptor))
SetLastError(0)
var readBytes: DWORD = 0
let clampedSize = UInt32(clamping: size)
let rf = ReadFile(file, pointer, clampedSize, &readBytes, &overlapped)
let lastError = GetLastError()
if rf == false && lastError != ERROR_HANDLE_EOF {
throw IOError(errnoCode: Self.windowsErrorToPosixErrno(lastError), reason: "pread")
}
return .processed(Int64(readBytes))
}
static func pwrite(
descriptor: CInt,
pointer: UnsafeRawPointer,
size: size_t,
offset: off_t
) throws -> IOResult<ssize_t> {
var overlapped = OVERLAPPED()
// off_t is Int32 anyway. Therefore high is always zero.
precondition(off_t.self == Int32.self)
overlapped.OffsetHigh = 0
overlapped.Offset = UInt32(offset)
let file = HANDLE(bitPattern: _get_osfhandle(descriptor))
SetLastError(0)
var writtenBytes: DWORD = 0
let clampedSize = UInt32(clamping: size)
if !WriteFile(file, pointer, clampedSize, &writtenBytes, &overlapped) {
throw IOError(errnoCode: Self.windowsErrorToPosixErrno(GetLastError()), reason: "pwrite")
}
return .processed(Int64(writtenBytes))
}
private static func windowsErrorToPosixErrno(_ dwError: DWORD) -> Int32 {
switch Int32(dwError) {
case ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND:
return ENOENT
case ERROR_TOO_MANY_OPEN_FILES:
return EMFILE
case ERROR_ACCESS_DENIED:
return EACCES
case ERROR_INVALID_HANDLE:
return EBADF
case ERROR_NOT_ENOUGH_MEMORY, ERROR_OUTOFMEMORY:
return ENOMEM
case ERROR_NOT_READY, ERROR_CRC:
return EIO
case ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION:
return EBUSY
case ERROR_HANDLE_EOF:
return 0
case ERROR_BROKEN_PIPE:
return EPIPE
default:
return EINVAL
}
}
}
#endif
#endif // !os(WASI)