From 61a2c52d1e15c09018efe60c8c5a52540f112cfa Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Thu, 25 May 2023 02:20:40 +0800 Subject: [PATCH] Make `fcntl` defined on all platforms (#13495) --- src/crystal/system/wasi/file_descriptor.cr | 6 ++++++ src/crystal/system/win32/file_descriptor.cr | 4 ++++ src/crystal/system/win32/socket.cr | 4 +--- src/io/file_descriptor.cr | 14 ++++++-------- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/crystal/system/wasi/file_descriptor.cr b/src/crystal/system/wasi/file_descriptor.cr index 9a2bc1a0bd96..6eae9b8832d2 100644 --- a/src/crystal/system/wasi/file_descriptor.cr +++ b/src/crystal/system/wasi/file_descriptor.cr @@ -11,6 +11,12 @@ module Crystal::System::FileDescriptor raise NotImplementedError.new "Crystal::System::FileDescriptor.pipe" end + def self.fcntl(fd, cmd, arg = 0) + r = LibC.fcntl(fd, cmd, arg) + raise IO::Error.from_errno("fcntl() failed") if r == -1 + r + end + private def system_blocking_init(value) end diff --git a/src/crystal/system/win32/file_descriptor.cr b/src/crystal/system/win32/file_descriptor.cr index 2bc4a7d55b37..84cfe12c6650 100644 --- a/src/crystal/system/win32/file_descriptor.cr +++ b/src/crystal/system/win32/file_descriptor.cr @@ -79,6 +79,10 @@ module Crystal::System::FileDescriptor false end + def self.fcntl(fd, cmd, arg = 0) + raise NotImplementedError.new "Crystal::System::FileDescriptor.fcntl" + end + private def windows_handle FileDescriptor.windows_handle!(fd) end diff --git a/src/crystal/system/win32/socket.cr b/src/crystal/system/win32/socket.cr index 86a787cef1d3..88278544dbf0 100644 --- a/src/crystal/system/win32/socket.cr +++ b/src/crystal/system/win32/socket.cr @@ -438,9 +438,7 @@ module Crystal::System::Socket end def self.fcntl(fd, cmd, arg = 0) - ret = LibC.fcntl fd, cmd, arg - raise Socket::Error.from_errno("fcntl() failed") if ret == -1 - ret + raise NotImplementedError.new "Crystal::System::Socket.fcntl" end private def system_tty? diff --git a/src/io/file_descriptor.cr b/src/io/file_descriptor.cr index 2929a62a410a..c367d2ecaf74 100644 --- a/src/io/file_descriptor.cr +++ b/src/io/file_descriptor.cr @@ -56,15 +56,13 @@ class IO::FileDescriptor < IO self.system_close_on_exec = value end - {% unless flag?(:win32) %} - def self.fcntl(fd, cmd, arg = 0) - Crystal::System::FileDescriptor.fcntl(fd, cmd, arg) - end + def self.fcntl(fd, cmd, arg = 0) + Crystal::System::FileDescriptor.fcntl(fd, cmd, arg) + end - def fcntl(cmd, arg = 0) - Crystal::System::FileDescriptor.fcntl(fd, cmd, arg) - end - {% end %} + def fcntl(cmd, arg = 0) + Crystal::System::FileDescriptor.fcntl(fd, cmd, arg) + end # Returns a `File::Info` object for this file descriptor, or raises # `IO::Error` in case of an error.