Merge pull request #4479 from reitermarkus/refactor-containers
Change `Container::me?` to `Container::can_extract?`.
This commit is contained in:
commit
7cc3c2a846
@ -21,7 +21,7 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def extract(command: nil, verbose: nil, **_)
|
def extract(command: nil, verbose: nil, **_)
|
||||||
container = Container.for_path(path, command)
|
container = Container.for_path(path)
|
||||||
|
|
||||||
unless container
|
unless container
|
||||||
raise CaskError, "Aw dang, could not identify nested container at '#{source}'"
|
raise CaskError, "Aw dang, could not identify nested container at '#{source}'"
|
||||||
|
|||||||
@ -6,6 +6,7 @@ require "hbc/container/bzip2"
|
|||||||
require "hbc/container/cab"
|
require "hbc/container/cab"
|
||||||
require "hbc/container/criteria"
|
require "hbc/container/criteria"
|
||||||
require "hbc/container/dmg"
|
require "hbc/container/dmg"
|
||||||
|
require "hbc/container/self_extracting_executable"
|
||||||
require "hbc/container/executable"
|
require "hbc/container/executable"
|
||||||
require "hbc/container/generic_unar"
|
require "hbc/container/generic_unar"
|
||||||
require "hbc/container/gpg"
|
require "hbc/container/gpg"
|
||||||
@ -32,6 +33,7 @@ module Hbc
|
|||||||
Ttf,
|
Ttf,
|
||||||
Otf,
|
Otf,
|
||||||
Air,
|
Air,
|
||||||
|
SelfExtractingExecutable,
|
||||||
Cab,
|
Cab,
|
||||||
Dmg,
|
Dmg,
|
||||||
SevenZip,
|
SevenZip,
|
||||||
@ -53,12 +55,18 @@ module Hbc
|
|||||||
# Hbc::Container::GenericUnar
|
# Hbc::Container::GenericUnar
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.for_path(path, command)
|
def self.for_path(path)
|
||||||
odebug "Determining which containers to use based on filetype"
|
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|
|
autodetect_containers.find do |c|
|
||||||
odebug "Checking container class #{c}"
|
odebug "Checking container class #{c}"
|
||||||
c.me?(criteria)
|
c.can_extract?(path: path, magic_number: magic_number)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Air < Base
|
class Air < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.path.extname == ".air"
|
path.extname == ".air"
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -34,7 +34,7 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def extract_nested_container(source)
|
def extract_nested_container(source)
|
||||||
container = Container.for_path(source, @command)
|
container = Container.for_path(source)
|
||||||
|
|
||||||
return false unless container
|
return false unless container
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Bzip2 < Base
|
class Bzip2 < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\ABZh/n)
|
magic_number.match?(/\ABZh/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Cab < Base
|
class Cab < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A(MSCF|MZ)/n)
|
magic_number.match?(/\A(MSCF|MZ)/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -5,11 +5,13 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Dmg < Base
|
class Dmg < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
!criteria.command.run("/usr/bin/hdiutil",
|
imageinfo = SystemCommand.run("/usr/bin/hdiutil",
|
||||||
# realpath is a failsafe against unusual filenames
|
# realpath is a failsafe against unusual filenames
|
||||||
args: ["imageinfo", Pathname.new(criteria.path).realpath],
|
args: ["imageinfo", path.realpath],
|
||||||
print_stderr: false).stdout.empty?
|
print_stderr: false).stdout
|
||||||
|
|
||||||
|
!imageinfo.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -4,11 +4,11 @@ require "vendor/macho/macho"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Executable < Naked
|
class Executable < Naked
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
return true if criteria.magic_number(/\A#!\s*\S+/n)
|
return true if magic_number.match?(/\A#!\s*\S+/n)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
criteria.path.file? && MachO.open(criteria.path).header.executable?
|
path.file? && MachO.open(path).header.executable?
|
||||||
rescue MachO::MagicError
|
rescue MachO::MagicError
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,7 +3,7 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class GenericUnar < Base
|
class GenericUnar < Base
|
||||||
def self.me?(_criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Gpg < Base
|
class Gpg < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.extension(/^(gpg)$/)
|
path.extname == ".gpg"
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_key
|
def import_key
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Gzip < Base
|
class Gzip < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A\037\213/n)
|
magic_number.match?(/\A\037\213/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Lzma < Base
|
class Lzma < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A\]\000\000\200\000/n)
|
magic_number.match?(/\A\]\000\000\200\000/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,10 +3,7 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Naked < Base
|
class Naked < Base
|
||||||
# Either inherit from this class and override with self.me?(criteria),
|
def self.can_extract?(path:, magic_number:)
|
||||||
# or use this class directly as "container type: :naked",
|
|
||||||
# in which case self.me? is not called.
|
|
||||||
def self.me?(*)
|
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/naked"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Otf < Naked
|
class Otf < Naked
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\AOTTO/n)
|
magic_number.match?(/\AOTTO/n)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,9 +3,9 @@ require "hbc/container/naked"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Pkg < Naked
|
class Pkg < Naked
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.extension(/m?pkg$/) &&
|
path.extname.match?(/\A.m?pkg\Z/) &&
|
||||||
(criteria.path.directory? || criteria.magic_number(/\Axar!/n))
|
(path.directory? || magic_number.match?(/\Axar!/n))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,9 +3,8 @@ require "hbc/container/generic_unar"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Rar
|
class Rar
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\ARar!/n) &&
|
magic_number.match?(/\ARar!/n)
|
||||||
super
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -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
|
||||||
@ -2,9 +2,9 @@ require "hbc/container/generic_unar"
|
|||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class SevenZip < GenericUnar
|
class SevenZip
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A7z\xBC\xAF\x27\x1C/n)
|
magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/generic_unar"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Sit < GenericUnar
|
class Sit < GenericUnar
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\AStuffIt/n)
|
magic_number.match?(/\AStuffIt/n)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class SvnRepository < Base
|
class SvnRepository < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.path.join(".svn").directory?
|
(path/".svn").directory?
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,10 +3,13 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Tar < Base
|
class Tar < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A.{257}ustar/n) ||
|
return true if magic_number.match?(/\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? }
|
# 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
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,11 +3,11 @@ require "hbc/container/naked"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Ttf < Naked
|
class Ttf < Naked
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
# TrueType Font
|
# 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
|
# Truetype Font Collection
|
||||||
criteria.magic_number(/\Attcf/n)
|
magic_number.match?(/\Attcf/n)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Xar < Base
|
class Xar < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\Axar!/n)
|
magic_number.match?(/\Axar!/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Xz < Base
|
class Xz < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\A\xFD7zXZ\x00/n)
|
magic_number.match?(/\A\xFD7zXZ\x00/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -3,8 +3,8 @@ require "hbc/container/base"
|
|||||||
module Hbc
|
module Hbc
|
||||||
class Container
|
class Container
|
||||||
class Zip < Base
|
class Zip < Base
|
||||||
def self.me?(criteria)
|
def self.can_extract?(path:, magic_number:)
|
||||||
criteria.magic_number(/\APK(\003\004|\005\006)/n)
|
magic_number.match?(/\APK(\003\004|\005\006)/n)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract
|
def extract
|
||||||
|
|||||||
@ -149,7 +149,7 @@ module Hbc
|
|||||||
container = if @cask.container&.type
|
container = if @cask.container&.type
|
||||||
Container.from_type(@cask.container.type)
|
Container.from_type(@cask.container.type)
|
||||||
else
|
else
|
||||||
Container.for_path(@downloaded_path, @command)
|
Container.for_path(@downloaded_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
container&.new(@cask, @downloaded_path, @command, verbose: verbose?)
|
container&.new(@cask, @downloaded_path, @command, verbose: verbose?)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user