From 78945e260700cda9f46078d1ba887d61fc3a4782 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Tue, 17 Sep 2013 16:09:33 +0100 Subject: [PATCH 1/8] Use quoting rather than shellescape to give Windows compatibility --- lib/ridley/chef/cookbook/syntax_check.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 4aa1c15..5db316d 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -132,7 +132,7 @@ def validate_templates end def validate_template(erb_file) - result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") + result = shell_out("erubis -x \"#{erb_file}\" | ruby -c") if result.error? file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -145,7 +145,7 @@ def validate_template(erb_file) end def validate_ruby_file(ruby_file) - result = shell_out("ruby -c #{ruby_file.shellescape}") + result = shell_out("ruby -c \"#{ruby_file}\"") if result.error? file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] From 4a0cebf594cf6e33415169f0655271f1b4fdc80a Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 20 Sep 2013 17:19:29 +0100 Subject: [PATCH 2/8] Revert "Use quoting rather than shellescape to give Windows compatibility" This reverts commit 78945e260700cda9f46078d1ba887d61fc3a4782. --- lib/ridley/chef/cookbook/syntax_check.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 5db316d..4aa1c15 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -132,7 +132,7 @@ def validate_templates end def validate_template(erb_file) - result = shell_out("erubis -x \"#{erb_file}\" | ruby -c") + result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") if result.error? file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -145,7 +145,7 @@ def validate_template(erb_file) end def validate_ruby_file(ruby_file) - result = shell_out("ruby -c \"#{ruby_file}\"") + result = shell_out("ruby -c #{ruby_file.shellescape}") if result.error? file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] From db0c8feffce8cb4e5395422ed919d4d31c1cb26d Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Mon, 23 Sep 2013 15:37:51 +0100 Subject: [PATCH 3/8] Use Windows API magic to get a safe path in Windows --- lib/ridley/chef/cookbook/syntax_check.rb | 33 ++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 4aa1c15..482358a 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -1,5 +1,10 @@ require 'shellwords' require 'buff/shell_out' +require 'rbconfig' + +if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) + require 'win32api' +end module Ridley::Chef class Cookbook @@ -130,9 +135,9 @@ def validate_templates end true end - - def validate_template(erb_file) - result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") + + def validate_template(erb_file) + result = shell_out("erubis -x #{erb_file.windows_safe_shellescape_path} | ruby -c") if result.error? file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -144,8 +149,8 @@ def validate_template(erb_file) true end - def validate_ruby_file(ruby_file) - result = shell_out("ruby -c #{ruby_file.shellescape}") + def validate_ruby_file(ruby_file) + result = shell_out("ruby -c #{ruby_file.windows_safe_shellescape_path}") if result.error? file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -168,3 +173,21 @@ def ignored?(file) end end end + +def get_short_win32_filename(long_name) + win_func = Win32API.new("kernel32","GetShortPathName","PPL"," L") + buf = 0.chr * 256 + buf[0..long_name.length-1] = long_name + win_func.call(long_name, buf, buf.length) + return buf.split(0.chr).first +end + +class String + def windows_safe_shellescape_path + if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) + get_short_win32_filename(self) + else + self.shellescape + end + end +end From 0f342c98a22a05f95baedea5597daff064c6fa1d Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 4 Oct 2013 11:41:07 +0100 Subject: [PATCH 4/8] Rewrite String.windows_safe_shellescape_path as just a redone String.shellescape --- lib/ridley/chef/cookbook/syntax_check.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 482358a..a9eccd2 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -137,7 +137,7 @@ def validate_templates end def validate_template(erb_file) - result = shell_out("erubis -x #{erb_file.windows_safe_shellescape_path} | ruby -c") + result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") if result.error? file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -150,7 +150,7 @@ def validate_template(erb_file) end def validate_ruby_file(ruby_file) - result = shell_out("ruby -c #{ruby_file.windows_safe_shellescape_path}") + result = shell_out("ruby -c #{ruby_file.shellescape}") if result.error? file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1] @@ -183,11 +183,11 @@ def get_short_win32_filename(long_name) end class String - def windows_safe_shellescape_path + def shellescape if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) get_short_win32_filename(self) else - self.shellescape + Shellwords.escape(self) end end end From 009120a687c907e0c0f02b4b91af2198bb4228e3 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 4 Oct 2013 11:41:22 +0100 Subject: [PATCH 5/8] Pull get_short_win32_filename into Shellwords --- lib/ridley/chef/cookbook/syntax_check.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index a9eccd2..253cb0b 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -174,12 +174,14 @@ def ignored?(file) end end -def get_short_win32_filename(long_name) - win_func = Win32API.new("kernel32","GetShortPathName","PPL"," L") - buf = 0.chr * 256 - buf[0..long_name.length-1] = long_name - win_func.call(long_name, buf, buf.length) - return buf.split(0.chr).first +module Shellwords + def get_short_win32_filename(long_name) + win_func = Win32API.new("kernel32","GetShortPathName","PPL"," L") + buf = 0.chr * 256 + buf[0..long_name.length-1] = long_name + win_func.call(long_name, buf, buf.length) + return buf.split(0.chr).first + end end class String From c48a426a249ce834f5cb2144a448bfcae515aa97 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 4 Oct 2013 11:42:01 +0100 Subject: [PATCH 6/8] Fix minor spacing errors --- lib/ridley/chef/cookbook/syntax_check.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 253cb0b..7950bd3 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -136,7 +136,7 @@ def validate_templates true end - def validate_template(erb_file) + def validate_template(erb_file) result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") if result.error? @@ -149,7 +149,7 @@ def validate_template(erb_file) true end - def validate_ruby_file(ruby_file) + def validate_ruby_file(ruby_file) result = shell_out("ruby -c #{ruby_file.shellescape}") if result.error? From 8636dd60878383993e2f62798c83fcd6ee246093 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 4 Oct 2013 11:42:27 +0100 Subject: [PATCH 7/8] One more spacing mishap --- lib/ridley/chef/cookbook/syntax_check.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index 7950bd3..cb39fb1 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -135,7 +135,7 @@ def validate_templates end true end - + def validate_template(erb_file) result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c") From 6642c07db69e844091b5842da9b933ff7d452ce4 Mon Sep 17 00:00:00 2001 From: Tom Parker Date: Fri, 4 Oct 2013 11:44:55 +0100 Subject: [PATCH 8/8] Fix location of get_short_win32_filename now we've moved in into Shellwords --- lib/ridley/chef/cookbook/syntax_check.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ridley/chef/cookbook/syntax_check.rb b/lib/ridley/chef/cookbook/syntax_check.rb index cb39fb1..b495825 100644 --- a/lib/ridley/chef/cookbook/syntax_check.rb +++ b/lib/ridley/chef/cookbook/syntax_check.rb @@ -182,12 +182,14 @@ def get_short_win32_filename(long_name) win_func.call(long_name, buf, buf.length) return buf.split(0.chr).first end + + module_function :get_short_win32_filename end class String def shellescape if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) - get_short_win32_filename(self) + Shellwords.get_short_win32_filename(self) else Shellwords.escape(self) end