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

552 lines
14 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
import CNIODarwin
#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
// MARK: - system
/// openat(2): Open or create a file for reading or writing
func system_fdopenat(
_ fd: FileDescriptor.RawValue,
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, path, oflag)
}
#endif
return openat(fd, path, oflag)
}
/// openat(2): Open or create a file for reading or writing
func system_fdopenat(
_ fd: FileDescriptor.RawValue,
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, path, oflag, mode)
}
#endif
return openat(fd, path, oflag, mode)
}
/// stat(2): Get file status
func system_stat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return stat(path, &info)
}
/// lstat(2): Get file status
internal func system_lstat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return lstat(path, &info)
}
/// fstat(2): Get file status
internal func system_fstat(
_ fd: FileDescriptor.RawValue,
_ info: inout CInterop.Stat
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd)
}
#endif
return fstat(fd, &info)
}
/// fchmod(2): Change mode of file
internal func system_fchmod(
_ fd: FileDescriptor.RawValue,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, mode)
}
#endif
return fchmod(fd, mode)
}
/// fsync(2): Synchronize modifications to a file to permanent storage
internal func system_fsync(
_ fd: FileDescriptor.RawValue
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd)
}
#endif
return fsync(fd)
}
/// mkdir(2): Make a directory file
internal func system_mkdir(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path, mode)
}
#endif
return mkdir(path, mode)
}
/// symlink(2): Make symolic link to a file
internal func system_symlink(
_ destination: UnsafePointer<CInterop.PlatformChar>,
_ source: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(destination, source)
}
#endif
return symlink(destination, source)
}
/// symlinkat(2): Make symbolic link to a file relative to directory file descriptor
internal func system_symlinkat(
_ destination: UnsafePointer<CInterop.PlatformChar>,
_ dirfd: FileDescriptor.RawValue,
_ source: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(destination, dirfd, source)
}
#endif
return symlinkat(destination, dirfd, source)
}
/// readlink(2): Read value of a symolic link
internal func system_readlink(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ bufferSize: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(path, buffer, bufferSize)
}
#endif
return readlink(path, buffer, bufferSize)
}
/// flistxattr(2): List extended attribute names
internal func system_flistxattr(
_ fd: FileDescriptor.RawValue,
_ namebuf: UnsafeMutablePointer<CChar>?,
_ size: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(fd, namebuf, size)
}
#endif
#if canImport(Darwin)
// The final parameter is 'options'; there is no equivalent on Linux.
return flistxattr(fd, namebuf, size, 0)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
return flistxattr(fd, namebuf, size)
#elseif os(Windows)
fatalError("flistxattr is unavailable on Windows")
#endif
}
/// fgetxattr(2): Get an extended attribute value
internal func system_fgetxattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CInterop.PlatformChar>,
_ value: UnsafeMutableRawPointer?,
_ size: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(fd, name, value, size)
}
#endif
#if canImport(Darwin)
// Penultimate parameter is position which is reserved and should be zero.
// The final parameter is 'options'; there is no equivalent on Linux.
return fgetxattr(fd, name, value, size, 0, 0)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
return fgetxattr(fd, name, value, size)
#elseif os(Windows)
fatalError("fgetxattr is unavailable on Windows")
#endif
}
/// fsetxattr(2): Set an extended attribute value
internal func system_fsetxattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CInterop.PlatformChar>,
_ value: UnsafeRawPointer?,
_ size: Int
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, name, value, size)
}
#endif
// The final parameter is 'options'/'flags' on Darwin/Linux respectively.
#if canImport(Darwin)
// Penultimate parameter is position which is reserved and should be zero.
return fsetxattr(fd, name, value, size, 0, 0)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
return fsetxattr(fd, name, value, size, 0)
#elseif os(Windows)
fatalError("fsetxattr is unavailable on Windows")
#endif
}
/// fremovexattr(2): Remove an extended attribute value
internal func system_fremovexattr(
_ fd: FileDescriptor.RawValue,
_ name: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, name)
}
#endif
#if canImport(Darwin)
// The final parameter is 'options'; there is no equivalent on Linux.
return fremovexattr(fd, name, 0)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
return fremovexattr(fd, name)
#elseif os(Windows)
fatalError("fremovexattr is unavailable on Windows")
#endif
}
/// rename(2): Change the name of a file
internal func system_rename(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new)
}
#endif
return rename(old, new)
}
#if canImport(Darwin)
internal func system_renamex_np(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CUnsignedInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new, flags)
}
#endif
return renamex_np(old, new, flags)
}
/// renameatx_np(2): Rename a file relative to directory file descriptors
internal func system_renameatx_np(
_ oldFD: FileDescriptor.RawValue,
_ old: UnsafePointer<CInterop.PlatformChar>,
_ newFD: FileDescriptor.RawValue,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CUnsignedInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(oldFD, old, newFD, new, flags)
}
#endif
return renameatx_np(oldFD, old, newFD, new, flags)
}
#endif
#if canImport(Glibc) || canImport(Musl) || canImport(Android)
internal func system_renameat2(
_ oldFD: FileDescriptor.RawValue,
_ old: UnsafePointer<CInterop.PlatformChar>,
_ newFD: FileDescriptor.RawValue,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CUnsignedInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(oldFD, old, newFD, new, flags)
}
#endif
return CNIOLinux_renameat2(oldFD, old, newFD, new, flags)
}
#endif
/// link(2): Creates a new link for a file.
#if canImport(Glibc) || canImport(Musl) || canImport(Android)
internal func system_linkat(
_ oldFD: FileDescriptor.RawValue,
_ old: UnsafePointer<CInterop.PlatformChar>,
_ newFD: FileDescriptor.RawValue,
_ new: UnsafePointer<CInterop.PlatformChar>,
_ flags: CInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(oldFD, old, newFD, new, flags)
}
#endif
return linkat(oldFD, old, newFD, new, flags)
}
#endif
/// link(2): Creates a new link for a file.
internal func system_link(
_ old: UnsafePointer<CInterop.PlatformChar>,
_ new: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(old, new)
}
#endif
return link(old, new)
}
/// unlink(2): Remove a directory entry.
internal func system_unlink(
_ path: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return unlink(path)
}
/// unlinkat(2): Remove a directory entry relative to a directory file descriptor
internal func system_unlinkat(
_ fd: FileDescriptor.RawValue,
_ path: UnsafePointer<CInterop.PlatformChar>,
_ flags: CInt
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, path, flags)
}
#endif
return unlinkat(fd, path, flags)
}
#if canImport(Glibc) || canImport(Musl) || canImport(Android)
/// sendfile(2): Transfer data between descriptors
internal func system_sendfile(
_ outFD: CInt,
_ inFD: CInt,
_ offset: off_t,
_ count: Int
) -> Int {
#if ENABLE_MOCKING
if mockingEnabled {
return mockInt(outFD, inFD, offset, count)
}
#endif
var offset = offset
return sendfile(outFD, inFD, &offset, count)
}
#endif
internal func system_futimens(
_ fd: CInt,
_ times: UnsafePointer<timespec>?
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(fd, times)
}
#endif
return futimens(fd, times)
}
// MARK: - libc
/// fdopendir(3): Opens a directory stream for the file descriptor
internal func libc_fdopendir(
_ fd: FileDescriptor.RawValue
) -> CInterop.DirPointer {
fdopendir(fd)!
}
/// readdir(3): Returns a pointer to the next directory entry
internal func libc_readdir(
_ dir: CInterop.DirPointer
) -> UnsafeMutablePointer<CInterop.DirEnt>? {
readdir(dir)
}
/// readdir(3): Closes the directory stream and frees associated structures
internal func libc_closedir(
_ dir: CInterop.DirPointer
) -> CInt {
closedir(dir)
}
/// remove(3): Remove directory entry
internal func libc_remove(
_ path: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(path)
}
#endif
return remove(path)
}
#if canImport(Darwin)
/// fcopyfile(3): Copy a file from one file to another.
internal func libc_fcopyfile(
_ from: CInt,
_ to: CInt,
_ state: copyfile_state_t?,
_ flags: copyfile_flags_t
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(from, to, state, flags)
}
#endif
return fcopyfile(from, to, state, flags)
}
#endif
#if canImport(Darwin)
/// copyfile(3): Copy a file from one file to another.
internal func libc_copyfile(
_ from: UnsafePointer<CInterop.PlatformChar>,
_ to: UnsafePointer<CInterop.PlatformChar>,
_ state: copyfile_state_t?,
_ flags: copyfile_flags_t
) -> CInt {
#if ENABLE_MOCKING
if mockingEnabled {
return mock(from, to, state, flags)
}
#endif
return copyfile(from, to, state, flags)
}
#endif
/// getcwd(3): Get working directory pathname
internal func libc_getcwd(
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ size: Int
) -> UnsafeMutablePointer<CInterop.PlatformChar>? {
getcwd(buffer, size)
}
/// confstr(3)
#if !os(Android)
internal func libc_confstr(
_ name: CInt,
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
_ size: Int
) -> Int {
confstr(name, buffer, size)
}
#endif
/// fts(3)
internal func libc_fts_open(
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
_ options: CInt
) -> UnsafeMutablePointer<CInterop.FTS> {
#if os(Android)
// This branch is a workaround for incorrect nullability annotations in the Android SDK.
// They were added in https://android.googlesource.com/platform/bionic/+/dec8efd72a6ad8b807a15a614ae1519487cfa456,
// and lasted for more than a year: https://android.googlesource.com/platform/bionic/+/da81ec4d1cbd0279014feb60535bf38defcd9346.
CNIOLinux_fts_open(path, options, nil)!
#else
fts_open(path, options, nil)!
#endif
}
/// fts(3)
internal func libc_fts_read(
_ fts: UnsafeMutablePointer<CInterop.FTS>
) -> UnsafeMutablePointer<CInterop.FTSEnt>? {
fts_read(fts)
}
/// fts(3)
internal func libc_fts_close(
_ fts: UnsafeMutablePointer<CInterop.FTS>
) -> CInt {
fts_close(fts)
}
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Android)
/// getuid(2): Get user identification
internal func system_getuid() -> uid_t {
getuid()
}
/// getpwuid_r(3): Get password file entry
internal func libc_getpwuid_r(
_ uid: uid_t,
_ pwd: UnsafeMutablePointer<passwd>,
_ buffer: UnsafeMutablePointer<CChar>,
_ bufferSize: Int,
_ result: UnsafeMutablePointer<UnsafeMutablePointer<passwd>?>
) -> CInt {
getpwuid_r(uid, pwd, buffer, bufferSize, result)
}
#endif