Module: Puppet::Util::Windows::Registry

Extended by:
FFI::Library
Included in:
Provider::Package::Windows::Package, Provider::Package::Windows::Package
Defined in:
lib/puppet/util/windows.rb,
lib/puppet/util/windows/registry.rb

Constant Summary collapse

KEY64 =
0x100
KEY32 =
0x200
KEY_READ =
0x20019
KEY_WRITE =
0x20006
KEY_ALL_ACCESS =
0x2003f
ERROR_NO_MORE_ITEMS =
259
WCHAR_SIZE =
FFI.type_size(:wchar)

Instance Method Summary collapse

Instance Method Details

#delete_key(key, subkey_name, mode = KEY64) ⇒ Object



63
64
65
# File 'lib/puppet/util/windows/registry.rb', line 63

def delete_key(key, subkey_name, mode = KEY64)
  reg_delete_key_ex(key, subkey_name, mode)
end

#delete_value(key, subkey_name) ⇒ Object



109
110
111
# File 'lib/puppet/util/windows/registry.rb', line 109

def delete_value(key, subkey_name)
  reg_delete_value(key, subkey_name)
end

#each_key(key, &block) ⇒ Object

subkey is String which contains name of subkey. wtime is last write time as FILETIME (64-bit integer). (see Registry.wtime2time)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/util/windows/registry.rb', line 47

def each_key(key, &block)
  index = 0
  subkey = nil

  subkey_max_len, _ = reg_query_info_key_max_lengths(key)

  loop do
    subkey, filetime = reg_enum_key(key, index, subkey_max_len)
    yield subkey, filetime unless subkey.nil?
    index += 1
    break if subkey.nil?
  end

  index
end

#each_value(key, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/util/windows/registry.rb', line 93

def each_value(key, &block)
  index = 0
  subkey = nil

  _, value_max_len = reg_query_info_key_max_lengths(key)

  loop do
    subkey, type, data = reg_enum_value(key, index, value_max_len)
    yield subkey, type, data unless subkey.nil?
    index += 1
    break if subkey.nil?
  end

  index
end

#keys(key) ⇒ Object



39
40
41
42
43
# File 'lib/puppet/util/windows/registry.rb', line 39

def keys(key)
  keys = {}
  each_key(key) { |subkey, filetime| keys[subkey] = filetime }
  keys
end

#open(name, path, mode = KEY_READ | KEY64, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet/util/windows/registry.rb', line 28

def open(name, path, mode = KEY_READ | KEY64, &block)
  hkey = root(name)
  begin
    hkey.open(path, mode) do |subkey|
      return yield subkey
    end
  rescue Win32::Registry::Error => error
    raise Puppet::Util::Windows::Error.new(_("Failed to open registry key '%{key}\\%{path}'") % { key: hkey.keyname, path: path }, error.code, error)
  end
end

#root(name) ⇒ Object



22
23
24
25
26
# File 'lib/puppet/util/windows/registry.rb', line 22

def root(name)
  Win32::Registry.const_get(name)
rescue NameError => e
  raise Puppet::Error, _("Invalid registry key '%{name}'") % { name: name }, e.backtrace
end

#values(key) ⇒ Object



67
68
69
70
71
# File 'lib/puppet/util/windows/registry.rb', line 67

def values(key)
  vals = {}
  each_value(key) { |subkey, _type, data| vals[subkey] = data }
  vals
end

#values_by_name(key, names) ⇒ Hashtable<String, Object>

Retrieve a set of values from a registry key given their names Value names listed but not found in the registry will not be added to the resultant Hashtable

Parameters:

  • key (RegistryKey)

    An open handle to a Registry Key

  • names (String[])

    An array of names of registry values to return if they exist

Returns:

  • (Hashtable<String, Object>)

    A hashtable of all of the found values in the registry key



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/util/windows/registry.rb', line 80

def values_by_name(key, names)
  vals = {}
  names.each do |name|
    FFI::Pointer.from_string_to_wide_string(name) do |subkeyname_ptr|
      _, vals[name] = read(key, subkeyname_ptr)
    rescue Puppet::Util::Windows::Error => e
      # ignore missing names, but raise other errors
      raise e unless e.code == Puppet::Util::Windows::Error::ERROR_FILE_NOT_FOUND
    end
  end
  vals
end