Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #45 from fabianofranz/master
Browse files Browse the repository at this point in the history
Fixes BZ 824312 and cartridge-lifecycle-jbossas.feature
  • Loading branch information
fabianofranz committed May 30, 2012
2 parents b77e6c9 + 98ecf47 commit 207be20
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
30 changes: 20 additions & 10 deletions express/lib/rhc/targz.rb
Expand Up @@ -4,26 +4,36 @@
require 'archive/tar/minitar'
include Archive::Tar

TAR_BIN = 'tar'
TAR_BIN = '/usr/bin/gnutar' if File.executable?('/usr/bin/gnutar')

module RHC

module TarGz

def self.contains(filename, search)
def self.contains(filename, search, force_ruby=false)

return false if ! (File.file? filename and File.basename(filename).downcase =~ /.\.tar\.gz$/i)
search = /#{search.to_s}/ if ! search.is_a?(Regexp)

contains = false
begin
RHC::Vendor::Zlib::GzipReader.open(filename) do |gz|
Minitar::Reader.open gz do |tar|
tar.each_entry do |entry|
if entry.full_name =~ search
contains = true
if RHC::Helpers.windows? or force_ruby then
search = /#{search.to_s}/ if ! search.is_a?(Regexp)
begin
RHC::Vendor::Zlib::GzipReader.open(filename) do |gz|
Minitar::Reader.open gz do |tar|
tar.each_entry do |entry|
if entry.full_name =~ search
contains = true
end
end
end
end
rescue RHC::Vendor::Zlib::GzipFile::Error
return false
end
rescue RHC::Vendor::Zlib::GzipFile::Error
return false
else
`#{TAR_BIN} --wildcards -tf #{filename} '#{search.to_s}'`
contains = $?.exitstatus == 0
end
contains
end
Expand Down
18 changes: 12 additions & 6 deletions express/spec/rhc/targz_spec.rb
Expand Up @@ -6,27 +6,33 @@
context 'with simple compressed .tar.gz' do
subject { File.expand_path('../assets/targz_sample.tar.gz', __FILE__) }
it('should wrap the right filename') { File.basename(subject).should == 'targz_sample.tar.gz' }
it('should contain the right files') { RHC::TarGz.contains(subject, /foo/).should be_true }
it('should contain the right files') { RHC::TarGz.contains(subject, /bar/).should be_false }
it('should contain the right files') { RHC::TarGz.contains(subject, /test/).should be_false }
it('should contain the right files') { RHC::TarGz.contains(subject, 'foo').should be_true }
it('should contain the right files') { RHC::TarGz.contains(subject, 'bar').should be_false }
it('should contain the right files') { RHC::TarGz.contains(subject, 'test').should be_false }
it('should contain the right files') { RHC::TarGz.contains(subject, 'foo').should be_true }
it('should contain the right files') { RHC::TarGz.contains(subject, 'bar').should be_false }
it('should contain the right files') { RHC::TarGz.contains(subject, 'test').should be_false }
end

context 'with file extension different than .tar.gz' do
subject { File.expand_path('../assets/foo.txt', __FILE__) }
it('should never return contains') { RHC::TarGz.contains(subject, /foo/).should be_false }
it('should never return contains') { RHC::TarGz.contains(subject, 'foo').should be_false }
it('should never return contains') { RHC::TarGz.contains(subject, 'foo', true).should be_false }
end

context 'with corrupted .tar.gz' do
subject { File.expand_path('../assets/targz_corrupted.tar.gz', __FILE__) }
it('should never return contains') { RHC::TarGz.contains(subject, /foo/).should be_false }
it('should never return contains') { RHC::TarGz.contains(subject, 'foo').should be_false }
it('should never return contains') { RHC::TarGz.contains(subject, 'foo', true).should be_false }
end

context 'with multiple threads' do
subject { File.expand_path('../assets/targz_sample.tar.gz', __FILE__) }
it('should be able to handle the same file') {
threads = []
30.times {
threads << Thread.new { Thread.current['result'] = RHC::TarGz.contains(subject, /foo/) }
threads << Thread.new { Thread.current['result'] = RHC::TarGz.contains(subject, 'foo') }
threads << Thread.new { Thread.current['result'] = RHC::TarGz.contains(subject, 'foo', true) }
}
threads.each { |thread| thread.join }
threads.each { |thread| thread['result'].should be_true }
Expand Down

0 comments on commit 207be20

Please sign in to comment.