From 361d41527a05c63c6f58b6d2a8c9f4fb35664b13 Mon Sep 17 00:00:00 2001 From: Richard Guin Date: Wed, 30 Jul 2014 15:43:11 -0400 Subject: [PATCH 1/3] Added mkdir command filter for WinRM to be compatible with PS4+ --- plugins/communicators/winrm/command_filter.rb | 1 + .../winrm/command_filters/mkdir.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100755 plugins/communicators/winrm/command_filters/mkdir.rb diff --git a/plugins/communicators/winrm/command_filter.rb b/plugins/communicators/winrm/command_filter.rb index f3ade202d..66a8b1d47 100644 --- a/plugins/communicators/winrm/command_filter.rb +++ b/plugins/communicators/winrm/command_filter.rb @@ -11,6 +11,7 @@ module VagrantPlugins "test", "uname", "which", + "mkdir", ] # Filter the given Vagrant command to ensure compatibility with Windows diff --git a/plugins/communicators/winrm/command_filters/mkdir.rb b/plugins/communicators/winrm/command_filters/mkdir.rb new file mode 100755 index 000000000..9d399c2f3 --- /dev/null +++ b/plugins/communicators/winrm/command_filters/mkdir.rb @@ -0,0 +1,25 @@ +module VagrantPlugins + module CommunicatorWinRM + module CommandFilters + # Converts a *nix 'mkdir' command to a PowerShell equivalent + class Mkdir + def filter(command) + # mkdir -p /some/dir + # mkdir /some/dir + cmd_parts = command.strip.split(/\s+/) + dir = cmd_parts.pop + while !dir.nil? && dir.start_with?('-') + dir = cmd_parts.pop + end + # This will ignore any -p switches, which are redundant in PowerShell, + # and ambiguous in PowerShell 4+ + return "mkdir #{dir} -force" + end + + def accept?(command) + command.start_with?('mkdir ') + end + end + end + end +end From 120b15bc397bab6eb788722b90a5e9d2c119ac7b Mon Sep 17 00:00:00 2001 From: Richard Guin Date: Wed, 30 Jul 2014 15:43:37 -0400 Subject: [PATCH 2/3] Modified WinRM rm filter to account for -f switch, handle recurse properly --- .../communicators/winrm/command_filters/rm.rb | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/plugins/communicators/winrm/command_filters/rm.rb b/plugins/communicators/winrm/command_filters/rm.rb index 5c0dd4c2b..251e26076 100644 --- a/plugins/communicators/winrm/command_filters/rm.rb +++ b/plugins/communicators/winrm/command_filters/rm.rb @@ -5,14 +5,35 @@ module VagrantPlugins class Rm def filter(command) # rm -Rf /some/dir + # rm -R /some/dir + # rm -R -f /some/dir + # rm -f /some/dir # rm /some/dir cmd_parts = command.strip.split(/\s+/) - dir = cmd_parts[1] - if dir == '-Rf' - dir = cmd_parts[2] - return "rm '#{dir}' -recurse -force" + + # Figure out if we need to do this recursively + recurse = false + cmd_parts.each do |k| + argument = k.downcase + if argument == '-r' || argument == '-rf' || argument == '-fr' + recurse = true + break + end end - return "rm '#{dir}' -force" + + # Figure out which argument is the path + dir = cmd_parts.pop + while !dir.nil? && dir.start_with?('-') + dir = cmd_parts.pop + end + + ret_cmd = '' + if recurse + ret_cmd = "rm #{dir} -recurse -force" + else + ret_cmd = "rm #{dir} -force" + end + return ret_cmd end def accept?(command) From 08732f5a391aa0a35584139527b25da125a1a8cb Mon Sep 17 00:00:00 2001 From: Richard Guin Date: Thu, 31 Jul 2014 14:42:11 -0400 Subject: [PATCH 3/3] Added/updated unit tests for WinRM mkdir/rm command filters --- .../winrm/command_filter_test.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/test/unit/plugins/communicators/winrm/command_filter_test.rb b/test/unit/plugins/communicators/winrm/command_filter_test.rb index f7ed9eb54..251defc7b 100644 --- a/test/unit/plugins/communicators/winrm/command_filter_test.rb +++ b/test/unit/plugins/communicators/winrm/command_filter_test.rb @@ -52,14 +52,27 @@ describe VagrantPlugins::CommunicatorWinRM::CommandFilter, unit: true do "if (Test-Path $p) {") end - it 'filters out rm -Rf commands' do + it 'filters out rm recurse commands' do expect(subject.filter('rm -Rf /some/dir')).to eq( - "rm '/some/dir' -recurse -force") + "rm /some/dir -recurse -force") + expect(subject.filter('rm -fr /some/dir')).to eq( + "rm /some/dir -recurse -force") + expect(subject.filter('rm -r /some/dir')).to eq( + "rm /some/dir -recurse -force") end it 'filters out rm commands' do expect(subject.filter('rm /some/dir')).to eq( - "rm '/some/dir' -force") + "rm /some/dir -force") + expect(subject.filter('rm -f /some/dir')).to eq( + "rm /some/dir -force") + end + + it 'filters out mkdir commands' do + expect(subject.filter('mkdir /some/dir')).to eq( + "mkdir /some/dir -force") + expect(subject.filter('mkdir -p /some/dir')).to eq( + "mkdir /some/dir -force") end it 'filters out chown commands' do