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.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/inspec/file_provider.rb', line 213

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.



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

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/inspec/file_provider.rb', line 246

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



257
258
259
# File 'lib/inspec/file_provider.rb', line 257

def read(file)
  @contents[file]
end