From b4e4f0211f04a7b596c1dbcbefd00109732d2f7d Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 7 Jun 2022 06:08:32 -0700 Subject: [PATCH] NIOCore: adjust system call helpers for Windows (#2155) Windows does not provide a 3 parameter `open` and needs to be passed all the parameters as `_open` is a variadic function (as per the specification). This provides a Windows specific path for `open` and `read` as on Darwin and Linux, `read` uses a non-standard return type (`ssize_t`). As it happens, the `size` parameter on Windows also uses `unsigned int` rather than `size_t` which would break on different bitnesses. Co-authored-by: Cory Benfield --- Sources/NIOCore/SystemCallHelpers.swift | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Sources/NIOCore/SystemCallHelpers.swift b/Sources/NIOCore/SystemCallHelpers.swift index 89a768f61..703dfcdcf 100644 --- a/Sources/NIOCore/SystemCallHelpers.swift +++ b/Sources/NIOCore/SystemCallHelpers.swift @@ -29,11 +29,18 @@ import CNIOWindows #error("bad os") #endif +#if os(Windows) +private let sysDup: @convention(c) (CInt) -> CInt = _dup +private let sysClose: @convention(c) (CInt) -> CInt = _close +private let sysLseek: @convention(c) (CInt, off_t, CInt) -> off_t = _lseek +private let sysRead: @convention(c) (CInt, UnsafeMutableRawPointer?, CUnsignedInt) -> CInt = _read +#else private let sysDup: @convention(c) (CInt) -> CInt = dup private let sysClose: @convention(c) (CInt) -> CInt = close private let sysOpenWithMode: @convention(c) (UnsafePointer, CInt, NIOPOSIXFileMode) -> CInt = open private let sysLseek: @convention(c) (CInt, off_t, CInt) -> off_t = lseek private let sysRead: @convention(c) (CInt, UnsafeMutableRawPointer?, size_t) -> size_t = read +#endif private let sysIfNameToIndex: @convention(c) (UnsafePointer?) -> CUnsignedInt = if_nametoindex #if !os(Windows) @@ -122,10 +129,19 @@ enum SystemCalls { } @inline(never) - internal static func open(file: UnsafePointer, oFlag: CInt, mode: NIOPOSIXFileMode) throws -> CInt { + internal static func open(file: UnsafePointer, oFlag: CInt, + mode: NIOPOSIXFileMode) throws -> CInt { +#if os(Windows) + return try syscall(blocking: false) { + var fh: CInt = -1 + let _ = ucrt._sopen_s(&fh, file, oFlag, _SH_DENYNO, mode) + return fh + }.result +#else return try syscall(blocking: false) { sysOpenWithMode(file, oFlag, mode) }.result +#endif } @discardableResult @@ -136,12 +152,21 @@ enum SystemCalls { }.result } +#if os(Windows) + @inline(never) + internal static func read(descriptor: CInt, pointer: UnsafeMutableRawPointer, size: CUnsignedInt) throws -> CoreIOResult { + return try syscall(blocking: true) { + sysRead(descriptor, pointer, size) + } + } +#else @inline(never) internal static func read(descriptor: CInt, pointer: UnsafeMutableRawPointer, size: size_t) throws -> CoreIOResult { return try syscall(blocking: true) { sysRead(descriptor, pointer, size) } } +#endif @inline(never) internal static func if_nametoindex(_ name: UnsafePointer?) throws -> CUnsignedInt {