Class: Hbc::Container::Dmg

Inherits:
Base
  • Object
show all
Defined in:
Library/Homebrew/cask/lib/hbc/container/dmg.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#extract_nested_container, #extract_nested_inside, #initialize, #verbose?

Constructor Details

This class inherits a constructor from Hbc::Container::Base

Class Method Details

.me?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'Library/Homebrew/cask/lib/hbc/container/dmg.rb', line 9

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?
end

Instance Method Details

#eject(mount) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'Library/Homebrew/cask/lib/hbc/container/dmg.rb', line 60

def eject(mount)
  # realpath is a failsafe against unusual filenames
  mountpath = Pathname.new(mount).realpath
  return unless mountpath.exist?

  begin
    tries ||= 3
    if tries > 1
      @command.run("/usr/sbin/diskutil",
                   args:         ["eject", mountpath],
                   print_stderr: false)
    else
      @command.run("/usr/sbin/diskutil",
                   args:         ["unmount", "force", mountpath],
                   print_stderr: false)
    end
    raise CaskError, "Failed to eject #{mountpath}" if mountpath.exist?
  rescue CaskError => e
    raise e if (tries -= 1).zero?
    sleep 1
    retry
  end
end

#extractObject



16
17
18
19
20
21
22
23
24
25
# File 'Library/Homebrew/cask/lib/hbc/container/dmg.rb', line 16

def extract
  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
      mounts.each(&method(:eject))
    end
  end
end

#mountObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'Library/Homebrew/cask/lib/hbc/container/dmg.rb', line 27

def mount
  # realpath is a failsafe against unusual filenames
  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

      with_eula.plist
    end

    yield mounts_from_plist(plist)
  end
end