module Puppet::Util::Windows::Security::FILE

Constants

FILE_ALL_ACCESS
FILE_APPEND_DATA
FILE_ATTRIBUTE_READONLY
FILE_ATTRIBUTE_REPARSE_POINT
FILE_DELETE_CHILD
FILE_EXECUTE
FILE_FLAG_BACKUP_SEMANTICS
FILE_FLAG_OPEN_REPARSE_POINT
FILE_GENERIC_EXECUTE
FILE_GENERIC_READ
FILE_GENERIC_WRITE
FILE_READ_ATTRIBUTES
FILE_READ_DATA
FILE_READ_EA
FILE_SHARE_READ
FILE_SHARE_WRITE
FILE_WRITE_ATTRIBUTES
FILE_WRITE_DATA
FILE_WRITE_EA
FSCTL_GET_REPARSE_POINT

msdn.microsoft.com/en-us/library/windows/desktop/aa364571(v=vs.85).aspx

GENERIC_ALL
GENERIC_EXECUTE
GENERIC_READ
GENERIC_WRITE
INVALID_FILE_ATTRIBUTES
INVALID_HANDLE_VALUE

define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)

MAXIMUM_REPARSE_DATA_BUFFER_SIZE
OPEN_EXISTING
SPECIFIC_RIGHTS_ALL
STANDARD_RIGHTS_ALL
STANDARD_RIGHTS_EXECUTE
STANDARD_RIGHTS_READ
STANDARD_RIGHTS_REQUIRED
STANDARD_RIGHTS_WRITE
SYNCHRONIZE

Public Class Methods

add_attributes(path, flags) click to toggle source
# File lib/puppet/util/windows/file.rb, line 107
def add_attributes(path, flags)
  oldattrs = get_attributes(path)

  if (oldattrs | flags) != oldattrs
    set_attributes(path, oldattrs | flags)
  end
end
create_file(file_name, desired_access, share_mode, security_attributes, creation_disposition, flags_and_attributes, template_file_handle) click to toggle source
# File lib/puppet/util/windows/file.rb, line 135
def self.create_file(file_name, desired_access, share_mode, security_attributes,
  creation_disposition, flags_and_attributes, template_file_handle)

  result = CreateFileW(wide_string(file_name.to_s),
    desired_access, share_mode, security_attributes, creation_disposition,
    flags_and_attributes, template_file_handle)

  return result unless result == INVALID_HANDLE_VALUE
  raise Puppet::Util::Windows::Error.new(
    "CreateFile(#{file_name}, #{desired_access.to_s(8)}, #{share_mode.to_s(8)}, " +
      "#{security_attributes}, #{creation_disposition.to_s(8)}, " +
      "#{flags_and_attributes.to_s(8)}, #{template_file_handle})")
end
device_io_control(handle, io_control_code, in_buffer = nil, out_buffer = nil) click to toggle source
# File lib/puppet/util/windows/file.rb, line 160
def self.device_io_control(handle, io_control_code, in_buffer = nil, out_buffer = nil)
  if out_buffer.nil?
    raise Puppet::Util::Windows::Error.new("out_buffer is required")
  end

  FFI::MemoryPointer.new(:dword, 1) do |bytes_returned_ptr|
    result = DeviceIoControl(
      handle,
      io_control_code,
      in_buffer, in_buffer.nil? ? 0 : in_buffer.size,
      out_buffer, out_buffer.size,
      bytes_returned_ptr,
      nil
    )

    if result == FFI::WIN32_FALSE
      raise Puppet::Util::Windows::Error.new(
        "DeviceIoControl(#{handle}, #{io_control_code}, " +
        "#{in_buffer}, #{in_buffer ? in_buffer.size : ''}, " +
        "#{out_buffer}, #{out_buffer ? out_buffer.size : ''}")
    end
  end

  out_buffer
end
get_attributes(file_name) click to toggle source
# File lib/puppet/util/windows/file.rb, line 100
def get_attributes(file_name)
  result = GetFileAttributesW(wide_string(file_name.to_s))
  return result unless result == INVALID_FILE_ATTRIBUTES
  raise Puppet::Util::Windows::Error.new("GetFileAttributes(#{file_name})")
