mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
[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>
This commit is contained in:
1 parent
01626145d0
commit
590dd7b4bc
33 files changed
+1192
-21
No files matched your search
@@ -110,7 +110,7 @@ jobs:
|
||||
MATRIX_LINUX_NIGHTLY_NEXT_ENABLED: false
|
||||
MATRIX_LINUX_NIGHTLY_MAIN_ENABLED: false
|
||||
MATRIX_WINDOWS_6_3_ENABLED: true
|
||||
MATRIX_WINDOWS_COMMAND: "swift build --target _NIOBase64 && swift build --target _NIOConcurrency && swift build --target _NIODataStructures && swift build --target NIO && swift build --target NIOAsyncAwaitDemo && swift build --target NIOChatClient && swift build --target NIOChatServer && swift build --target NIOConcurrencyHelpers && swift build --target NIOCore && swift build --target NIOEchoClient && swift build --target NIOEchoServer && swift build --target NIOEmbedded && swift build --target NIOFoundationCompat && swift build --target NIOFoundationEssentialsCompat && swift build --target NIOHTTP1 && swift build --target NIOHTTP1Client && swift build --target NIOHTTP1Server && swift build --target NIOMulticastChat && swift build --target NIOPerformanceTester && swift build --target NIOPosix && swift build --target NIOTCPEchoClient && swift build --target NIOTCPEchoServer && swift build --target NIOTLS && swift build --target NIOTestUtils && swift build --target NIOUDPEchoClient && swift build --target NIOUDPEchoServer && swift build --target NIOWebSocket && swift build --target NIOWebSocketClient && swift build --target NIOWebSocketServer"
|
||||
MATRIX_WINDOWS_COMMAND: "swift build"
|
||||
|
||||
windows-build:
|
||||
name: Windows build (currently-supported targets)
|
||||
|
||||
@@ -86,7 +86,7 @@ jobs:
|
||||
MATRIX_LINUX_NIGHTLY_NEXT_ENABLED: false
|
||||
MATRIX_LINUX_NIGHTLY_MAIN_ENABLED: false
|
||||
MATRIX_WINDOWS_6_3_ENABLED: true
|
||||
MATRIX_WINDOWS_COMMAND: "swift build --target _NIOBase64 && swift build --target _NIOConcurrency && swift build --target _NIODataStructures && swift build --target NIO && swift build --target NIOAsyncAwaitDemo && swift build --target NIOChatClient && swift build --target NIOChatServer && swift build --target NIOConcurrencyHelpers && swift build --target NIOCore && swift build --target NIOEchoClient && swift build --target NIOEchoServer && swift build --target NIOEmbedded && swift build --target NIOFoundationCompat && swift build --target NIOFoundationEssentialsCompat && swift build --target NIOHTTP1 && swift build --target NIOHTTP1Client && swift build --target NIOHTTP1Server && swift build --target NIOMulticastChat && swift build --target NIOPerformanceTester && swift build --target NIOPosix && swift build --target NIOTCPEchoClient && swift build --target NIOTCPEchoServer && swift build --target NIOTLS && swift build --target NIOTestUtils && swift build --target NIOUDPEchoClient && swift build --target NIOUDPEchoServer && swift build --target NIOWebSocket && swift build --target NIOWebSocketClient && swift build --target NIOWebSocketServer"
|
||||
MATRIX_WINDOWS_COMMAND: "swift build"
|
||||
|
||||
windows-build:
|
||||
name: Windows build (currently-supported targets)
|
||||
|
||||
@@ -257,6 +257,7 @@ let package = Package(
|
||||
"NIOPosix",
|
||||
"CNIOLinux",
|
||||
"CNIODarwin",
|
||||
"CNIOWindows",
|
||||
swiftAtomics,
|
||||
swiftCollections,
|
||||
swiftSystem,
|
||||
@@ -285,6 +286,7 @@ let package = Package(
|
||||
"NIOPosix",
|
||||
"CNIOLinux",
|
||||
"CNIODarwin",
|
||||
"CNIOWindows",
|
||||
swiftAtomics,
|
||||
swiftCollections,
|
||||
swiftSystem,
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
#include <basetsd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define NIO(name) CNIOWindows_ ## name
|
||||
|
||||
@@ -107,7 +108,13 @@ WSACMSGHDR *NIO(CMSG_NXTHDR)(const WSAMSG *, LPWSACMSGHDR);
|
||||
size_t NIO(CMSG_LEN)(size_t);
|
||||
size_t NIO(CMSG_SPACE)(size_t);
|
||||
|
||||
int NIO(errno)(void);
|
||||
// Reads `errno`. Windows spells the errno type `errno_t` (a typedef for `int`)
|
||||
// and exposes it only through the `_get_errno`/`_set_errno` accessors, since
|
||||
// `errno` itself is a macro. Wrap those here so Swift can read/write it.
|
||||
errno_t NIO(errno)(void);
|
||||
|
||||
// Sets `errno`; see `CNIOWindows_errno`.
|
||||
void NIO(set_errno)(errno_t value);
|
||||
|
||||
DWORD NIO(FormatGetLastError)(DWORD errorCode, LPSTR errorMsg);
|
||||
|
||||
|
||||
@@ -58,8 +58,14 @@ size_t CNIOWindows_CMSG_SPACE(size_t length) {
|
||||
return WSA_CMSG_SPACE(length);
|
||||
}
|
||||
|
||||
int CNIOWindows_errno(void) {
|
||||
return errno;
|
||||
errno_t CNIOWindows_errno(void) {
|
||||
errno_t value = 0;
|
||||
_get_errno(&value);
|
||||
return value;
|
||||
}
|
||||
|
||||
void CNIOWindows_set_errno(errno_t value) {
|
||||
_set_errno(value);
|
||||
}
|
||||
|
||||
DWORD CNIOWindows_FormatGetLastError(DWORD errorCode, LPSTR errorMsg) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin) || os(Linux) || os(Android)
|
||||
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
|
||||
import NIOCore
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin) || os(Linux) || os(Android)
|
||||
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
|
||||
import NIOCore
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
|
||||
@@ -556,6 +556,8 @@ private struct DirectoryEnumerator: Sendable {
|
||||
// Empty is checked for above, root can't exist within a directory, and directory
|
||||
// items must be a single path component.
|
||||
name = FilePath.Component(platformString: CNIODarwin_dirent_dname(entry))!
|
||||
#elseif os(Windows)
|
||||
name = FilePath.Component(platformString: CNIOWindows_dirent_dname(entry))!
|
||||
#else
|
||||
name = FilePath.Component(platformString: CNIOLinux_dirent_dname(entry))!
|
||||
#endif
|
||||
|
||||
@@ -77,6 +77,17 @@ public struct FileInfo: Hashable, Sendable {
|
||||
/// Creates a ``FileInfo`` by deriving values from a platform-specific value.
|
||||
public init(platformSpecificStatus: CInterop.Stat) {
|
||||
self._platformSpecificStatus = Stat(platformSpecificStatus)
|
||||
#if os(Windows)
|
||||
self.type = FileType(platformSpecificMode: platformSpecificStatus.nioMode)
|
||||
self.permissions = FilePermissions(masking: platformSpecificStatus.nioMode)
|
||||
self.size = platformSpecificStatus.nioSize
|
||||
// Windows has no POSIX owner/group; report the defaults.
|
||||
self.userID = UserID(rawValue: 0)
|
||||
self.groupID = GroupID(rawValue: 0)
|
||||
self.lastAccessTime = platformSpecificStatus.nioLastAccessTime
|
||||
self.lastDataModificationTime = platformSpecificStatus.nioLastDataModificationTime
|
||||
self.lastStatusChangeTime = platformSpecificStatus.nioLastStatusChangeTime
|
||||
#else
|
||||
self.type = FileType(platformSpecificMode: CInterop.Mode(platformSpecificStatus.st_mode))
|
||||
self.permissions = FilePermissions(masking: CInterop.Mode(platformSpecificStatus.st_mode))
|
||||
self.size = Int64(platformSpecificStatus.st_size)
|
||||
@@ -92,6 +103,7 @@ public struct FileInfo: Hashable, Sendable {
|
||||
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
|
||||
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Creates a ``FileInfo`` from the provided values.
|
||||
@@ -149,7 +161,7 @@ extension FileInfo {
|
||||
|
||||
/// A time interval consisting of whole seconds and nanoseconds.
|
||||
public struct Timespec: Hashable, Sendable {
|
||||
#if canImport(Darwin)
|
||||
#if canImport(Darwin) || os(Windows)
|
||||
private static let utimeOmit = Int(UTIME_OMIT)
|
||||
private static let utimeNow = Int(UTIME_NOW)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
@@ -200,6 +212,9 @@ private struct Stat: Hashable {
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
let stat = self.stat
|
||||
#if os(Windows)
|
||||
stat.nioHash(into: &hasher)
|
||||
#else
|
||||
// Different platforms have different underlying values; these are
|
||||
// common between Darwin and Glibc.
|
||||
hasher.combine(stat.st_dev)
|
||||
@@ -225,6 +240,7 @@ private struct Stat: Hashable {
|
||||
hasher.combine(FileInfo.Timespec(stat.st_mtim))
|
||||
hasher.combine(FileInfo.Timespec(stat.st_ctim))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -232,6 +248,9 @@ private struct Stat: Hashable {
|
||||
let lStat = lhs.stat
|
||||
let rStat = rhs.stat
|
||||
|
||||
#if os(Windows)
|
||||
return CInterop.Stat.nioIsEqual(lStat, rStat)
|
||||
#else
|
||||
// Different platforms have different underlying values; these are
|
||||
// common between Darwin and Glibc.
|
||||
var isEqual = lStat.st_dev == rStat.st_dev
|
||||
@@ -268,6 +287,7 @@ private struct Stat: Hashable {
|
||||
#endif
|
||||
|
||||
return isEqual
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1346,6 +1346,8 @@ extension FileSystem {
|
||||
}
|
||||
}
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("_copyRegularFile is unavailable on Windows")
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1733,6 +1735,8 @@ extension FileSystem {
|
||||
)
|
||||
return .failure(error)
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("_copySymbolicLink is unavailable on Windows")
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ extension FileType {
|
||||
/// Initializes a file type from the `d_type` from `dirent`.
|
||||
@_spi(Testing)
|
||||
public init(direntType: UInt8) {
|
||||
#if canImport(Darwin) || canImport(Musl) || os(Android)
|
||||
#if canImport(Darwin) || canImport(Musl) || os(Android) || os(Windows)
|
||||
let value = Int32(direntType)
|
||||
#elseif canImport(Glibc)
|
||||
let value = Int(direntType)
|
||||
|
||||
@@ -26,6 +26,8 @@ import CNIOLinux
|
||||
#elseif canImport(Android)
|
||||
@preconcurrency import Android
|
||||
import CNIOLinux
|
||||
#elseif os(Windows)
|
||||
import CNIOWindows
|
||||
#endif
|
||||
|
||||
/// Aliases for platform-dependent types used for system calls.
|
||||
@@ -38,6 +40,11 @@ extension CInterop {
|
||||
public typealias Stat = Musl.stat
|
||||
#elseif canImport(Android)
|
||||
public typealias Stat = Android.stat
|
||||
#elseif os(Windows)
|
||||
// `BY_HANDLE_FILE_INFORMATION` is the Windows analogue of `struct stat`; it
|
||||
// is what `GetFileInformationByHandle` fills in. As on other platforms this
|
||||
// is re-exposed via `FileInfo.platformSpecificStatus`.
|
||||
public typealias Stat = BY_HANDLE_FILE_INFORMATION
|
||||
#endif
|
||||
|
||||
#if canImport(Darwin)
|
||||
@@ -52,6 +59,11 @@ extension CInterop {
|
||||
#elseif canImport(Android)
|
||||
@_spi(Testing)
|
||||
public static let maxPathLength = Android.PATH_MAX
|
||||
#elseif os(Windows)
|
||||
// The maximum length of an extended-length (`\\?\`-prefixed) path, which is
|
||||
// longer than the legacy `MAX_PATH` (260) limit.
|
||||
@_spi(Testing)
|
||||
public static let maxPathLength = Int32(Int16.max)
|
||||
#endif
|
||||
|
||||
#if canImport(Darwin)
|
||||
|
||||
@@ -36,6 +36,8 @@ extension Errno {
|
||||
return Errno(rawValue: Musl.errno)
|
||||
#elseif canImport(Android)
|
||||
return Errno(rawValue: Android.errno)
|
||||
#elseif os(Windows)
|
||||
return Errno(rawValue: _nio_fs_errno)
|
||||
#endif
|
||||
}
|
||||
set {
|
||||
@@ -47,6 +49,8 @@ extension Errno {
|
||||
Musl.errno = newValue.rawValue
|
||||
#elseif canImport(Android)
|
||||
Android.errno = newValue.rawValue
|
||||
#elseif os(Windows)
|
||||
_nio_fs_errno = newValue.rawValue
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -60,6 +64,8 @@ extension Errno {
|
||||
Musl.errno = 0
|
||||
#elseif canImport(Android)
|
||||
Android.errno = 0
|
||||
#elseif os(Windows)
|
||||
_nio_fs_errno = 0
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ 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 {
|
||||
|
||||
@@ -300,6 +300,11 @@ 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
|
||||
@@ -343,14 +348,22 @@ extension String {
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
// Need to #if because CChar may be signed
|
||||
try withCString(body)
|
||||
#if os(Windows)
|
||||
return try self.withCString(encodedAs: UTF16.self, body)
|
||||
#else
|
||||
return try withCString(body)
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ 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
|
||||
|
||||
@_spi(Testing)
|
||||
|
||||
@@ -193,13 +193,15 @@ internal func system_flistxattr(
|
||||
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<CChar>,
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ value: UnsafeMutableRawPointer?,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
@@ -215,13 +217,15 @@ internal func system_fgetxattr(
|
||||
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<CChar>,
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ value: UnsafeRawPointer?,
|
||||
_ size: Int
|
||||
) -> CInt {
|
||||
@@ -237,13 +241,15 @@ internal func system_fsetxattr(
|
||||
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<CChar>
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
@@ -256,6 +262,8 @@ internal func system_fremovexattr(
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2026 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if os(Windows)
|
||||
|
||||
// Windows-only compatibility shim for the NIOFileSystem family.
|
||||
//
|
||||
// This is a *compile-only* port: it supplies the POSIX-shaped types, constants
|
||||
// and free functions the rest of the target expects on Windows so the file
|
||||
// system targets build. The functions that map onto Win32 are stubbed with
|
||||
// `fatalError(...)` here; real implementations land in follow-up PRs. The stubs
|
||||
// deliberately match the bare-libc signatures the `system_*` / `libc_*`
|
||||
// wrappers call, so those wrappers (and their non-Windows call sites) need no
|
||||
// changes.
|
||||
//
|
||||
// Everything here is defined at module scope so, thanks to same-module
|
||||
// visibility, the rest of the target can use these types, constants and
|
||||
// free-functions without importing anything.
|
||||
|
||||
import CNIOWindows
|
||||
import SystemPackage
|
||||
|
||||
// MARK: - Scalar types
|
||||
|
||||
/// POSIX `off_t`.
|
||||
typealias off_t = Int64
|
||||
|
||||
/// POSIX `timespec`.
|
||||
///
|
||||
/// Defined locally rather than reused from ucrt. ucrt does declare a
|
||||
/// `struct timespec`, but its `tv_nsec` is a C `long` (32-bit under Windows'
|
||||
/// LLP64 model), whereas the shared `FileInfo.Timespec` initializer assigns
|
||||
/// `tv_sec`/`tv_nsec` straight into `Int` fields. Declaring a POSIX-shaped
|
||||
/// `timespec` with `Int` fields lets the cross-platform `FileInfo` and
|
||||
/// `futimens` code compile unchanged, and lets the struct be `Sendable`
|
||||
/// (the imported C type is not).
|
||||
public struct timespec: Sendable {
|
||||
public var tv_sec: Int
|
||||
public var tv_nsec: Int
|
||||
|
||||
public init(tv_sec: Int = 0, tv_nsec: Int = 0) {
|
||||
self.tv_sec = tv_sec
|
||||
self.tv_nsec = tv_nsec
|
||||
}
|
||||
}
|
||||
|
||||
/// POSIX `pthread_key_t`.
|
||||
typealias pthread_key_t = UInt32
|
||||
|
||||
// MARK: - `CInterop.Stat` and friends
|
||||
//
|
||||
// `CInterop.Stat` itself is `BY_HANDLE_FILE_INFORMATION`, aliased in
|
||||
// `CInterop.swift` alongside the other platforms. The bridging that turns it
|
||||
// into the POSIX-shaped values the shared `FileInfo` code reads lives at the
|
||||
// bottom of this file.
|
||||
|
||||
extension CInterop {
|
||||
/// Directory-stream handle. Matches the Linux representation (opaque).
|
||||
typealias DirPointer = OpaquePointer
|
||||
|
||||
/// Windows stand-in for `struct dirent`. `d_name` is a fixed-size tuple of
|
||||
/// `CChar` so the existing `.0/.1/.2` "is this '.' or '..'" check compiles.
|
||||
struct WindowsDirEnt {
|
||||
var d_type: UInt8 = 0
|
||||
var d_name:
|
||||
(
|
||||
CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar,
|
||||
CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar
|
||||
) = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
typealias DirEnt = WindowsDirEnt
|
||||
|
||||
/// Opaque FTS handle.
|
||||
typealias FTS = OpaquePointer
|
||||
|
||||
/// Windows stand-in for `FTSENT`. Only the fields NIOFS reads.
|
||||
struct WindowsFTSEnt {
|
||||
var fts_info: UInt16 = 0
|
||||
var fts_errno: CInt = 0
|
||||
var fts_path: UnsafeMutablePointer<CInterop.PlatformChar>? = nil
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
typealias FTSEnt = WindowsFTSEnt
|
||||
}
|
||||
|
||||
// MARK: - swift-system gap extensions
|
||||
|
||||
extension FileDescriptor.OpenOptions {
|
||||
// Distinct high bits we own. These are placeholders for a real Win32
|
||||
// mapping in the future implementation.
|
||||
static var noFollow: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x4000_0000)
|
||||
}
|
||||
static var closeOnExec: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x2000_0000)
|
||||
}
|
||||
static var nonBlocking: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x1000_0000)
|
||||
}
|
||||
static var directory: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x0800_0000)
|
||||
}
|
||||
}
|
||||
|
||||
extension Errno {
|
||||
/// No direct equivalent on Windows; alias to `EINVAL` so switches stay
|
||||
/// exhaustive.
|
||||
static var noData: Errno { Errno.invalidArgument }
|
||||
}
|
||||
|
||||
// MARK: - `stat` mode constants (typed to `CInterop.Mode`)
|
||||
|
||||
let S_IFMT: CInterop.Mode = 0o170000
|
||||
let S_IFSOCK: CInterop.Mode = 0o140000
|
||||
let S_IFLNK: CInterop.Mode = 0o120000
|
||||
let S_IFREG: CInterop.Mode = 0o100000
|
||||
let S_IFBLK: CInterop.Mode = 0o060000
|
||||
let S_IFDIR: CInterop.Mode = 0o040000
|
||||
let S_IFCHR: CInterop.Mode = 0o020000
|
||||
let S_IFIFO: CInterop.Mode = 0o010000
|
||||
|
||||
// MARK: - `dirent` `d_type` constants (typed to `CInt`)
|
||||
|
||||
let DT_UNKNOWN: CInt = 0
|
||||
let DT_FIFO: CInt = 1
|
||||
let DT_CHR: CInt = 2
|
||||
let DT_DIR: CInt = 4
|
||||
let DT_BLK: CInt = 6
|
||||
let DT_REG: CInt = 8
|
||||
let DT_LNK: CInt = 10
|
||||
let DT_SOCK: CInt = 12
|
||||
|
||||
// MARK: - FTS constants (typed to `CInt`)
|
||||
|
||||
let FTS_D: CInt = 1
|
||||
let FTS_DC: CInt = 2
|
||||
let FTS_DEFAULT: CInt = 3
|
||||
let FTS_DNR: CInt = 4
|
||||
let FTS_DOT: CInt = 5
|
||||
let FTS_DP: CInt = 6
|
||||
let FTS_ERR: CInt = 7
|
||||
let FTS_F: CInt = 8
|
||||
let FTS_NS: CInt = 10
|
||||
let FTS_NSOK: CInt = 11
|
||||
let FTS_SL: CInt = 12
|
||||
let FTS_SLNONE: CInt = 13
|
||||
|
||||
let FTS_PHYSICAL: CInt = 0x0010
|
||||
let FTS_LOGICAL: CInt = 0x0002
|
||||
let FTS_NOCHDIR: CInt = 0x0004
|
||||
|
||||
// MARK: - `UTIME_*` sentinels
|
||||
|
||||
let UTIME_NOW: CInt = (1 << 30) - 1
|
||||
let UTIME_OMIT: CInt = (1 << 30) - 2
|
||||
|
||||
// MARK: - errno access
|
||||
|
||||
// `errno` is a macro on the Windows CRT that Swift cannot read or assign
|
||||
// directly, so bridge through the `CNIOWindows` C shim, which expands the macro
|
||||
// in C. This is the real thread-local `errno` backing the CRT calls the higher
|
||||
// level file-system code inspects.
|
||||
var _nio_fs_errno: CInt {
|
||||
get { CNIOWindows_errno() }
|
||||
set { CNIOWindows_set_errno(newValue) }
|
||||
}
|
||||
|
||||
// MARK: - libc string helpers (stubs / trivial)
|
||||
|
||||
func strlen(_ s: UnsafePointer<CChar>) -> Int {
|
||||
var length = 0
|
||||
while s[length] != 0 { length += 1 }
|
||||
return length
|
||||
}
|
||||
|
||||
func strlen(_ s: UnsafeMutablePointer<CChar>) -> Int {
|
||||
strlen(UnsafePointer(s))
|
||||
}
|
||||
|
||||
func strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
|
||||
var length = 0
|
||||
while s[length] != 0 { length += 1 }
|
||||
return length
|
||||
}
|
||||
|
||||
func strerror(_ code: CInt) -> UnsafeMutablePointer<CChar>? {
|
||||
fatalError("strerror is unavailable on Windows")
|
||||
}
|
||||
|
||||
func memset(_ b: UnsafeMutableRawPointer, _ c: CInt, _ len: Int) -> UnsafeMutableRawPointer {
|
||||
b.initializeMemory(as: UInt8.self, repeating: UInt8(truncatingIfNeeded: c), count: len)
|
||||
return b
|
||||
}
|
||||
|
||||
func getenv(_ name: UnsafePointer<CChar>) -> UnsafeMutablePointer<CChar>? {
|
||||
fatalError("getenv is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - POSIX file-system syscall stubs
|
||||
//
|
||||
// These match the bare-libc call signatures used by the `system_*` / `libc_*`
|
||||
// wrappers, so those wrappers need no edits. All are stubs.
|
||||
|
||||
func openat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: CInt
|
||||
) -> CInt {
|
||||
fatalError("openat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func openat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: CInt,
|
||||
_ mode: CInterop.Mode
|
||||
) -> CInt {
|
||||
fatalError("openat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func stat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("stat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func lstat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("lstat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fstat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("fstat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fchmod(_ fd: FileDescriptor.RawValue, _ mode: CInterop.Mode) -> CInt {
|
||||
fatalError("fchmod is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fsync(_ fd: FileDescriptor.RawValue) -> CInt {
|
||||
fatalError("fsync is unavailable on Windows")
|
||||
}
|
||||
|
||||
func mkdir(_ path: UnsafePointer<CInterop.PlatformChar>, _ mode: CInterop.Mode) -> CInt {
|
||||
fatalError("mkdir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func symlink(
|
||||
_ destination: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ source: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("symlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func symlinkat(
|
||||
_ destination: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ dirfd: FileDescriptor.RawValue,
|
||||
_ source: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("symlinkat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func readlink(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
fatalError("readlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func rename(
|
||||
_ old: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ new: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("rename is unavailable on Windows")
|
||||
}
|
||||
|
||||
func link(
|
||||
_ old: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ new: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("link is unavailable on Windows")
|
||||
}
|
||||
|
||||
func unlink(_ path: UnsafePointer<CInterop.PlatformChar>) -> CInt {
|
||||
fatalError("unlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func unlinkat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ flags: CInt
|
||||
) -> CInt {
|
||||
fatalError("unlinkat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func futimens(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ times: UnsafePointer<timespec>?
|
||||
) -> CInt {
|
||||
fatalError("futimens is unavailable on Windows")
|
||||
}
|
||||
|
||||
func remove(_ path: UnsafePointer<CInterop.PlatformChar>) -> CInt {
|
||||
fatalError("remove is unavailable on Windows")
|
||||
}
|
||||
|
||||
func getcwd(
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> UnsafeMutablePointer<CInterop.PlatformChar>? {
|
||||
fatalError("getcwd is unavailable on Windows")
|
||||
}
|
||||
|
||||
func confstr(
|
||||
_ name: CInt,
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
fatalError("confstr is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fdopendir(_ fd: FileDescriptor.RawValue) -> CInterop.DirPointer? {
|
||||
fatalError("fdopendir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func readdir(_ dir: CInterop.DirPointer) -> UnsafeMutablePointer<CInterop.DirEnt>? {
|
||||
fatalError("readdir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func closedir(_ dir: CInterop.DirPointer) -> CInt {
|
||||
fatalError("closedir is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - FTS stubs
|
||||
|
||||
func fts_open(
|
||||
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
|
||||
_ options: CInt,
|
||||
_ compare: UnsafeRawPointer?
|
||||
) -> UnsafeMutablePointer<CInterop.FTS>? {
|
||||
fatalError("fts_open is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fts_read(
|
||||
_ fts: UnsafeMutablePointer<CInterop.FTS>
|
||||
) -> UnsafeMutablePointer<CInterop.FTSEnt>? {
|
||||
fatalError("fts_read is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fts_close(_ fts: UnsafeMutablePointer<CInterop.FTS>) -> CInt {
|
||||
fatalError("fts_close is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - dirent name accessor
|
||||
|
||||
/// Windows equivalent of `CNIOLinux_dirent_dname` / `CNIODarwin_dirent_dname`.
|
||||
func CNIOWindows_dirent_dname(
|
||||
_ entry: UnsafeMutablePointer<CInterop.DirEnt>
|
||||
) -> UnsafePointer<CInterop.PlatformChar> {
|
||||
fatalError("CNIOWindows_dirent_dname is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - pthread TLS stubs
|
||||
|
||||
func pthread_key_create(
|
||||
_ key: UnsafeMutablePointer<pthread_key_t>,
|
||||
_ destructor: (@convention(c) (UnsafeMutableRawPointer?) -> Void)?
|
||||
) -> CInt {
|
||||
fatalError("pthread_key_create is unavailable on Windows")
|
||||
}
|
||||
|
||||
func pthread_setspecific(_ key: pthread_key_t, _ value: UnsafeRawPointer?) -> CInt {
|
||||
fatalError("pthread_setspecific is unavailable on Windows")
|
||||
}
|
||||
|
||||
func pthread_getspecific(_ key: pthread_key_t) -> UnsafeMutableRawPointer? {
|
||||
fatalError("pthread_getspecific is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - `BY_HANDLE_FILE_INFORMATION` -> `FileInfo` bridging
|
||||
//
|
||||
// `CInterop.Stat` is `BY_HANDLE_FILE_INFORMATION` on Windows. These helpers
|
||||
// translate it into the POSIX-shaped values the shared `FileInfo` code reads,
|
||||
// keeping the Win32 specifics (and the `CNIOWindows`/WinSDK dependency) out of
|
||||
// `FileInfo.swift`.
|
||||
|
||||
extension CInterop.Stat {
|
||||
/// A POSIX-style `st_mode` synthesised from `dwFileAttributes`.
|
||||
///
|
||||
/// This mirrors the Windows CRT's `stat` (`convert_to_stat_mode`): a
|
||||
/// directory maps to `S_IFDIR` and gains the execute bits, anything else is
|
||||
/// a regular file, and the read/write bits follow `FILE_ATTRIBUTE_READONLY`,
|
||||
/// replicated across owner/group/other. The CRT's executable-by-extension
|
||||
/// heuristic is intentionally dropped: it needs the path, which isn't part
|
||||
/// of `BY_HANDLE_FILE_INFORMATION`.
|
||||
var nioMode: CInterop.Mode {
|
||||
var mode: CInterop.Mode =
|
||||
(self.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0
|
||||
? S_IFDIR | 0o100
|
||||
: S_IFREG
|
||||
mode |=
|
||||
(self.dwFileAttributes & DWORD(FILE_ATTRIBUTE_READONLY)) != 0
|
||||
? 0o400
|
||||
: 0o600
|
||||
mode |= (mode & 0o700) >> 3
|
||||
mode |= (mode & 0o700) >> 6
|
||||
return mode
|
||||
}
|
||||
|
||||
/// The file size assembled from its high and low halves.
|
||||
var nioSize: Int64 {
|
||||
Int64(self.nFileSizeHigh) << 32 | Int64(self.nFileSizeLow)
|
||||
}
|
||||
|
||||
var nioLastAccessTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftLastAccessTime)
|
||||
}
|
||||
|
||||
var nioLastDataModificationTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftLastWriteTime)
|
||||
}
|
||||
|
||||
/// Windows has no POSIX status-change time; the creation time is the closest
|
||||
/// analogue.
|
||||
var nioLastStatusChangeTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftCreationTime)
|
||||
}
|
||||
|
||||
func nioHash(into hasher: inout Hasher) {
|
||||
hasher.combine(self.dwFileAttributes)
|
||||
hasher.combine(self.dwVolumeSerialNumber)
|
||||
hasher.combine(self.nFileSizeHigh)
|
||||
hasher.combine(self.nFileSizeLow)
|
||||
hasher.combine(self.nNumberOfLinks)
|
||||
hasher.combine(self.nFileIndexHigh)
|
||||
hasher.combine(self.nFileIndexLow)
|
||||
hasher.combine(self.ftCreationTime.dwLowDateTime)
|
||||
hasher.combine(self.ftCreationTime.dwHighDateTime)
|
||||
hasher.combine(self.ftLastAccessTime.dwLowDateTime)
|
||||
hasher.combine(self.ftLastAccessTime.dwHighDateTime)
|
||||
hasher.combine(self.ftLastWriteTime.dwLowDateTime)
|
||||
hasher.combine(self.ftLastWriteTime.dwHighDateTime)
|
||||
}
|
||||
|
||||
static func nioIsEqual(_ lhs: CInterop.Stat, _ rhs: CInterop.Stat) -> Bool {
|
||||
lhs.dwFileAttributes == rhs.dwFileAttributes
|
||||
&& lhs.dwVolumeSerialNumber == rhs.dwVolumeSerialNumber
|
||||
&& lhs.nFileSizeHigh == rhs.nFileSizeHigh
|
||||
&& lhs.nFileSizeLow == rhs.nFileSizeLow
|
||||
&& lhs.nNumberOfLinks == rhs.nNumberOfLinks
|
||||
&& lhs.nFileIndexHigh == rhs.nFileIndexHigh
|
||||
&& lhs.nFileIndexLow == rhs.nFileIndexLow
|
||||
&& lhs.ftCreationTime.dwLowDateTime == rhs.ftCreationTime.dwLowDateTime
|
||||
&& lhs.ftCreationTime.dwHighDateTime == rhs.ftCreationTime.dwHighDateTime
|
||||
&& lhs.ftLastAccessTime.dwLowDateTime == rhs.ftLastAccessTime.dwLowDateTime
|
||||
&& lhs.ftLastAccessTime.dwHighDateTime == rhs.ftLastAccessTime.dwHighDateTime
|
||||
&& lhs.ftLastWriteTime.dwLowDateTime == rhs.ftLastWriteTime.dwLowDateTime
|
||||
&& lhs.ftLastWriteTime.dwHighDateTime == rhs.ftLastWriteTime.dwHighDateTime
|
||||
}
|
||||
}
|
||||
|
||||
extension FileInfo.Timespec {
|
||||
/// Converts a Win32 `FILETIME` (100 ns ticks since 1601-01-01 UTC) into a
|
||||
/// POSIX timespec (seconds and nanoseconds since 1970-01-01 UTC).
|
||||
init(_ filetime: FILETIME) {
|
||||
// The number of 100 ns ticks between 1601-01-01 and 1970-01-01.
|
||||
let epochDelta: Int64 = 116_444_736_000_000_000
|
||||
let ticks =
|
||||
Int64(bitPattern: UInt64(filetime.dwHighDateTime) << 32 | UInt64(filetime.dwLowDateTime))
|
||||
- epochDelta
|
||||
self.init(
|
||||
seconds: Int(ticks / 10_000_000),
|
||||
nanoseconds: Int(ticks % 10_000_000) * 100
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#endif // os(Windows)
|
||||
@@ -848,6 +848,8 @@ extension SystemFileHandle.SendableView {
|
||||
flags: materialization.exclusive ? [.exclusive] : []
|
||||
)
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("file materialization via rename is unavailable on Windows")
|
||||
#endif
|
||||
|
||||
if materialization.exclusive {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin) || os(Linux) || os(Android)
|
||||
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
|
||||
import NIOCore
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if canImport(Darwin) || os(Linux) || os(Android)
|
||||
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
|
||||
import NIOCore
|
||||
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
|
||||
@@ -567,6 +567,8 @@ private struct DirectoryEnumerator: Sendable {
|
||||
// Empty is checked for above, root can't exist within a directory, and directory
|
||||
// items must be a single path component.
|
||||
name = FilePath.Component(platformString: CNIODarwin_dirent_dname(entry))!
|
||||
#elseif os(Windows)
|
||||
name = FilePath.Component(platformString: CNIOWindows_dirent_dname(entry))!
|
||||
#else
|
||||
name = FilePath.Component(platformString: CNIOLinux_dirent_dname(entry))!
|
||||
#endif
|
||||
|
||||
@@ -77,6 +77,17 @@ public struct FileInfo: Hashable, Sendable {
|
||||
/// Creates a ``FileInfo`` by deriving values from a platform-specific value.
|
||||
public init(platformSpecificStatus: CInterop.Stat) {
|
||||
self._platformSpecificStatus = Stat(platformSpecificStatus)
|
||||
#if os(Windows)
|
||||
self.type = FileType(platformSpecificMode: platformSpecificStatus.nioMode)
|
||||
self.permissions = FilePermissions(masking: platformSpecificStatus.nioMode)
|
||||
self.size = platformSpecificStatus.nioSize
|
||||
// Windows has no POSIX owner/group; report the defaults.
|
||||
self.userID = UserID(rawValue: 0)
|
||||
self.groupID = GroupID(rawValue: 0)
|
||||
self.lastAccessTime = platformSpecificStatus.nioLastAccessTime
|
||||
self.lastDataModificationTime = platformSpecificStatus.nioLastDataModificationTime
|
||||
self.lastStatusChangeTime = platformSpecificStatus.nioLastStatusChangeTime
|
||||
#else
|
||||
self.type = FileType(platformSpecificMode: CInterop.Mode(platformSpecificStatus.st_mode))
|
||||
self.permissions = FilePermissions(masking: CInterop.Mode(platformSpecificStatus.st_mode))
|
||||
self.size = Int64(platformSpecificStatus.st_size)
|
||||
@@ -92,6 +103,7 @@ public struct FileInfo: Hashable, Sendable {
|
||||
self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
|
||||
self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Creates a ``FileInfo`` from the provided values.
|
||||
@@ -149,7 +161,7 @@ extension FileInfo {
|
||||
|
||||
/// A time interval consisting of whole seconds and nanoseconds.
|
||||
public struct Timespec: Hashable, Sendable {
|
||||
#if canImport(Darwin)
|
||||
#if canImport(Darwin) || os(Windows)
|
||||
private static let utimeOmit = Int(UTIME_OMIT)
|
||||
private static let utimeNow = Int(UTIME_NOW)
|
||||
#elseif canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
@@ -200,6 +212,9 @@ private struct Stat: Hashable {
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
let stat = self.stat
|
||||
#if os(Windows)
|
||||
stat.nioHash(into: &hasher)
|
||||
#else
|
||||
// Different platforms have different values; these are
|
||||
// common between Darwin and Glibc.
|
||||
hasher.combine(stat.st_dev)
|
||||
@@ -225,6 +240,7 @@ private struct Stat: Hashable {
|
||||
hasher.combine(FileInfo.Timespec(stat.st_mtim))
|
||||
hasher.combine(FileInfo.Timespec(stat.st_ctim))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -232,6 +248,9 @@ private struct Stat: Hashable {
|
||||
let lStat = lhs.stat
|
||||
let rStat = rhs.stat
|
||||
|
||||
#if os(Windows)
|
||||
return CInterop.Stat.nioIsEqual(lStat, rStat)
|
||||
#else
|
||||
// Different platforms have different values; these are
|
||||
// common between Darwin and Glibc.
|
||||
var isEqual = lStat.st_dev == rStat.st_dev
|
||||
@@ -268,6 +287,7 @@ private struct Stat: Hashable {
|
||||
#endif
|
||||
|
||||
return isEqual
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1344,6 +1344,8 @@ extension FileSystem {
|
||||
}
|
||||
}
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("_copyRegularFile is unavailable on Windows")
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1727,6 +1729,8 @@ extension FileSystem {
|
||||
)
|
||||
return .failure(error)
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("_copySymbolicLink is unavailable on Windows")
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ extension FileType {
|
||||
/// Initializes a file type from the `d_type` from `dirent`.
|
||||
@_spi(Testing)
|
||||
public init(direntType: UInt8) {
|
||||
#if canImport(Darwin) || canImport(Musl) || os(Android)
|
||||
#if canImport(Darwin) || canImport(Musl) || os(Android) || os(Windows)
|
||||
let value = Int32(direntType)
|
||||
#elseif canImport(Glibc)
|
||||
let value = Int(direntType)
|
||||
|
||||
@@ -26,6 +26,8 @@ import CNIOLinux
|
||||
#elseif canImport(Android)
|
||||
@preconcurrency import Android
|
||||
import CNIOLinux
|
||||
#elseif os(Windows)
|
||||
import CNIOWindows
|
||||
#endif
|
||||
|
||||
/// Aliases for platform-dependent types used for system calls.
|
||||
@@ -38,6 +40,11 @@ extension CInterop {
|
||||
public typealias Stat = Musl.stat
|
||||
#elseif canImport(Android)
|
||||
public typealias Stat = Android.stat
|
||||
#elseif os(Windows)
|
||||
// `BY_HANDLE_FILE_INFORMATION` is the Windows analogue of `struct stat`; it
|
||||
// is what `GetFileInformationByHandle` fills in. As on other platforms this
|
||||
// is re-exposed via `FileInfo.platformSpecificStatus`.
|
||||
public typealias Stat = BY_HANDLE_FILE_INFORMATION
|
||||
#endif
|
||||
|
||||
#if canImport(Darwin)
|
||||
@@ -52,6 +59,11 @@ extension CInterop {
|
||||
#elseif canImport(Android)
|
||||
@_spi(Testing)
|
||||
public static let maxPathLength = Android.PATH_MAX
|
||||
#elseif os(Windows)
|
||||
// The maximum length of an extended-length (`\\?\`-prefixed) path, which is
|
||||
// longer than the legacy `MAX_PATH` (260) limit.
|
||||
@_spi(Testing)
|
||||
public static let maxPathLength = Int32(Int16.max)
|
||||
#endif
|
||||
|
||||
#if canImport(Darwin)
|
||||
|
||||
@@ -36,6 +36,8 @@ extension Errno {
|
||||
return Errno(rawValue: Musl.errno)
|
||||
#elseif canImport(Android)
|
||||
return Errno(rawValue: Android.errno)
|
||||
#elseif os(Windows)
|
||||
return Errno(rawValue: _nio_fs_errno)
|
||||
#endif
|
||||
}
|
||||
set {
|
||||
@@ -47,6 +49,8 @@ extension Errno {
|
||||
Musl.errno = newValue.rawValue
|
||||
#elseif canImport(Android)
|
||||
Android.errno = newValue.rawValue
|
||||
#elseif os(Windows)
|
||||
_nio_fs_errno = newValue.rawValue
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -60,6 +64,8 @@ extension Errno {
|
||||
Musl.errno = 0
|
||||
#elseif canImport(Android)
|
||||
Android.errno = 0
|
||||
#elseif os(Windows)
|
||||
_nio_fs_errno = 0
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ 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 {
|
||||
|
||||
@@ -300,6 +300,11 @@ 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
|
||||
@@ -343,19 +348,31 @@ extension String {
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
// Need to #if because CChar may be signed
|
||||
try withCString(body)
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ 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
|
||||
|
||||
@_spi(Testing)
|
||||
|
||||
@@ -193,13 +193,15 @@ internal func system_flistxattr(
|
||||
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<CChar>,
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ value: UnsafeMutableRawPointer?,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
@@ -215,13 +217,15 @@ internal func system_fgetxattr(
|
||||
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<CChar>,
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ value: UnsafeRawPointer?,
|
||||
_ size: Int
|
||||
) -> CInt {
|
||||
@@ -237,13 +241,15 @@ internal func system_fsetxattr(
|
||||
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<CChar>
|
||||
_ name: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
if mockingEnabled {
|
||||
@@ -256,6 +262,8 @@ internal func system_fremovexattr(
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2026 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if os(Windows)
|
||||
|
||||
// Windows-only compatibility shim for the NIOFileSystem family.
|
||||
//
|
||||
// This is a *compile-only* port: it supplies the POSIX-shaped types, constants
|
||||
// and free functions the rest of the target expects on Windows so the file
|
||||
// system targets build. The functions that map onto Win32 are stubbed with
|
||||
// `fatalError(...)` here; real implementations land in follow-up PRs. The stubs
|
||||
// deliberately match the bare-libc signatures the `system_*` / `libc_*`
|
||||
// wrappers call, so those wrappers (and their non-Windows call sites) need no
|
||||
// changes.
|
||||
//
|
||||
// Everything here is defined at module scope so, thanks to same-module
|
||||
// visibility, the rest of the target can use these types, constants and
|
||||
// free-functions without importing anything.
|
||||
|
||||
import CNIOWindows
|
||||
import SystemPackage
|
||||
|
||||
// MARK: - Scalar types
|
||||
|
||||
/// POSIX `off_t`.
|
||||
typealias off_t = Int64
|
||||
|
||||
/// POSIX `timespec`.
|
||||
///
|
||||
/// Defined locally rather than reused from ucrt. ucrt does declare a
|
||||
/// `struct timespec`, but its `tv_nsec` is a C `long` (32-bit under Windows'
|
||||
/// LLP64 model), whereas the shared `FileInfo.Timespec` initializer assigns
|
||||
/// `tv_sec`/`tv_nsec` straight into `Int` fields. Declaring a POSIX-shaped
|
||||
/// `timespec` with `Int` fields lets the cross-platform `FileInfo` and
|
||||
/// `futimens` code compile unchanged, and lets the struct be `Sendable`
|
||||
/// (the imported C type is not).
|
||||
public struct timespec: Sendable {
|
||||
public var tv_sec: Int
|
||||
public var tv_nsec: Int
|
||||
|
||||
public init(tv_sec: Int = 0, tv_nsec: Int = 0) {
|
||||
self.tv_sec = tv_sec
|
||||
self.tv_nsec = tv_nsec
|
||||
}
|
||||
}
|
||||
|
||||
/// POSIX `pthread_key_t`.
|
||||
typealias pthread_key_t = UInt32
|
||||
|
||||
// MARK: - `CInterop.Stat` and friends
|
||||
//
|
||||
// `CInterop.Stat` itself is `BY_HANDLE_FILE_INFORMATION`, aliased in
|
||||
// `CInterop.swift` alongside the other platforms. The bridging that turns it
|
||||
// into the POSIX-shaped values the shared `FileInfo` code reads lives at the
|
||||
// bottom of this file.
|
||||
|
||||
extension CInterop {
|
||||
/// Directory-stream handle. Matches the Linux representation (opaque).
|
||||
typealias DirPointer = OpaquePointer
|
||||
|
||||
/// Windows stand-in for `struct dirent`. `d_name` is a fixed-size tuple of
|
||||
/// `CChar` so the existing `.0/.1/.2` "is this '.' or '..'" check compiles.
|
||||
struct WindowsDirEnt {
|
||||
var d_type: UInt8 = 0
|
||||
var d_name:
|
||||
(
|
||||
CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar,
|
||||
CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar
|
||||
) = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
typealias DirEnt = WindowsDirEnt
|
||||
|
||||
/// Opaque FTS handle.
|
||||
typealias FTS = OpaquePointer
|
||||
|
||||
/// Windows stand-in for `FTSENT`. Only the fields NIOFS reads.
|
||||
struct WindowsFTSEnt {
|
||||
var fts_info: UInt16 = 0
|
||||
var fts_errno: CInt = 0
|
||||
var fts_path: UnsafeMutablePointer<CInterop.PlatformChar>? = nil
|
||||
|
||||
init() {}
|
||||
}
|
||||
|
||||
typealias FTSEnt = WindowsFTSEnt
|
||||
}
|
||||
|
||||
// MARK: - swift-system gap extensions
|
||||
|
||||
extension FileDescriptor.OpenOptions {
|
||||
// Distinct high bits we own. These are placeholders for a real Win32
|
||||
// mapping in the future implementation.
|
||||
static var noFollow: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x4000_0000)
|
||||
}
|
||||
static var closeOnExec: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x2000_0000)
|
||||
}
|
||||
static var nonBlocking: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x1000_0000)
|
||||
}
|
||||
static var directory: FileDescriptor.OpenOptions {
|
||||
FileDescriptor.OpenOptions(rawValue: 0x0800_0000)
|
||||
}
|
||||
}
|
||||
|
||||
extension Errno {
|
||||
/// No direct equivalent on Windows; alias to `EINVAL` so switches stay
|
||||
/// exhaustive.
|
||||
static var noData: Errno { Errno.invalidArgument }
|
||||
}
|
||||
|
||||
// MARK: - `stat` mode constants (typed to `CInterop.Mode`)
|
||||
|
||||
let S_IFMT: CInterop.Mode = 0o170000
|
||||
let S_IFSOCK: CInterop.Mode = 0o140000
|
||||
let S_IFLNK: CInterop.Mode = 0o120000
|
||||
let S_IFREG: CInterop.Mode = 0o100000
|
||||
let S_IFBLK: CInterop.Mode = 0o060000
|
||||
let S_IFDIR: CInterop.Mode = 0o040000
|
||||
let S_IFCHR: CInterop.Mode = 0o020000
|
||||
let S_IFIFO: CInterop.Mode = 0o010000
|
||||
|
||||
// MARK: - `dirent` `d_type` constants (typed to `CInt`)
|
||||
|
||||
let DT_UNKNOWN: CInt = 0
|
||||
let DT_FIFO: CInt = 1
|
||||
let DT_CHR: CInt = 2
|
||||
let DT_DIR: CInt = 4
|
||||
let DT_BLK: CInt = 6
|
||||
let DT_REG: CInt = 8
|
||||
let DT_LNK: CInt = 10
|
||||
let DT_SOCK: CInt = 12
|
||||
|
||||
// MARK: - FTS constants (typed to `CInt`)
|
||||
|
||||
let FTS_D: CInt = 1
|
||||
let FTS_DC: CInt = 2
|
||||
let FTS_DEFAULT: CInt = 3
|
||||
let FTS_DNR: CInt = 4
|
||||
let FTS_DOT: CInt = 5
|
||||
let FTS_DP: CInt = 6
|
||||
let FTS_ERR: CInt = 7
|
||||
let FTS_F: CInt = 8
|
||||
let FTS_NS: CInt = 10
|
||||
let FTS_NSOK: CInt = 11
|
||||
let FTS_SL: CInt = 12
|
||||
let FTS_SLNONE: CInt = 13
|
||||
|
||||
let FTS_PHYSICAL: CInt = 0x0010
|
||||
let FTS_LOGICAL: CInt = 0x0002
|
||||
let FTS_NOCHDIR: CInt = 0x0004
|
||||
|
||||
// MARK: - `UTIME_*` sentinels
|
||||
|
||||
let UTIME_NOW: CInt = (1 << 30) - 1
|
||||
let UTIME_OMIT: CInt = (1 << 30) - 2
|
||||
|
||||
// MARK: - errno access
|
||||
|
||||
// `errno` is a macro on the Windows CRT that Swift cannot read or assign
|
||||
// directly, so bridge through the `CNIOWindows` C shim, which expands the macro
|
||||
// in C. This is the real thread-local `errno` backing the CRT calls the higher
|
||||
// level file-system code inspects.
|
||||
var _nio_fs_errno: CInt {
|
||||
get { CNIOWindows_errno() }
|
||||
set { CNIOWindows_set_errno(newValue) }
|
||||
}
|
||||
|
||||
// MARK: - libc string helpers (stubs / trivial)
|
||||
|
||||
func strlen(_ s: UnsafePointer<CChar>) -> Int {
|
||||
var length = 0
|
||||
while s[length] != 0 { length += 1 }
|
||||
return length
|
||||
}
|
||||
|
||||
func strlen(_ s: UnsafeMutablePointer<CChar>) -> Int {
|
||||
strlen(UnsafePointer(s))
|
||||
}
|
||||
|
||||
func strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
|
||||
var length = 0
|
||||
while s[length] != 0 { length += 1 }
|
||||
return length
|
||||
}
|
||||
|
||||
func strerror(_ code: CInt) -> UnsafeMutablePointer<CChar>? {
|
||||
fatalError("strerror is unavailable on Windows")
|
||||
}
|
||||
|
||||
func memset(_ b: UnsafeMutableRawPointer, _ c: CInt, _ len: Int) -> UnsafeMutableRawPointer {
|
||||
b.initializeMemory(as: UInt8.self, repeating: UInt8(truncatingIfNeeded: c), count: len)
|
||||
return b
|
||||
}
|
||||
|
||||
func getenv(_ name: UnsafePointer<CChar>) -> UnsafeMutablePointer<CChar>? {
|
||||
fatalError("getenv is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - POSIX file-system syscall stubs
|
||||
//
|
||||
// These match the bare-libc call signatures used by the `system_*` / `libc_*`
|
||||
// wrappers, so those wrappers need no edits. All are stubs.
|
||||
|
||||
func openat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: CInt
|
||||
) -> CInt {
|
||||
fatalError("openat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func openat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: CInt,
|
||||
_ mode: CInterop.Mode
|
||||
) -> CInt {
|
||||
fatalError("openat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func stat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("stat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func lstat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("lstat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fstat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ info: UnsafeMutablePointer<CInterop.Stat>
|
||||
) -> CInt {
|
||||
fatalError("fstat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fchmod(_ fd: FileDescriptor.RawValue, _ mode: CInterop.Mode) -> CInt {
|
||||
fatalError("fchmod is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fsync(_ fd: FileDescriptor.RawValue) -> CInt {
|
||||
fatalError("fsync is unavailable on Windows")
|
||||
}
|
||||
|
||||
func mkdir(_ path: UnsafePointer<CInterop.PlatformChar>, _ mode: CInterop.Mode) -> CInt {
|
||||
fatalError("mkdir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func symlink(
|
||||
_ destination: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ source: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("symlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func symlinkat(
|
||||
_ destination: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ dirfd: FileDescriptor.RawValue,
|
||||
_ source: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("symlinkat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func readlink(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
fatalError("readlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func rename(
|
||||
_ old: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ new: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("rename is unavailable on Windows")
|
||||
}
|
||||
|
||||
func link(
|
||||
_ old: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ new: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
fatalError("link is unavailable on Windows")
|
||||
}
|
||||
|
||||
func unlink(_ path: UnsafePointer<CInterop.PlatformChar>) -> CInt {
|
||||
fatalError("unlink is unavailable on Windows")
|
||||
}
|
||||
|
||||
func unlinkat(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ flags: CInt
|
||||
) -> CInt {
|
||||
fatalError("unlinkat is unavailable on Windows")
|
||||
}
|
||||
|
||||
func futimens(
|
||||
_ fd: FileDescriptor.RawValue,
|
||||
_ times: UnsafePointer<timespec>?
|
||||
) -> CInt {
|
||||
fatalError("futimens is unavailable on Windows")
|
||||
}
|
||||
|
||||
func remove(_ path: UnsafePointer<CInterop.PlatformChar>) -> CInt {
|
||||
fatalError("remove is unavailable on Windows")
|
||||
}
|
||||
|
||||
func getcwd(
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> UnsafeMutablePointer<CInterop.PlatformChar>? {
|
||||
fatalError("getcwd is unavailable on Windows")
|
||||
}
|
||||
|
||||
func confstr(
|
||||
_ name: CInt,
|
||||
_ buffer: UnsafeMutablePointer<CInterop.PlatformChar>,
|
||||
_ size: Int
|
||||
) -> Int {
|
||||
fatalError("confstr is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fdopendir(_ fd: FileDescriptor.RawValue) -> CInterop.DirPointer? {
|
||||
fatalError("fdopendir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func readdir(_ dir: CInterop.DirPointer) -> UnsafeMutablePointer<CInterop.DirEnt>? {
|
||||
fatalError("readdir is unavailable on Windows")
|
||||
}
|
||||
|
||||
func closedir(_ dir: CInterop.DirPointer) -> CInt {
|
||||
fatalError("closedir is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - FTS stubs
|
||||
|
||||
func fts_open(
|
||||
_ path: [UnsafeMutablePointer<CInterop.PlatformChar>?],
|
||||
_ options: CInt,
|
||||
_ compare: UnsafeRawPointer?
|
||||
) -> UnsafeMutablePointer<CInterop.FTS>? {
|
||||
fatalError("fts_open is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fts_read(
|
||||
_ fts: UnsafeMutablePointer<CInterop.FTS>
|
||||
) -> UnsafeMutablePointer<CInterop.FTSEnt>? {
|
||||
fatalError("fts_read is unavailable on Windows")
|
||||
}
|
||||
|
||||
func fts_close(_ fts: UnsafeMutablePointer<CInterop.FTS>) -> CInt {
|
||||
fatalError("fts_close is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - dirent name accessor
|
||||
|
||||
/// Windows equivalent of `CNIOLinux_dirent_dname` / `CNIODarwin_dirent_dname`.
|
||||
func CNIOWindows_dirent_dname(
|
||||
_ entry: UnsafeMutablePointer<CInterop.DirEnt>
|
||||
) -> UnsafePointer<CInterop.PlatformChar> {
|
||||
fatalError("CNIOWindows_dirent_dname is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - pthread TLS stubs
|
||||
|
||||
func pthread_key_create(
|
||||
_ key: UnsafeMutablePointer<pthread_key_t>,
|
||||
_ destructor: (@convention(c) (UnsafeMutableRawPointer?) -> Void)?
|
||||
) -> CInt {
|
||||
fatalError("pthread_key_create is unavailable on Windows")
|
||||
}
|
||||
|
||||
func pthread_setspecific(_ key: pthread_key_t, _ value: UnsafeRawPointer?) -> CInt {
|
||||
fatalError("pthread_setspecific is unavailable on Windows")
|
||||
}
|
||||
|
||||
func pthread_getspecific(_ key: pthread_key_t) -> UnsafeMutableRawPointer? {
|
||||
fatalError("pthread_getspecific is unavailable on Windows")
|
||||
}
|
||||
|
||||
// MARK: - `BY_HANDLE_FILE_INFORMATION` -> `FileInfo` bridging
|
||||
//
|
||||
// `CInterop.Stat` is `BY_HANDLE_FILE_INFORMATION` on Windows. These helpers
|
||||
// translate it into the POSIX-shaped values the shared `FileInfo` code reads,
|
||||
// keeping the Win32 specifics (and the `CNIOWindows`/WinSDK dependency) out of
|
||||
// `FileInfo.swift`.
|
||||
|
||||
extension CInterop.Stat {
|
||||
/// A POSIX-style `st_mode` synthesised from `dwFileAttributes`.
|
||||
///
|
||||
/// This mirrors the Windows CRT's `stat` (`convert_to_stat_mode`): a
|
||||
/// directory maps to `S_IFDIR` and gains the execute bits, anything else is
|
||||
/// a regular file, and the read/write bits follow `FILE_ATTRIBUTE_READONLY`,
|
||||
/// replicated across owner/group/other. The CRT's executable-by-extension
|
||||
/// heuristic is intentionally dropped: it needs the path, which isn't part
|
||||
/// of `BY_HANDLE_FILE_INFORMATION`.
|
||||
var nioMode: CInterop.Mode {
|
||||
var mode: CInterop.Mode =
|
||||
(self.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY)) != 0
|
||||
? S_IFDIR | 0o100
|
||||
: S_IFREG
|
||||
mode |=
|
||||
(self.dwFileAttributes & DWORD(FILE_ATTRIBUTE_READONLY)) != 0
|
||||
? 0o400
|
||||
: 0o600
|
||||
mode |= (mode & 0o700) >> 3
|
||||
mode |= (mode & 0o700) >> 6
|
||||
return mode
|
||||
}
|
||||
|
||||
/// The file size assembled from its high and low halves.
|
||||
var nioSize: Int64 {
|
||||
Int64(self.nFileSizeHigh) << 32 | Int64(self.nFileSizeLow)
|
||||
}
|
||||
|
||||
var nioLastAccessTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftLastAccessTime)
|
||||
}
|
||||
|
||||
var nioLastDataModificationTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftLastWriteTime)
|
||||
}
|
||||
|
||||
/// Windows has no POSIX status-change time; the creation time is the closest
|
||||
/// analogue.
|
||||
var nioLastStatusChangeTime: FileInfo.Timespec {
|
||||
FileInfo.Timespec(self.ftCreationTime)
|
||||
}
|
||||
|
||||
func nioHash(into hasher: inout Hasher) {
|
||||
hasher.combine(self.dwFileAttributes)
|
||||
hasher.combine(self.dwVolumeSerialNumber)
|
||||
hasher.combine(self.nFileSizeHigh)
|
||||
hasher.combine(self.nFileSizeLow)
|
||||
hasher.combine(self.nNumberOfLinks)
|
||||
hasher.combine(self.nFileIndexHigh)
|
||||
hasher.combine(self.nFileIndexLow)
|
||||
hasher.combine(self.ftCreationTime.dwLowDateTime)
|
||||
hasher.combine(self.ftCreationTime.dwHighDateTime)
|
||||
hasher.combine(self.ftLastAccessTime.dwLowDateTime)
|
||||
hasher.combine(self.ftLastAccessTime.dwHighDateTime)
|
||||
hasher.combine(self.ftLastWriteTime.dwLowDateTime)
|
||||
hasher.combine(self.ftLastWriteTime.dwHighDateTime)
|
||||
}
|
||||
|
||||
static func nioIsEqual(_ lhs: CInterop.Stat, _ rhs: CInterop.Stat) -> Bool {
|
||||
lhs.dwFileAttributes == rhs.dwFileAttributes
|
||||
&& lhs.dwVolumeSerialNumber == rhs.dwVolumeSerialNumber
|
||||
&& lhs.nFileSizeHigh == rhs.nFileSizeHigh
|
||||
&& lhs.nFileSizeLow == rhs.nFileSizeLow
|
||||
&& lhs.nNumberOfLinks == rhs.nNumberOfLinks
|
||||
&& lhs.nFileIndexHigh == rhs.nFileIndexHigh
|
||||
&& lhs.nFileIndexLow == rhs.nFileIndexLow
|
||||
&& lhs.ftCreationTime.dwLowDateTime == rhs.ftCreationTime.dwLowDateTime
|
||||
&& lhs.ftCreationTime.dwHighDateTime == rhs.ftCreationTime.dwHighDateTime
|
||||
&& lhs.ftLastAccessTime.dwLowDateTime == rhs.ftLastAccessTime.dwLowDateTime
|
||||
&& lhs.ftLastAccessTime.dwHighDateTime == rhs.ftLastAccessTime.dwHighDateTime
|
||||
&& lhs.ftLastWriteTime.dwLowDateTime == rhs.ftLastWriteTime.dwLowDateTime
|
||||
&& lhs.ftLastWriteTime.dwHighDateTime == rhs.ftLastWriteTime.dwHighDateTime
|
||||
}
|
||||
}
|
||||
|
||||
extension FileInfo.Timespec {
|
||||
/// Converts a Win32 `FILETIME` (100 ns ticks since 1601-01-01 UTC) into a
|
||||
/// POSIX timespec (seconds and nanoseconds since 1970-01-01 UTC).
|
||||
init(_ filetime: FILETIME) {
|
||||
// The number of 100 ns ticks between 1601-01-01 and 1970-01-01.
|
||||
let epochDelta: Int64 = 116_444_736_000_000_000
|
||||
let ticks =
|
||||
Int64(bitPattern: UInt64(filetime.dwHighDateTime) << 32 | UInt64(filetime.dwLowDateTime))
|
||||
- epochDelta
|
||||
self.init(
|
||||
seconds: Int(ticks / 10_000_000),
|
||||
nanoseconds: Int(ticks % 10_000_000) * 100
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#endif // os(Windows)
|
||||
@@ -848,6 +848,8 @@ extension SystemFileHandle.SendableView {
|
||||
flags: materialization.exclusive ? [.exclusive] : []
|
||||
)
|
||||
}
|
||||
#elseif os(Windows)
|
||||
fatalError("file materialization via rename is unavailable on Windows")
|
||||
#endif
|
||||
|
||||
if materialization.exclusive {
|
||||
|
||||
Reference in new issue
Block a user