Class: OsCtld::Devices::Mode

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/devices/mode.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Mode

Returns a new instance of Mode.

Parameters:

  • str (String)

    mode as string, e.g. ‘rwm`, `r`, rw`



16
17
18
# File 'lib/osctld/devices/mode.rb', line 16

def initialize(str)
  @mode = self.class.normalize(str)
end

Instance Attribute Details

#modeArray<String> (readonly)

Returns:

  • (Array<String>)


13
14
15
# File 'lib/osctld/devices/mode.rb', line 13

def mode
  @mode
end

Class Method Details

.normalize(str) ⇒ Array<String>

Parameters:

  • str (String)

    mode as string, e.g. ‘rwm`, `r`, rw`

Returns:

  • (Array<String>)


5
6
7
8
9
10
# File 'lib/osctld/devices/mode.rb', line 5

def self.normalize(str)
  mode = str.chars
  mode.sort!
  mode.uniq!
  mode
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
# File 'lib/osctld/devices/mode.rb', line 74

def ==(other)
  other.mode == mode
end

#cloneObject



70
71
72
# File 'lib/osctld/devices/mode.rb', line 70

def clone
  self.class.new(to_s)
end

#compatible?(other) ⇒ Boolean

Return ‘true` if self is a superset of `other` or equal to `other`

Parameters:

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/osctld/devices/mode.rb', line 22

def compatible?(other)
  return true if other.mode == mode

  other.mode.each do |m|
    return false unless mode.include?(m)
  end

  true
end

#complement(other) ⇒ Object

Expand access mode with modes from ‘other` that are missing in self

Parameters:



34
35
36
37
# File 'lib/osctld/devices/mode.rb', line 34

def complement(other)
  @mode = (@mode + other.mode).sort!
  @mode.uniq!
end

#diff(target) ⇒ Hash<Symbol, String>

Generate diff to reach the ‘target` mode from the current mode

The return value is a hash describing actions that need to be taken to update cgroups. The hash has two keys: ‘:allow` and `:deny`, each pointing to a mode that has to be allowed/denied.

For example, for transition from ‘rm` to `wm`, the return value would be:

{allow: 'w', deny: 'r'}

Parameters:

  • target (Mode)

    target mode

Returns:

  • (Hash<Symbol, String>)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osctld/devices/mode.rb', line 51

def diff(target)
  ret = { allow: [], deny: [] }

  %w[r w m].each do |m|
    if target.mode.include?(m) && !mode.include?(m)
      ret[:allow] << m

    elsif !target.mode.include?(m) && mode.include?(m)
      ret[:deny] << m
    end
  end

  ret.transform_values(&:join)
end

#to_sObject



66
67
68
# File 'lib/osctld/devices/mode.rb', line 66

def to_s
  %w[r w m].select { |m| mode.include?(m) }.join
end