Sha256: be505b83994fe71c9df98fbc32aae0bc3a7372de675febddaa12537fa1355350

Contents?: true

Size: 999 Bytes

Versions: 101

Compression:

Stored size: 999 Bytes

Contents

# copyright: 2015, Vulcano Security GmbH

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_warn(path, opts) || []
  end

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

    # If `path` contains a `'` we must modify how we quote the `sh -c` argument
    quote = path.include?("'") ? '"' : "'"

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

    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

101 entries across 101 versions & 2 rubygems

Version Path
inspec-core-4.52.9 lib/inspec/utils/find_files.rb
inspec-core-4.50.3 lib/inspec/utils/find_files.rb
inspec-core-4.49.0 lib/inspec/utils/find_files.rb
inspec-core-4.46.13 lib/inspec/utils/find_files.rb
inspec-core-4.41.20 lib/inspec/utils/find_files.rb
inspec-core-4.41.2 lib/inspec/utils/find_files.rb
inspec-core-4.38.9 lib/inspec/utils/find_files.rb
inspec-core-4.38.3 lib/inspec/utils/find_files.rb
inspec-core-4.37.30 lib/inspec/utils/find_files.rb
inspec-core-4.37.25 lib/inspec/utils/find_files.rb
inspec-core-4.37.23 lib/inspec/utils/find_files.rb
inspec-core-4.37.20 lib/inspec/utils/find_files.rb
inspec-core-4.37.17 lib/inspec/utils/find_files.rb
inspec-core-4.37.8 lib/inspec/utils/find_files.rb
inspec-core-4.37.0 lib/inspec/utils/find_files.rb
inspec-core-4.36.4 lib/inspec/utils/find_files.rb
inspec-core-4.33.1 lib/inspec/utils/find_files.rb
inspec-core-4.32.0 lib/inspec/utils/find_files.rb
inspec-core-4.31.1 lib/inspec/utils/find_files.rb
inspec-core-4.31.0 lib/inspec/utils/find_files.rb