Class: Fluent::WindowsFile

Inherits:
Object
  • Object
show all
Includes:
Windows::Error, Windows::File, Windows::Handle, Windows::NIO
Defined in:
lib/fluent/plugin/file_wrapper.rb

Overview

To open and get stat with setting FILE_SHARE_DELETE

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = 'r', sharemode = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE) ⇒ WindowsFile

Returns a new instance of WindowsFile.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fluent/plugin/file_wrapper.rb', line 97

def initialize(path, mode='r', sharemode=FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE)
  @path = path
  @file_handle = INVALID_HANDLE_VALUE
  @mode = mode


  access, creationdisposition, seektoend = case mode.delete('b')
  when "r" ; [FILE_GENERIC_READ                     , OPEN_EXISTING, false]
  when "r+"; [FILE_GENERIC_READ | FILE_GENERIC_WRITE, OPEN_ALWAYS  , false]
  when "w" ; [FILE_GENERIC_WRITE                    , CREATE_ALWAYS, false]
  when "w+"; [FILE_GENERIC_READ | FILE_GENERIC_WRITE, CREATE_ALWAYS, false]
  when "a" ; [FILE_GENERIC_WRITE                    , OPEN_ALWAYS  , true]
  when "a+"; [FILE_GENERIC_READ | FILE_GENERIC_WRITE, OPEN_ALWAYS  , true]
  else raise "unknown mode '#{mode}'"
  end

  @file_handle = CreateFile.call(@path, access, sharemode,
                 0, creationdisposition, FILE_ATTRIBUTE_NORMAL, 0)
  if @file_handle == INVALID_HANDLE_VALUE
    err = Win32::API.last_error
    if err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND || err == ERROR_ACCESS_DENIED
      raise Errno::ENOENT
    end
    raise Win32Error.new(err, path)
  end
end

Instance Method Details

#closeObject



124
125
126
127
# File 'lib/fluent/plugin/file_wrapper.rb', line 124

def close
  CloseHandle.call(@file_handle)
  @file_handle = INVALID_HANDLE_VALUE
end

#inoObject



139
140
141
142
143
144
145
146
147
# File 'lib/fluent/plugin/file_wrapper.rb', line 139

def ino
  by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4)   #72bytes

  unless GetFileInformationByHandle.call(@file_handle, by_handle_file_information)
    return 0
  end

  by_handle_file_information.unpack("I11Q1")[11] # fileindex
end

#ioObject

Raises:

  • (Errno::ENOENT)


129
130
131
132
133
134
135
136
137
# File 'lib/fluent/plugin/file_wrapper.rb', line 129

def io
  fd = _open_osfhandle(@file_handle, 0)
  raise Errno::ENOENT if fd == -1
  io = File.for_fd(fd, @mode)
  io.instance_variable_set :@ino, self.ino
  io.instance_variable_set :@path, @path
  io.extend WindowsFileExtension
  io
end

#statObject



149
150
151
152
153
154
# File 'lib/fluent/plugin/file_wrapper.rb', line 149

def stat
  s = File.stat(@path)
  s.instance_variable_set :@ino, self.ino
  def s.ino; @ino; end
  s
end