module HDIUtil

/usr/bin/hdiutil

developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/hdiutil.1.html

Constants

DEFAULT_MOUNT_OPTIONS

The default options for hdiutil attach.

DEFAULT_OPTIONS

The default options for hdiutil.

Public Class Methods

read(input) { |mountpoint| ... } click to toggle source

Perform read-only actions on the input image.

If a block is given the block will be yielded with the path of the mount point directory, otherwise a shell will be open.

input - The String path to the input image.

# File lib/iesd/utility/hdiutil.rb, line 22
def self.read input
  Dir.mktmpdir { |mountpoint|
    attach input, mountpoint, [*DEFAULT_OPTIONS, *DEFAULT_MOUNT_OPTIONS]
    if block_given?
      yield mountpoint
    else
      shell mountpoint
    end
    detach input, mountpoint, [*DEFAULT_OPTIONS]
  }
end
validate(url) click to toggle source

Returns true if the image is valid, false otherwise.

url - The String path to the image.

# File lib/iesd/utility/hdiutil.rb, line 75
def self.validate url
  Kernel.system(%Q[/usr/bin/env hdiutil imageinfo #{url.shellescape} >/dev/null 2>&1])
end
write(input, output, options = {}) { |mountpoint| ... } click to toggle source

Perform read-write actions on the input image and export as the output image.

If a block is given the block will be yielded with the path of the mount point directory, otherwise a shell will be open.

input - The String path to the input image. output - The String path to the output image. options - The Dictionary of hdiutil options.

# File lib/iesd/utility/hdiutil.rb, line 41
def self.write input, output, options = {}
  options = {
    :resize => {
      :grow => 0,
      :shrink => false
    }
  }.merge(options)

  Dir.mktmpdir { |tmp|
    shadow = File.join(tmp, "#{File.basename input}.shadow")
    shadow_options = ["-shadow", shadow]
    format_options = ["-format", %x`/usr/bin/env hdiutil imageinfo -format #{input.shellescape}`.chomp]
    Dir.mktmpdir(nil, tmp) { |mountpoint|
      resize_limits = %x`/usr/bin/env hdiutil resize -limits -shadow #{shadow.shellescape} #{input.shellescape}`.chomp.split.map { |s| s.to_i }
      sectors = (resize_limits[1] + options[:resize][:grow]).to_s
      system("/usr/bin/env", "hdiutil", "resize", "-growonly", "-sectors", sectors, *shadow_options, input)
      attach input, mountpoint, [*DEFAULT_OPTIONS, *DEFAULT_MOUNT_OPTIONS, *shadow_options]
      if block_given?
        yield mountpoint
      else
        shell mountpoint
      end
      detach input, mountpoint, [*DEFAULT_OPTIONS]
      system("/usr/bin/env", "hdiutil", "resize", "-shrinkonly", "-sectors", "min", *shadow_options, input) if options[:resize][:shrink]
    }
    oh1 "Merging #{shadow}"
    system("/usr/bin/env", "hdiutil", "convert", *DEFAULT_OPTIONS, *format_options, *shadow_options, "-o", output, input)
    puts "Merged: #{output}"
  }
end

Private Class Methods

attach(dmg, mountpoint, arguments = []) click to toggle source

Mount a DMG.

dmg - The String path to the DMG. mountpoint - The String path to the mount directory. arguments - The Array of the hdiutil attach options.

# File lib/iesd/utility/hdiutil.rb, line 86
def self.attach dmg, mountpoint, arguments = []
  ohai "Mounting #{dmg}"
  system("/usr/bin/env", "hdiutil", "attach", *arguments, "-mountpoint", mountpoint, dmg)
  puts "Mounted: #{mountpoint}"
end
detach(dmg, mountpoint, arguments = []) click to toggle source

Unmount a DMG.

dmg - The String path to the DMG. mountpoint - The String path to the unmount directory. arguments - The Array of the hdiutil detach options.

# File lib/iesd/utility/hdiutil.rb, line 97
def self.detach dmg, mountpoint, arguments = []
  ohai "Unmounting #{dmg}"
  system("/usr/bin/env", "hdiutil", "detach", *arguments, mountpoint)
  puts "Unmounted: #{mountpoint}"
end
shell(dir) click to toggle source

Open a shell in the directory.

dir - The String path to the directory.

# File lib/iesd/utility/hdiutil.rb, line 106
def self.shell dir
  Dir.chdir(dir) {
    ohai ENV['SHELL']
    Kernel.system ENV, ENV['SHELL']
  }
end