mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
## Motivation
This continues the incremental Windows port of SwiftNIO. `NIOPosix`
already builds on Windows (on Winsock2 with a POSIX-shim mapping); this
PR gets the **L4 NIOFileSystem family** to **compile** on Windows:
- `_NIOFileSystem`
- `NIOFS`
- `NIOFileSystem`
- `_NIOFileSystemFoundationCompat`
- `NIOFSFoundationCompat`
With these compiling, **all non-test targets now build on Windows**, so
the Windows CI job is simplified to a bare `swift build` (which builds
the whole non-test graph) rather than an explicit per-target list.
## Scope — compile-only
**This PR gets the targets compiling; it does not yet make them
functional.** The POSIX file-system syscalls that don't exist (or aren't
exposed by swift-system) on Windows are provided as `fatalError("…
unavailable on Windows")` **stubs** with the correct signatures, so the
library type-checks and links. Real Win32 implementations, and bringing
the test targets online in CI to validate them, are deliberately left to
follow-up PRs.
The intent is to land the smallest reviewable slice first and **get
feedback on the overall shape and organization** before building on it.
## What this does
All changes are behind `#if os(Windows)` / `#if defined(_WIN32)`, with
**zero behavior change on Darwin/Linux/BSD**:
- **`Sources/*/Internal/System Calls/SystemPackage+Windows.swift`**
(new, one per tree): a module-scope compatibility shim supplying the
pieces the rest of the target expects on Windows —
- the swift-system-gap extensions swift-system doesn't provide on
Windows
(`FileDescriptor.OpenOptions.{noFollow,closeOnExec,nonBlocking,directory}`,
`Errno.noData`);
- `CInterop.Stat`/`DirEnt`/`FTS`/`FTSEnt` Windows stand-ins and the
`S_IF*`/`DT_*`/`FTS_*`/`UTIME_*` constants the call sites reference;
- `fatalError` stubs for the bare-libc syscalls (`openat`,
`stat`/`lstat`/`fstat`, `readdir`/`fdopendir`/`closedir`, `fts_*`,
`futimens`, `rename`, `symlink`, xattr-adjacent helpers, `getcwd`,
pthread TLS keys, …), matching the exact signatures the existing
`system_*` / `libc_*` wrappers call, so those wrappers and their
non-Windows call sites need no changes.
- Small `#elseif os(Windows)` branches in the existing platform switches
(`FileInfo`, `FileType`, `Errno`, `Mocking`, `Syscall(s)`,
`SystemFileHandle`, `FileSystem`) — including `fatalError` stubs for
`_copyRegularFile` / `_copySymbolicLink` /
file-materialization-via-rename.
- A tiny `CNIOWindows` addition: `errno` accessors (`CNIOWindows_errno`
/ `CNIOWindows_set_errno`, typed `errno_t` via
`_get_errno`/`_set_errno`) so the Swift errno indirection can bridge the
CRT `errno` macro.
- `CNIOWindows` added to the `NIOFS` / `_NIOFileSystem` target
dependencies in `Package.swift`.
`NIOFS` and `_NIOFileSystem` are lockstep near-duplicates, so every
Windows change is applied identically to both.
## Verification
Verified by source review and by confirming a clean `swift build` on
`aarch64-unknown-windows-msvc`. Since the syscalls are stubbed, there is
no runtime behavior to test yet.
## What I'm looking for
Feedback on the **high-level organization and shape** of the NIOFS
Windows shim before the real implementations follow:
1. Is the layering right — a single module-scope
`SystemPackage+Windows.swift` gap-shim, with the upstream `system_*` /
`libc_*` call sites left untouched?
2. When the implementations land, is emulating the POSIX seams (a
`fts(3)`-shaped tree walk, POSIX directory streams) the right approach,
or should Windows fork at a higher layer (e.g. a Windows-specific
enumerator in `DirectoryEntries`) rather than reimplementing
`fts`/`readdir`?
3. Anything about the swift-system-gap extensions or the `CInterop.*`
Windows stand-ins that should be shaped differently.
## Next steps
Follow-up PRs will replace the stubs with real Win32 implementations,
then bring the NIOFileSystem **test targets** building and running in CI
— where these shims get real validation and we start gaining deeper
confidence in Windows support across the stack.
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
398 lines
11 KiB
Swift
398 lines
11 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2023 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This source file is part of the Swift System open source project//
|
|
// Copyright (c) 2020 Apple Inc. and the Swift System project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
|
|
import SystemPackage
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
@preconcurrency import Glibc
|
|
import CNIOLinux
|
|
#elseif canImport(Musl)
|
|
@preconcurrency import Musl
|
|
import CNIOLinux
|
|
#elseif canImport(Android)
|
|
@preconcurrency import Android
|
|
import CNIOLinux
|
|
#endif
|
|
|
|
// Syscall mocking support.
|
|
//
|
|
// NOTE: This is currently the bare minimum needed for System's testing purposes, though we do
|
|
// eventually want to expose some solution to users.
|
|
//
|
|
// Mocking is contextual, accessible through MockingDriver.withMockingEnabled. Mocking
|
|
// state, including whether it is enabled, is stored in thread-local storage. Mocking is only
|
|
// enabled in testing builds of System currently, to minimize runtime overhead of release builds.
|
|
//
|
|
|
|
#if ENABLE_MOCKING
|
|
@_spi(Testing)
|
|
public struct Trace {
|
|
@_spi(Testing)
|
|
public struct Entry {
|
|
@_spi(Testing)
|
|
public var name: String
|
|
@_spi(Testing)
|
|
public var arguments: [AnyHashable]
|
|
|
|
@_spi(Testing)
|
|
public init(name: String, _ arguments: [AnyHashable]) {
|
|
self.name = name
|
|
self.arguments = arguments
|
|
}
|
|
}
|
|
|
|
private var entries: [Entry] = []
|
|
private var firstEntry: Int = 0
|
|
|
|
@_spi(Testing)
|
|
public var isEmpty: Bool { firstEntry >= entries.count }
|
|
|
|
@_spi(Testing)
|
|
public mutating func dequeue() -> Entry? {
|
|
guard !self.isEmpty else { return nil }
|
|
defer { firstEntry += 1 }
|
|
return entries[firstEntry]
|
|
}
|
|
|
|
fileprivate mutating func add(_ e: Entry) {
|
|
entries.append(e)
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension Trace: Sendable {}
|
|
|
|
@available(*, unavailable)
|
|
extension Trace.Entry: Sendable {}
|
|
|
|
@_spi(Testing)
|
|
public enum ForceErrno: Equatable, Sendable {
|
|
case none
|
|
case always(errno: CInt)
|
|
|
|
case counted(errno: CInt, count: Int)
|
|
}
|
|
|
|
// Provide access to the driver, context, and trace stack of mocking
|
|
@_spi(Testing)
|
|
public final class MockingDriver {
|
|
// Record syscalls and their arguments
|
|
@_spi(Testing)
|
|
public var trace = Trace()
|
|
|
|
// Mock errors inside syscalls
|
|
@_spi(Testing)
|
|
public var forceErrno = ForceErrno.none
|
|
|
|
// Whether we should pretend to be Windows for syntactic operations
|
|
// inside FilePath
|
|
fileprivate var forceWindowsSyntaxForPaths = false
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension MockingDriver: Sendable {}
|
|
|
|
private let driverKey: _PlatformTLSKey = { makeTLSKey() }()
|
|
|
|
internal var currentMockingDriver: MockingDriver? {
|
|
#if !ENABLE_MOCKING
|
|
fatalError("Contextual mocking in non-mocking build")
|
|
#endif
|
|
guard let rawPtr = getTLS(driverKey) else { return nil }
|
|
|
|
return Unmanaged<MockingDriver>.fromOpaque(rawPtr).takeUnretainedValue()
|
|
}
|
|
|
|
extension MockingDriver {
|
|
/// Enables mocking for the duration of `f` with a clean trace queue
|
|
/// Restores prior mocking status and trace queue after execution
|
|
@_spi(Testing)
|
|
public static func withMockingEnabled(
|
|
_ f: (MockingDriver) throws -> Void
|
|
) rethrows {
|
|
let priorMocking = currentMockingDriver
|
|
let driver = MockingDriver()
|
|
|
|
defer {
|
|
if let object = priorMocking {
|
|
setTLS(driverKey, Unmanaged.passUnretained(object).toOpaque())
|
|
} else {
|
|
setTLS(driverKey, nil)
|
|
}
|
|
_fixLifetime(driver)
|
|
}
|
|
|
|
setTLS(driverKey, Unmanaged.passUnretained(driver).toOpaque())
|
|
return try f(driver)
|
|
}
|
|
}
|
|
|
|
// Check TLS for mocking
|
|
@inline(never)
|
|
private var contextualMockingEnabled: Bool {
|
|
currentMockingDriver != nil
|
|
}
|
|
|
|
extension MockingDriver {
|
|
@_spi(Testing)
|
|
public static var enabled: Bool { mockingEnabled }
|
|
|
|
@_spi(Testing)
|
|
public static var forceWindowsPaths: Bool {
|
|
currentMockingDriver?.forceWindowsSyntaxForPaths ?? false
|
|
}
|
|
}
|
|
|
|
#endif // ENABLE_MOCKING
|
|
|
|
@inline(__always)
|
|
internal var mockingEnabled: Bool {
|
|
// Fast constant-foldable check for release builds
|
|
#if ENABLE_MOCKING
|
|
return contextualMockingEnabled
|
|
#else
|
|
return false
|
|
#endif
|
|
}
|
|
|
|
@inline(__always)
|
|
internal var forceWindowsPaths: Bool {
|
|
#if !ENABLE_MOCKING
|
|
return false
|
|
#else
|
|
return MockingDriver.forceWindowsPaths
|
|
#endif
|
|
}
|
|
|
|
#if ENABLE_MOCKING
|
|
// Strip the mock_system prefix and the arg list suffix
|
|
private func originalSyscallName(_ function: String) -> String {
|
|
// `function` must be of format `system_<name>(<parameters>)`
|
|
for `prefix` in ["system_", "libc_"] {
|
|
if function.starts(with: `prefix`) {
|
|
return String(function.dropFirst(`prefix`.count).prefix { $0 != "(" })
|
|
}
|
|
}
|
|
preconditionFailure("\(function) must start with 'system_' or 'libc_'")
|
|
}
|
|
|
|
private func mockImpl(syscall name: String, args: [AnyHashable]) -> CInt {
|
|
precondition(mockingEnabled)
|
|
let origName = originalSyscallName(name)
|
|
guard let driver = currentMockingDriver else {
|
|
fatalError("Mocking requested from non-mocking context")
|
|
}
|
|
|
|
driver.trace.add(Trace.Entry(name: origName, args))
|
|
|
|
switch driver.forceErrno {
|
|
case .none: break
|
|
case .always(let e):
|
|
system_errno = e
|
|
return -1
|
|
case .counted(let e, let count):
|
|
assert(count >= 1)
|
|
system_errno = e
|
|
driver.forceErrno = count > 1 ? .counted(errno: e, count: count - 1) : .none
|
|
return -1
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
private func reinterpret(_ args: [AnyHashable?]) -> [AnyHashable] {
|
|
args.map { arg in
|
|
switch arg {
|
|
case let charPointer as UnsafePointer<CInterop.PlatformChar>:
|
|
return String(_errorCorrectingPlatformString: charPointer)
|
|
case is UnsafeMutablePointer<CInterop.PlatformChar>:
|
|
return "<buffer>"
|
|
case is UnsafeMutableRawPointer:
|
|
return "<buffer>"
|
|
case is UnsafeRawPointer:
|
|
return "<buffer>"
|
|
case .none:
|
|
return "nil"
|
|
case let .some(arg):
|
|
return arg
|
|
}
|
|
}
|
|
}
|
|
|
|
func mock(
|
|
syscall name: String = #function,
|
|
_ args: AnyHashable?...
|
|
) -> CInt {
|
|
mockImpl(syscall: name, args: reinterpret(args))
|
|
}
|
|
|
|
func mockInt(
|
|
syscall name: String = #function,
|
|
_ args: AnyHashable?...
|
|
) -> Int {
|
|
Int(mockImpl(syscall: name, args: reinterpret(args)))
|
|
}
|
|
|
|
#endif // ENABLE_MOCKING
|
|
|
|
// Force paths to be treated as Windows syntactically if `enabled` is
|
|
// true.
|
|
@_spi(Testing)
|
|
public func _withWindowsPaths(enabled: Bool, _ body: () -> Void) {
|
|
#if ENABLE_MOCKING
|
|
guard enabled else {
|
|
body()
|
|
return
|
|
}
|
|
MockingDriver.withMockingEnabled { driver in
|
|
driver.forceWindowsSyntaxForPaths = true
|
|
body()
|
|
}
|
|
#else
|
|
body()
|
|
#endif
|
|
}
|
|
|
|
// Internal wrappers and typedefs which help reduce #if littering in System's
|
|
// code base.
|
|
|
|
// TODO: Should CSystem just include all the header files we need?
|
|
|
|
internal typealias _COffT = off_t
|
|
|
|
// MARK: syscalls and variables
|
|
|
|
#if canImport(Darwin)
|
|
internal var system_errno: CInt {
|
|
get { Darwin.errno }
|
|
set { Darwin.errno = newValue }
|
|
}
|
|
#elseif canImport(Glibc)
|
|
internal var system_errno: CInt {
|
|
get { Glibc.errno }
|
|
set { Glibc.errno = newValue }
|
|
}
|
|
#elseif canImport(Musl)
|
|
internal var system_errno: CInt {
|
|
get { Musl.errno }
|
|
set { Musl.errno = newValue }
|
|
}
|
|
#elseif canImport(Android)
|
|
internal var system_errno: CInt {
|
|
get { Android.errno }
|
|
set { Android.errno = newValue }
|
|
}
|
|
#elseif os(Windows)
|
|
internal var system_errno: CInt {
|
|
get { _nio_fs_errno }
|
|
set { _nio_fs_errno = newValue }
|
|
}
|
|
#endif
|
|
|
|
// MARK: C stdlib decls
|
|
|
|
// Convention: `system_foo` is system's wrapper for `foo`.
|
|
|
|
internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
|
|
strerror(__errnum)
|
|
}
|
|
|
|
internal func system_strlen(_ s: UnsafePointer<CChar>) -> Int {
|
|
strlen(s)
|
|
}
|
|
internal func system_strlen(_ s: UnsafeMutablePointer<CChar>) -> Int {
|
|
strlen(s)
|
|
}
|
|
|
|
// Convention: `system_platform_foo` is a
|
|
// platform-representation-abstracted wrapper around `foo`-like functionality.
|
|
// Type and layout differences such as the `char` vs `wchar` are abstracted.
|
|
//
|
|
|
|
// strlen for the platform string
|
|
internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
|
|
strlen(s)
|
|
}
|
|
|
|
// memset for raw buffers
|
|
// FIXME: Do we really not have something like this in the stdlib already?
|
|
internal func system_memset(
|
|
_ buffer: UnsafeMutableRawBufferPointer,
|
|
to byte: UInt8
|
|
) {
|
|
guard buffer.count > 0 else { return }
|
|
memset(buffer.baseAddress!, CInt(byte), buffer.count)
|
|
}
|
|
|
|
// Interop between String and platfrom string
|
|
extension String {
|
|
internal func _withPlatformString<Result>(
|
|
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
|
) rethrows -> Result {
|
|
// Need to #if because CChar may be signed
|
|
#if os(Windows)
|
|
return try self.withCString(encodedAs: UTF16.self, body)
|
|
#else
|
|
return try withCString(body)
|
|
#endif
|
|
}
|
|
|
|
internal init?(_platformString platformString: UnsafePointer<CInterop.PlatformChar>) {
|
|
// Need to #if because CChar may be signed
|
|
#if os(Windows)
|
|
self.init(decodingCString: platformString, as: UTF16.self)
|
|
#else
|
|
self.init(validatingCString: platformString)
|
|
#endif
|
|
}
|
|
|
|
internal init(
|
|
_errorCorrectingPlatformString platformString: UnsafePointer<CInterop.PlatformChar>
|
|
) {
|
|
// Need to #if because CChar may be signed
|
|
#if os(Windows)
|
|
self.init(decodingCString: platformString, as: UTF16.self)
|
|
#else
|
|
self.init(cString: platformString)
|
|
#endif
|
|
}
|
|
}
|
|
|
|
internal typealias _PlatformTLSKey = pthread_key_t
|
|
|
|
internal func makeTLSKey() -> _PlatformTLSKey {
|
|
var raw = pthread_key_t()
|
|
guard 0 == pthread_key_create(&raw, nil) else {
|
|
fatalError("Unable to create key")
|
|
}
|
|
return raw
|
|
}
|
|
|
|
internal func setTLS(_ key: _PlatformTLSKey, _ p: UnsafeMutableRawPointer?) {
|
|
guard 0 == pthread_setspecific(key, p) else {
|
|
fatalError("Unable to set TLS")
|
|
}
|
|
}
|
|
|
|
internal func getTLS(_ key: _PlatformTLSKey) -> UnsafeMutableRawPointer? {
|
|
pthread_getspecific(key)
|
|
}
|