<commit_before>require 'chef/provisioning/machine/basic_machine'
require 'digest'

class Chef
module Provisioning
  class Machine
    class UnixMachine < BasicMachine
      def initialize(machine_spec, transport, convergence_strategy)
        super

        @tmp_dir = '/tmp'
      end

      # Options include:
      #
      # command_prefix - prefix to put in front of any command, e.g. sudo
      attr_reader :options

      # Delete file
      def delete_file(action_handler, path)
        if file_exists?(path)
          action_handler.perform_action "delete file #{path} on #{machine_spec.name}" do
            transport.execute("rm -f #{path}").error!
          end
        end
      end

      def is_directory?(path)
        result = transport.execute("stat -c '%F' #{path}", :read_only => true)
        return nil if result.exitstatus != 0
        result.stdout.chomp == 'directory'
      end

      # Return true or false depending on whether file exists
      def file_exists?(path)
        result = transport.execute("ls -d #{path}", :read_only => true)
        result.exitstatus == 0 && result.stdout != ''
      end

      def files_different?(path, local_path, content=nil)
        if !file_exists?(path) || (local_path && !File.exists?(local_path))
          return true
        end

        # Get remote checksum of file
        result = transport.execute("md5sum -b #{path}", :read_only => true)
        result.error!
        remote_sum = result.stdout.split(' ')[0]

        digest = Digest::MD5.new
        if content
          digest.update(content)
        else
          File.open(local_path, 'rb') do |io|
            while (buf = io.read(4096)) && buf.length > 0
              digest.update(buf)
            end
          end
        end
        remote_sum != digest.hexdigest
      end

      def create_dir(action_handler, path)
        if !file_exists?(path)
          action_handler.perform_action "create directory #{path} on #{machine_spec.name}" do
            transport.execute("mkdir -p #{path}").error!
          end
        end
      end

      # Set file attributes { mode, :owner, :group }
      def set_attributes(action_handler, path, attributes)
        if attributes[:mode] || attributes[:owner] || attributes[:group]
          current_attributes = get_attributes(path)
          if attributes[:mode] && current_attributes[:mode].to_i != attributes[:mode].to_i
            action_handler.perform_action "change mode of #{path} on #{machine_spec.name} from #{current_attributes[:mode].to_i} to #{attributes[:mode].to_i}" do<commit_msg>Fallback to BSD md5(1)<commit_after>require 'chef/provisioning/machine/basic_machine'
require 'digest'

class Chef
module Provisioning
  class Machine
    class UnixMachine < BasicMachine
      def initialize(machine_spec, transport, convergence_strategy)
        super

        @tmp_dir = '/tmp'
      end

      # Options include:
      #
      # command_prefix - prefix to put in front of any command, e.g. sudo
      attr_reader :options

      # Delete file
      def delete_file(action_handler, path)
        if file_exists?(path)
          action_handler.perform_action "delete file #{path} on #{machine_spec.name}" do
            transport.execute("rm -f #{path}").error!
          end
        end
      end

      def is_directory?(path)
        result = transport.execute("stat -c '%F' #{path}", :read_only => true)
        return nil if result.exitstatus != 0
        result.stdout.chomp == 'directory'
      end

      # Return true or false depending on whether file exists
      def file_exists?(path)
        result = transport.execute("ls -d #{path}", :read_only => true)
        result.exitstatus == 0 && result.stdout != ''
      end

      def files_different?(path, local_path, content=nil)
        if !file_exists?(path) || (local_path && !File.exists?(local_path))
          return true
        end

        # Get remote checksum of file
        result = transport.execute("md5sum -b #{path}", :read_only => true)
        unless result.exitstatus == 0
          result = transport.execute("md5 -r #{path}", :read_only => true)
        end
        result.error!
        remote_sum = result.stdout.split(' ')[0]

        digest = Digest::MD5.new
        if content
          digest.update(content)
        else
          File.open(local_path, 'rb') do |io|
            while (buf = io.read(4096)) && buf.length > 0
              digest.update(buf)
            end
          end
        end
        remote_sum != digest.hexdigest
      end

      def create_dir(action_handler, path)
        if !file_exists?(path)
          action_handler.perform_action "create directory #{path} on #{machine_spec.name}" do
            transport.execute("mkdir -p #{path}").error!
          end
        end
      end

      # Set file attributes { mode, :owner, :group }
      def set_attributes(action_handler, path, attributes)
        if attributes[:mode] || attributes[:owner] || attributes[:group]
          current_attributes = get_attributes(path)
          if attributes[:mode] && current_attributes[:mode].to_i != attributes[:mode].to_i
            action_handler.perform_action "change mode of #{path} on #{machine_spec.name} from #{current_attributes[:mode].to_i} to #{attributes[:mode].to_i}" do
{'fraction_non_alphanumeric': 0.1117624810892587, 'fraction_numerical': 0.0045385779122541605, 'mean_word_length': 2.343236409608091, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1552388', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1471, 'n_words': 396}