Module: FileType::ExtractableDir

Defined in:
lib/file_type.rb

Class Method Summary collapse

Class Method Details

.included(aClass) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/file_type.rb', line 188

def self.included ( aClass )

  aClass.module_eval do

    include Extractable

    remove_method :mk_cmd

    def mk_cmd ( out )
      @log = TempPath.new('log')
      cmd = extract_command[@tmp] < @path > @log
      @tmp.mkpath
      cmd
    end

    alias :extract_extractable :extract

    def extract
      dir = extract_extractable
      FileType.guess(dir.path + longest_common_path(@log).join('/'))
    end

  end

end

.longest_common_path(log) ⇒ Object

We want the longest common path



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/file_type.rb', line 215

def longest_common_path ( log )
  longest = nil
  raise CorruptedTarballError, 'tar output empty' if log.zero?
  log.each_line do |line|
    line.chomp!
    path = line.split(/\//)
    longest = path if longest.nil?
    longest &= path
    raise CorruptedTarballError, log.read if longest.empty?
  end
  raise CorruptedTarballError, log.read if longest.nil?
  longest
end