From 3ad19e08b710eabb64a932df2fb359a260f4ae7b Mon Sep 17 00:00:00 2001 From: Adam Vandenberg Date: Sun, 29 Apr 2012 11:45:46 -0700 Subject: [PATCH] Extract detection of compression types Separate out detecting compression types from uncompressing and staging. --- Library/Homebrew/download_strategy.rb | 24 +++++++----------------- Library/Homebrew/extend/pathname.rb | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index b8594f6252..718643bf97 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -70,33 +70,23 @@ class CurlDownloadStrategy < AbstractDownloadStrategy 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 diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 3dda828d56..5153568612 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -240,6 +240,31 @@ class Pathname 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|