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.



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/puppet/util/windows/security.rb', line 513

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, _("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