Class: Inspec::TarProvider

Inherits:
FileProvider show all
Defined in:
lib/inspec/file_provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FileProvider

#binread, for_path, #relative_provider

Constructor Details

#initialize(path) ⇒ TarProvider

Returns a new instance of TarProvider.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/inspec/file_provider.rb', line 151

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

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



149
150
151
# File 'lib/inspec/file_provider.rb', line 149

def files
  @files
end

Instance Method Details

#extract(destination_path = '.') ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/inspec/file_provider.rb', line 169

def extract(destination_path = '.')
  FileUtils.mkdir_p(destination_path)

  walk_tar(@path) do |files|
    files.each do |file|
      next unless @files.include?(file.full_name)
      final_path = File.join(destination_path, file.full_name)

      # This removes the top level directory (and any other files) to ensure
      # extracted files do not conflict.
      FileUtils.remove_entry(final_path) if File.exist?(final_path)

      FileUtils.mkdir_p(File.dirname(final_path))
      File.open(final_path, 'wb') { |f| f.write(file.read) }
    end
  end
end

#read(file) ⇒ Object



187
188
189
# File 'lib/inspec/file_provider.rb', line 187

def read(file)
  @contents[file] ||= read_from_tar(file)
end