Method: ZK::Client::Unixisms#mkdir_p

Defined in:
lib/zk/client/unixisms.rb

#mkdir_p(path, opts = {}) ⇒ Object

Creates all parent paths and 'path' in zookeeper as persistent nodes with zero data.

Examples:


zk.exists?('/path')
# => false

zk.mkdir_p('/path/to/blah')
# => "/path/to/blah"  

Parameters:

  • path (String)

    An absolute znode path to create

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :data (String) — default: ''

    The data to place at path



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zk/client/unixisms.rb', line 22

def mkdir_p(path, opts={})
  data = ''

  # if we haven't recursed, or we recursed and now we're back at the top
  if !opts.has_key?(:orig_path) or (path == opts[:orig_path])
    data = opts.fetch(:data, '')  # only put the data at the leaf node
  end

  create(path, data, :mode => :persistent)
rescue NodeExists
  if !opts.has_key?(:orig_path) or (path == opts[:orig_path])  # we're at the leaf node
    set(path, data)
  end

  return
rescue NoNode
  if File.dirname(path) == '/'
    # ok, we're screwed, blow up
    raise NonExistentRootError, "could not create '/', are you chrooted into a non-existent path?", caller
  end

  opts[:orig_path] ||= path

  mkdir_p(File.dirname(path), opts)
  retry
end