commit
						eb110e94f0
					
				@ -16,7 +16,7 @@ module Hbc
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        ohai "Extracting nested container #{source.basename}"
 | 
			
		||||
        container.new(@cask, source, @command).extract
 | 
			
		||||
        container.new(@cask, source, @command, verbose: verbose?).extract
 | 
			
		||||
        FileUtils.remove_entry_secure(source)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,16 @@
 | 
			
		||||
module Hbc
 | 
			
		||||
  class Container
 | 
			
		||||
    class Base
 | 
			
		||||
      def initialize(cask, path, command, nested: false)
 | 
			
		||||
      def initialize(cask, path, command, nested: false, verbose: false)
 | 
			
		||||
        @cask = cask
 | 
			
		||||
        @path = path
 | 
			
		||||
        @command = command
 | 
			
		||||
        @nested = nested
 | 
			
		||||
        @verbose = verbose
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def verbose?
 | 
			
		||||
        @verbose
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def extract_nested_inside(dir)
 | 
			
		||||
@ -32,7 +37,7 @@ module Hbc
 | 
			
		||||
        return false unless container
 | 
			
		||||
 | 
			
		||||
        ohai "Extracting nested container #{source.basename}"
 | 
			
		||||
        container.new(@cask, source, @command, nested: true).extract
 | 
			
		||||
        container.new(@cask, source, @command, nested: true, verbose: verbose?).extract
 | 
			
		||||
 | 
			
		||||
        true
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
@ -13,34 +13,54 @@ module Hbc
 | 
			
		||||
                              print_stderr: false).stdout.empty?
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      attr_reader :mounts
 | 
			
		||||
      def initialize(*args)
 | 
			
		||||
        super(*args)
 | 
			
		||||
        @mounts = []
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def extract
 | 
			
		||||
        mount!
 | 
			
		||||
        assert_mounts_found
 | 
			
		||||
        extract_mounts
 | 
			
		||||
        mount do |mounts|
 | 
			
		||||
          begin
 | 
			
		||||
            raise CaskError, "No mounts found in '#{@path}'; perhaps it is a bad DMG?" if mounts.empty?
 | 
			
		||||
            mounts.each(&method(:extract_mount))
 | 
			
		||||
          ensure
 | 
			
		||||
        eject!
 | 
			
		||||
            mounts.each(&method(:eject))
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def mount!
 | 
			
		||||
        plist = @command.run!("/usr/bin/hdiutil",
 | 
			
		||||
      def mount
 | 
			
		||||
        # realpath is a failsafe against unusual filenames
 | 
			
		||||
                              args:  %w[mount -plist -nobrowse -readonly -noidme -mountrandom /tmp] + [Pathname.new(@path).realpath],
 | 
			
		||||
                              input: "y\n")
 | 
			
		||||
                        .plist
 | 
			
		||||
        @mounts = mounts_from_plist(plist)
 | 
			
		||||
        path = Pathname.new(@path).realpath
 | 
			
		||||
 | 
			
		||||
        Dir.mktmpdir do |unpack_dir|
 | 
			
		||||
          cdr_path = Pathname.new(unpack_dir).join("#{path.basename(".dmg")}.cdr")
 | 
			
		||||
 | 
			
		||||
          without_eula = @command.run("/usr/bin/hdiutil",
 | 
			
		||||
                                 args:  ["attach", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", unpack_dir, path],
 | 
			
		||||
                                 input: "qn\n",
 | 
			
		||||
                                 print_stderr: false)
 | 
			
		||||
 | 
			
		||||
          # If mounting without agreeing to EULA succeeded, there is none.
 | 
			
		||||
          plist = if without_eula.success?
 | 
			
		||||
            without_eula.plist
 | 
			
		||||
          else
 | 
			
		||||
            @command.run!("/usr/bin/hdiutil", args: ["convert", "-quiet", "-format", "UDTO", "-o", cdr_path, path])
 | 
			
		||||
 | 
			
		||||
            with_eula = @command.run!("/usr/bin/hdiutil",
 | 
			
		||||
                          args: ["attach", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", unpack_dir, cdr_path])
 | 
			
		||||
 | 
			
		||||
            if verbose? && !(eula_text = without_eula.stdout).empty?
 | 
			
		||||
              ohai "Software License Agreement for '#{path}':"
 | 
			
		||||
              puts eula_text
 | 
			
		||||
            end
 | 
			
		||||
 | 
			
		||||
      def eject!
 | 
			
		||||
        @mounts.each do |mount|
 | 
			
		||||
            with_eula.plist
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          yield mounts_from_plist(plist)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def eject(mount)
 | 
			
		||||
        # realpath is a failsafe against unusual filenames
 | 
			
		||||
        mountpath = Pathname.new(mount).realpath
 | 
			
		||||
          next unless mountpath.exist?
 | 
			
		||||
        return unless mountpath.exist?
 | 
			
		||||
 | 
			
		||||
        begin
 | 
			
		||||
          tries ||= 3
 | 
			
		||||
@ -60,14 +80,9 @@ module Hbc
 | 
			
		||||
          retry
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      private
 | 
			
		||||
 | 
			
		||||
      def extract_mounts
 | 
			
		||||
        @mounts.each(&method(:extract_mount))
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def extract_mount(mount)
 | 
			
		||||
        Tempfile.open(["", ".bom"]) do |bomfile|
 | 
			
		||||
          bomfile.close
 | 
			
		||||
@ -124,10 +139,6 @@ module Hbc
 | 
			
		||||
        return [] unless plist.respond_to?(:fetch)
 | 
			
		||||
        plist.fetch("system-entities", []).map { |e| e["mount-point"] }.compact
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def assert_mounts_found
 | 
			
		||||
        raise CaskError, "No mounts found in '#{@path}'; perhaps it is a bad DMG?" if @mounts.empty?
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -153,7 +153,7 @@ module Hbc
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      odebug "Using container class #{container} for #{@downloaded_path}"
 | 
			
		||||
      container.new(@cask, @downloaded_path, @command).extract
 | 
			
		||||
      container.new(@cask, @downloaded_path, @command, verbose: verbose?).extract
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def install_artifacts
 | 
			
		||||
 | 
			
		||||
@ -9,11 +9,12 @@ describe Hbc::Container::Dmg, :cask do
 | 
			
		||||
        Hbc::SystemCommand,
 | 
			
		||||
      )
 | 
			
		||||
 | 
			
		||||
      dmg.mount do |mounts|
 | 
			
		||||
        begin
 | 
			
		||||
        dmg.mount!
 | 
			
		||||
        expect(dmg.mounts).not_to include nil
 | 
			
		||||
          expect(mounts).not_to include nil
 | 
			
		||||
        ensure
 | 
			
		||||
        dmg.eject!
 | 
			
		||||
          mounts.each(&dmg.public_method(:eject))
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user