Skip to content

Commit

Permalink
Extract Win32 FFI code to lib/fluent/win32.rb
Browse files Browse the repository at this point in the history
Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Aug 9, 2022
1 parent 7d55f79 commit a6e6fed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
27 changes: 5 additions & 22 deletions lib/fluent/plugin/file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#

require 'fluent/win32'

module Fluent
module FileWrapper
def self.open(path, mode='r')
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions lib/fluent/win32.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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.
#

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

0 comments on commit a6e6fed

Please sign in to comment.