Class: Inspec::TarProvider

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

Overview

class ZipProvider

Direct Known Subclasses

IafProvider

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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/inspec/file_provider.rb', line 167

def initialize(path)
  @path = path
  @contents = {}

  here = Pathname.new(".")

  walk_tar(@path) do |entries|
    entries.each do |entry|
      name = entry.full_name

      # rubocop:disable Layout/MultilineOperationIndentation
      # rubocop:disable Style/ParenthesesAroundCondition
      next unless (entry.file? &&              # duh
                   !name.empty? &&             # for empty filenames?
                   name !~ %r{\.\.(?:/|\z)} && # .. (to avoid attacks?)
                   !name.include?("PaxHeader/"))

      path = Pathname.new(name).relative_path_from(here).to_s

      @contents[path] = begin # not ||= in a tarball, last one wins
                          res = entry.read || ""
                          try = res.dup
                          try.force_encoding Encoding::UTF_8
                          res = try.encode(try.encoding, universal_newline: true) if
                            try.valid_encoding?
                          res
                        end
    end

    @files = @contents.keys
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



165
166
167
# File 'lib/inspec/file_provider.rb', line 165

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/inspec/file_provider.rb', line 200

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

  @contents.each do |path, body|
    full_path = File.join(destination_path, path)

    FileUtils.mkdir_p(File.dirname(full_path))
    File.open(full_path, "wb") { |f| f.write(body) }
  end
end

#read(file) ⇒ Object



211
212
213
# File 'lib/inspec/file_provider.rb', line 211

def read(file)
  @contents[file]
end