end
get_file_attributes(file_name) click to toggle source
# File lib/puppet/util/windows/file.rb, line 94
def get_file_attributes(file_name)
  Puppet.deprecation_warning('Puppet::Util::Windows::File.get_file_attributes is deprecated; please use Puppet::Util::Windows::File.get_attributes')
  get_attributes(file_name)
end
get_reparse_point_data(handle) { |reparse_data_buffer| ... } click to toggle source
# File lib/puppet/util/windows/file.rb, line 149
def self.get_reparse_point_data(handle, &block)
  # must be multiple of 1024, min 10240
  FFI::MemoryPointer.new(REPARSE_DATA_BUFFER.size) do |reparse_data_buffer_ptr|
    device_io_control(handle, FSCTL_GET_REPARSE_POINT, nil, reparse_data_buffer_ptr)
    yield REPARSE_DATA_BUFFER.new(reparse_data_buffer_ptr)
  end

  # underlying struct MemoryPointer has been cleaned up by this point, nothing to return
  nil
end
lstat(file_name) click to toggle source
# File lib/puppet/util/windows/file.rb, line 260
def lstat(file_name)
  file_name = file_name.to_s # accomodate PathName or String
  # monkey'ing around!
  stat = File.lstat(file_name)

  singleton_class = class << stat; self; end
  singleton_class.send(:define_method, :mode) do
    Puppet::Util::Windows::Security.get_mode(file_name)
  end

  if symlink?(file_name)
    def stat.ftype
      "link"
    end
  end
  stat
end
move_file_ex(source, target, flags = 0) click to toggle source
# File lib/puppet/util/windows/file.rb, line 71
def move_file_ex(source, target, flags = 0)
  result = MoveFileExW(wide_string(source.to_s),
                       wide_string(target.to_s),
                       flags)

  return true if result != FFI::WIN32_FALSE
  raise Puppet::Util::Windows::Error.
    new("MoveFileEx(#{source}, #{target}, #{flags.to_s(8)})")
end
remove_attributes(path, flags) click to toggle source
# File lib/puppet/util/windows/file.rb, line 116
def remove_attributes(path, flags)
  oldattrs = get_attributes(path)

  if (oldattrs & ~flags) != oldattrs
    set_attributes(path, oldattrs & ~flags)
  end
end
replace_file(target, source) click to toggle source
# File lib/puppet/util/windows/file.rb, line 51
def replace_file(target, source)
  target_encoded = wide_string(target.to_s)
  source_encoded = wide_string(source.to_s)

  flags = 0x1
  backup_file = nil
  result = ReplaceFileW(
    target_encoded,
    source_encoded,
    backup_file,
    flags,
    FFI::Pointer::NULL,
    FFI::Pointer::NULL
  )

  return true if result != FFI::WIN32_FALSE
  raise Puppet::Util::Windows::Error.new("ReplaceFile(#{target}, #{source})")
end
set_attributes(path, flags) click to toggle source
# File lib/puppet/util/windows/file.rb, line 125
def set_attributes(path, flags)
  success = SetFileAttributesW(wide_string(path), flags) != FFI::WIN32_FALSE
  raise Puppet::Util::Windows::Error.new("Failed to set file attributes") if !success

  success
end
stat(file_name) click to toggle source
# File lib/puppet/util/windows/file.rb, line 236
def stat(file_name)
  file_name = file_name.to_s # accomodate PathName or String
  stat = File.stat(file_name)
  singleton_class = class << stat; self; end
  target_path = file_name

  if symlink?(file_name)
    target_path = readlink(file_name)
    link_ftype = File.stat(target_path).ftype

    # sigh, monkey patch instance method for instance, and close over link_ftype
    singleton_class.send(:define_method, :ftype) do
      link_ftype
    end
  end

  singleton_class.send(:define_method, :mode) do
    Puppet::Util::Windows::Security.get_mode(target_path)
  end

  stat
end