Module: Libvirt::Util

Extended by:
Loggable::ClassMethods
Defined in:
lib/libvirt/util.rb

Constant Summary collapse

UNIT_TO_BYTES =
{
    b: 1,
    bytes: 1,
    KB: 1_000,
    KiB: 1_024,
    k: 1_024,
    MB: 1_000_000,
    M: 1_048_576,
    MiB: 1_048_576,
    GB: 1_000_000_000,
    G: 1_073_741_824,
    GiB: 1_073_741_824,
    TB: 1_000_000_000_000,
    T: 1_099_511_627_776,
    TiB: 1_099_511_627_776
}.freeze
UUID_STRING_BUFLEN =

RFC4122

0x80

Class Method Summary collapse

Methods included from Loggable::ClassMethods

dbg, err

Class Method Details

.define_finalizer(object, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/libvirt/util.rb', line 111

def define_finalizer(object, &block)
  free = ->(obj_id) do
    obj_id_hex = "0x#{obj_id.to_s(16)}"
    ptr = object.to_ptr
    ptr_hex = "0x#{ptr.address.to_s(16)}"
    klass = object.class
    dbg { "Finalize #{klass} object_id=#{obj_id_hex}, pointer=#{ptr_hex}" }
    return if ptr.null?

    cl_result = block.call(ptr)
    err { "Couldn't close #{klass} object_id=#{obj_id_hex}, pointer=#{ptr_hex}" } if cl_result.negative?
  end
  ObjectSpace.define_finalizer(object, free)
end

.library_pathObject



44
45
46
# File 'lib/libvirt/util.rb', line 44

def library_path
  %w[libvirt libvirt.so.0]
end

.log(severity, prog = nil, &block) ⇒ Object



38
39
40
41
42
# File 'lib/libvirt/util.rb', line 38

def log(severity, prog = nil, &block)
  return if logger.nil?

  logger.public_send(severity, prog, &block)
end

.loggerObject



30
31
32
# File 'lib/libvirt/util.rb', line 30

def logger
  @logger if defined?(@logger)
end

.logger=(value) ⇒ Object



34
35
36
# File 'lib/libvirt/util.rb', line 34

def logger=(value)
  @logger = value
end

.parse_enum(enum, value) ⇒ Array

Returns event_id, event_id_sym.

Parameters:

  • enum (FFI::Enum)
  • value (Symbol, Integer)

Returns:

  • (Array)

    event_id, event_id_sym

Raises:

  • ArgumentError



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/libvirt/util.rb', line 60

def parse_enum(enum, value)
  if value.is_a?(Symbol)
    raise ArgumentError, 'invalid enum value' unless enum.symbols.include?(value)

    return [enum.find(value), value]
  end

  raise ArgumentError, 'invalid enum value' unless enum.symbol_map.values.include?(value)

  [value, enum.symbol_map[value]]
end

.parse_flags(flags, enum, default: 0x0) ⇒ Integer

Bitwise OR integer flags calculation for C language.

Parameters:

  • flags (Integer, Symbol, Array<Symbol>, Hash{Symbol=>Boolean}, nil)
  • enum (FFI::Enum)
  • default (Integer) (defaults to: 0x0)

    optional (default 0x0)

Returns:

  • (Integer)

    bitwise OR of keys @example Usage:

    parse_flags(nil, enum)
    parse_flags({MANAGED_SAVE: true, SNAPSHOTS_METADATA: true, NVRAM: false}, enum)
    parse_flags({managed_save: true, snapshots_metadata: true, keep_nvram: nil}, enum)
    parse_flags(3, enum)
    parse_flags([:MANAGED_SAVE, :SNAPSHOTS_METADATA], enum)
    parse_flags([:managed_save, :snapshots_metadata], enum)
    

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/libvirt/util.rb', line 85

def parse_flags(flags, enum, default: 0x0)
  flags = default if flags.nil?
  flags = enum[flags] if flags.is_a?(Symbol)
  return flags if flags.is_a?(Integer)

  result = 0x0
  flags = flags.select { |_, v| v }.keys if flags.is_a?(Hash)

  raise ArgumentError, 'flags must be an Integer or a Hash or an Array' unless flags.is_a?(Array)

  flags.each do |key|
    result |= enum[key.to_s.upcase.to_sym]
  end

  result
end

.parse_memory(value, unit) ⇒ Integer

Returns memory in bytes.

Parameters:

  • value (Integer, String)
  • unit (String, Symbol)

    default ‘bytes’

Returns:

  • (Integer)

    memory in bytes



105
106
107
108
109
# File 'lib/libvirt/util.rb', line 105

def parse_memory(value, unit)
  unit ||= 'bytes'
  multiplier = UNIT_TO_BYTES.fetch(unit.to_sym)
  Integer(value) * multiplier
end

.parse_version(version_number) ⇒ Object

Parameters:

  • version_number (Integer)

    ulong



49
50
51
52
53
54
# File 'lib/libvirt/util.rb', line 49

def parse_version(version_number)
  major = version_number / 1_000_000
  minor = (version_number - major * 1_000_000) / 1_000
  release = version_number - major * 1_000_000 - minor * 1_000
  "#{major}.#{minor}.#{release}"
end