From f61c08ca11da4cbaabc30bd27803bdf645333e0f Mon Sep 17 00:00:00 2001 From: Fujimoto Seiji Date: Tue, 11 May 2021 11:45:15 +0900 Subject: [PATCH] WindowsFile: Allow Fluentd to respect DeletePending This is a Fluentd port of the following fix: fluent-bit#2027 Windows has a concept of DeletePending, which means "DeleteFile() was called on that file. NTFS will remove this file once everyone close the handlers for it". This patch teaches Fluentd to respect the DeletePending flag. Signed-off-by: Fujimoto Seiji --- lib/fluent/plugin/file_wrapper.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/fluent/plugin/file_wrapper.rb b/lib/fluent/plugin/file_wrapper.rb index 488bf8a1f8..c0ce2315a5 100644 --- a/lib/fluent/plugin/file_wrapper.rb +++ b/lib/fluent/plugin/file_wrapper.rb @@ -155,7 +155,29 @@ def ino by_handle_file_information.unpack("I11Q1")[11] # fileindex end + # DeletePending is a Windows-specific file state that roughly means + # "this file is queued for deletion, so close any open handlers" + # + # This flag can be retrieved via GetFileInformationByHandleEx(). + # + # https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfileinformationbyhandleex + # + def delete_pending + file_standard_info = 0x01 + bufsize = 1024 + buf = '\0' * bufsize + + unless GetFileInformationByHandleEx.call(@file_handle, file_standard_info, buf, bufsize) + return false + end + + return buf.unpack("QQICC")[3] != 0 + end + + private :delete_pending + def stat + raise Errno::ENOENT if delete_pending s = File.stat(@path) s.instance_variable_set :@ino, self.ino def s.ino; @ino; end