Fix DMG mounting.

This commit is contained in:
Markus Reiter 2017-06-11 03:55:20 +02:00
parent 054ed10cb1
commit 2f0aad5d88
2 changed files with 51 additions and 55 deletions

View File

@ -13,34 +13,38 @@ module Hbc
print_stderr: false).stdout.empty? print_stderr: false).stdout.empty?
end end
attr_reader :mounts
def initialize(*args)
super(*args)
@mounts = []
end
def extract def extract
mount! mount do |mounts|
assert_mounts_found begin
extract_mounts raise CaskError, "No mounts found in '#{@path}'; perhaps it is a bad DMG?" if mounts.empty?
mounts.each(&method(:extract_mount))
ensure ensure
eject! mounts.each(&method(:eject))
end
end
end end
def mount! def mount
plist = @command.run!("/usr/bin/hdiutil",
# realpath is a failsafe against unusual filenames # realpath is a failsafe against unusual filenames
args: %w[mount -plist -nobrowse -readonly -noidme -mountrandom /tmp] + [Pathname.new(@path).realpath], path = Pathname.new(@path).realpath
input: "y\n")
.plist Dir.mktmpdir do |unpack_dir|
@mounts = mounts_from_plist(plist) cdr_path = Pathname.new(unpack_dir).join("#{path.basename(".dmg")}.cdr")
@command.run!("/usr/bin/hdiutil", args: ["convert", "-quiet", "-format", "UDTO", "-o", cdr_path, path])
plist = @command.run!("/usr/bin/hdiutil",
args: ["attach", "-plist", "-nobrowse", "-readonly", "-noidme", "-mountrandom", unpack_dir, cdr_path],
input: "qy\n").plist
yield mounts_from_plist(plist)
end
end end
def eject! def eject(mount)
@mounts.each do |mount|
# realpath is a failsafe against unusual filenames # realpath is a failsafe against unusual filenames
mountpath = Pathname.new(mount).realpath mountpath = Pathname.new(mount).realpath
next unless mountpath.exist? return unless mountpath.exist?
begin begin
tries ||= 3 tries ||= 3
@ -60,14 +64,9 @@ module Hbc
retry retry
end end
end end
end
private private
def extract_mounts
@mounts.each(&method(:extract_mount))
end
def extract_mount(mount) def extract_mount(mount)
Tempfile.open(["", ".bom"]) do |bomfile| Tempfile.open(["", ".bom"]) do |bomfile|
bomfile.close bomfile.close
@ -124,10 +123,6 @@ module Hbc
return [] unless plist.respond_to?(:fetch) return [] unless plist.respond_to?(:fetch)
plist.fetch("system-entities", []).map { |e| e["mount-point"] }.compact plist.fetch("system-entities", []).map { |e| e["mount-point"] }.compact
end 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 end
end end

View File

@ -9,11 +9,12 @@ describe Hbc::Container::Dmg, :cask do
Hbc::SystemCommand, Hbc::SystemCommand,
) )
dmg.mount do |mounts|
begin begin
dmg.mount! expect(mounts).not_to include nil
expect(dmg.mounts).not_to include nil
ensure ensure
dmg.eject! mounts.each(&dmg.public_method(:eject))
end
end end
end end
end end