GitHub PullRequestReviewCommentEvent at 2014-05-21 22:23:22
Repository: puppet
Actor: joshcooper
Payload: {"comment":{"url":"https://api.github.com/repos/puppetlabs/puppet/pulls/comments/12926815","id":12926815,"diff_hunk":"@@ -24,13 +24,39 @@ def delete(name, resource_type)\n       Puppet::Util::ADSI.connect(computer_uri).Delete(resource_type, name)\n     end\n \n-    def computer_name\n-      unless @computer_name\n-        buf = \" \" * 128\n-        Win32API.new('kernel32', 'GetComputerName', ['P','P'], 'I').call(buf, buf.length.to_s)\n-        @computer_name = buf.unpack(\"A*\")[0]\n+    if Puppet::Util::Platform.windows?\n+      require 'ffi'\n+      extend FFI::Library\n+\n+      ffi_convention :stdcall\n+\n+      # http://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx\n+      # BOOL WINAPI GetComputerName(\n+      #   _Out_    LPTSTR lpBuffer,\n+      #   _Inout_  LPDWORD lpnSize\n+      # );\n+      ffi_lib :kernel32\n+      attach_function_private :GetComputerNameW,\n+        [:lpwstr, :lpdword], :bool\n+\n+      # taken from winbase.h\n+      MAX_COMPUTERNAME_LENGTH = 31\n+\n+      def computer_name\n+        unless @computer_name\n+          max_length = MAX_COMPUTERNAME_LENGTH + 1 # NULL terminated\n+          buffer = FFI::MemoryPointer.new(max_length * 2) # wide string\n+          buffer_size = FFI::MemoryPointer.new(:dword, 1)\n+          buffer_size.write_uint32(max_length) # length in TCHARs\n+\n+          if ! GetComputerNameW(buffer, buffer_size)\n+            raise Puppet::Util::Windows::Error.new(\"Failed to get computer name\")\n+          end\n+          bytes = buffer_size.read_uint32 * 2\n+          @computer_name = buffer.read_string(bytes).force_encoding('UTF-16LE')","path":"lib/puppet/util/adsi.rb","position":38,"original_position":38,"commit_id":"d385135d50d9aea52906211664499362ea7f0883","original_commit_id":"d385135d50d9aea52906211664499362ea7f0883","user":{"login":"joshcooper","id":737664,"avatar_url":"https://avatars.githubusercontent.com/u/737664?","gravatar_id":"c7ff0fe7a67ef06a7717c0dde345acc7","url":"https://api.github.com/users/joshcooper","html_url":"https://github.com/joshcooper","followers_url":"https://api.github.com/users/joshcooper/followers","following_url":"https://api.github.com/users/joshcooper/following{/other_user}","gists_url":"https://api.github.com/users/joshcooper/gists{/gist_id}","starred_url":"https://api.github.com/users/joshcooper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshcooper/subscriptions","organizations_url":"https://api.github.com/users/joshcooper/orgs","repos_url":"https://api.github.com/users/joshcooper/repos","events_url":"https://api.github.com/users/joshcooper/events{/privacy}","received_events_url":"https://api.github.com/users/joshcooper/received_events","type":"User","site_admin":false},"body":"maybe a helper method to read a wide_string from a buffer? Also Puppet::Util::Windows::File.resolve_symlink?","created_at":"2014-05-21T22:23:22Z","updated_at":"2014-05-21T22:23:22Z","html_url":"https://github.com/puppetlabs/puppet/pull/2677#discussion_r12926815","pull_request_url":"https://api.github.com/repos/puppetlabs/puppet/pulls/2677","_links":{"self":{"href":"https://api.github.com/repos/puppetlabs/puppet/pulls/comments/12926815"},"html":{"href":"https://github.com/puppetlabs/puppet/pull/2677#discussion_r12926815"},"pull_request":{"href":"https://api.github.com/repos/puppetlabs/puppet/pulls/2677"}}}}