From c565d54847b6e149c0992fb8100fa7ccc6b73e64 Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Tue, 9 Aug 2022 11:31:48 +0900 Subject: [PATCH] Extract Win32 FFI code to lib/fluent/win32.rb Signed-off-by: Takuro Ashie --- lib/fluent/plugin/file_wrapper.rb | 27 ++++------------------ lib/fluent/win32.rb | 38 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 lib/fluent/win32.rb diff --git a/lib/fluent/plugin/file_wrapper.rb b/lib/fluent/plugin/file_wrapper.rb index e99928b11a..3314c1ddf0 100644 --- a/lib/fluent/plugin/file_wrapper.rb +++ b/lib/fluent/plugin/file_wrapper.rb @@ -14,6 +14,8 @@ # limitations under the License. # +require 'fluent/win32' + module Fluent module FileWrapper def self.open(path, mode='r') @@ -35,25 +37,6 @@ def self.stat(path) end end - module Win32API - require 'fiddle/import' - require 'fiddle/types' - extend Fiddle::Importer - - if RUBY_PLATFORM.split('-')[-1] == "ucrt" - MSVCRT_DLL = 'ucrtbase.dll' - else - MSVCRT_DLL = 'msvcrt.dll' - end - - dlload MSVCRT_DLL, "kernel32.dll" - include Fiddle::Win32Types - - extern "intptr_t _get_osfhandle(int)" - extern "BOOL GetFileInformationByHandle(HANDLE, void *)" - extern "BOOL GetFileInformationByHandleEx(HANDLE, int, void *, DWORD)" - end - class WindowsFile include File::Constants @@ -64,7 +47,7 @@ class WindowsFile def initialize(path, mode='r') @path = path @io = File.open(path, mode2flags(mode)) - @file_handle = Win32API._get_osfhandle(@io.to_i) + @file_handle = Win32._get_osfhandle(@io.to_i) @io.instance_variable_set(:@file_index, self.ino) def @io.ino @file_index @@ -84,7 +67,7 @@ def close def ino by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4) #72bytes - unless Win32API.GetFileInformationByHandle(@file_handle, by_handle_file_information) + unless Win32.GetFileInformationByHandle(@file_handle, by_handle_file_information) return 0 end @@ -138,7 +121,7 @@ def delete_pending bufsize = 1024 buf = '\0' * bufsize - unless Win32API.GetFileInformationByHandleEx(@file_handle, file_standard_info, buf, bufsize) + unless Win32.GetFileInformationByHandleEx(@file_handle, file_standard_info, buf, bufsize) return false end diff --git a/lib/fluent/win32.rb b/lib/fluent/win32.rb new file mode 100644 index 0000000000..1d89347d53 --- /dev/null +++ b/lib/fluent/win32.rb @@ -0,0 +1,38 @@ +# +# Fluentd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'fluent/env' + +module Fluent + module Win32 + require 'fiddle/import' + require 'fiddle/types' + extend Fiddle::Importer + + if RUBY_PLATFORM.split('-')[-1] == "ucrt" + MSVCRT_DLL = 'ucrtbase.dll' + else + MSVCRT_DLL = 'msvcrt.dll' + end + + dlload MSVCRT_DLL, "kernel32.dll" + include Fiddle::Win32Types + + extern "intptr_t _get_osfhandle(int)" + extern "BOOL GetFileInformationByHandle(HANDLE, void *)" + extern "BOOL GetFileInformationByHandleEx(HANDLE, int, void *, DWORD)" + end if Fluent.windows? +end