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.



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
185
186
187
188
189
190
# File 'lib/inspec/file_provider.rb', line 159

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.



157
158
159
# File 'lib/inspec/file_provider.rb', line 157

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/inspec/file_provider.rb', line 192

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



203
204
205
# File 'lib/inspec/file_provider.rb', line 203

def read(file)
  @contents[file]
end