diff options
author | Tim Smith <tsmith@chef.io> | 2020-04-23 08:37:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-23 08:37:14 -0700 |
commit | ce5cecb3de3576bb0a553be556668eed62e41eb5 (patch) | |
tree | 5282e84c20a06ce723c13ce34f607f3f4e938ce5 /lib | |
parent | 9b90cdebf9453c7e2afe3b9828cdd451b0eef7ee (diff) | |
parent | beb3610ed88189708c1318ce5d0904ca18026562 (diff) | |
download | chef-ce5cecb3de3576bb0a553be556668eed62e41eb5.tar.gz |
Merge pull request #9509 from chef/Kapil/Github-9413_Chocolate_resource_options_causing_extra_quotes
Fix for Chocolate_resource options causing extra quotes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider/package/chocolatey.rb | 5 | ||||
-rw-r--r-- | lib/chef/win32/api/command_line_helper.rb | 89 |
2 files changed, 92 insertions, 2 deletions
diff --git a/lib/chef/provider/package/chocolatey.rb b/lib/chef/provider/package/chocolatey.rb index f1f7dac8ce..f1f2307f5b 100644 --- a/lib/chef/provider/package/chocolatey.rb +++ b/lib/chef/provider/package/chocolatey.rb @@ -17,14 +17,15 @@ require_relative "../package" require_relative "../../resource/chocolatey_package" +require_relative "../../win32/api/command_line_helper" if ChefUtils.windows? class Chef class Provider class Package class Chocolatey < Chef::Provider::Package + include Chef::ReservedNames::Win32::API::CommandLineHelper if ChefUtils.windows? provides :chocolatey_package - # Declare that our arguments should be arrays use_multipackage_api @@ -207,7 +208,7 @@ class Chef # @param include_source [Boolean] should the source parameter be added # @return [String] options from new_resource or empty string def cmd_args(include_source: true) - cmd_args = [ new_resource.options ] + cmd_args = new_resource.options.is_a?(String) ? command_line_to_argv_w_helper(new_resource.options) : Array(new_resource.options) cmd_args += common_options(include_source: include_source) cmd_args end diff --git a/lib/chef/win32/api/command_line_helper.rb b/lib/chef/win32/api/command_line_helper.rb new file mode 100644 index 0000000000..6ddc74ae0f --- /dev/null +++ b/lib/chef/win32/api/command_line_helper.rb @@ -0,0 +1,89 @@ +# +# Author:: Kapil Chouhan <kapil.chouhan@msystechnologies.com> +# Copyright:: Copyright 2013-2020, Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../api" + +class Chef + module ReservedNames::Win32 + module API + module CommandLineHelper + # extend Chef::ReservedNames::Win32 + extend Chef::ReservedNames::Win32::API + + ############################################### + # Win32 API Bindings + ############################################### + + ffi_lib "Shell32" + +=begin +LPWSTR * CommandLineToArgvW( + LPCWSTR lpCmdLine, + int *pNumArgs +); +=end + + safe_attach_function :command_line_to_argv_w, :CommandLineToArgvW, %i{pointer pointer}, :pointer + + ffi_lib "Kernel32" + +=begin +LPSTR GetCommandLineA(); +=end + + safe_attach_function :get_command_line, :GetCommandLineA, [], :pointer + +=begin +HLOCAL LocalFree( + _Frees_ptr_opt_ HLOCAL hMem +); +=end + + safe_attach_function :local_free, :LocalFree, [:pointer], :pointer + + ############################################### + # Helpers + ############################################### + + # It takes the supplied string and splits it into an array. + def command_line_to_argv_w_helper(args) + arguments_list = [] + argv = args.to_wstring + result = get_command_line + argc = FFI::MemoryPointer.new(:int) + + # Parses a Unicode command line string + # It is return an array of pointers to the command line arguments. + # Along with a count of such arguments + result = command_line_to_argv_w(argv, argc) + str_ptr = result.read_pointer + offset = 0 + number_of_agrs = argc.read_int + number_of_agrs.times do + new_str_pointer = str_ptr.+(offset) + argument = new_str_pointer.read_wstring + arguments_list << argument + offset = offset + argument.length * 2 + 2 + end + local_free(result) + arguments_list + end + end + end + end +end |