From 589c5b4e8dc4fab625868672d138e46290b19ecf Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 16 Jul 2018 19:00:49 +0200 Subject: [PATCH] Add support for nested archives. --- Library/Homebrew/test/unpack_strategy_spec.rb | 23 +++++++++++ Library/Homebrew/unpack_strategy.rb | 38 +++++++++++++++---- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/test/unpack_strategy_spec.rb b/Library/Homebrew/test/unpack_strategy_spec.rb index c24e417309..69efd7d886 100644 --- a/Library/Homebrew/test/unpack_strategy_spec.rb +++ b/Library/Homebrew/test/unpack_strategy_spec.rb @@ -15,6 +15,29 @@ RSpec.shared_examples "#extract" do |children: []| end end +describe UnpackStrategy do + describe "#extract_nestedly" do + let(:file_name) { "file" } + let(:nested_archive) { + dir = mktmpdir + + (dir/"file").write "This file was inside a GZIP inside a BZIP2." + system "gzip", dir.children.first + system "bzip2", dir.children.first + + dir.children.first + } + let(:unpack_dir) { mktmpdir } + subject(:strategy) { described_class.detect(nested_archive) } + + it "can extract nested archives" do + strategy.extract_nestedly(to: unpack_dir) + + expect(File.read(unpack_dir/file_name)).to eq("This file was inside a GZIP inside a BZIP2.") + end + end +end + describe UncompressedUnpackStrategy do let(:path) { (mktmpdir/"test").tap do |path| diff --git a/Library/Homebrew/unpack_strategy.rb b/Library/Homebrew/unpack_strategy.rb index 96c0f14ce6..657e89bc1e 100644 --- a/Library/Homebrew/unpack_strategy.rb +++ b/Library/Homebrew/unpack_strategy.rb @@ -67,6 +67,30 @@ class UnpackStrategy unpack_dir.mkpath extract_to_dir(unpack_dir, basename: basename) end + + def extract_nestedly(to: nil, basename: nil) + if is_a?(UncompressedUnpackStrategy) + extract(to: to, basename: basename) + return + end + + Dir.mktmpdir do |tmp_unpack_dir| + tmp_unpack_dir = Pathname(tmp_unpack_dir) + + extract(to: tmp_unpack_dir, basename: basename) + + children = tmp_unpack_dir.children + + if children.count == 1 + s = self.class.detect(children.first) + + s.extract_nestedly(to: to, basename: basename) + next + end + + DirectoryUnpackStrategy.new(tmp_unpack_dir).extract(to: to) + end + end end class DirectoryUnpackStrategy < UnpackStrategy @@ -166,7 +190,7 @@ class CompressUnpackStrategy < TarUnpackStrategy end end -class XzUnpackStrategy < UncompressedUnpackStrategy +class XzUnpackStrategy < UnpackStrategy def self.can_extract?(path:, magic_number:) magic_number.match?(/\A\xFD7zXZ\x00/n) end @@ -192,7 +216,7 @@ class XzUnpackStrategy < UncompressedUnpackStrategy end end -class Bzip2UnpackStrategy < UncompressedUnpackStrategy +class Bzip2UnpackStrategy < UnpackStrategy def self.can_extract?(path:, magic_number:) magic_number.match?(/\ABZh/n) end @@ -200,12 +224,12 @@ class Bzip2UnpackStrategy < UncompressedUnpackStrategy private def extract_to_dir(unpack_dir, basename:) - super + FileUtils.cp path, unpack_dir/basename, preserve: true safe_system "bunzip2", "-q", unpack_dir/basename end end -class GzipUnpackStrategy < UncompressedUnpackStrategy +class GzipUnpackStrategy < UnpackStrategy def self.can_extract?(path:, magic_number:) magic_number.match?(/\A\037\213/n) end @@ -213,12 +237,12 @@ class GzipUnpackStrategy < UncompressedUnpackStrategy private def extract_to_dir(unpack_dir, basename:) - super + FileUtils.cp path, unpack_dir/basename, preserve: true safe_system "gunzip", "-q", "-N", unpack_dir/basename end end -class LzipUnpackStrategy < UncompressedUnpackStrategy +class LzipUnpackStrategy < UnpackStrategy def self.can_extract?(path:, magic_number:) magic_number.match?(/\ALZIP/n) end @@ -226,7 +250,7 @@ class LzipUnpackStrategy < UncompressedUnpackStrategy private def extract_to_dir(unpack_dir, basename:) - super + FileUtils.cp path, unpack_dir/basename, preserve: true safe_system Formula["lzip"].opt_bin/"lzip", "-d", "-q", unpack_dir/basename end end