class Inspec::TarProvider
def initialize(path)
def initialize(path) @path = path @contents = {} @files = [] walk_tar(@path) do |tar| @files = tar.find_all(&:file?) # delete all entries with no name @files = @files.find_all { |x| !x.full_name.empty? } # delete all entries that have a PaxHeader @files = @files.delete_if { |x| x.full_name.include?('PaxHeader/') } # replace all items of the array simply with the relative filename of the file @files.map! { |x| Pathname.new(x.full_name).relative_path_from(Pathname.new('.')).to_s } end end
def read(file)
def read(file) @contents[file] ||= read_from_tar(file) end
def read_from_tar(file)
def read_from_tar(file) return nil unless @files.include?(file) res = nil # NB `TarReader` includes `Enumerable` beginning with Ruby 2.x walk_tar(@path) do |tar| tar.each do |entry| next unless entry.file? && [file, "./#{file}"].include?(entry.full_name) res = entry.read break end end res end
def walk_tar(path, &callback)
def walk_tar(path, &callback) Gem::Package::TarReader.new(Zlib::GzipReader.open(path), &callback) end