Import ucrt on Windows if available#294
Open
Joannis wants to merge 12 commits intoapple:mainfrom
Open
Conversation
Lukasa
reviewed
Nov 4, 2025
…between Windows and POSIX
Contributor
|
Looks like there are format tweaks that need to happen. |
Contributor
Author
|
@Lukasa I tried |
Contributor
|
It would probably be enough to apply this diff: diff --git a/Package.swift b/Package.swift
index 0707fe6..7d6d0dd 100644
--- a/Package.swift
+++ b/Package.swift
@@ -111,7 +111,7 @@ var targets: [PackageDescription.Target] = [
name: "CNIOExtrasZlib",
dependencies: [],
cSettings: [
- .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
+ .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
],
linkerSettings: [
.linkedLibrary("z", .when(platforms: [.linux, .macOS, .iOS, .tvOS, .watchOS, .visionOS])),
diff --git a/Sources/HTTPServerWithQuiescingDemo/main.swift b/Sources/HTTPServerWithQuiescingDemo/main.swift
index 41c1458..a84ba37 100644
--- a/Sources/HTTPServerWithQuiescingDemo/main.swift
+++ b/Sources/HTTPServerWithQuiescingDemo/main.swift
@@ -136,4 +136,4 @@ private func runServer() throws {
try runServer()
#else
print("This demo is not supported on Windows.")
-#endif
\ No newline at end of file
+#endif
diff --git a/Sources/NIOExtras/WritePCAPHandler.swift b/Sources/NIOExtras/WritePCAPHandler.swift
index 2e02cac..23c9978 100644
--- a/Sources/NIOExtras/WritePCAPHandler.swift
+++ b/Sources/NIOExtras/WritePCAPHandler.swift
@@ -26,21 +26,21 @@ import Android
import ucrt
import WinSDK
-fileprivate func gettimeofday(_ tp: inout timeval, _ tzp: Never?) {
+private func gettimeofday(_ tp: inout timeval, _ tzp: Never?) {
var file_time = FILETIME()
var system_time = SYSTEMTIME()
var time: UInt64 = 0
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
- // until 00:00:00 January 1, 1970
- let epoch: UInt64 = 116444736000000000
+ // until 00:00:00 January 1, 1970
+ let epoch: UInt64 = 116_444_736_000_000_000
- GetSystemTime(&system_time);
- SystemTimeToFileTime(&system_time, &file_time);
- time = UInt64(file_time.dwLowDateTime)
+ GetSystemTime(&system_time)
+ SystemTimeToFileTime(&system_time, &file_time)
+ time = UInt64(file_time.dwLowDateTime)
time += UInt64(file_time.dwHighDateTime) << 32
- tp.tv_sec = Int32((time - epoch) / 10000000)
+ tp.tv_sec = Int32((time - epoch) / 10_000_000)
tp.tv_usec = Int32(system_time.wMilliseconds * 1000)
}
#else
@@ -752,7 +752,14 @@ extension NIOWritePCAPHandler {
) throws -> SynchronizedFileSink {
#if os(Windows)
let fd = try path.withCString(encodedAs: UTF16.self) { pathPtr -> CInt in
- let fd = _wsopen_s(nil, pathPtr, _O_WRONLY | (fileWritingMode == FileWritingMode.createNewPCAPFile ? (_O_TRUNC | _O_CREAT) : _O_APPEND), _SH_DENYNO, _S_IREAD | _S_IWRITE)
+ let fd = _wsopen_s(
+ nil,
+ pathPtr,
+ _O_WRONLY
+ | (fileWritingMode == FileWritingMode.createNewPCAPFile ? (_O_TRUNC | _O_CREAT) : _O_APPEND),
+ _SH_DENYNO,
+ _S_IREAD | _S_IWRITE
+ )
guard fd >= 0 else {
throw SynchronizedFileSink.Error(errorCode: Error.ErrorCode.cannotOpenFileError.rawValue)
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This should enable NIOExtras to work on Windows, except for modules that depend on NIOSSL.