Sha256: 5ad6e9aef99f7333872b559250a474a0b1dfd1408c790c44c241ef346d15529e

Contents?: true

Size: 995 Bytes

Versions: 5

Compression:

Stored size: 995 Bytes

Contents

# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
# author: Dominik Richter
# author: Christoph Hartmann

module FindFiles
  TYPES = {
    block: 'b',
    character: 'c',
    directory: 'd',
    pipe: 'p',
    file: 'f',
    link: 'l',
    socket: 's',
    door: 'D',
  }.freeze

  # ignores errors
  def find_files(path, opts = {})
    find_files_or_error(path, opts) || []
  end

  def find_files_or_error(path, opts = {})
    depth = opts[:depth]
    type = TYPES[opts[:type].to_sym] if opts[:type]

    cmd = "sh -c \'find #{path}"
    cmd += " -type #{type}" unless type.nil?
    cmd += " -maxdepth #{depth.to_i}" if depth.to_i > 0
    cmd += "\'"

    result = inspec.command(cmd)
    exit_status = result.exit_status

    unless exit_status == 0
      warn "find_files(): exit #{exit_status} from `#{cmd}`"
      return nil
    end

    result.stdout.split("\n")
          .map(&:strip)
          .find_all { |x| !x.empty? }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
inspec-2.1.21 lib/utils/find_files.rb
inspec-2.1.10 lib/utils/find_files.rb
inspec-2.0.32 lib/utils/find_files.rb
inspec-2.0.17 lib/utils/find_files.rb
inspec-1.51.15 lib/utils/find_files.rb