Method: Puppet::Util::Windows::Security#open_file

Defined in:
lib/puppet/util/windows/security.rb

#open_file(path, access, &block) ⇒ Object

Open an existing file with the specified access mode, and execute a block with the opened file HANDLE.



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/puppet/util/windows/security.rb', line 461

def open_file(path, access, &block)
  handle = CreateFileW(
           wide_string(path),
           access,
           FILE::FILE_SHARE_READ | FILE::FILE_SHARE_WRITE,
           FFI::Pointer::NULL, # security_attributes
           FILE::OPEN_EXISTING,
           FILE::FILE_FLAG_OPEN_REPARSE_POINT | FILE::FILE_FLAG_BACKUP_SEMANTICS,
           FFI::Pointer::NULL_HANDLE) # template

  if handle == Puppet::Util::Windows::File::INVALID_HANDLE_VALUE
    raise Puppet::Util::Windows::Error.new(_("Failed to open '%{path}'") % { path: path })
  end

  begin
    yield handle
  ensure
    FFI::WIN32.CloseHandle(handle) if handle
  end

  # handle has already had CloseHandle called against it, nothing to return
  nil
end