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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/inspec/file_provider.rb', line 221

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

#files ⇒ Object (readonly)

Returns the value of attribute files.



219
220
221
# File 'lib/inspec/file_provider.rb', line 219

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



254
255
256
257
258
259
260
261
262
263
# File 'lib/inspec/file_provider.rb', line 254

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



265
266
267
# File 'lib/inspec/file_provider.rb', line 265

def read(file)
  @contents[file]
end