module VagrantPlugins::HostManager::HostsFile::Updater::WindowsSupport

Windows support for copying files, requesting elevated privileges if necessary

Public Class Methods

windows?() click to toggle source
# File lib/vagrant-hostmanager/hosts_file/updater.rb, line 167
def self.windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end

Public Instance Methods

windows_copy_file(source, dest) click to toggle source
# File lib/vagrant-hostmanager/hosts_file/updater.rb, line 173
def windows_copy_file(source, dest)
  begin
    # First, try Ruby copy
    FileUtils.cp(source, dest)
  rescue Errno::EACCES
    # Access denied, try with elevated privileges
    windows_copy_file_elevated(source, dest)
  end
end

Private Instance Methods

windows_copy_file_elevated(source, dest) click to toggle source
# File lib/vagrant-hostmanager/hosts_file/updater.rb, line 185
def windows_copy_file_elevated(source, dest)
  # copy command only supports backslashes as separators
  source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\') }

  # run 'cmd /C copy ...' with elevated privilege, minimized
  copy_cmd = "copy \"#{source}\" \"#{dest}\""
  WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)

  # Unfortunately, ShellExecute does not give us a status code,
  # and it is non-blocking so we can't reliably compare the file contents
  # to see if they were copied.
  #
  # If the user rejects the UAC prompt, vagrant will silently continue
  # without updating the hostsfile.
end