module XenStore::Utils

XenStore::Utils implements utility methods which are unlikely to be required by users but are used by the rest of the module

Public Class Methods

error(n) click to toggle source

Convert an error number or symbol to an Errno exception

@param n [Integer, Symbol] An Integer or symbol representing the

Errno exception to return.

@return [Exception] The Exception representing the provided type

of error.
# File lib/xsrb/utils.rb, line 68
def error(n)
  if n.is_a? Integer
    @errno_exception_map[n]
  else
    Errno.send(n)
  end
end
next_request_id() click to toggle source

Get the next request ID to contact XenStore with.

@return [Integer] The next ID in the sequence.

# File lib/xsrb/utils.rb, line 79
def next_request_id
  @reqid += 1

  # Ensure no larger than uint32_t which is used in xs_wire.h
  @reqid %= MAX_UINT
end
unix_socket_path() click to toggle source

Get the path of the XenStore unix socket.

@return [String] The path to the XenStore unix socket.

# File lib/xsrb/utils.rb, line 89
def unix_socket_path
  dp = '/var/run/xenstored'
  ENV['XENSTORED_PATH'] || File.join(ENV['XENSTORED_RUNDIR'] || dp,
                                     'socket')
end
valid_path?(path) click to toggle source

Raise an exception if the provided path is invalid.

@param path [String] The XenStore path to check. @return [String] The valid path.

# File lib/xsrb/utils.rb, line 99
def valid_path?(path)
  pathname = Pathname.new path
  max_len = pathname.absolute? ? 3072 : 2048

  if path.length > max_len
    raise XenStore::Exceptions::InvalidPath,
          "Path too long: #{path}"
  end

  unless @path_regex =~ path
    raise XenStore::Exceptions::InvalidPath,
          path.to_s
  end

  path
end
valid_permissions?(perms) click to toggle source

Check if every member of a list of permissions strings is valid.

@param perms [Array, String] An Array of XenStore permissions

specifications

@return [Array] The list of permissions.

# File lib/xsrb/utils.rb, line 133
def valid_permissions?(perms)
  perms = [perms] if perms.is_a? String
  perms.each do |perm|
    unless perm =~ @perms_regex
      raise XenStore::Exceptions::InvalidPermission,
            "Invalid permission string: #{perm}"
    end
  end
end
valid_watch_path?(path) click to toggle source

Raise an exception if the provided XenStore watch path is invalid.

@param path [String] The XenStore watch path to check. @return [String] The valid path.

# File lib/xsrb/utils.rb, line 120
def valid_watch_path?(path)
  if path.starts_with?('@') && (@watch_path_regex !~ path)
    raise XenStore::Exceptions::InvalidPath, path.to_s
  end

  valid_path? path
end
xenbus_path() click to toggle source

Get the XenBus path on this system

@return [String] The path to the XenBus device

# File lib/xsrb/utils.rb, line 146
def xenbus_path
  default = '/dev/xen/xenbus'
  host_os = RbConfig::CONFIG['host_os']

  case host_os
  when 'netbsd'
    '/kern/xen/xenbus'
  when 'linux'
    File.readable?('/dev/xen/xenbus') ? '/proc/xen/xenbus' : default
  when /mswin|windows/i
    raise NotImplementedError,
          "OS '#{RbConfig::CONFIG['host_os']}' is not supported"
  else
    default
  end
end