2016-02-21 01:48:45 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
module Fetchers
|
|
|
|
class Tar < Inspec.fetcher(1)
|
|
|
|
name 'tar'
|
|
|
|
priority 100
|
|
|
|
|
|
|
|
attr_reader :files
|
|
|
|
|
|
|
|
def self.resolve(target)
|
2016-03-17 21:54:26 +00:00
|
|
|
unless target.is_a?(String) && File.file?(target) && target.end_with?('.tar.gz', '.tgz')
|
|
|
|
return nil
|
|
|
|
end
|
2016-02-21 01:48:45 +00:00
|
|
|
new(target)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(target)
|
|
|
|
@target = target
|
|
|
|
@contents = {}
|
|
|
|
@files = []
|
|
|
|
Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar|
|
|
|
|
@files = tar.map(&:full_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(file)
|
|
|
|
@contents[file] ||= read_from_tar(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_from_tar(file)
|
|
|
|
return nil unless @files.include?(file)
|
|
|
|
res = nil
|
2016-02-22 10:23:49 +00:00
|
|
|
# NB `TarReader` includes `Enumerable` beginning with Ruby 2.x
|
2016-02-21 01:48:45 +00:00
|
|
|
Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar|
|
|
|
|
tar.each do |entry|
|
2016-02-22 10:23:49 +00:00
|
|
|
next unless entry.file? && file == entry.full_name
|
2016-02-21 01:48:45 +00:00
|
|
|
res = entry.read
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|