Skip to content

Commit

Permalink
Extract detection of compression types
Browse files Browse the repository at this point in the history
Separate out detecting compression types from uncompressing and staging.
  • Loading branch information
adamv committed May 3, 2012
1 parent 41ba5a2 commit 52732da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
24 changes: 7 additions & 17 deletions Library/Homebrew/download_strategy.rb
Expand Up @@ -70,33 +70,23 @@ def fetch
end

def stage
if @tarball_path.extname == '.jar'
magic_bytes = nil
elsif @tarball_path.extname == '.pkg'
# Use more than 4 characters to not clash with magicbytes
magic_bytes = "____pkg"
else
# get the first six bytes
File.open(@tarball_path) { |f| magic_bytes = f.read(6) }
end

# magic numbers stolen from /usr/share/file/magic/
case magic_bytes
when /^PK\003\004/ # .zip archive
case @tarball_path.compression_type
when :zip
quiet_safe_system '/usr/bin/unzip', {:quiet_flag => '-qq'}, @tarball_path
chdir
when /^\037\213/, /^BZh/, /^\037\235/ # gzip/bz2/compress compressed
when :gzip, :bzip2, :compress
# Assume these are also tarred
# TODO check if it's really a tar archive
safe_system '/usr/bin/tar', 'xf', @tarball_path
chdir
when /^\xFD7zXZ\x00/ # xz compressed
when :xz
raise "You must install XZutils: brew install xz" unless which_s "xz"
safe_system "xz -dc \"#{@tarball_path}\" | /usr/bin/tar xf -"
chdir
when '____pkg'
when :pkg
safe_system '/usr/sbin/pkgutil', '--expand', @tarball_path, File.basename(@url)
chdir
when /Rar!/
when :rar
quiet_safe_system 'unrar', 'x', {:quiet_flag => '-inul'}, @tarball_path
else
# we are assuming it is not an archive, use original filename
Expand Down
25 changes: 25 additions & 0 deletions Library/Homebrew/extend/pathname.rb
Expand Up @@ -240,6 +240,31 @@ def version
nil
end
def compression_type
# Don't treat jars as compressed
return nil if self.extname == '.jar'
# OS X installer package
return :pkg if self.extname == '.pkg'
# get the first six bytes
magic_bytes = nil
File.open(self) { |f| magic_bytes = f.read(6) }
# magic numbers stolen from /usr/share/file/magic/
case magic_bytes
when /^PK\003\004/ then :zip
when /^\037\213/ then :gzip
when /^BZh/ then :bzip2
when /^\037\235/ then :compress
when /^\xFD7zXZ\x00/ then :xz
when /^Rar!/ then :rar
else
# Assume it is not an archive
nil
end
end
def incremental_hash(hasher)
incr_hash = hasher.new
self.open('r') do |f|
Expand Down

0 comments on commit 52732da

Please sign in to comment.