strangeman

registry.rb

Jul 23rd, 2014
230
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'puppet/util/windows'
  2.  
  3. module Puppet::Util::Windows
  4.   module Registry
  5.     # http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx
  6.     KEY64 = 0x100
  7.     KEY32 = 0x200
  8.  
  9.     KEY_READ       = 0x20019
  10.     KEY_WRITE      = 0x20006
  11.     KEY_ALL_ACCESS = 0x2003f
  12.  
  13.     def root(name)
  14.       Win32::Registry.const_get(name)
  15.     rescue NameError
  16.       raise Puppet::Error, "Invalid registry key '#{name}'", $!.backtrace
  17.     end
  18.  
  19.     def open(name, path, mode = KEY_READ | KEY64, &block)
  20.       hkey = root(name)
  21.       begin
  22.         hkey.open(path, mode) do |subkey|
  23.           ####added for debugging####
  24.         print "All good with '#{hkey.keyname}\\#{path}'\n "
  25.           ###########################
  26.           return yield subkey
  27.         end
  28.       rescue Win32::Registry::Error => error
  29.         ####added for debugging####
  30.       print "Error with '#{hkey.keyname}\\#{path}'\n #{error} \n \n"
  31.         ###########################
  32.         #raise Puppet::Util::Windows::Error.new("Failed to open registry key '#{hkey.keyname}\\#{path}'", error.code, error)
  33.       end
  34.     end
  35.  
  36.     def values(subkey)
  37.       values = {}
  38.       subkey.each_value do |name, type, data|
  39.         case type
  40.         when Win32::Registry::REG_MULTI_SZ
  41.           data.each { |str| force_encoding(str) }
  42.         when Win32::Registry::REG_SZ, Win32::Registry::REG_EXPAND_SZ
  43.           force_encoding(data)
  44.         end
  45.         values[name] = data
  46.       end
  47.       values
  48.     end
  49.  
  50.     if defined?(Encoding)
  51.       def force_encoding(str)
  52.         if @encoding.nil?
  53.           # See https://bugs.ruby-lang.org/issues/8943
  54.           # Ruby uses ANSI versions of Win32 APIs to read values from the
  55.           # registry. The encoding of these strings depends on the active
  56.           # code page. However, ruby incorrectly sets the string
  57.           # encoding to US-ASCII. So we must force the encoding to the
  58.           # correct value.
  59.           require 'windows/national'
  60.           begin
  61.             cp = Windows::National::GetACP.call
  62.             @encoding = Encoding.const_get("CP#{cp}")
  63.           rescue
  64.             @encoding = Encoding.default_external
  65.           end
  66.         end
  67.  
  68.         str.force_encoding(@encoding)
  69.       end
  70.     else
  71.       def force_encoding(str, enc)
  72.       end
  73.     end
  74.     private :force_encoding
  75.   end
  76. end
RAW Paste Data