mirror of
https://github.com/apple/swift-nio
synced 2026-11-23 04:34:23 +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>
172 lines
4.2 KiB
Swift
172 lines
4.2 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
|
|
|
|
/// The type of a file system object.
|
|
public struct FileType: Hashable, Sendable {
|
|
internal enum Wrapped: Hashable, Sendable, CaseIterable {
|
|
case regular
|
|
case block
|
|
case character
|
|
case fifo
|
|
case directory
|
|
case symlink
|
|
case socket
|
|
case whiteout
|
|
case unknown
|
|
}
|
|
|
|
internal let wrapped: Wrapped
|
|
private init(_ wrapped: Wrapped) {
|
|
self.wrapped = wrapped
|
|
}
|
|
}
|
|
|
|
extension FileType {
|
|
/// Regular file.
|
|
public static var regular: Self { Self(.regular) }
|
|
|
|
/// Directory.
|
|
public static var directory: Self { Self(.directory) }
|
|
|
|
/// Symbolic link.
|
|
public static var symlink: Self { Self(.symlink) }
|
|
|
|
/// Hardware block device.
|
|
public static var block: Self { Self(.block) }
|
|
|
|
/// Hardware character device.
|
|
public static var character: Self { Self(.character) }
|
|
|
|
/// FIFO (or named pipe).
|
|
public static var fifo: Self { Self(.fifo) }
|
|
|
|
/// Socket.
|
|
public static var socket: Self { Self(.socket) }
|
|
|
|
/// Whiteout file.
|
|
public static var whiteout: Self { Self(.whiteout) }
|
|
|
|
/// A file of unknown type.
|
|
public static var unknown: Self { Self(.unknown) }
|
|
}
|
|
|
|
extension FileType: CustomStringConvertible {
|
|
public var description: String {
|
|
switch self.wrapped {
|
|
case .regular:
|
|
return "regular"
|
|
case .block:
|
|
return "block"
|
|
case .character:
|
|
return "character"
|
|
case .fifo:
|
|
return "fifo"
|
|
case .directory:
|
|
return "directory"
|
|
case .symlink:
|
|
return "symlink"
|
|
case .socket:
|
|
return "socket"
|
|
case .whiteout:
|
|
return "whiteout"
|
|
case .unknown:
|
|
return "unknown"
|
|
}
|
|
}
|
|
}
|
|
|
|
extension FileType: CaseIterable {
|
|
public static var allCases: [FileType] {
|
|
Self.Wrapped.allCases.map { FileType($0) }
|
|
}
|
|
}
|
|
|
|
extension FileType {
|
|
/// Initializes a file type from a `CInterop.Mode`.
|
|
///
|
|
/// Note: an appropriate mask is applied to `mode`.
|
|
public init(platformSpecificMode: CInterop.Mode) {
|
|
// See: `man 2 stat`
|
|
switch platformSpecificMode & S_IFMT {
|
|
case S_IFIFO:
|
|
self = .fifo
|
|
case S_IFCHR:
|
|
self = .character
|
|
case S_IFDIR:
|
|
self = .directory
|
|
case S_IFBLK:
|
|
self = .block
|
|
case S_IFREG:
|
|
self = .regular
|
|
case S_IFLNK:
|
|
self = .symlink
|
|
case S_IFSOCK:
|
|
self = .socket
|
|
#if canImport(Darwin)
|
|
case S_IFWHT:
|
|
self = .whiteout
|
|
#endif
|
|
default:
|
|
self = .unknown
|
|
}
|
|
}
|
|
|
|
/// Initializes a file type from the `d_type` from `dirent`.
|
|
@_spi(Testing)
|
|
public init(direntType: UInt8) {
|
|
#if canImport(Darwin) || canImport(Musl) || os(Android) || os(Windows)
|
|
let value = Int32(direntType)
|
|
#elseif canImport(Glibc)
|
|
let value = Int(direntType)
|
|
#endif
|
|
|
|
switch value {
|
|
case DT_FIFO:
|
|
self = .fifo
|
|
case DT_CHR:
|
|
self = .character
|
|
case DT_DIR:
|
|
self = .directory
|
|
case DT_BLK:
|
|
self = .block
|
|
case DT_REG:
|
|
self = .regular
|
|
case DT_LNK:
|
|
self = .symlink
|
|
case DT_SOCK:
|
|
self = .socket
|
|
#if canImport(Darwin)
|
|
case DT_WHT:
|
|
self = .whiteout
|
|
#endif
|
|
case DT_UNKNOWN:
|
|
self = .unknown
|
|
default:
|
|
self = .unknown
|
|
}
|
|
}
|
|
}
|