inspec/lib/utils/find_files.rb

45 lines
951 B
Ruby
Raw Normal View History

# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-10-06 16:55:44 +00:00
# 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',
2016-01-15 02:59:00 +00:00
}.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]
2015-12-04 13:27:32 +00:00
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
2016-02-01 07:54:24 +00:00
warn "find_files(): exit #{exit_status} from `#{cmd}`"
return nil
end
result.stdout.split("\n")
2016-01-15 02:59:00 +00:00
.map(&:strip)
.find_all { |x| !x.empty? }
end
end