Sha256: 49aae803be078a079bcc7e2f3973185ca535a08fdba6c8ea6d25fd9404b0cc23

Contents?: true

Size: 1.98 KB

Versions: 18

Compression:

Stored size: 1.98 KB

Contents

require 'puppet/util/windows'

module Puppet::Util::Windows
  module Registry
    # http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx
    KEY64 = 0x100
    KEY32 = 0x200

    KEY_READ       = 0x20019
    KEY_WRITE      = 0x20006
    KEY_ALL_ACCESS = 0x2003f

    def root(name)
      Win32::Registry.const_get(name)
    rescue NameError
      raise Puppet::Error, "Invalid registry key '#{name}'", $!.backtrace
    end

    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 '#{hkey.keyname}\\#{path}'", error.code, error)
      end
    end

    def values(subkey)
      values = {}
      subkey.each_value do |name, type, data|
        case type
        when Win32::Registry::REG_MULTI_SZ
          data.each { |str| force_encoding(str) }
        when Win32::Registry::REG_SZ, Win32::Registry::REG_EXPAND_SZ
          force_encoding(data)
        end
        values[name] = data
      end
      values
    end

    if defined?(Encoding)
      def force_encoding(str)
        if @encoding.nil?
          # See https://bugs.ruby-lang.org/issues/8943
          # Ruby uses ANSI versions of Win32 APIs to read values from the
          # registry. The encoding of these strings depends on the active
          # code page. However, ruby incorrectly sets the string
          # encoding to US-ASCII. So we must force the encoding to the
          # correct value.
          require 'windows/national'
          begin
            cp = Windows::National::GetACP.call
            @encoding = Encoding.const_get("CP#{cp}")
          rescue
            @encoding = Encoding.default_external
          end
        end

        str.force_encoding(@encoding)
      end
    else
      def force_encoding(str, enc)
      end
    end
    private :force_encoding
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
puppet-3.6.2 lib/puppet/util/windows/registry.rb
puppet-3.6.2-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.6.1 lib/puppet/util/windows/registry.rb
puppet-3.6.1-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.6.0 lib/puppet/util/windows/registry.rb
puppet-3.6.0-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.6.0.rc1 lib/puppet/util/windows/registry.rb
puppet-3.6.0.rc1-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.5.1 lib/puppet/util/windows/registry.rb
puppet-3.5.1-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.5.1.rc1 lib/puppet/util/windows/registry.rb
puppet-3.5.1.rc1-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc3 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc3-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc2 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc2-x86-mingw32 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc1 lib/puppet/util/windows/registry.rb
puppet-3.5.0.rc1-x86-mingw32 lib/puppet/util/windows/registry.rb