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

333 lines
12 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 NIOCore
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(Bionic)
@preconcurrency import Bionic
import CNIOLinux
#elseif os(Windows)
// `CInterop.Stat` is `BY_HANDLE_FILE_INFORMATION`; its initializer is only
// visible where the defining module is imported.
import CNIOWindows
#endif
extension FileDescriptor {
/// Opens or creates a file for reading or writing.
///
/// The corresponding C function is `fdopenat`.
///
/// - Parameters:
/// - path: The location of the file to open. If the path is relative then the file is opened
/// relative to the descriptor.
/// - mode: The read and write access to use.
/// - options: The behavior for opening the file.
/// - permissions: The file permissions to use for created files.
/// - retryOnInterrupt: Whether to retry the operation if it throws `Errno.interrupted`. The
/// default is true. Pass false to try only once and throw an error upon interruption.
/// - Returns: A file descriptor for the open file.
@_spi(Testing)
public func `open`(
atPath path: FilePath,
mode: FileDescriptor.AccessMode,
options: FileDescriptor.OpenOptions,
permissions: FilePermissions?,
retryOnInterrupt: Bool = true
) -> Result<FileDescriptor, Errno> {
let oFlag = mode.rawValue | options.rawValue
let rawValue = valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
path.withPlatformString {
if let permissions = permissions {
return system_fdopenat(self.rawValue, $0, oFlag, permissions.rawValue)
} else {
precondition(!options.contains(.create), "Create must be given permissions")
return system_fdopenat(self.rawValue, $0, oFlag)
}
}
}
return rawValue.map { FileDescriptor(rawValue: $0) }
}
/// Returns information about the status of the open file.
///
/// The corresponding C function is `fstat`.
///
/// - Returns: Information about the open file.
@_spi(Testing)
public func status() -> Result<CInterop.Stat, Errno> {
var status = CInterop.Stat()
return nothingOrErrno(retryOnInterrupt: false) {
system_fstat(self.rawValue, &status)
}.map { status }
}
/// Sets the permission bits of the open file.
///
/// The corresponding C function is `fchmod`.
///
/// - Parameters:
/// - mode: The permissions to set on the file.
/// - retryOnInterrupt: Whether to retry the operation if it throws `Errno.interrupted`. The
/// default is true. Pass false to try only once and throw an error upon interruption.
@_spi(Testing)
public func changeMode(
_ mode: FilePermissions,
retryOnInterrupt: Bool = true
) -> Result<Void, Errno> {
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
system_fchmod(self.rawValue, mode.rawValue)
}
}
/// List the names of extended attributes.
///
/// The corresponding C function is `flistxattr`.
///
/// - Parameter buffer: The buffer into which names are written. Names are written are NULL
/// terminated UTF-8 strings and are returned in an arbitrary order. There is no padding
/// between strings. If `buffer` is `nil` then the return value is the size of the buffer
/// required to list all extended attributes. If there is not enough space in the `buffer`
/// then `Errno.outOfRange` is returned.
/// - Returns: The size of the extended attribute list.
@_spi(Testing)
public func listExtendedAttributes(
_ buffer: UnsafeMutableBufferPointer<CChar>?
) -> Result<Int, Errno> {
valueOrErrno(retryOnInterrupt: false) {
system_flistxattr(self.rawValue, buffer?.baseAddress, buffer?.count ?? 0)
}
}
/// Get the value of the named extended attribute.
///
/// The corresponding C function is `fgetxattr`.
///
/// - Parameters:
/// - name: The name of the extended attribute.
/// - buffer: The buffer into which the value is written. If `buffer` is `nil` then the return
/// value is the size of the buffer required to read the value. If there is not enough
/// space in the `buffer` then `Errno.outOfRange` is returned.
/// - Returns: The size of the extended attribute value.
@_spi(Testing)
public func getExtendedAttribute(
named name: String,
buffer: UnsafeMutableRawBufferPointer?
) -> Result<Int, Errno> {
valueOrErrno(retryOnInterrupt: false) {
name.withPlatformString {
system_fgetxattr(self.rawValue, $0, buffer?.baseAddress, buffer?.count ?? 0)
}
}
}
/// Set the value of the named extended attribute.
///
/// The corresponding C function is `fsetxattr`.
///
/// - Parameters:
/// - name: The name of the extended attribute.
/// - value: The data to set for the attribute.
@_spi(Testing)
public func setExtendedAttribute(
named name: String,
to value: UnsafeRawBufferPointer?
) -> Result<Void, Errno> {
nothingOrErrno(retryOnInterrupt: false) {
name.withPlatformString { namePointer in
system_fsetxattr(self.rawValue, namePointer, value?.baseAddress, value?.count ?? 0)
}
}
}
/// Remove the value for the named extended attribute.
///
/// The corresponding C function is `fremovexattr`.
///
/// - Parameters:
/// - name: The name of the extended attribute.
@_spi(Testing)
public func removeExtendedAttribute(_ name: String) -> Result<Void, Errno> {
nothingOrErrno(retryOnInterrupt: false) {
name.withPlatformString {
system_fremovexattr(self.rawValue, $0)
}
}
}
/// Synchronize modified data and metadata to a permanent storage device.
///
/// The corresponding C functions is `fsync`.
///
/// - Parameter retryOnInterrupt: Whether the call should be retried on `Errno.interrupted`.
@_spi(Testing)
public func synchronize(retryOnInterrupt: Bool = true) -> Result<Void, Errno> {
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
system_fsync(self.rawValue)
}
}
/// Returns a pointer to a directory structure.
///
/// The corresponding C function is `fdopendir`
///
/// - Important: Calling this function cedes ownership of the file descriptor to the system. The
/// caller should not modify the descriptor or close the descriptor via `close()`. Once
/// directory iteration has been completed then `Libc.closdir(_:)` must be called.
internal func opendir() -> Result<CInterop.DirPointer, Errno> {
valueOrErrno(retryOnInterrupt: false) {
libc_fdopendir(self.rawValue)
}
}
}
extension FileDescriptor {
func listExtendedAttributes() -> Result<[String], Errno> {
// Required capacity is returned if a no buffer is passed to flistxattr.
self.listExtendedAttributes(nil).flatMap { capacity in
guard capacity > 0 else {
// Required capacity is zero: no attributes to read.
return .success([])
}
// Read and decode.
var buffer = [UInt8](repeating: 0, count: capacity)
return buffer.withUnsafeMutableBufferPointer { pointer in
pointer.withMemoryRebound(to: CChar.self) { buffer in
self.listExtendedAttributes(buffer)
}
}.map { size in
// The buffer contains null terminated C-strings.
var attributes = [String]()
var slice = buffer.prefix(size)
while let index = slice.firstIndex(of: 0) {
// TODO: can we do this more cheaply?
let prefix = slice[..<index]
attributes.append(String(decoding: Array(prefix), as: UTF8.self))
slice = slice.dropFirst(prefix.count + 1)
}
return attributes
}
}
}
func readExtendedAttribute(named name: String) -> Result<[UInt8], Errno> {
// Required capacity is returned if a no buffer is passed to fgetxattr.
self.getExtendedAttribute(named: name, buffer: nil).flatMap { capacity in
guard capacity > 0 else {
// Required capacity is zero: no values to read.
return .success([])
}
// Read and decode.
var buffer = [UInt8](repeating: 0, count: capacity)
return buffer.withUnsafeMutableBytes { bytes in
self.getExtendedAttribute(named: name, buffer: bytes)
}.map { size in
// Remove any trailing zeros.
buffer.removeLast(buffer.count - size)
return buffer
}
}
}
}
extension FileDescriptor {
func readChunk(fromAbsoluteOffset offset: Int64, length: Int64) -> Result<ByteBuffer, Error> {
self._readChunk(fromAbsoluteOffset: offset, length: length)
}
func readChunk(length: Int64) -> Result<ByteBuffer, Error> {
self._readChunk(fromAbsoluteOffset: nil, length: length)
}
private func _readChunk(
fromAbsoluteOffset offset: Int64?,
length: Int64
) -> Result<ByteBuffer, Error> {
// This is used by the `FileChunks` and means we allocate for every chunk that we read for
// the file. That's fine for now because the syscall cost is likely to be the dominant
// factor here. However we should investigate whether it's possible to have a pool of
// buffers which we can reuse. This would need to be at least as large as the high watermark
// of the chunked file for it to be useful.
Result {
var buffer = ByteBuffer()
try buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: Int(length)) { buffer in
let bufferPointer: UnsafeMutableRawBufferPointer
// Don't vend a buffer which is larger than `length`; we can read less but we must
// not read more.
if length < buffer.count {
bufferPointer = UnsafeMutableRawBufferPointer(
start: buffer.baseAddress,
count: Int(length)
)
} else {
bufferPointer = buffer
}
if let offset {
return try self.read(fromAbsoluteOffset: offset, into: bufferPointer)
} else {
return try self.read(into: bufferPointer)
}
}
return buffer
}
}
}
extension FileDescriptor {
func write(
contentsOf bytes: some Sequence<UInt8>,
toAbsoluteOffset offset: Int64
) -> Result<Int64, Error> {
Result {
Int64(try self.writeAll(toAbsoluteOffset: offset, bytes))
}
}
func write(
contentsOf bytes: some Sequence<UInt8>
) -> Result<Int64, Error> {
Result {
Int64(try self.writeAll(bytes))
}
}
}
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
extension FileDescriptor.OpenOptions {
static var temporaryFile: Self {
Self(rawValue: CNIOLinux_O_TMPFILE)
}
}
extension FileDescriptor {
static var currentWorkingDirectory: Self {
Self(rawValue: AT_FDCWD)
}
}
#endif