Class: Inspec::TarProvider

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

Overview

class ZipProvider

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.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/inspec/file_provider.rb', line 154

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 if try.valid_encoding?
                          res
                        end
    end

    @files = @contents.keys
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



152
153
154
# File 'lib/inspec/file_provider.rb', line 152

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/inspec/file_provider.rb', line 186

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



197
198
199
# File 'lib/inspec/file_provider.rb', line 197

def read(file)
  @contents[file]
end