summaryrefslogtreecommitdiff
path: root/lib/chef/win32/registry.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/win32/registry.rb')
-rw-r--r--lib/chef/win32/registry.rb53
1 files changed, 25 insertions, 28 deletions
diff --git a/lib/chef/win32/registry.rb b/lib/chef/win32/registry.rb
index 1a1aa12fad..12ad08a965 100644
--- a/lib/chef/win32/registry.rb
+++ b/lib/chef/win32/registry.rb
@@ -17,8 +17,12 @@
# limitations under the License.
#
require 'chef/reserved_names'
+require 'chef/win32/api'
+require 'chef/mixin/wide_string'
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ require 'chef/monkey_patches/win32/registry'
+ require 'chef/win32/api/registry'
require 'win32/registry'
require 'win32/api'
end
@@ -27,6 +31,14 @@ class Chef
class Win32
class Registry
+ if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ include Chef::ReservedNames::Win32::API::Registry
+ extend Chef::ReservedNames::Win32::API::Registry
+ end
+
+ include Chef::Mixin::WideString
+ extend Chef::Mixin::WideString
+
attr_accessor :run_context
attr_accessor :architecture
@@ -115,38 +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)
- regDeleteKeyEx = ::Win32::API.new('RegDeleteKeyEx', 'LPLL', 'L', 'advapi32')
- hive_num = hive.hkey - (1 << 32)
- regDeleteKeyEx.call(hive_num, key, ::Win32::Registry::KEY_WRITE | registry_system_architecture, 0)
- end
-
def key_exists?(key_path)
hive, key = get_hive_and_key(key_path)
begin