Merge pull request #4479 from reitermarkus/refactor-containers

Change `Container::me?` to `Container::can_extract?`.
This commit is contained in:
Markus Reiter 2018-07-16 11:11:59 +02:00 committed by GitHub
commit 7cc3c2a846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 83 additions and 59 deletions

View File

@ -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}'"

View File

@ -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,
@ -53,12 +55,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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?)