<commit_before>require 'tempfile'

module VagrantPlugins
  module HostManager
    module HostsFile
      def update_guest(machine)
        return unless machine.communicate.ready?

        if (machine.communicate.test("uname -s | grep SunOS"))
          realhostfile = '/etc/inet/hosts'
		  move_cmd = 'mv'
        elsif (machine.communicate.test("test -d $Env:SystemRoot"))
		  realhostfile = 'C:\Windows\System32\Drivers\etc\hosts'
		  move_cmd = 'mv -force'
        else 
          realhostfile = '/etc/hosts'
		  move_cmd = 'mv'
        end
        # download and modify file with Vagrant-managed entries
        file = @global_env.tmp_path.join("hosts.#{machine.name}")
        machine.communicate.download(realhostfile, file)
        update_file(file)

        # upload modified file and remove temporary file
        machine.communicate.upload(file, '/tmp/hosts')
        machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
        # i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
        begin
          FileUtils.rm(file) 
        rescue Exception => e
        end
      end

      def update_host
        # copy and modify hosts file on host with Vagrant-managed entries
        file = @global_env.tmp_path.join('hosts.local')
        # add a "if windows..."
        hosts_location = '/etc/hosts'
        copy_cmd = 'sudo cp'
        # handles the windows hosts file...
        if ENV['OS'] == 'Windows_NT'
          hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
          copy_cmd = 'cp'
        end
        FileUtils.cp(hosts_location, file)
        update_file(file)

        # copy modified file using sudo for permission
        `#{copy_cmd} #{file} #{hosts_location}`
      end

      private

      def update_file(file)
        # build array of host file entries from Vagrant configuration
        entries = []
        ids = []
        get_machines.each do |name, p|
          if @provider == p
            machine = @global_env.machine(name, p)
            host = machine.config.vm.hostname || name
            id = machine.id
            ip = get_ip_address(machine)
            aliases = machine.config.hostmanager.aliases.join(' ').chomp
            entries <<  "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n"
            ids << id unless ids.include?(id)<commit_msg>Use :SystemRoot to detect Windows, including git-bash. Also use :Windir to ensure we get correct hosts file path<commit_after>require 'tempfile'

module VagrantPlugins
  module HostManager
    module HostsFile
      def update_guest(machine)
        return unless machine.communicate.ready?

        if (machine.communicate.test("uname -s | grep SunOS"))
          realhostfile = '/etc/inet/hosts'
		  move_cmd = 'mv'
        elsif (machine.communicate.test("test -d $Env:SystemRoot"))
      realhostfile = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
      
		  move_cmd = 'mv -force'
        else 
          realhostfile = '/etc/hosts'
		  move_cmd = 'mv'
        end
        # download and modify file with Vagrant-managed entries
        file = @global_env.tmp_path.join("hosts.#{machine.name}")
        machine.communicate.download(realhostfile, file)
        update_file(file)

        # upload modified file and remove temporary file
        machine.communicate.upload(file, '/tmp/hosts')
        machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
        # i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
        begin
          FileUtils.rm(file) 
        rescue Exception => e
        end
      end

      def update_host
        # copy and modify hosts file on host with Vagrant-managed entries
        file = @global_env.tmp_path.join('hosts.local')
        # add a "if windows..."
        hosts_location = '/etc/hosts'
        copy_cmd = 'sudo cp'
        # handles the windows hosts file...
        if ENV['SystemRoot'] != nil
          hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
          copy_cmd = 'cp'
        end
        FileUtils.cp(hosts_location, file)
        update_file(file)

        # copy modified file using sudo for permission
        `#{copy_cmd} #{file} #{hosts_location}`
      end

      private

      def update_file(file)
        # build array of host file entries from Vagrant configuration
        entries = []
        ids = []
        get_machines.each do |name, p|
          if @provider == p
            machine = @global_env.machine(name, p)
            host = machine.config.vm.hostname || name
            id = machine.id
            ip = get_ip_address(machine)
            aliases = machine.config.hostmanager.aliases.join(' ').chomp
            entries <<  "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n"
            ids << id unless ids.include?(id)
{'fraction_non_alphanumeric': 0.10909090909090909, 'fraction_numerical': 0.001634320735444331, 'mean_word_length': 2.166882276843467, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6597238', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1410, 'n_words': 382}