From 25b77f4e175e8a56865d3c908f29147c6d41c93a Mon Sep 17 00:00:00 2001 From: Claire McQuin Date: Thu, 3 Sep 2015 11:15:27 -0700 Subject: Implement delete_key with Win32::Registry#delete_key --- lib/chef/win32/registry.rb | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'lib/chef/win32') diff --git a/lib/chef/win32/registry.rb b/lib/chef/win32/registry.rb index e699a9bc06..12ad08a965 100644 --- a/lib/chef/win32/registry.rb +++ b/lib/chef/win32/registry.rb @@ -127,36 +127,23 @@ class Chef Chef::Log.debug("Registry key #{key_path}, does not exist, not deleting") return true end - #key_path is in the form "HKLM\Software\Opscode" for example, extracting - #hive = HKLM, - #hive_namespace = ::Win32::Registry::HKEY_LOCAL_MACHINE - hive = key_path.split("\\").shift - hive_namespace, key_including_parent = get_hive_and_key(key_path) - if has_subkeys?(key_path) - if recursive == true - subkeys = get_subkeys(key_path) - subkeys.each do |key| - keypath_to_check = hive+"\\"+key_including_parent+"\\"+key - Chef::Log.debug("Deleting registry key #{key_path} recursively") - delete_key(keypath_to_check, true) - end - delete_key_ex(hive_namespace, key_including_parent) - else - raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified" - end - else - delete_key_ex(hive_namespace, key_including_parent) - return true + if has_subkeys?(key_path) && !recursive + raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified" + end + hive, key_including_parent = get_hive_and_key(key_path) + # key_including_parent: Software\\Root\\Branch\\Fruit + # key => Fruit + # key_parent => Software\\Root\\Branch + key_parts = key_including_parent.split("\\") + key = key_parts.pop + key_parent = key_parts.join("\\") + hive.open(key_parent, ::Win32::Registry::KEY_WRITE | registry_system_architecture) do |reg| + reg.delete_key(key, recursive) end + Chef::Log.debug("Registry key #{key_path} deleted") true end - #Using the 'RegDeleteKeyEx' Windows API that correctly supports WOW64 systems (Win2003) - #instead of the 'RegDeleteKey' - def delete_key_ex(hive, key) - RegDeleteKeyExW(hive.hkey, wstring(key), 0, 0) == 0 - end - def key_exists?(key_path) hive, key = get_hive_and_key(key_path) begin -- cgit v1.2.1