Files
Jake PetroulesandGeorge Barnett 590dd7b4bc [Windows] Compile the NIOFileSystem family (stubbed); all non-test targets now build (#3649)
## 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>
2026-07-17 07:53:12 +00:00

155 lines
4.3 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
//
//===----------------------------------------------------------------------===//
import SystemPackage
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
@preconcurrency import Musl
#elseif canImport(Android)
@preconcurrency import Android
#endif
extension Errno {
@_spi(Testing)
public static var _current: Errno {
get {
#if canImport(Darwin)
return Errno(rawValue: Darwin.errno)
#elseif canImport(Glibc)
return Errno(rawValue: Glibc.errno)
#elseif canImport(Musl)
return Errno(rawValue: Musl.errno)
#elseif canImport(Android)
return Errno(rawValue: Android.errno)
#elseif os(Windows)
return Errno(rawValue: _nio_fs_errno)
#endif
}
set {
#if canImport(Darwin)
Darwin.errno = newValue.rawValue
#elseif canImport(Glibc)
Glibc.errno = newValue.rawValue
#elseif canImport(Musl)
Musl.errno = newValue.rawValue
#elseif canImport(Android)
Android.errno = newValue.rawValue
#elseif os(Windows)
_nio_fs_errno = newValue.rawValue
#endif
}
}
fileprivate static func clear() {
#if canImport(Darwin)
Darwin.errno = 0
#elseif canImport(Glibc)
Glibc.errno = 0
#elseif canImport(Musl)
Musl.errno = 0
#elseif canImport(Android)
Android.errno = 0
#elseif os(Windows)
_nio_fs_errno = 0
#endif
}
}
/// Returns a `Result` representing the value returned from the given closure
/// or an `Errno` if that value was -1.
///
/// If desired this function can call the closure in a loop until it does not
/// result in `Errno` being `.interrupted`.
@_spi(Testing)
public func valueOrErrno<I: FixedWidthInteger>(
retryOnInterrupt: Bool = true,
_ fn: () -> I
) -> Result<I, Errno> {
while true {
Errno.clear()
let result = fn()
if result == -1 {
let errno = Errno._current
if errno == .interrupted, retryOnInterrupt {
continue
} else {
return .failure(errno)
}
} else {
return .success(result)
}
}
}
/// As `valueOrErrno` but discards the success value.
@_spi(Testing)
public func nothingOrErrno<I: FixedWidthInteger>(
retryOnInterrupt: Bool = true,
_ fn: () -> I
) -> Result<Void, Errno> {
valueOrErrno(retryOnInterrupt: retryOnInterrupt, fn).map { _ in }
}
/// Returns a `Result` representing the value returned from the given closure
/// or an `Errno` if that value was `nil`.
///
/// If desired this function can call the closure in a loop until it does not
/// result in `Errno` being `.interrupted`. `Errno` is only checked if the
/// closure returns `nil`.
@_spi(Testing)
public func optionalValueOrErrno<R>(
retryOnInterrupt: Bool = true,
_ fn: () -> R?
) -> Result<R?, Errno> {
while true {
Errno.clear()
if let result = fn() {
return .success(result)
} else {
let errno = Errno._current
if errno == .interrupted, retryOnInterrupt {
continue
} else if errno.rawValue == 0 {
return .success(nil)
} else {
return .failure(errno)
}
}
}
}
/// As `valueOrErrno` but unconditionally checks the current `Errno`.
@_spi(Testing)
public func valueOrErrno<R>(
retryOnInterrupt: Bool = true,
_ fn: () -> R
) -> Result<R, Errno> {
while true {
Errno.clear()
let value = fn()
let errno = Errno._current
if errno.rawValue == 0 {
return .success(value)
} else if errno == .interrupted, retryOnInterrupt {
continue
} else {
return .failure(errno)
}
}
}