Class: Mountfd::FsContext

Inherits:
Object
  • Object
show all
Defined in:
lib/mountfd/core.rb,
sig/mountfd.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filesystem, flags: 0, handle: nil) ⇒ FsContext

Returns a new instance of FsContext.

Parameters:

  • filesystem (String)
  • flags: (Integer) (defaults to: 0)
  • handle: (Object) (defaults to: nil)


40
41
42
43
# File 'lib/mountfd/core.rb', line 40

def initialize(filesystem, flags: 0, handle: nil)
  @handle = handle || Native.fsopen(filesystem.to_s, flags | Native::FSOPEN_CLOEXEC)
  @diagnostics = []
end

Instance Attribute Details

#diagnosticsArray[Diagnostic] (readonly)

Returns the value of attribute diagnostics.

Returns:



23
24
25
# File 'lib/mountfd/core.rb', line 23

def diagnostics
  @diagnostics
end

Class Method Details

.openvoid .open(filesystem, flags:) ⇒ FsContext

Overloads:

  • .openvoid

    This method returns an undefined value.

  • .open(filesystem, flags:) ⇒ FsContext

    Parameters:

    • filesystem (String)
    • flags: (Integer)

    Returns:



25
26
27
28
29
30
31
32
33
34
# File 'lib/mountfd/core.rb', line 25

def self.open(filesystem, flags: 0)
  context = new(filesystem, flags: flags)
  return context unless block_given?

  begin
    yield context
  ensure
    context.close unless context.closed?
  end
end

.pick(path, flags: 0) ⇒ FsContext

Parameters:

  • path (String)
  • flags: (Integer) (defaults to: 0)

Returns:



36
37
38
# File 'lib/mountfd/core.rb', line 36

def self.pick(path, flags: 0)
  new(nil, handle: Native.fspick(AT_FDCWD, File.path(path), flags | Native::FSPICK_CLOEXEC))
end

Instance Method Details

#closenil

Returns:

  • (nil)


85
# File 'lib/mountfd/core.rb', line 85

def close = @handle.close

#closed?Boolean

Returns:

  • (Boolean)


46
# File 'lib/mountfd/core.rb', line 46

def closed? = @handle.closed?

#create!(exclusive: false) ⇒ FsContext

Parameters:

  • exclusive: (Boolean) (defaults to: false)

Returns:



63
64
65
66
67
68
69
70
# File 'lib/mountfd/core.rb', line 63

def create!(exclusive: false)
  if exclusive && !Mountfd.features.include?(:create_excl)
    raise UnsupportedError, "exclusive fsconfig creation requires Linux 6.6 or newer"
  end

  command = exclusive ? Native::FSCONFIG_CMD_CREATE_EXCL : Native::FSCONFIG_CMD_CREATE
  configure(command, nil, nil, 0)
end

#filenoInteger

Returns:

  • (Integer)


45
# File 'lib/mountfd/core.rb', line 45

def fileno = @handle.fileno

#mount(attrs: {}) ⇒ DetachedMount

Parameters:

  • attrs: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
# File 'lib/mountfd/core.rb', line 74

def mount(attrs: {})
  attr_set, attr_clr = Attributes.build(attrs)
  raise ArgumentError, "idmap must be applied to a detached mount with a user namespace" if
    ((attr_set | attr_clr) & Native::MOUNT_ATTR_IDMAP).positive?

  handle = with_diagnostics("fsmount", MountError) do
    Native.fsmount(@handle, Native::FSMOUNT_CLOEXEC, attr_set)
  end
  DetachedMount.new(handle)
end

#reconfigure!FsContext

Returns:



72
# File 'lib/mountfd/core.rb', line 72

def reconfigure! = configure(Native::FSCONFIG_CMD_RECONFIGURE, nil, nil, 0)

#set(key, value = nil) ⇒ FsContext

Parameters:

  • key (String, Symbol)
  • value (Object) (defaults to: nil)

Returns:



49
50
51
52
53
# File 'lib/mountfd/core.rb', line 49

def set(key, value = nil)
  return set_flag(key) if value.nil? || value == true

  configure(Native::FSCONFIG_SET_STRING, key, value.to_s, 0)
end

#set_binary(key, bytes) ⇒ FsContext

Parameters:

  • key (String, Symbol)
  • bytes (String)

Returns:



58
59
60
61
# File 'lib/mountfd/core.rb', line 58

def set_binary(key, bytes)
  value = String(bytes)
  configure(Native::FSCONFIG_SET_BINARY, key, value, value.bytesize)
end

#set_fd(key, io) ⇒ FsContext

Parameters:

  • key (String, Symbol)
  • io (_Fileno)

Returns:



57
# File 'lib/mountfd/core.rb', line 57

def set_fd(key, io) = configure(Native::FSCONFIG_SET_FD, key, nil, Mountfd.fileno(io))

#set_flag(key) ⇒ FsContext

Parameters:

  • key (String, Symbol)

Returns:



55
# File 'lib/mountfd/core.rb', line 55

def set_flag(key) = configure(Native::FSCONFIG_SET_FLAG, key, nil, 0)

#set_path(key, path, dfd: AT_FDCWD) ⇒ FsContext

Parameters:

  • key (String, Symbol)
  • path (String)
  • dfd: (Integer) (defaults to: AT_FDCWD)

Returns:



56
# File 'lib/mountfd/core.rb', line 56

def set_path(key, path, dfd: AT_FDCWD) = configure(Native::FSCONFIG_SET_PATH, key, File.path(path), dfd)

#warningsArray[Diagnostic]

Returns:



47
# File 'lib/mountfd/core.rb', line 47

def warnings = diagnostics.select { _1.level == :warning }