Skip to content

Commit 7d55f79

Browse files
committed
Remove dependency to win32-api gem
Recently we fail to load win32-api on Ruby 3.2-dev: https://github.com/fluent/fluentd/runs/7660210030?check_suite_focus=true D:/rubyinstaller-head-x64/lib/ruby/gems/3.2.0+2/gems/windows-api-0.4.5/lib/windows/api.rb:335:in `initialize': uninitialized constant Win32::API (NameError) @api = Win32::API.new(func, proto, rtype, dll) ^^^^^ from D:/rubyinstaller-head-x64/lib/ruby/gems/3.2.0+2/gems/windows-pr-1.2.6/lib/windows/msvcrt/string.rb:12:in `new' but Fluentd doesn't seem depend on win32-api so much. Using Fiddle is enough instead. It's time to drop win32-api gem. Signed-off-by: Takuro Ashie <[email protected]>
1 parent cf241f0 commit 7d55f79

File tree

6 files changed

+29
-37
lines changed

6 files changed

+29
-37
lines changed

fluentd.gemspec

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ Gem::Specification.new do |gem|
3434
fake_platform = ENV['GEM_BUILD_FAKE_PLATFORM'].to_s
3535
gem.platform = fake_platform unless fake_platform.empty?
3636
if /mswin|mingw/ =~ fake_platform || (/mswin|mingw/ =~ RUBY_PLATFORM && fake_platform.empty?)
37-
gem.add_runtime_dependency("win32-api", [">= 1.10", "< 2.0.0"])
3837
gem.add_runtime_dependency("win32-service", ["~> 2.3.0"])
3938
gem.add_runtime_dependency("win32-ipc", ["~> 0.7.0"])
4039
gem.add_runtime_dependency("win32-event", ["~> 0.6.3"])
41-
gem.add_runtime_dependency("windows-api", ["~> 0.4.5"])
42-
gem.add_runtime_dependency("windows-pr", ["~> 1.2.6"])
4340
gem.add_runtime_dependency("certstore_c", ["~> 0.1.7"])
4441
end
4542

lib/fluent/command/fluentd.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@
189189
}
190190

191191
if Fluent.windows?
192-
require 'windows/library'
193-
include Windows::Library
194-
195192
opts.merge!(
196193
:winsvc_name => 'fluentdwinsvc',
197194
:winsvc_display_name => 'Fluentd Windows Service',
@@ -292,9 +289,7 @@
292289
case winsvcinstmode
293290
when 'i'
294291
binary_path = File.join(File.dirname(__FILE__), "..")
295-
ruby_path = "\0" * 256
296-
GetModuleFileName.call(0,ruby_path,256)
297-
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
292+
ruby_path = ServerEngine.ruby_bin_path
298293
start_type = Service::DEMAND_START
299294
if opts[:regwinsvcautostart]
300295
start_type = Service::AUTO_START

lib/fluent/plugin/file_wrapper.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,36 @@ def self.stat(path)
3535
end
3636
end
3737

38-
class WindowsFile
39-
require 'windows/file'
40-
require 'windows/handle'
38+
module Win32API
39+
require 'fiddle/import'
40+
require 'fiddle/types'
41+
extend Fiddle::Importer
42+
43+
if RUBY_PLATFORM.split('-')[-1] == "ucrt"
44+
MSVCRT_DLL = 'ucrtbase.dll'
45+
else
46+
MSVCRT_DLL = 'msvcrt.dll'
47+
end
4148

49+
dlload MSVCRT_DLL, "kernel32.dll"
50+
include Fiddle::Win32Types
51+
52+
extern "intptr_t _get_osfhandle(int)"
53+
extern "BOOL GetFileInformationByHandle(HANDLE, void *)"
54+
extern "BOOL GetFileInformationByHandleEx(HANDLE, int, void *, DWORD)"
55+
end
56+
57+
class WindowsFile
4258
include File::Constants
43-
include Windows::File
44-
include Windows::Handle
4559

4660
attr_reader :io
4761

62+
INVALID_HANDLE_VALUE = -1
63+
4864
def initialize(path, mode='r')
4965
@path = path
5066
@io = File.open(path, mode2flags(mode))
51-
@file_handle = _get_osfhandle(@io.to_i)
67+
@file_handle = Win32API._get_osfhandle(@io.to_i)
5268
@io.instance_variable_set(:@file_index, self.ino)
5369
def @io.ino
5470
@file_index
@@ -68,7 +84,7 @@ def close
6884
def ino
6985
by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4) #72bytes
7086

71-
unless GetFileInformationByHandle.call(@file_handle, by_handle_file_information)
87+
unless Win32API.GetFileInformationByHandle(@file_handle, by_handle_file_information)
7288
return 0
7389
end
7490

@@ -122,7 +138,7 @@ def delete_pending
122138
bufsize = 1024
123139
buf = '\0' * bufsize
124140

125-
unless GetFileInformationByHandleEx.call(@file_handle, file_standard_info, buf, bufsize)
141+
unless Win32API.GetFileInformationByHandleEx(@file_handle, file_standard_info, buf, bufsize)
126142
return false
127143
end
128144

lib/fluent/supervisor.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
require 'serverengine'
3333

3434
if Fluent.windows?
35-
require 'windows/library'
36-
require 'windows/synchronize'
37-
require 'windows/system_info'
38-
include Windows::Library
39-
include Windows::Synchronize
40-
include Windows::SystemInfo
4135
require 'win32/ipc'
4236
require 'win32/event'
4337
end
@@ -235,7 +229,8 @@ def install_windows_event_handler
235229
end
236230
begin
237231
loop do
238-
ipc_idx = ipc.wait_any(events.map {|e| e[:win32_event]}, Windows::Synchronize::INFINITE)
232+
infinite = 0xFFFFFFFF
233+
ipc_idx = ipc.wait_any(events.map {|e| e[:win32_event]}, infinite)
239234
event_idx = ipc_idx - 1
240235

241236
if event_idx >= 0 && event_idx < events.length

lib/fluent/winsvc.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717
begin
1818

1919
require 'optparse'
20-
require 'windows/debug'
21-
require 'Windows/Library'
2220
require 'win32/daemon'
2321
require 'win32/event'
2422

2523
include Win32
26-
include Windows::Library
27-
include Windows::Debug
2824

2925
op = OptionParser.new
3026
opts = {service_name: nil}
@@ -37,16 +33,14 @@
3733
end
3834

3935
def read_fluentdopt(service_name)
40-
require 'win32/Registry'
36+
require 'win32/registry'
4137
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\#{service_name}") do |reg|
4238
reg.read("fluentdopt")[1] rescue ""
4339
end
4440
end
4541

4642
def service_main_start(service_name)
47-
ruby_path = 0.chr * 260
48-
GetModuleFileName.call(0, ruby_path,260)
49-
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
43+
ruby_path = ServerEngine.ruby_bin_path
5044
rubybin_dir = ruby_path[0, ruby_path.rindex("/")]
5145
opt = read_fluentdopt(service_name)
5246
Process.spawn("\"#{rubybin_dir}/ruby.exe\" \"#{rubybin_dir}/fluentd\" #{opt} -x #{service_name}")

test/plugin/test_file_wrapper.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
require 'fluent/plugin/file_wrapper'
33

44
class FileWrapperTest < Test::Unit::TestCase
5-
require 'windows/file'
6-
require 'windows/error'
7-
include Windows::File
8-
include Windows::Error
9-
105
TMP_DIR = File.dirname(__FILE__) + "/../tmp/file_wrapper#{ENV['TEST_ENV_NUMBER']}"
116

127
def setup

0 commit comments

Comments
 (0)