From c9c5d8e008670067f57ea32d4842a1cd14ad99a2 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 16 Jul 2018 09:16:50 +0200 Subject: [PATCH 1/2] Change `Container::me?` to `Container::can_extract?`. --- .../cask/lib/hbc/artifact/nested_container.rb | 2 +- Library/Homebrew/cask/lib/hbc/container.rb | 12 +++++++++--- Library/Homebrew/cask/lib/hbc/container/air.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/base.rb | 2 +- Library/Homebrew/cask/lib/hbc/container/bzip2.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/cab.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/dmg.rb | 12 +++++++----- .../Homebrew/cask/lib/hbc/container/executable.rb | 6 +++--- .../Homebrew/cask/lib/hbc/container/generic_unar.rb | 2 +- Library/Homebrew/cask/lib/hbc/container/gpg.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/gzip.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/lzma.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/naked.rb | 5 +---- Library/Homebrew/cask/lib/hbc/container/otf.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/pkg.rb | 6 +++--- Library/Homebrew/cask/lib/hbc/container/rar.rb | 5 ++--- Library/Homebrew/cask/lib/hbc/container/seven_zip.rb | 6 +++--- Library/Homebrew/cask/lib/hbc/container/sit.rb | 4 ++-- .../cask/lib/hbc/container/svn_repository.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/tar.rb | 11 +++++++---- Library/Homebrew/cask/lib/hbc/container/ttf.rb | 6 +++--- Library/Homebrew/cask/lib/hbc/container/xar.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/xz.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/container/zip.rb | 4 ++-- Library/Homebrew/cask/lib/hbc/installer.rb | 2 +- 25 files changed, 66 insertions(+), 59 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb b/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb index c9fd3dc271..801fdfdf74 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb @@ -21,7 +21,7 @@ module Hbc end def extract(command: nil, verbose: nil, **_) - container = Container.for_path(path, command) + container = Container.for_path(path) unless container raise CaskError, "Aw dang, could not identify nested container at '#{source}'" diff --git a/Library/Homebrew/cask/lib/hbc/container.rb b/Library/Homebrew/cask/lib/hbc/container.rb index b3d3041739..cae8ea5fb8 100644 --- a/Library/Homebrew/cask/lib/hbc/container.rb +++ b/Library/Homebrew/cask/lib/hbc/container.rb @@ -53,12 +53,18 @@ module Hbc # Hbc::Container::GenericUnar end - def self.for_path(path, command) + def self.for_path(path) odebug "Determining which containers to use based on filetype" - criteria = Criteria.new(path, command) + + magic_number = if path.directory? + "" + else + File.binread(path, 262) || "" + end + autodetect_containers.find do |c| odebug "Checking container class #{c}" - c.me?(criteria) + c.can_extract?(path: path, magic_number: magic_number) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/air.rb b/Library/Homebrew/cask/lib/hbc/container/air.rb index daafc8afed..d4bafc1b80 100644 --- a/Library/Homebrew/cask/lib/hbc/container/air.rb +++ b/Library/Homebrew/cask/lib/hbc/container/air.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Air < Base - def self.me?(criteria) - criteria.path.extname == ".air" + def self.can_extract?(path:, magic_number:) + path.extname == ".air" end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/base.rb b/Library/Homebrew/cask/lib/hbc/container/base.rb index 61de19d1b2..ab81173bfa 100644 --- a/Library/Homebrew/cask/lib/hbc/container/base.rb +++ b/Library/Homebrew/cask/lib/hbc/container/base.rb @@ -34,7 +34,7 @@ module Hbc end def extract_nested_container(source) - container = Container.for_path(source, @command) + container = Container.for_path(source) return false unless container diff --git a/Library/Homebrew/cask/lib/hbc/container/bzip2.rb b/Library/Homebrew/cask/lib/hbc/container/bzip2.rb index 9e06d68900..e9746d6772 100644 --- a/Library/Homebrew/cask/lib/hbc/container/bzip2.rb +++ b/Library/Homebrew/cask/lib/hbc/container/bzip2.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Bzip2 < Base - def self.me?(criteria) - criteria.magic_number(/\ABZh/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\ABZh/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/cab.rb b/Library/Homebrew/cask/lib/hbc/container/cab.rb index d128aa05d3..2615e8e680 100644 --- a/Library/Homebrew/cask/lib/hbc/container/cab.rb +++ b/Library/Homebrew/cask/lib/hbc/container/cab.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Cab < Base - def self.me?(criteria) - criteria.magic_number(/\A(MSCF|MZ)/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\A(MSCF|MZ)/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/dmg.rb b/Library/Homebrew/cask/lib/hbc/container/dmg.rb index c34b80fed6..bd3e9103a8 100644 --- a/Library/Homebrew/cask/lib/hbc/container/dmg.rb +++ b/Library/Homebrew/cask/lib/hbc/container/dmg.rb @@ -5,11 +5,13 @@ require "hbc/container/base" module Hbc class Container class Dmg < Base - def self.me?(criteria) - !criteria.command.run("/usr/bin/hdiutil", - # realpath is a failsafe against unusual filenames - args: ["imageinfo", Pathname.new(criteria.path).realpath], - print_stderr: false).stdout.empty? + def self.can_extract?(path:, magic_number:) + imageinfo = SystemCommand.run("/usr/bin/hdiutil", + # realpath is a failsafe against unusual filenames + args: ["imageinfo", path.realpath], + print_stderr: false).stdout + + !imageinfo.empty? end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/executable.rb b/Library/Homebrew/cask/lib/hbc/container/executable.rb index 286f435622..4f2ebdcc97 100644 --- a/Library/Homebrew/cask/lib/hbc/container/executable.rb +++ b/Library/Homebrew/cask/lib/hbc/container/executable.rb @@ -4,11 +4,11 @@ require "vendor/macho/macho" module Hbc class Container class Executable < Naked - def self.me?(criteria) - return true if criteria.magic_number(/\A#!\s*\S+/n) + def self.can_extract?(path:, magic_number:) + return true if magic_number.match?(/\A#!\s*\S+/n) begin - criteria.path.file? && MachO.open(criteria.path).header.executable? + path.file? && MachO.open(path).header.executable? rescue MachO::MagicError false end diff --git a/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb b/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb index 21cac5a882..bfd1c53729 100644 --- a/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb @@ -3,7 +3,7 @@ require "hbc/container/base" module Hbc class Container class GenericUnar < Base - def self.me?(_criteria) + def self.can_extract?(path:, magic_number:) false end diff --git a/Library/Homebrew/cask/lib/hbc/container/gpg.rb b/Library/Homebrew/cask/lib/hbc/container/gpg.rb index 4c3a4925a1..fc39488ea8 100644 --- a/Library/Homebrew/cask/lib/hbc/container/gpg.rb +++ b/Library/Homebrew/cask/lib/hbc/container/gpg.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Gpg < Base - def self.me?(criteria) - criteria.extension(/^(gpg)$/) + def self.can_extract?(path:, magic_number:) + path.extname == ".gpg" end def import_key diff --git a/Library/Homebrew/cask/lib/hbc/container/gzip.rb b/Library/Homebrew/cask/lib/hbc/container/gzip.rb index a98f1d1d1a..7263f91f8a 100644 --- a/Library/Homebrew/cask/lib/hbc/container/gzip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/gzip.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Gzip < Base - def self.me?(criteria) - criteria.magic_number(/\A\037\213/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\A\037\213/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/lzma.rb b/Library/Homebrew/cask/lib/hbc/container/lzma.rb index 70f2dce459..a1980e903a 100644 --- a/Library/Homebrew/cask/lib/hbc/container/lzma.rb +++ b/Library/Homebrew/cask/lib/hbc/container/lzma.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Lzma < Base - def self.me?(criteria) - criteria.magic_number(/\A\]\000\000\200\000/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\A\]\000\000\200\000/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/naked.rb b/Library/Homebrew/cask/lib/hbc/container/naked.rb index dc265c4025..7ba94f8040 100644 --- a/Library/Homebrew/cask/lib/hbc/container/naked.rb +++ b/Library/Homebrew/cask/lib/hbc/container/naked.rb @@ -3,10 +3,7 @@ require "hbc/container/base" module Hbc class Container class Naked < Base - # Either inherit from this class and override with self.me?(criteria), - # or use this class directly as "container type: :naked", - # in which case self.me? is not called. - def self.me?(*) + def self.can_extract?(path:, magic_number:) false end diff --git a/Library/Homebrew/cask/lib/hbc/container/otf.rb b/Library/Homebrew/cask/lib/hbc/container/otf.rb index 428545d569..d8aa9dbcbe 100644 --- a/Library/Homebrew/cask/lib/hbc/container/otf.rb +++ b/Library/Homebrew/cask/lib/hbc/container/otf.rb @@ -3,8 +3,8 @@ require "hbc/container/naked" module Hbc class Container class Otf < Naked - def self.me?(criteria) - criteria.magic_number(/\AOTTO/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\AOTTO/n) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/pkg.rb b/Library/Homebrew/cask/lib/hbc/container/pkg.rb index bed9aa4839..98f250c949 100644 --- a/Library/Homebrew/cask/lib/hbc/container/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/container/pkg.rb @@ -3,9 +3,9 @@ require "hbc/container/naked" module Hbc class Container class Pkg < Naked - def self.me?(criteria) - criteria.extension(/m?pkg$/) && - (criteria.path.directory? || criteria.magic_number(/\Axar!/n)) + def self.can_extract?(path:, magic_number:) + path.extname.match?(/\A.m?pkg\Z/) && + (path.directory? || magic_number.match?(/\Axar!/n)) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/rar.rb b/Library/Homebrew/cask/lib/hbc/container/rar.rb index 515f3ce160..1ad334202a 100644 --- a/Library/Homebrew/cask/lib/hbc/container/rar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/rar.rb @@ -3,9 +3,8 @@ require "hbc/container/generic_unar" module Hbc class Container class Rar - def self.me?(criteria) - criteria.magic_number(/\ARar!/n) && - super + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\ARar!/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb b/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb index 6938d77b20..33612fb9c0 100644 --- a/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb @@ -2,9 +2,9 @@ require "hbc/container/generic_unar" module Hbc class Container - class SevenZip < GenericUnar - def self.me?(criteria) - criteria.magic_number(/\A7z\xBC\xAF\x27\x1C/n) + class SevenZip + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/sit.rb b/Library/Homebrew/cask/lib/hbc/container/sit.rb index 68cdd821a2..3a22b48ab9 100644 --- a/Library/Homebrew/cask/lib/hbc/container/sit.rb +++ b/Library/Homebrew/cask/lib/hbc/container/sit.rb @@ -3,8 +3,8 @@ require "hbc/container/generic_unar" module Hbc class Container class Sit < GenericUnar - def self.me?(criteria) - criteria.magic_number(/\AStuffIt/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\AStuffIt/n) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb b/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb index d8795f91d5..fd49857fd8 100644 --- a/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb +++ b/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb @@ -1,8 +1,8 @@ module Hbc class Container class SvnRepository < Base - def self.me?(criteria) - criteria.path.join(".svn").directory? + def self.can_extract?(path:, magic_number:) + (path/".svn").directory? end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/tar.rb b/Library/Homebrew/cask/lib/hbc/container/tar.rb index 6d938ee5ee..3cdbcb226d 100644 --- a/Library/Homebrew/cask/lib/hbc/container/tar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/tar.rb @@ -3,10 +3,13 @@ require "hbc/container/base" module Hbc class Container class Tar < Base - def self.me?(criteria) - criteria.magic_number(/\A.{257}ustar/n) || - # or compressed tar (bzip2/gzip/lzma/xz) - IO.popen(["/usr/bin/tar", "-t", "-f", criteria.path.to_s], err: File::NULL) { |io| !io.read(1).nil? } + def self.can_extract?(path:, magic_number:) + return true if magic_number.match?(/\A.{257}ustar/n) + + # Check if `tar` can list the contents, then it can also extract it. + IO.popen(["tar", "tf", path], err: File::NULL) do |stdout| + !stdout.read(1).nil? + end end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/ttf.rb b/Library/Homebrew/cask/lib/hbc/container/ttf.rb index cfce72807d..3a64c796a2 100644 --- a/Library/Homebrew/cask/lib/hbc/container/ttf.rb +++ b/Library/Homebrew/cask/lib/hbc/container/ttf.rb @@ -3,11 +3,11 @@ require "hbc/container/naked" module Hbc class Container class Ttf < Naked - def self.me?(criteria) + def self.can_extract?(path:, magic_number:) # TrueType Font - criteria.magic_number(/\A\000\001\000\000\000/n) || + magic_number.match?(/\A\000\001\000\000\000/n) || # Truetype Font Collection - criteria.magic_number(/\Attcf/n) + magic_number.match?(/\Attcf/n) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/xar.rb b/Library/Homebrew/cask/lib/hbc/container/xar.rb index 47ac99aee2..0e665b64a0 100644 --- a/Library/Homebrew/cask/lib/hbc/container/xar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/xar.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Xar < Base - def self.me?(criteria) - criteria.magic_number(/\Axar!/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\Axar!/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/xz.rb b/Library/Homebrew/cask/lib/hbc/container/xz.rb index 2063eaf0e5..5980e84d58 100644 --- a/Library/Homebrew/cask/lib/hbc/container/xz.rb +++ b/Library/Homebrew/cask/lib/hbc/container/xz.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Xz < Base - def self.me?(criteria) - criteria.magic_number(/\A\xFD7zXZ\x00/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\A\xFD7zXZ\x00/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/container/zip.rb b/Library/Homebrew/cask/lib/hbc/container/zip.rb index 42c5e5f84f..aee5485ffe 100644 --- a/Library/Homebrew/cask/lib/hbc/container/zip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/zip.rb @@ -3,8 +3,8 @@ require "hbc/container/base" module Hbc class Container class Zip < Base - def self.me?(criteria) - criteria.magic_number(/\APK(\003\004|\005\006)/n) + def self.can_extract?(path:, magic_number:) + magic_number.match?(/\APK(\003\004|\005\006)/n) end def extract diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index bb976bc7be..47bb770261 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -149,7 +149,7 @@ module Hbc container = if @cask.container&.type Container.from_type(@cask.container.type) else - Container.for_path(@downloaded_path, @command) + Container.for_path(@downloaded_path) end container&.new(@cask, @downloaded_path, @command, verbose: verbose?) From 82482f4787e61bfb5a9dd0551b8941aef25b9ff0 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 16 Jul 2018 09:54:37 +0200 Subject: [PATCH 2/2] Add support for self-extracting `.exe` archives. --- Library/Homebrew/cask/lib/hbc/container.rb | 2 ++ .../hbc/container/self_extracting_executable.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 Library/Homebrew/cask/lib/hbc/container/self_extracting_executable.rb diff --git a/Library/Homebrew/cask/lib/hbc/container.rb b/Library/Homebrew/cask/lib/hbc/container.rb index cae8ea5fb8..cce04afd81 100644 --- a/Library/Homebrew/cask/lib/hbc/container.rb +++ b/Library/Homebrew/cask/lib/hbc/container.rb @@ -6,6 +6,7 @@ require "hbc/container/bzip2" require "hbc/container/cab" require "hbc/container/criteria" require "hbc/container/dmg" +require "hbc/container/self_extracting_executable" require "hbc/container/executable" require "hbc/container/generic_unar" require "hbc/container/gpg" @@ -32,6 +33,7 @@ module Hbc Ttf, Otf, Air, + SelfExtractingExecutable, Cab, Dmg, SevenZip, diff --git a/Library/Homebrew/cask/lib/hbc/container/self_extracting_executable.rb b/Library/Homebrew/cask/lib/hbc/container/self_extracting_executable.rb new file mode 100644 index 0000000000..e620c510d3 --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/container/self_extracting_executable.rb @@ -0,0 +1,15 @@ +require "hbc/container/generic_unar" + +module Hbc + class Container + class SelfExtractingExecutable < GenericUnar + def self.can_extract?(path:, magic_number:) + return false unless magic_number.match?(/\AMZ/n) + + SystemCommand.run("file", + args: [path], + print_stderr: false).stdout.include?("self-extracting") + end + end + end +end