Module: Mountfd::Attributes

Defined in:
lib/mountfd/attributes.rb,
sig/mountfd.rbs

Constant Summary collapse

VALUES =
{
  rdonly: Native::MOUNT_ATTR_RDONLY,
  nosuid: Native::MOUNT_ATTR_NOSUID,
  nodev: Native::MOUNT_ATTR_NODEV,
  noexec: Native::MOUNT_ATTR_NOEXEC,
  nodiratime: Native::MOUNT_ATTR_NODIRATIME,
  nosymfollow: Native::MOUNT_ATTR_NOSYMFOLLOW,
  idmap: Native::MOUNT_ATTR_IDMAP
}.freeze
ATIME =
{
  relatime: Native::MOUNT_ATTR_RELATIME,
  noatime: Native::MOUNT_ATTR_NOATIME,
  strictatime: Native::MOUNT_ATTR_STRICTATIME
}.freeze
PROPAGATION =
{
  unbindable: Native::MS_UNBINDABLE,
  private: Native::MS_PRIVATE,
  slave: Native::MS_SLAVE,
  shared: Native::MS_SHARED
}.freeze

Class Method Summary collapse

Class Method Details

.build(attributes) ⇒ [Integer, Integer]

Parameters:

  • attributes (Hash[Symbol | String, untyped])

Returns:

  • ([Integer, Integer])


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mountfd/attributes.rb', line 26

def self.build(attributes)
  set = clr = 0
  seen = {}
  attributes.each do |name, value|
    key = name.respond_to?(:to_sym) ? name.to_sym : name
    raise ArgumentError, "duplicate mount attribute: #{name.inspect}" if seen[key]

    seen[key] = true
    if key == :atime
      mode = value.respond_to?(:to_sym) ? value.to_sym : value
      raise ArgumentError, "unknown atime mode: #{value.inspect}" unless ATIME.key?(mode)

      clr |= Native::MOUNT_ATTR__ATIME
      set |= ATIME.fetch(mode)
      next
    end

    flag = VALUES.fetch(key) { raise ArgumentError, "unknown mount attribute: #{name.inspect}" }
    value ? set |= flag : clr |= flag
  end
  [set, clr]
end

.flags(names) ⇒ Integer

Parameters:

  • names (Integer, Array[Symbol | String])

Returns:

  • (Integer)


49
50
51
52
53
54
55
56
# File 'lib/mountfd/attributes.rb', line 49

def self.flags(names)
  return names if names.is_a?(Integer)

  Array(names).reduce(0) do |flags, name|
    key = name.respond_to?(:to_sym) ? name.to_sym : name
    flags | VALUES.fetch(key) { raise ArgumentError, "unknown mount attribute: #{name.inspect}" }
  end
end

.propagation(value) ⇒ Integer

Parameters:

  • value (Symbol, String, nil)

Returns:

  • (Integer)


58
59
60
61
62
63
# File 'lib/mountfd/attributes.rb', line 58

def self.propagation(value)
  return 0 if value.nil?

  key = value.respond_to?(:to_sym) ? value.to_sym : value
  PROPAGATION.fetch(key) { raise ArgumentError, "unknown propagation: #{value.inspect}" }
end